Skip to content

Commit

Permalink
[Usage] Use more consistent colors for the widget
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 23, 2024
1 parent a39c3cf commit 1adcac5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Size;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RemoteViews;

import androidx.annotation.NonNull;
import androidx.core.app.PendingIntentCompat;
import androidx.core.content.ContextCompat;

import com.google.android.material.color.MaterialColors;

Expand Down Expand Up @@ -101,23 +104,26 @@ static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
}
// Set colors
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
ColorStateList colorPrimary = ColorStateList.valueOf(MaterialColors.getColor(context, com.google.android.material.R.attr.colorPrimary, "colorPrimary"));
int colorOnPrimary = MaterialColors.getColor(context, com.google.android.material.R.attr.colorOnPrimary, "colorOnPrimary");
ColorStateList colorSecondary = ColorStateList.valueOf(MaterialColors.getColor(context, com.google.android.material.R.attr.colorSecondary, "colorSecondary"));
int colorOnSecondary = MaterialColors.getColor(context, com.google.android.material.R.attr.colorOnSecondary, "colorOnSecondary");
ColorStateList colorTertiary = ColorStateList.valueOf(MaterialColors.getColor(context, com.google.android.material.R.attr.colorTertiary, "colorTertiary"));
int colorOnTertiary = MaterialColors.getColor(context, com.google.android.material.R.attr.colorOnTertiary, "colorOnTertiary");
views.setTextColor(R.id.app1_time, colorOnPrimary);
views.setColorStateList(R.id.app1_time, "setBackgroundTintList", colorPrimary);
views.setColorStateList(R.id.app1_circle, "setBackgroundTintList", colorPrimary);
views.setTextColor(R.id.app2_time, colorOnSecondary);
views.setColorStateList(R.id.app2_time, "setBackgroundTintList", colorSecondary);
views.setColorStateList(R.id.app2_circle, "setBackgroundTintList", colorSecondary);
views.setTextColor(R.id.app3_time, colorOnTertiary);
views.setColorStateList(R.id.app3_time, "setBackgroundTintList", colorTertiary);
views.setColorStateList(R.id.app3_circle, "setBackgroundTintList", colorTertiary);
int colorSurface = MaterialColors.getColor(context, com.google.android.material.R.attr.colorSurface, "colorSurface");
int textColorPrimary = MaterialColors.getColor(context, android.R.attr.textColorPrimary, "textColorSecondary");
int textColorSecondary = MaterialColors.getColor(context, android.R.attr.textColorSecondary, "textColorSecondary");
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")));
views.setTextColor(R.id.screen_time_label, textColorSecondary);
views.setTextColor(R.id.screen_time, textColorPrimary);
views.setTextColor(R.id.app1_label, textColorSecondary);
views.setTextColor(R.id.app2_label, textColorSecondary);
views.setTextColor(R.id.app3_label, textColorSecondary);
views.setColorStateList(R.id.app1_time, "setBackgroundTintList", color1);
views.setColorStateList(R.id.app1_circle, "setBackgroundTintList", color1);
views.setColorStateList(R.id.app2_time, "setBackgroundTintList", color2);
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);
}
views.setOnClickPendingIntent(android.R.id.background, pendingIntent);
views.setOnClickPendingIntent(R.id.widget_background, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/app_widget_screen_time.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widget_background"
style="@style/Widget.AppTheme.AppWidget"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/app_widget_screen_time_large.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widget_background"
style="@style/Widget.AppTheme.AppWidget"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/layout/app_widget_screen_time_small.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widget_background"
style="@style/Widget.AppTheme.AppWidget"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down Expand Up @@ -31,13 +32,13 @@
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:autoSizeMaxTextSize="38sp"
android:autoSizeMinTextSize="16sp"
android:autoSizeTextType="uniform"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="?android:attr/textColorPrimary"
android:autoSizeMinTextSize="16sp"
android:autoSizeMaxTextSize="38sp"
android:autoSizeTextType="uniform"
android:textSize="27sp"
android:textStyle="bold"
tools:targetApi="o"
Expand Down

0 comments on commit 1adcac5

Please sign in to comment.