diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0c74788..69cb267 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -45,6 +45,16 @@ + + + + + + = Build.VERSION_CODES.S) { + // Resolved now, so a new wallpaper is picked up on the next widget update. + bgLight = context.getColor(android.R.color.system_accent1_100); + bgDark = context.getColor(android.R.color.system_accent1_800); + } + break; + default: + break; + } + + // Explicit text colors stay the same across all backgrounds and system modes. + switch (WidgetSkinPrefs.getTextMode(context, appWidgetId)) { + case WidgetSkinPrefs.TEXT_WHITE: + onLight = onDark = context.getColor(R.color.widget_text_white); + onLightSecondary = onDarkSecondary = context.getColor(R.color.widget_text_white_secondary); + break; + case WidgetSkinPrefs.TEXT_BLACK: + onLight = onDark = context.getColor(R.color.widget_text_black); + onLightSecondary = onDarkSecondary = context.getColor(R.color.widget_text_black_secondary); + break; + default: + break; + } + + return new WidgetSkin(withAlpha(bgLight, alpha), withAlpha(bgDark, alpha), + onLight, onDark, onLightSecondary, onDarkSecondary); + } + + /** + * Set a color with a {@link RemoteViews} int method. On Android 12+ the launcher picks the + * light or dark color when applying the views, so it follows dark mode changes without an + * update. On older versions the color for the current mode is used. + */ + public static void setColor(@NonNull Context context, @NonNull RemoteViews remoteViews, @IdRes int viewId, + @NonNull String methodName, @ColorInt int lightColor, @ColorInt int darkColor) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + remoteViews.setColorInt(viewId, methodName, lightColor, darkColor); + } else { + int nightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; + remoteViews.setInt(viewId, methodName, nightMode == Configuration.UI_MODE_NIGHT_YES ? darkColor : lightColor); + } + } + + @ColorInt + private static int withAlpha(@ColorInt int color, int alpha) { + return (alpha << 24) | (color & 0x00FFFFFF); + } + +} diff --git a/app/src/main/java/com/termux/widget/WidgetSkinPrefs.java b/app/src/main/java/com/termux/widget/WidgetSkinPrefs.java new file mode 100644 index 0000000..48e8f21 --- /dev/null +++ b/app/src/main/java/com/termux/widget/WidgetSkinPrefs.java @@ -0,0 +1,89 @@ +package com.termux.widget; + +import android.content.Context; +import android.content.SharedPreferences; + +import androidx.annotation.NonNull; + +/** + * Per widget skin settings, stored by appWidgetId in a separate {@link SharedPreferences} file so + * that termux-shared preferences are not touched. + */ +public final class WidgetSkinPrefs { + + private static final String PREFERENCES_NAME = "widget_skin"; + + private static final String KEY_ALPHA_PREFIX = "alpha_"; + private static final String KEY_COLOR_PREFIX = "color_"; + private static final String KEY_TEXT_PREFIX = "text_"; + + /** Background color follows the system dark mode. */ + public static final int COLOR_AUTO = 0; + public static final int COLOR_WHITE = 1; + public static final int COLOR_BLACK = 2; + /** Background color is taken from the wallpaper based system palette on Android 12+. */ + public static final int COLOR_WALLPAPER = 3; + + /** Text colors follow the selected background by default. */ + public static final int TEXT_AUTO = 0; + public static final int TEXT_WHITE = 1; + public static final int TEXT_BLACK = 2; + + /** Default background opacity in percent. */ + public static final int DEFAULT_ALPHA_PERCENT = 100; + + private WidgetSkinPrefs() {} + + private static SharedPreferences getSharedPreferences(@NonNull Context context) { + return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE); + } + + /** Get background opacity in percent (0-100) for the widget. */ + public static int getAlphaPercent(@NonNull Context context, int appWidgetId) { + int percent = getSharedPreferences(context).getInt(KEY_ALPHA_PREFIX + appWidgetId, DEFAULT_ALPHA_PERCENT); + return Math.max(0, Math.min(100, percent)); + } + + /** Set background opacity in percent (0-100) for the widget. */ + public static void setAlphaPercent(@NonNull Context context, int appWidgetId, int percent) { + getSharedPreferences(context).edit() + .putInt(KEY_ALPHA_PREFIX + appWidgetId, Math.max(0, Math.min(100, percent))) + .apply(); + } + + /** Get background color mode, one of the {@code COLOR_*} constants, for the widget. */ + public static int getColorMode(@NonNull Context context, int appWidgetId) { + int mode = getSharedPreferences(context).getInt(KEY_COLOR_PREFIX + appWidgetId, COLOR_AUTO); + return mode >= COLOR_AUTO && mode <= COLOR_WALLPAPER ? mode : COLOR_AUTO; + } + + /** Set background color mode, one of the {@code COLOR_*} constants, for the widget. */ + public static void setColorMode(@NonNull Context context, int appWidgetId, int mode) { + getSharedPreferences(context).edit() + .putInt(KEY_COLOR_PREFIX + appWidgetId, mode) + .apply(); + } + + /** Get text color mode, one of the {@code TEXT_*} constants, for the widget. */ + public static int getTextMode(@NonNull Context context, int appWidgetId) { + int mode = getSharedPreferences(context).getInt(KEY_TEXT_PREFIX + appWidgetId, TEXT_AUTO); + return mode >= TEXT_AUTO && mode <= TEXT_BLACK ? mode : TEXT_AUTO; + } + + /** Set text color mode, one of the {@code TEXT_*} constants, for the widget. */ + public static void setTextMode(@NonNull Context context, int appWidgetId, int mode) { + getSharedPreferences(context).edit() + .putInt(KEY_TEXT_PREFIX + appWidgetId, mode) + .apply(); + } + + /** Remove all skin settings of the widget. */ + public static void remove(@NonNull Context context, int appWidgetId) { + getSharedPreferences(context).edit() + .remove(KEY_ALPHA_PREFIX + appWidgetId) + .remove(KEY_COLOR_PREFIX + appWidgetId) + .remove(KEY_TEXT_PREFIX + appWidgetId) + .apply(); + } + +} diff --git a/app/src/main/java/com/termux/widget/activities/TermuxWidgetConfigureActivity.java b/app/src/main/java/com/termux/widget/activities/TermuxWidgetConfigureActivity.java new file mode 100644 index 0000000..ca10bfe --- /dev/null +++ b/app/src/main/java/com/termux/widget/activities/TermuxWidgetConfigureActivity.java @@ -0,0 +1,165 @@ +package com.termux.widget.activities; + +import android.appwidget.AppWidgetManager; +import android.content.Intent; +import android.os.Build; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.RadioGroup; +import android.widget.SeekBar; +import android.widget.TextView; + +import androidx.appcompat.app.AppCompatActivity; + +import com.termux.shared.activity.media.AppCompatActivityUtils; +import com.termux.shared.logger.Logger; +import com.termux.shared.theme.NightMode; +import com.termux.widget.R; +import com.termux.widget.TermuxWidgetProvider; +import com.termux.widget.WidgetSkinPrefs; + +/** + * Widget settings activity. Opened by the launcher when the widget is reconfigured (long press -> + * settings) or by clicking the widget title. + */ +public class TermuxWidgetConfigureActivity extends AppCompatActivity { + + public static final String LOG_TAG = "TermuxWidgetConfigureActivity"; + + private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; + + private RadioGroup mColorRadioGroup; + private RadioGroup mTextRadioGroup; + private TextView mAlphaValueTextView; + private SeekBar mAlphaSeekBar; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + Logger.logDebug(LOG_TAG, "onCreate"); + + setContentView(R.layout.activity_termux_widget_configure); + + AppCompatActivityUtils.setNightMode(this, NightMode.getAppNightMode().getName(), true); + + // If user backs out of the activity, then the launcher should treat it as cancelled. + setResult(RESULT_CANCELED); + + mAppWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); + if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { + Logger.logError(LOG_TAG, "Invalid widget id received, finishing"); + finish(); + return; + } + + Logger.logDebug(LOG_TAG, "Configuring widget id: " + mAppWidgetId); + + setColorViews(); + setTextViews(); + setAlphaViews(); + + Button saveButton = findViewById(R.id.button_save); + saveButton.setOnClickListener(v -> saveAndFinish()); + } + + private void setColorViews() { + mColorRadioGroup = findViewById(R.id.radio_group_color); + + // Wallpaper colors are only available on Android 12+. + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) + findViewById(R.id.radio_color_wallpaper).setVisibility(View.GONE); + + switch (WidgetSkinPrefs.getColorMode(this, mAppWidgetId)) { + case WidgetSkinPrefs.COLOR_WHITE: + mColorRadioGroup.check(R.id.radio_color_white); + break; + case WidgetSkinPrefs.COLOR_BLACK: + mColorRadioGroup.check(R.id.radio_color_black); + break; + case WidgetSkinPrefs.COLOR_WALLPAPER: + mColorRadioGroup.check(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? + R.id.radio_color_wallpaper : R.id.radio_color_auto); + break; + default: + mColorRadioGroup.check(R.id.radio_color_auto); + break; + } + } + + private int getCheckedColorMode() { + int checkedId = mColorRadioGroup.getCheckedRadioButtonId(); + if (checkedId == R.id.radio_color_white) return WidgetSkinPrefs.COLOR_WHITE; + if (checkedId == R.id.radio_color_black) return WidgetSkinPrefs.COLOR_BLACK; + if (checkedId == R.id.radio_color_wallpaper) return WidgetSkinPrefs.COLOR_WALLPAPER; + return WidgetSkinPrefs.COLOR_AUTO; + } + + private void setTextViews() { + mTextRadioGroup = findViewById(R.id.radio_group_text); + switch (WidgetSkinPrefs.getTextMode(this, mAppWidgetId)) { + case WidgetSkinPrefs.TEXT_WHITE: + mTextRadioGroup.check(R.id.radio_text_white); + break; + case WidgetSkinPrefs.TEXT_BLACK: + mTextRadioGroup.check(R.id.radio_text_black); + break; + default: + mTextRadioGroup.check(R.id.radio_text_auto); + break; + } + } + + private int getCheckedTextMode() { + int checkedId = mTextRadioGroup.getCheckedRadioButtonId(); + if (checkedId == R.id.radio_text_white) return WidgetSkinPrefs.TEXT_WHITE; + if (checkedId == R.id.radio_text_black) return WidgetSkinPrefs.TEXT_BLACK; + return WidgetSkinPrefs.TEXT_AUTO; + } + + private void setAlphaViews() { + mAlphaValueTextView = findViewById(R.id.text_alpha_value); + mAlphaSeekBar = findViewById(R.id.seekbar_alpha); + + int percent = WidgetSkinPrefs.getAlphaPercent(this, mAppWidgetId); + mAlphaSeekBar.setProgress(percent); + setAlphaValueText(percent); + + mAlphaSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + setAlphaValueText(progress); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) {} + + @Override + public void onStopTrackingTouch(SeekBar seekBar) {} + }); + } + + private void setAlphaValueText(int percent) { + mAlphaValueTextView.setText(getString(R.string.label_widget_background_opacity, percent)); + } + + private void saveAndFinish() { + int colorMode = getCheckedColorMode(); + int textMode = getCheckedTextMode(); + int percent = mAlphaSeekBar.getProgress(); + Logger.logDebug(LOG_TAG, "Saving background color mode " + colorMode + " and text color mode " + textMode + " and opacity " + percent + "% for widget id: " + mAppWidgetId); + + WidgetSkinPrefs.setColorMode(this, mAppWidgetId, colorMode); + WidgetSkinPrefs.setTextMode(this, mAppWidgetId, textMode); + WidgetSkinPrefs.setAlphaPercent(this, mAppWidgetId, percent); + AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); + TermuxWidgetProvider.updateAppWidgetRemoteViews(this, appWidgetManager, mAppWidgetId); + // List item text colors are set by TermuxWidgetService, so reload the items too. + appWidgetManager.notifyAppWidgetViewDataChanged(mAppWidgetId, R.id.widget_list); + + setResult(RESULT_OK, new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId)); + finish(); + } + +} diff --git a/app/src/main/res/drawable/ic_widget_refresh.xml b/app/src/main/res/drawable/ic_widget_refresh.xml new file mode 100644 index 0000000..6dbaa5e --- /dev/null +++ b/app/src/main/res/drawable/ic_widget_refresh.xml @@ -0,0 +1,14 @@ + + + + + + + diff --git a/app/src/main/res/drawable/ripple_circle.xml b/app/src/main/res/drawable/ripple_circle.xml new file mode 100644 index 0000000..68cbc71 --- /dev/null +++ b/app/src/main/res/drawable/ripple_circle.xml @@ -0,0 +1,11 @@ + + + + + + + + + + diff --git a/app/src/main/res/drawable/ripple_mask.xml b/app/src/main/res/drawable/ripple_mask.xml index fe34991..2082a7b 100644 --- a/app/src/main/res/drawable/ripple_mask.xml +++ b/app/src/main/res/drawable/ripple_mask.xml @@ -1,6 +1,6 @@ + android:color="@color/widget_ripple" > @@ -8,4 +8,4 @@ - \ No newline at end of file + diff --git a/app/src/main/res/layout/activity_termux_widget_configure.xml b/app/src/main/res/layout/activity_termux_widget_configure.xml new file mode 100644 index 0000000..41d9e22 --- /dev/null +++ b/app/src/main/res/layout/activity_termux_widget_configure.xml @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +