From 6631599fb627a849e64e76693cb3b0eb775b1a87 Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Sun, 23 Jan 2022 00:18:33 +0500 Subject: [PATCH] Added: Add support for shared day/night theming across termux apps With this commit, activities will automatically change theme between day/night if `night-mode` `termux.properties` is not set or is set to `system` without requiring app restart. Dialog theming will be fully added in a later commit and may currently be in an inconsistent state or have crashes. The `uiMode` has been removed from `configChanges` of `TermuxActivity`, this may cause termux app to restart if samsung DEX mode is changed, if it does, then users should report it so that it can be fixed by re-adding the value and ignoring the change inside `TermuxActivity.onConfigurationChanged()`. The docs don't state if its necessary. Check related pull request #1446. Running `termux-reload-settings` will also restart `TermuxActivity`, the activity data should be preserved. --- app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 17 ++- .../java/com/termux/app/TermuxActivity.java | 40 ++---- .../termux/app/activities/HelpActivity.java | 5 +- .../app/activities/SettingsActivity.java | 14 +- .../TermuxFileReceiverActivity.java | 4 +- app/src/main/res/layout/activity_settings.xml | 9 +- app/src/main/res/layout/activity_termux.xml | 12 +- .../layout/item_terminal_sessions_list.xml | 2 +- app/src/main/res/values-night/themes.xml | 40 ++++++ app/src/main/res/values/attrs.xml | 5 + app/src/main/res/values/styles.xml | 51 ++------ app/src/main/res/values/themes.xml | 49 +++++++ .../shared/activities/ReportActivity.java | 4 + .../media/AppCompatActivityUtils.java | 120 ++++++++++++++++++ .../termux/shared/markdown/MarkdownUtils.java | 14 +- .../termux/extrakeys/ExtraKeysView.java | 29 ++++- .../com/termux/shared/theme/NightMode.java | 2 +- .../com/termux/shared/theme/ThemeUtils.java | 55 ++++++++ .../src/main/res/layout/activity_report.xml | 4 +- .../src/main/res/layout/activity_text_io.xml | 4 +- .../markdown_adapter_node_code_block.xml | 2 + .../layout/markdown_adapter_node_default.xml | 3 +- ...oolbar.xml => partial_primary_toolbar.xml} | 8 +- .../src/main/res/values-night/themes.xml | 56 +++++++- termux-shared/src/main/res/values/attrs.xml | 7 + termux-shared/src/main/res/values/colors.xml | 5 + termux-shared/src/main/res/values/dimens.xml | 3 +- termux-shared/src/main/res/values/styles.xml | 67 ++++++++++ termux-shared/src/main/res/values/themes.xml | 94 +++++++++++--- 30 files changed, 586 insertions(+), 140 deletions(-) create mode 100644 app/src/main/res/values-night/themes.xml create mode 100644 app/src/main/res/values/attrs.xml create mode 100644 app/src/main/res/values/themes.xml create mode 100644 termux-shared/src/main/java/com/termux/shared/activity/media/AppCompatActivityUtils.java rename termux-shared/src/main/res/layout/{partial_toolbar.xml => partial_primary_toolbar.xml} (69%) create mode 100644 termux-shared/src/main/res/values/attrs.xml create mode 100644 termux-shared/src/main/res/values/styles.xml diff --git a/app/build.gradle b/app/build.gradle index df251f0c..593e508d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,6 +16,7 @@ android { implementation "androidx.drawerlayout:drawerlayout:1.1.1" implementation "androidx.preference:preference:1.1.1" implementation "androidx.viewpager:viewpager:1.0.0" + implementation "com.google.android.material:material:1.4.0" implementation "com.google.guava:guava:24.1-jre" implementation "io.noties.markwon:core:$markwonVersion" implementation "io.noties.markwon:ext-strikethrough:$markwonVersion" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 21f4cf7a..abb569b9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -46,14 +46,15 @@ android:requestLegacyExternalStorage="true" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="false" - android:theme="@style/Theme.Termux"> + android:theme="@style/Theme.TermuxApp.DayNight.DarkActionBar"> + android:resizeableActivity="true" + android:theme="@style/Theme.TermuxActivity.DayNight.NoActionBar"> @@ -88,20 +89,18 @@ android:exported="false" android:label="@string/application_name" android:parentActivityName=".app.TermuxActivity" - android:resizeableActivity="true" - android:theme="@android:style/Theme.Material.Light.DarkActionBar" /> + android:resizeableActivity="true" /> + android:theme="@style/Theme.TermuxApp.DayNight.NoActionBar" /> + android:theme="@style/Theme.MarkdownViewActivity.DayNight" + android:documentLaunchMode="intoExisting" /> * about memory leaks. */ -public final class TermuxActivity extends Activity implements ServiceConnection { +public final class TermuxActivity extends AppCompatActivity implements ServiceConnection { /** * The connection to the {@link TermuxService}. Requested in {@link #onCreate(Bundle)} with a call to @@ -228,8 +226,6 @@ public final class TermuxActivity extends Activity implements ServiceConnection getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } - setDrawerTheme(); - setTermuxTerminalViewAndClients(); setTerminalToolbarView(savedInstanceState); @@ -340,6 +336,8 @@ public final class TermuxActivity extends Activity implements ServiceConnection @Override public void onSaveInstanceState(@NonNull Bundle savedInstanceState) { + Logger.logVerbose(LOG_TAG, "onSaveInstanceState"); + super.onSaveInstanceState(savedInstanceState); saveTerminalToolbarTextInput(savedInstanceState); } @@ -413,19 +411,10 @@ public final class TermuxActivity extends Activity implements ServiceConnection // Update NightMode.APP_NIGHT_MODE TermuxThemeUtils.setAppNightMode(mProperties.getNightMode()); - if (ThemeUtils.shouldEnableDarkTheme(this, NightMode.getAppNightMode().getName())) { - this.setTheme(R.style.Theme_Termux_Black); - } else { - this.setTheme(R.style.Theme_Termux); - } - } - - private void setDrawerTheme() { - if (ThemeUtils.shouldEnableDarkTheme(this, NightMode.getAppNightMode().getName())) { - findViewById(R.id.left_drawer).setBackgroundColor(ContextCompat.getColor(this, - android.R.color.background_dark)); - ((ImageButton) findViewById(R.id.settings_button)).setColorFilter(Color.WHITE); - } + // Set activity night mode. If NightMode.SYSTEM is set, then android will automatically + // trigger recreation of activity when uiMode/dark mode configuration is changed so that + // day or night theme takes affect. + AppCompatActivityUtils.setNightMode(this, NightMode.getAppNightMode().getName(), true); } private void setMargins() { @@ -518,7 +507,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection private void saveTerminalToolbarTextInput(Bundle savedInstanceState) { if (savedInstanceState == null) return; - final EditText textInputView = findViewById(R.id.terminal_toolbar_text_input); + final EditText textInputView = findViewById(R.id.terminal_toolbar_text_input); if (textInputView != null) { String textInput = textInputView.getText().toString(); if (!textInput.isEmpty()) savedInstanceState.putString(ARG_TERMINAL_TOOLBAR_TEXT_INPUT, textInput); @@ -934,12 +923,9 @@ public final class TermuxActivity extends Activity implements ServiceConnection mTermuxService.setTerminalTranscriptRows(); // To change the activity and drawer theme, activity needs to be recreated. - // But this will destroy the activity, and will call the onCreate() again. - // We need to investigate if enabling this is wise, since all stored variables and - // views will be destroyed and bindService() will be called again. Extra keys input - // text will we restored since that has already been implemented. Terminal sessions - // and transcripts are also already preserved. Theme does change properly too. - // TermuxActivity.this.recreate(); + // It will destroy the activity, including all stored variables and views, and onCreate() + // will be called again. Extra keys input text, terminal sessions and transcripts will be preserved. + TermuxActivity.this.recreate(); } diff --git a/app/src/main/java/com/termux/app/activities/HelpActivity.java b/app/src/main/java/com/termux/app/activities/HelpActivity.java index facbf36e..a2e4d6a9 100644 --- a/app/src/main/java/com/termux/app/activities/HelpActivity.java +++ b/app/src/main/java/com/termux/app/activities/HelpActivity.java @@ -1,6 +1,5 @@ package com.termux.app.activities; -import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; @@ -12,10 +11,12 @@ import android.webkit.WebViewClient; import android.widget.ProgressBar; import android.widget.RelativeLayout; +import androidx.appcompat.app.AppCompatActivity; + import com.termux.shared.termux.TermuxConstants; /** Basic embedded browser for viewing help pages. */ -public final class HelpActivity extends Activity { +public final class HelpActivity extends AppCompatActivity { WebView mWebView; diff --git a/app/src/main/java/com/termux/app/activities/SettingsActivity.java b/app/src/main/java/com/termux/app/activities/SettingsActivity.java index 6631f8d1..2b6063d4 100644 --- a/app/src/main/java/com/termux/app/activities/SettingsActivity.java +++ b/app/src/main/java/com/termux/app/activities/SettingsActivity.java @@ -5,7 +5,6 @@ import android.os.Bundle; import android.os.Environment; import androidx.annotation.NonNull; -import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; @@ -24,12 +23,17 @@ import com.termux.shared.termux.settings.preferences.TermuxWidgetAppSharedPrefer import com.termux.shared.android.AndroidUtils; import com.termux.shared.termux.TermuxConstants; import com.termux.shared.termux.TermuxUtils; +import com.termux.shared.activity.media.AppCompatActivityUtils; +import com.termux.shared.theme.NightMode; public class SettingsActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + AppCompatActivityUtils.setNightMode(this, NightMode.getAppNightMode().getName(), true); + setContentView(R.layout.activity_settings); if (savedInstanceState == null) { getSupportFragmentManager() @@ -37,11 +41,9 @@ public class SettingsActivity extends AppCompatActivity { .replace(R.id.settings, new RootPreferencesFragment()) .commit(); } - ActionBar actionBar = getSupportActionBar(); - if (actionBar != null) { - actionBar.setDisplayHomeAsUpEnabled(true); - actionBar.setDisplayShowHomeEnabled(true); - } + + AppCompatActivityUtils.setToolbar(this, com.termux.shared.R.id.toolbar); + AppCompatActivityUtils.setShowBackButtonInActionBar(this, true); } @Override diff --git a/app/src/main/java/com/termux/filepicker/TermuxFileReceiverActivity.java b/app/src/main/java/com/termux/filepicker/TermuxFileReceiverActivity.java index 5370b81a..019fb4a9 100644 --- a/app/src/main/java/com/termux/filepicker/TermuxFileReceiverActivity.java +++ b/app/src/main/java/com/termux/filepicker/TermuxFileReceiverActivity.java @@ -1,6 +1,5 @@ package com.termux.filepicker; -import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; @@ -8,6 +7,7 @@ import android.provider.OpenableColumns; import android.util.Patterns; import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; import com.termux.R; import com.termux.shared.data.DataUtils; @@ -31,7 +31,7 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.regex.Pattern; -public class TermuxFileReceiverActivity extends Activity { +public class TermuxFileReceiverActivity extends AppCompatActivity { static final String TERMUX_RECEIVEDIR = TermuxConstants.TERMUX_FILES_DIR_PATH + "/home/downloads"; static final String EDITOR_PROGRAM = TermuxConstants.TERMUX_HOME_DIR_PATH + "/bin/termux-file-editor"; diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 7dc1a6c5..d3914191 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -1,9 +1,16 @@ + + android:layout_height="match_parent" + android:orientation="vertical"> + + + diff --git a/app/src/main/res/layout/activity_termux.xml b/app/src/main/res/layout/activity_termux.xml index 144e846c..4bd61bab 100644 --- a/app/src/main/res/layout/activity_termux.xml +++ b/app/src/main/res/layout/activity_termux.xml @@ -1,4 +1,5 @@ + android:orientation="vertical" + android:background="?attr/termuxActivityDrawerBackground"> + android:contentDescription="@string/action_open_settings" + app:tint="?attr/termuxActivityDrawerImageTint" /> -