6 package com.google.appinventor.components.runtime;
8 import android.app.Activity;
9 import android.content.res.Configuration;
10 import android.graphics.drawable.ColorDrawable;
11 import android.os.Bundle;
12 import android.util.Log;
13 import android.view.Gravity;
14 import android.view.View;
15 import android.view.ViewGroup;
16 import android.view.Window;
17 import android.widget.FrameLayout.LayoutParams;
18 import android.widget.TextView;
19 import androidx.annotation.Nullable;
20 import androidx.appcompat.app.ActionBar;
21 import androidx.appcompat.app.AppCompatCallback;
22 import androidx.appcompat.app.AppCompatDelegate;
23 import androidx.appcompat.view.ActionMode;
24 import androidx.appcompat.view.ActionMode.Callback;
51 static final int DEFAULT_PRIMARY_COLOR =
53 private static boolean classicMode =
false;
54 private static boolean actionBarEnabled;
56 private static int primaryColor;
57 private AppCompatDelegate appCompatDelegate;
58 android.widget.LinearLayout frameWithTitle;
60 private static boolean didSetClassicModeFromYail =
false;
61 @SuppressWarnings(
"WeakerAccess")
73 actionBarEnabled =
true;
82 appCompatDelegate = AppCompatDelegate.create(
this,
this);
83 appCompatDelegate.onCreate(icicle);
86 super.onCreate(icicle);
88 frameWithTitle =
new android.widget.LinearLayout(
this);
89 frameWithTitle.setOrientation(android.widget.LinearLayout.VERTICAL);
94 titleBar = (TextView) findViewById(android.R.id.title);
95 if (shouldCreateTitleBar()) {
96 titleBar =
new TextView(
this);
97 titleBar.setBackgroundResource(android.R.drawable.title_bar);
98 titleBar.setTextAppearance(
this, android.R.style.TextAppearance_WindowTitle);
99 titleBar.setGravity(Gravity.CENTER_VERTICAL);
100 titleBar.setSingleLine();
101 titleBar.setShadowLayer(2, 0, 0, 0xBB000000);
108 frameWithTitle.addView(titleBar,
new ViewGroup.LayoutParams(
109 ViewGroup.LayoutParams.MATCH_PARENT,
110 ViewGroup.LayoutParams.WRAP_CONTENT
114 Log.d(LOG_TAG,
"Already have a title bar (classic mode): " + titleBar);
118 @SuppressWarnings(
"WeakerAccess")
120 return appCompatDelegate !=
null;
125 super.onPostCreate(savedInstanceState);
126 if (appCompatDelegate !=
null) {
127 appCompatDelegate.onPostCreate(savedInstanceState);
133 super.onPostResume();
134 if (appCompatDelegate !=
null) {
135 appCompatDelegate.onPostResume();
141 super.onConfigurationChanged(newConfig);
142 if (appCompatDelegate !=
null) {
143 appCompatDelegate.onConfigurationChanged(newConfig);
150 if (appCompatDelegate !=
null) {
151 appCompatDelegate.onStop();
158 if (appCompatDelegate !=
null) {
159 appCompatDelegate.onDestroy();
165 super.onTitleChanged(title, color);
166 if (appCompatDelegate !=
null) {
167 appCompatDelegate.setTitle(title);
168 }
else if (titleBar !=
null) {
169 titleBar.setText(title);
192 if (view != frameWithTitle) {
193 frameWithTitle.addView(view,
new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
194 ViewGroup.LayoutParams.MATCH_PARENT));
195 view = frameWithTitle;
199 if (appCompatDelegate !=
null) {
200 appCompatDelegate.setContentView(view);
202 super.setContentView(view);
206 @SuppressWarnings(
"WeakerAccess")
208 Window.Callback classicCallback = getWindow().getCallback();
210 return appCompatDelegate ==
null ? null : appCompatDelegate.getSupportActionBar();
211 }
catch (IllegalStateException e) {
213 appCompatDelegate =
null;
215 getWindow().setCallback(classicCallback);
221 return android.os.Build.PRODUCT.contains(
"google_sdk") ||
222 android.os.Build.PRODUCT.equals(
"sdk") ||
223 android.os.Build.PRODUCT.contains(
"sdk_gphone");
226 @SuppressWarnings(
"unused")
228 return actionBarEnabled;
231 @SuppressWarnings(
"WeakerAccess")
233 actionBarEnabled = enabled;
236 @SuppressWarnings(
"unused")
241 @SuppressWarnings(
"WeakerAccess")
244 classicMode = classic;
252 @SuppressWarnings(
"WeakerAccess")
256 if (actionBar !=
null && newColor != primaryColor) {
258 primaryColor = newColor;
259 actionBar.setBackgroundDrawable(
new ColorDrawable(color));
267 @SuppressWarnings(
"WeakerAccess")
269 if (titleBar !=
null) {
270 if (titleBar.getParent() != frameWithTitle) {
271 if (titleBar.getParent() !=
null) {
272 ((View)titleBar.getParent()).setVisibility(View.GONE);
275 titleBar.setVisibility(View.GONE);
281 if (titleBar !=
null) {
282 titleBar.setVisibility(View.VISIBLE);
283 Log.d(LOG_TAG,
"titleBar visible");
284 if (titleBar.getParent() !=
null && titleBar.getParent() != frameWithTitle) {
285 Log.d(LOG_TAG,
"Setting parent visible");
286 ((View) titleBar.getParent()).setVisibility(View.VISIBLE);
291 @SuppressWarnings(
"WeakerAccess")
294 Log.d(LOG_TAG,
"actionBarEnabled = " + actionBarEnabled);
295 Log.d(LOG_TAG,
"!classicMode = " + !classicMode);
296 Log.d(LOG_TAG,
"actionBar = " + actionBar);
297 if (actionBar !=
null) {
299 if (actionBarEnabled) {
310 @SuppressWarnings(
"WeakerAccess")
315 if (theme == currentTheme) {
318 currentTheme = theme;
322 private void applyTheme() {
323 Log.d(LOG_TAG,
"applyTheme " + currentTheme);
325 switch (currentTheme) {
328 setTheme(android.R.style.Theme);
331 case BLACK_TITLE_TEXT:
332 setTheme(android.R.style.Theme_DeviceDefault_Light_NoActionBar);
335 setTheme(android.R.style.Theme_DeviceDefault_NoActionBar);
340 private boolean shouldCreateTitleBar() {
343 }
else if (titleBar ==
null && (
isRepl() || classicMode)) {
349 @SuppressWarnings(
"unused")
351 if (!didSetClassicModeFromYail) {
352 Log.d(LOG_TAG,
"Setting classic mode from YAIL: " + newClassicMode);
353 classicMode = newClassicMode;
354 didSetClassicModeFromYail =
true;