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