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 /** 34 * Handles the menu for the theme playground app 35 */ 36 public abstract class AbstractSampleActivity extends Activity implements 37 PopupMenu.OnMenuItemClickListener { 38 39 private UiModeManager mUiModeManager; 40 41 @Override onCreateOptionsMenu(Menu menu)42 public boolean onCreateOptionsMenu(Menu menu) { 43 // Inflate the menu; this adds items to the action bar if it is present. 44 getMenuInflater().inflate(R.menu.menu_main, menu); 45 mUiModeManager = (UiModeManager) this.getSystemService(Context.UI_MODE_SERVICE); 46 return true; 47 } 48 49 50 @Override onOptionsItemSelected(MenuItem item)51 public boolean onOptionsItemSelected(MenuItem item) { 52 switch (item.getItemId()) { 53 case R.id.text_elements: 54 return startSampleActivity(TextSamples.class); 55 case R.id.button_elements: 56 return startSampleActivity(ButtonSamples.class); 57 case R.id.progress_bar_elements: 58 return startSampleActivity(ProgressBarSamples.class); 59 case R.id.panel_elements: 60 return startSampleActivity(ColorSamples.class); 61 case R.id.palette_elements: 62 return startSampleActivity(ColorPalette.class); 63 case R.id.dialog_elements: 64 return startSampleActivity(DialogSamples.class); 65 case R.id.toggle_theme: 66 return toggleDayNight(); 67 case R.id.widgets: 68 return startSampleActivity(WidgetsSamples.class); 69 case R.id.recycler_view: 70 return startSampleActivity(RecyclerViewSamples.class); 71 case R.id.car_ui_recycler_view: 72 return startSampleActivity(CarUiRecyclerViewSamples.class); 73 case R.id.default_themes: 74 return startSampleActivity(DefaultThemeSamples.class); 75 case R.id.multiple_intent: 76 return startSampleActivity(MultipleIntentSamples.class); 77 default: 78 return true; 79 } 80 } 81 82 /** 83 * Will show the menu onclick of the menu button. This button will only appear when the theme is 84 * set to NoActionBar. 85 */ showPopupMenu(View v)86 private void showPopupMenu(View v) { 87 PopupMenu popup = new PopupMenu(this, v); 88 popup.setOnMenuItemClickListener(this); 89 popup.inflate(R.menu.menu_main); 90 popup.show(); 91 } 92 93 @Override onMenuItemClick(MenuItem item)94 public boolean onMenuItemClick(MenuItem item) { 95 return onOptionsItemSelected(item); 96 } 97 98 @Override onStart()99 protected void onStart() { 100 super.onStart(); 101 bindMenuButton(); 102 } 103 104 105 /** 106 * When theme is set to NoActionBar then the menu also disappears blocking the user to navigate 107 * between the activities. At that point this method will bring up the menu button that will 108 * help user to navigate between activities. 109 */ bindMenuButton()110 private void bindMenuButton() { 111 Button buttonMenu = findViewById(R.id.button_menu); 112 if (Utils.sThemeName.equals("Theme.DeviceDefault.NoActionBar")) { 113 buttonMenu.setVisibility(View.VISIBLE); 114 } else { 115 buttonMenu.setVisibility(View.GONE); 116 } 117 buttonMenu.setOnClickListener(v -> { 118 showPopupMenu(v); 119 }); 120 } 121 122 /** 123 * Launch the given sample activity 124 */ startSampleActivity(Class<?> cls)125 private boolean startSampleActivity(Class<?> cls) { 126 Intent dialogIntent = new Intent(this, cls); 127 startActivity(dialogIntent); 128 return true; 129 } 130 toggleDayNight()131 private boolean toggleDayNight() { 132 mUiModeManager.setNightMode( 133 (mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES) 134 ? UiModeManager.MODE_NIGHT_NO : UiModeManager.MODE_NIGHT_YES); 135 return true; 136 } 137 setupBackgroundColorControls(int backgroundLayoutId)138 void setupBackgroundColorControls(int backgroundLayoutId) { 139 Button colorSetButton = findViewById(R.id.set_background_color); 140 ((EditText) findViewById(R.id.background_input_color)).setText( 141 R.string.default_background_color, 142 TextView.BufferType.EDITABLE); 143 colorSetButton.setOnClickListener(v -> { 144 String value = ((EditText) findViewById(R.id.background_input_color)).getText() 145 .toString(); 146 try { 147 int color = Color.parseColor(value); 148 View dialogLayout = findViewById(backgroundLayoutId); 149 dialogLayout.setBackgroundColor(color); 150 } catch (Exception e) { 151 Toast.makeText(this, "not a color", Toast.LENGTH_LONG).show(); 152 } 153 }); 154 Button colorResetButton = findViewById(R.id.reset); 155 colorResetButton.setOnClickListener(v -> { 156 try { 157 View dialogLayout = findViewById(backgroundLayoutId); 158 dialogLayout.setBackgroundColor(android.R.color.black); 159 } catch (Exception e) { 160 Toast.makeText(this, "Something went Wrong. Try again later.", 161 Toast.LENGTH_LONG).show(); 162 } 163 }); 164 } 165 } 166