Skip to content

Commit

Permalink
[Usage] Add support for auto-switch between light and dark mode
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Dec 28, 2024
1 parent 39aa608 commit e570483
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.github.muntashirakon.AppManager.utils.DateUtils;
import io.github.muntashirakon.AppManager.utils.ExUtils;
import io.github.muntashirakon.AppManager.utils.appearance.AppearanceUtils;
import io.github.muntashirakon.util.UiUtils;

public class ScreenTimeAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Expand Down Expand Up @@ -97,7 +98,9 @@ static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
}
// Set colors
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
boolean isNight = UiUtils.isDarkMode(context);
int colorSurface = MaterialColors.getColor(context, com.google.android.material.R.attr.colorSurface, "colorSurface");
int colorSurfaceInverse = MaterialColors.getColor(context, com.google.android.material.R.attr.colorSurfaceInverse, "colorSurfaceInverse");
ColorStateList color1 = ColorStateList.valueOf(MaterialColors.harmonizeWithPrimary(context, Color.parseColor("#1b1b1b")));
ColorStateList color2 = ColorStateList.valueOf(MaterialColors.harmonizeWithPrimary(context, Color.parseColor("#565e71")));
ColorStateList color3 = ColorStateList.valueOf(MaterialColors.harmonizeWithPrimary(context, Color.parseColor("#d4e3ff")));
Expand All @@ -107,7 +110,9 @@ static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
views.setColorStateList(R.id.app2_circle, "setBackgroundTintList", color2);
views.setColorStateList(R.id.app3_time, "setBackgroundTintList", color3);
views.setColorStateList(R.id.app3_circle, "setBackgroundTintList", color3);
views.setInt(R.id.widget_background, "setBackgroundColor", colorSurface);
if (isNight) {
views.setColorInt(R.id.widget_background, "setBackgroundColor", colorSurfaceInverse, colorSurface);
} else views.setColorInt(R.id.widget_background, "setBackgroundColor", colorSurface, colorSurfaceInverse);
}
// Get PendingIntent for App Usage page
Intent appUsageIntent = new Intent(context, AppUsageActivity.class);
Expand Down
14 changes: 10 additions & 4 deletions libcore/ui/src/main/java/io/github/muntashirakon/util/UiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,11 @@ public static void doOnApplyWindowInsets(@NonNull View view, @NonNull OnApplyWin
}

public static boolean isDarkMode(@NonNull Context context) {
return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK)
== Configuration.UI_MODE_NIGHT_YES;
Configuration conf = context.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return conf.isNightModeActive();
}
return (conf.uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}

@SuppressWarnings("deprecation")
Expand All @@ -269,8 +272,11 @@ public static boolean isDarkMode() {
}

public static boolean isDarkModeOnSystem() {
return (Resources.getSystem().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK)
== Configuration.UI_MODE_NIGHT_YES;
Configuration conf = Resources.getSystem().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return conf.isNightModeActive();
}
return (conf.uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}

@SuppressWarnings("deprecation")
Expand Down

0 comments on commit e570483

Please sign in to comment.