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