Added day/night mode for count widget

This commit is contained in:
M66B 2022-05-06 22:25:37 +02:00
parent 7a9daf7d86
commit 4e38642fb6
9 changed files with 122 additions and 17 deletions

View File

@ -25,6 +25,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
@ -51,6 +52,7 @@ public class ActivityWidget extends ActivityBase {
private int appWidgetId;
private Spinner spAccount;
private CheckBox cbDayNight;
private CheckBox cbSemiTransparent;
private ViewButtonColor btnColor;
private View inOld;
@ -78,15 +80,19 @@ public class ActivityWidget extends ActivityBase {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
long account = prefs.getLong("widget." + appWidgetId + ".account", -1L);
boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false);
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);
int layout = prefs.getInt("widget." + appWidgetId + ".layout", 1 /* new */);
daynight = daynight && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setSubtitle(R.string.title_widget_title_count);
setContentView(R.layout.activity_widget);
spAccount = findViewById(R.id.spAccount);
cbDayNight = findViewById(R.id.cbDayNight);
cbSemiTransparent = findViewById(R.id.cbSemiTransparent);
btnColor = findViewById(R.id.btnColor);
inOld = findViewById(R.id.inOld);
@ -100,6 +106,14 @@ public class ActivityWidget extends ActivityBase {
final Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
cbDayNight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
cbSemiTransparent.setEnabled(!checked);
btnColor.setEnabled(!checked);
}
});
cbSemiTransparent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@ -167,6 +181,7 @@ public class ActivityWidget extends ActivityBase {
else
editor.remove("widget." + appWidgetId + ".name");
editor.putLong("widget." + appWidgetId + ".account", account == null ? -1L : account.id);
editor.putBoolean("widget." + appWidgetId + ".daynight", cbDayNight.isChecked());
editor.putBoolean("widget." + appWidgetId + ".semi", cbSemiTransparent.isChecked());
editor.putInt("widget." + appWidgetId + ".background", btnColor.getColor());
editor.putInt("widget." + appWidgetId + ".layout", rbNew.isChecked() ? 1 : 0);
@ -188,8 +203,12 @@ public class ActivityWidget extends ActivityBase {
((TextView) inOld.findViewById(R.id.tvCount)).setText("12");
((TextView) inNew.findViewById(R.id.tvCount)).setText("12");
cbDayNight.setChecked(daynight);
cbDayNight.setVisibility(Build.VERSION.SDK_INT < Build.VERSION_CODES.S ? View.GONE : View.VISIBLE);
cbSemiTransparent.setChecked(semi);
cbSemiTransparent.setEnabled(!daynight);
btnColor.setColor(background);
btnColor.setEnabled(!daynight);
rbOld.setChecked(layout != 1);
rbNew.setChecked(layout == 1);
setBackground();

View File

@ -27,6 +27,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;
@ -58,6 +59,7 @@ public class Widget extends AppWidgetProvider {
for (int appWidgetId : appWidgetIds) {
String name = prefs.getString("widget." + appWidgetId + ".name", null);
long account = prefs.getLong("widget." + appWidgetId + ".account", -1L);
boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false);
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);
int layout = prefs.getInt("widget." + appWidgetId + ".layout", 0);
@ -114,7 +116,12 @@ public class Widget extends AppWidgetProvider {
views.setOnClickPendingIntent(R.id.background, pi);
// Set background
if (semi)
if (!daynight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
views.setColorStateListAttr(R.id.background, "setBackgroundTintList", 0);
if (daynight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
views.setInt(R.id.background, "setBackgroundColor", Color.WHITE);
views.setColorStateListAttr(R.id.background, "setBackgroundTintList", android.R.attr.colorBackground);
} else if (semi)
if (background == Color.TRANSPARENT)
views.setInt(R.id.background, "setBackgroundResource",
R.drawable.widget_background);
@ -125,17 +132,35 @@ public class Widget extends AppWidgetProvider {
views.setInt(R.id.background, "setBackgroundColor", background);
// Set image
if (layout == 1)
views.setImageViewResource(R.id.ivMessage, unseen == 0
? R.drawable.baseline_mail_outline_widget_24
: R.drawable.baseline_mail_widget_24);
else
views.setImageViewResource(R.id.ivMessage, unseen == 0
? R.drawable.twotone_mail_outline_24
: R.drawable.baseline_mail_24);
if (daynight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (layout == 1)
views.setImageViewResource(R.id.ivMessage, unseen == 0
? R.drawable.baseline_mail_outline_widget_24_dn
: R.drawable.baseline_mail_widget_24_dn);
else
views.setImageViewResource(R.id.ivMessage, unseen == 0
? R.drawable.twotone_mail_outline_24_dn
: R.drawable.baseline_mail_24_dn);
} else {
if (layout == 1)
views.setImageViewResource(R.id.ivMessage, unseen == 0
? R.drawable.baseline_mail_outline_widget_24
: R.drawable.baseline_mail_widget_24);
else
views.setImageViewResource(R.id.ivMessage, unseen == 0
? R.drawable.twotone_mail_outline_24
: R.drawable.baseline_mail_24);
}
// Set color
if (background == Color.TRANSPARENT) {
if (daynight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
views.setColorAttr(R.id.ivMessage, "setColorFilter", android.R.attr.textColorPrimary);
if (layout == 0)
views.setColorStateListAttr(R.id.tvCount, "setTextColor", android.R.attr.textColorPrimary);
else
views.setTextColor(R.id.tvCount, colorWidgetForeground);
views.setColorStateListAttr(R.id.tvAccount, "setTextColor", android.R.attr.textColorPrimary);
} else if (background == Color.TRANSPARENT) {
views.setInt(R.id.ivMessage, "setColorFilter", colorWidgetForeground);
views.setTextColor(R.id.tvCount, colorWidgetForeground);
views.setTextColor(R.id.tvAccount, colorWidgetForeground);

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
android:tint="?android:textColorPrimary">
<path
android:fillColor="@android:color/white"
android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/>
</vector>

View File

@ -0,0 +1,14 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="20dp"
android:height="16dp"
android:tint="?android:textColorPrimary"
android:viewportWidth="20.0"
android:viewportHeight="16.0">
<group
android:translateX="-2.0"
android:translateY="-4.0">
<path
android:fillColor="@android:color/white"
android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,18L4,18L4,8l8,5 8,-5v10zM12,11L4,6h16l-8,5z" />
</group>
</vector>

View File

@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="20dp"
android:height="16dp"
android:tint="?android:textColorPrimary"
android:viewportWidth="20.0"
android:viewportHeight="16.0">
<group
android:translateX="-2.0"
android:translateY="-4.0">
<path
android:fillColor="@android:color/white"
android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z" />
</group>
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?android:textColorPrimary">
<path
android:fillColor="@android:color/white"
android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,18L4,18L4,8l8,5 8,-5v10zM12,11L4,6h16l-8,5z"/>
</vector>

View File

@ -29,22 +29,35 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvAccount" />
<CheckBox
android:id="@+id/cbDayNight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:checked="true"
android:text="@string/title_widget_day_night"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/spAccount" />
<CheckBox
android:id="@+id/cbSemiTransparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:checked="true"
android:text="@string/title_widget_semi_transparent"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/spAccount" />
app:layout_constraintTop_toBottomOf="@id/cbDayNight" />
<eu.faircode.email.ViewButtonColor
android:id="@+id/btnColor"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:paddingHorizontal="6dp"
android:tag="disable"

View File

@ -4,7 +4,8 @@
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/widget_background">
android:background="?android:colorBackground"
android:theme="@style/Theme.AppCompat.DayNight">
<LinearLayout
android:id="@+id/content"
@ -20,8 +21,7 @@
android:layout_gravity="center_horizontal"
android:layout_weight="2"
android:contentDescription="@string/title_legend_count"
android:src="@drawable/baseline_mail_24"
app:tint="@color/colorWidgetForeground" />
android:src="@drawable/baseline_mail_24" />
<TextView
android:id="@+id/tvCount"
@ -34,7 +34,6 @@
android:lines="1"
android:text="\?"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/colorWidgetForeground"
android:textSize="18dp"
android:textStyle="bold" />
@ -50,7 +49,6 @@
android:lines="1"
android:text="account"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/colorWidgetForeground"
android:textSize="12dp"
android:visibility="gone" />
</LinearLayout>

View File

@ -4,7 +4,8 @@
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/widget_background">
android:background="?android:colorBackground"
android:theme="@style/Theme.AppCompat.DayNight">
<RelativeLayout
android:id="@+id/content"