Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
</intent-filter>
</activity-alias>

<activity
android:name=".activities.TermuxWidgetConfigureActivity"
android:exported="true"
android:label="@string/title_widget_settings"
android:theme="@style/Theme.BaseActivity.DayNight.NoActionBar">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>

<activity
android:name=".TermuxCreateShortcutActivity"
android:exported="true"
Expand Down
25 changes: 24 additions & 1 deletion app/src/main/java/com/termux/widget/TermuxWidgetProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.termux.shared.termux.TermuxUtils;
import com.termux.shared.termux.file.TermuxFileUtils;
import com.termux.shared.termux.settings.preferences.TermuxWidgetAppSharedPreferences;
import com.termux.widget.activities.TermuxWidgetConfigureActivity;
import com.termux.widget.utils.ShortcutUtils;

import java.io.File;
Expand Down Expand Up @@ -83,6 +84,14 @@ public static void updateAppWidgetRemoteViews(@NonNull Context context, @NonNull

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

// Set background color and opacity, and matching text colors. See WidgetSkin for the
// One UI Home blur.
WidgetSkin skin = WidgetSkin.get(context, appWidgetId);
WidgetSkin.setColor(context, remoteViews, android.R.id.background, "setBackgroundColor", skin.backgroundLight, skin.backgroundDark);
WidgetSkin.setColor(context, remoteViews, R.id.widget_title, "setTextColor", skin.textPrimaryLight, skin.textPrimaryDark);
WidgetSkin.setColor(context, remoteViews, R.id.empty_view, "setTextColor", skin.textSecondaryLight, skin.textSecondaryDark);
WidgetSkin.setColor(context, remoteViews, R.id.refresh_button, "setColorFilter", skin.textPrimaryLight, skin.textPrimaryDark);

// The empty view is displayed when the collection has no items. It should be a sibling
// of the collection view:
remoteViews.setEmptyView(R.id.widget_list, R.id.empty_view);
Expand All @@ -106,6 +115,15 @@ public static void updateAppWidgetRemoteViews(@NonNull Context context, @NonNull
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.refresh_button, refreshPendingIntent);

// Setup title to open widget settings, in case launcher does not show a reconfigure option:
Intent configureIntent = new Intent(context, TermuxWidgetConfigureActivity.class);
configureIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
configureIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
configureIntent.setData(Uri.parse(configureIntent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent configurePendingIntent = PendingIntent.getActivity(context, appWidgetId, configureIntent,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widget_title, configurePendingIntent);

// Here we setup the a pending intent template. Individuals items of a collection
// cannot setup their own pending intents, instead, the collection as a whole can
// setup a pending intent template, and the individual items can set a fillInIntent
Expand All @@ -124,7 +142,12 @@ public static void updateAppWidgetRemoteViews(@NonNull Context context, @NonNull

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Logger.logDebug(LOG_TAG, "onDeleted");
Logger.logDebug(LOG_TAG, "onDeleted: " + Arrays.toString(appWidgetIds));
if (appWidgetIds == null) return;

for (int appWidgetId : appWidgetIds) {
WidgetSkinPrefs.remove(context, appWidgetId);
}
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/termux/widget/TermuxWidgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public int getCount() {
@Override
public RemoteViews getViewAt(int position) {
// Position will always range from 0 to getCount() - 1.
return shortcutFiles.get(position).getListWidgetView(mContext);
RemoteViews remoteViews = shortcutFiles.get(position).getListWidgetView(mContext);
WidgetSkin skin = WidgetSkin.get(mContext, mAppWidgetId);
WidgetSkin.setColor(mContext, remoteViews, R.id.widget_item, "setTextColor", skin.textPrimaryLight, skin.textPrimaryDark);
return remoteViews;
}

@Override
Expand Down
117 changes: 117 additions & 0 deletions app/src/main/java/com/termux/widget/WidgetSkin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.termux.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.widget.RemoteViews;

import androidx.annotation.ColorInt;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;

/**
* Widget colors resolved from {@link WidgetSkinPrefs}, with separate colors for light and dark
* system mode.
*
* One UI Home 7.0+ blurs the wallpaper behind the {@link android.R.id#background} root view and
* tints it with the view background color if its alpha is between 1 and 254 and the provider info
* declares {@code app:widgetStyle} and {@code app:widgetSize}, so background alpha is kept in that
* range. Other launchers just draw the translucent color.
*/
public final class WidgetSkin {

/** Lowest and highest background alpha for which One UI Home still applies its blur. */
private static final int MIN_BACKGROUND_ALPHA = 1;
private static final int MAX_BACKGROUND_ALPHA = 254;

@ColorInt public final int backgroundLight;
@ColorInt public final int backgroundDark;
@ColorInt public final int textPrimaryLight;
@ColorInt public final int textPrimaryDark;
@ColorInt public final int textSecondaryLight;
@ColorInt public final int textSecondaryDark;

private WidgetSkin(int backgroundLight, int backgroundDark,
int textPrimaryLight, int textPrimaryDark,
int textSecondaryLight, int textSecondaryDark) {
this.backgroundLight = backgroundLight;
this.backgroundDark = backgroundDark;
this.textPrimaryLight = textPrimaryLight;
this.textPrimaryDark = textPrimaryDark;
this.textSecondaryLight = textSecondaryLight;
this.textSecondaryDark = textSecondaryDark;
}

@NonNull
public static WidgetSkin get(@NonNull Context context, int appWidgetId) {
int bgLight = context.getColor(R.color.widget_bg_light);
int bgDark = context.getColor(R.color.widget_bg_dark);
int onLight = context.getColor(R.color.widget_text_primary_light);
int onDark = context.getColor(R.color.widget_text_primary_dark);
int onLightSecondary = context.getColor(R.color.widget_text_secondary_light);
int onDarkSecondary = context.getColor(R.color.widget_text_secondary_dark);

int alpha = Math.round(WidgetSkinPrefs.getAlphaPercent(context, appWidgetId) * 255f / 100f);
alpha = Math.max(MIN_BACKGROUND_ALPHA, Math.min(MAX_BACKGROUND_ALPHA, alpha));

switch (WidgetSkinPrefs.getColorMode(context, appWidgetId)) {
case WidgetSkinPrefs.COLOR_WHITE:
bgDark = bgLight;
onDark = onLight;
onDarkSecondary = onLightSecondary;
break;
case WidgetSkinPrefs.COLOR_BLACK:
bgLight = bgDark = context.getColor(R.color.widget_bg_black);
onLight = onDark;
onLightSecondary = onDarkSecondary;
break;
case WidgetSkinPrefs.COLOR_WALLPAPER:
if (Build.VERSION.SDK_INT >= 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);
}

}
89 changes: 89 additions & 0 deletions app/src/main/java/com/termux/widget/WidgetSkinPrefs.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
Loading