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