1 /* 2 * Copyright (C) 2019 The Android Open Source Project. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.car.themeplayground; 18 19 import android.app.Activity; 20 import android.app.UiModeManager; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.graphics.Color; 24 import android.view.Menu; 25 import android.view.MenuItem; 26 import android.view.View; 27 import android.widget.Button; 28 import android.widget.EditText; 29 import android.widget.PopupMenu; 30 import android.widget.TextView; 31 import android.widget.Toast; 32 33 import java.util.Objects; 34 35 /** 36 * Handles the menu for the theme playground app 37 */ 38 public abstract class AbstractSampleActivity extends Activity implements 39 PopupMenu.OnMenuItemClickListener { 40 41 private UiModeManager mUiModeManager; 42 43 @Override onCreateOptionsMenu(Menu menu)44 public boolean onCreateOptionsMenu(Menu menu) { 45 // Inflate the menu; this adds items to the action bar if it is present. 46 getMenuInflater().inflate(R.menu.menu_main, menu); 47 mUiModeManager = (UiModeManager) this.getSystemService(Context.UI_MODE_SERVICE); 48 return true; 49 } 50 51 52 @Override onOptionsItemSelected(MenuItem item)53 public boolean onOptionsItemSelected(MenuItem item) { 54 switch (item.getItemId()) { 55 case R.id.text_elements: 56 return startSampleActivity(TextSamples.class); 57 case R.id.button_elements: 58 return startSampleActivity(ButtonSamples.class); 59 case R.id.progress_bar_elements: 60 return startSampleActivity(ProgressBarSamples.class); 61 case R.id.panel_elements: 62 return startSampleActivity(ColorSamples.class); 63 case R.id.palette_elements: 64 return startSampleActivity(ColorPalette.class); 65 case R.id.dialog_elements: 66 return startSampleActivity(DialogSamples.class); 67 case R.id.toggle_theme: 68 return toggleDayNight(); 69 case R.id.widgets: 70 return startSampleActivity(WidgetsSamples.class); 71 case R.id.recycler_view: 72 return startSampleActivity(RecyclerViewSamples.class); 73 case R.id.car_ui_recycler_view: 74 return startSampleActivity(CarUiRecyclerViewSamples.class); 75 case R.id.default_themes: 76 return startSampleActivity(DefaultThemeSamples.class); 77 case R.id.multiple_intent: 78 return startSampleActivity(MultipleIntentSamples.class); 79 default: 80 return true; 81 } 82 } 83 84 /** 85 * Will show the menu onclick of the menu button. This button will only appear when the theme is 86 * set to NoActionBar. 87 */ showPopupMenu(View v)88 private void showPopupMenu(View v) { 89 PopupMenu popup = new PopupMenu(this, v); 90 popup.setOnMenuItemClickListener(this); 91 popup.inflate(R.menu.menu_main); 92 popup.show(); 93 } 94 95 @Override onMenuItemClick(MenuItem item)96 public boolean onMenuItemClick(MenuItem item) { 97 return onOptionsItemSelected(item); 98 } 99 100 @Override onStart()101 protected void onStart() { 102 super.onStart(); 103 bindMenuButton(); 104 } 105 106 107 /** 108 * When theme is set to NoActionBar then the menu also disappears blocking the user to navigate 109 * between the activities. At that point this method will bring up the menu button that will 110 * help user to navigate between activities. 111 */ bindMenuButton()112 private void bindMenuButton() { 113 Button buttonMenu = findViewById(R.id.button_menu); 114 if (Objects.equals(Utils.sThemeName, "Theme.DeviceDefault.NoActionBar")) { 115 buttonMenu.setVisibility(View.VISIBLE); 116 } else { 117 buttonMenu.setVisibility(View.GONE); 118 } 119 buttonMenu.setOnClickListener(v -> { 120 showPopupMenu(v); 121 }); 122 } 123 124 /** 125 * Launch the given sample activity 126 */ startSampleActivity(Class<?> cls)127 private boolean startSampleActivity(Class<?> cls) { 128 Intent dialogIntent = new Intent(this, cls); 129 startActivity(dialogIntent); 130 return true; 131 } 132 toggleDayNight()133 private boolean toggleDayNight() { 134 mUiModeManager.setNightMode( 135 (mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES) 136 ? UiModeManager.MODE_NIGHT_NO : UiModeManager.MODE_NIGHT_YES); 137 return true; 138 } 139 setupBackgroundColorControls(int backgroundLayoutId)140 void setupBackgroundColorControls(int backgroundLayoutId) { 141 Button colorSetButton = findViewById(R.id.set_background_color); 142 ((EditText) findViewById(R.id.background_input_color)).setText( 143 R.string.default_background_color, 144 TextView.BufferType.EDITABLE); 145 colorSetButton.setOnClickListener(v -> { 146 String value = ((EditText) findViewById(R.id.background_input_color)).getText() 147 .toString(); 148 try { 149 int color = Color.parseColor(value); 150 View dialogLayout = findViewById(backgroundLayoutId); 151 dialogLayout.setBackgroundColor(color); 152 } catch (Exception e) { 153 Toast.makeText(this, "not a color", Toast.LENGTH_LONG).show(); 154 } 155 }); 156 Button colorResetButton = findViewById(R.id.reset); 157 colorResetButton.setOnClickListener(v -> { 158 try { 159 View dialogLayout = findViewById(backgroundLayoutId); 160 dialogLayout.setBackgroundColor(android.R.color.black); 161 } catch (Exception e) { 162 Toast.makeText(this, "Something went Wrong. Try again later.", 163 Toast.LENGTH_LONG).show(); 164 } 165 }); 166 } 167 } 168