Select sync widget background color

This commit is contained in:
M66B 2021-03-26 07:51:53 +01:00
parent 6e154d2b33
commit 06212ba5ff
8 changed files with 256 additions and 2 deletions

View File

@ -175,6 +175,15 @@
</intent-filter>
</activity>
<activity
android:name=".ActivityWidgetSync"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<activity
android:name=".ActivityWidgetUnified"
android:exported="true">

View File

@ -175,6 +175,15 @@
</intent-filter>
</activity>
<activity
android:name=".ActivityWidgetSync"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<activity
android:name=".ActivityWidgetUnified"
android:exported="true">

View File

@ -175,6 +175,15 @@
</intent-filter>
</activity>
<activity
android:name=".ActivityWidgetSync"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<activity
android:name=".ActivityWidgetUnified"
android:exported="true">

View File

@ -0,0 +1,137 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2021 by Marcel Bokhorst (M66B)
*/
import android.appwidget.AppWidgetManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import androidx.preference.PreferenceManager;
import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
public class ActivityWidgetSync extends ActivityBase {
private int appWidgetId;
private CheckBox cbSemiTransparent;
private Button btnColor;
private Button btnSave;
private int background = Color.TRANSPARENT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras == null) {
finish();
return;
}
if (savedInstanceState != null)
background = savedInstanceState.getInt("fair:color");
appWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
getSupportActionBar().setSubtitle(R.string.title_widget_title_sync);
setContentView(R.layout.activity_widget_sync);
cbSemiTransparent = findViewById(R.id.cbSemiTransparent);
btnColor = findViewById(R.id.btnColor);
btnSave = findViewById(R.id.btnSave);
final Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
cbSemiTransparent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
btnColor.setEnabled(!isChecked);
}
});
btnColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int editTextColor = Helper.resolveColor(ActivityWidgetSync.this, android.R.attr.editTextColor);
ColorPickerDialogBuilder
.with(ActivityWidgetSync.this)
.setTitle(R.string.title_widget_background)
.showColorEdit(true)
.setColorEditTextColor(editTextColor)
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
.density(6)
.lightnessSliderOnly()
.setPositiveButton(android.R.string.ok, new ColorPickerClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
background = selectedColor;
}
})
.setNegativeButton(R.string.title_reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
background = Color.TRANSPARENT;
}
})
.build()
.show();
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityWidgetSync.this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("widget." + appWidgetId + ".semi", cbSemiTransparent.isChecked());
editor.putInt("widget." + appWidgetId + ".background", background);
editor.apply();
WidgetSync.init(ActivityWidgetSync.this, appWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
});
btnColor.setEnabled(!cbSemiTransparent.isChecked());
setResult(RESULT_CANCELED, resultValue);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("fair:color", background);
super.onSaveInstanceState(outState);
}
}

View File

@ -26,8 +26,10 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.widget.RemoteViews;
import androidx.core.graphics.ColorUtils;
import androidx.preference.PreferenceManager;
public class WidgetSync extends AppWidgetProvider {
@ -39,17 +41,51 @@ public class WidgetSync extends AppWidgetProvider {
try {
Intent intent = new Intent(enabled ? ServiceExternal.ACTION_DISABLE : ServiceExternal.ACTION_ENABLE);
PendingIntent pi = PendingIntent.getService(context, ServiceExternal.PI_WIDGET_ENABLE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
for (int id : appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_sync);
views.setOnClickPendingIntent(R.id.ivSync, pi);
views.setImageViewResource(R.id.ivSync, enabled ? R.drawable.twotone_sync_24 : R.drawable.twotone_sync_disabled_24);
appWidgetManager.updateAppWidget(id, views);
if (!semi)
views.setInt(R.id.widget, "setBackgroundColor", background);
if (!semi && background != Color.TRANSPARENT) {
float lum = (float) ColorUtils.calculateLuminance(background);
if (lum > 0.7f)
views.setInt(R.id.ivSync, "setColorFilter", Color.BLACK);
}
appWidgetManager.updateAppWidget(appWidgetId, views);
}
} catch (Throwable ex) {
Log.e(ex);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
for (int appWidgetId : appWidgetIds) {
String prefix = "widget." + appWidgetId + ".";
for (String key : prefs.getAll().keySet())
if (key.startsWith(prefix))
editor.remove(key);
}
editor.apply();
} catch (Throwable ex) {
Log.e(ex);
}
}
static void init(Context context, int appWidgetId) {
update(context);
}
static void update(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (appWidgetManager == null) {

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<eu.faircode.email.ScrollViewEx xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp"
android:scrollbarStyle="outsideOverlay">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cbSemiTransparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_toTopOf="parent" />
<Button
android:id="@+id/btnColor"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:tag="disable"
android:text="@string/title_widget_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbSemiTransparent" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/title_save"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnColor" />
</androidx.constraintlayout.widget.ConstraintLayout>
</eu.faircode.email.ScrollViewEx>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="eu.faircode.email.ActivityWidgetSync"
android:initialLayout="@layout/widget_sync"
android:minWidth="40dp"
android:minHeight="40dp"

View File

@ -176,6 +176,15 @@
</intent-filter>
</activity>
<activity
android:name=".ActivityWidgetSync"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<activity
android:name=".ActivityWidgetUnified"
android:exported="true">