Added sync on/off widget

pull/196/head
M66B 4 years ago
parent f9fd8f2247
commit cdebb39e54

@ -411,6 +411,20 @@
</intent-filter>
</receiver>
<receiver
android:name=".WidgetSync"
android:exported="true"
android:label="@string/title_widget_title_sync">
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_sync" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>
<service
android:name="WidgetUnifiedService"
android:exported="true"

@ -411,6 +411,20 @@
</intent-filter>
</receiver>
<receiver
android:name=".WidgetSync"
android:exported="true"
android:label="@string/title_widget_title_sync">
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_sync" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>
<service
android:name="WidgetUnifiedService"
android:exported="true"

@ -411,6 +411,20 @@
</intent-filter>
</receiver>
<receiver
android:name=".WidgetSync"
android:exported="true"
android:label="@string/title_widget_title_sync">
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_sync" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>
<service
android:name="WidgetUnifiedService"
android:exported="true"

@ -197,6 +197,7 @@ public class ApplicationEx extends Application
ServiceSynchronize.reschedule(this);
WorkerCleanup.init(this);
WorkerWatchdog.init(this);
WidgetSync.update(this);
break;
case "poll_interval":
case "schedule":

@ -38,10 +38,12 @@ import java.util.List;
import java.util.concurrent.ExecutorService;
public class ServiceExternal extends Service {
private static final String ACTION_POLL = BuildConfig.APPLICATION_ID + ".POLL";
private static final String ACTION_ENABLE = BuildConfig.APPLICATION_ID + ".ENABLE";
private static final String ACTION_DISABLE = BuildConfig.APPLICATION_ID + ".DISABLE";
private static final String ACTION_DISCONNECT_ME = BuildConfig.APPLICATION_ID + ".DISCONNECT.ME";
static final String ACTION_POLL = BuildConfig.APPLICATION_ID + ".POLL";
static final String ACTION_ENABLE = BuildConfig.APPLICATION_ID + ".ENABLE";
static final String ACTION_DISABLE = BuildConfig.APPLICATION_ID + ".DISABLE";
static final String ACTION_DISCONNECT_ME = BuildConfig.APPLICATION_ID + ".DISCONNECT.ME";
static final int PI_WIDGET_ENABLE = 1;
// adb shell am start-foreground-service -a eu.faircode.email.POLL --es account Gmail
// adb shell am start-foreground-service -a eu.faircode.email.ENABLE --es account Gmail
@ -79,11 +81,7 @@ public class ServiceExternal extends Service {
return START_NOT_STICKY;
final String action = intent.getAction();
boolean pro = ActivityBilling.isPro(this);
EntityLog.log(this, action + " pro=" + pro);
if (!pro)
return START_NOT_STICKY;
EntityLog.log(this, action);
final Context context = getApplicationContext();
executor.submit(new Runnable() {

@ -0,0 +1,83 @@
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.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.RemoteViews;
import androidx.preference.PreferenceManager;
public class WidgetSync extends AppWidgetProvider {
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", true);
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) {
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);
}
} catch (Throwable ex) {
Log.e(ex);
}
}
static void update(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (appWidgetManager == null) {
Log.w("No app widget manager"); // Fairphone FP2
return;
}
try {
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetSync.class));
Intent intent = new Intent(context, WidgetSync.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
context.sendBroadcast(intent);
} catch (Throwable ex) {
Log.e(ex);
/*
java.lang.RuntimeException: system server dead?
at android.appwidget.AppWidgetManager.getAppWidgetIds(AppWidgetManager.java:1053)
at eu.faircode.email.Widget.update(SourceFile:111)
at eu.faircode.email.ServiceSynchronize$6.onChanged(SourceFile:460)
at eu.faircode.email.ServiceSynchronize$6.onChanged(SourceFile:439)
at androidx.lifecycle.LiveData.considerNotify(SourceFile:131)
at androidx.lifecycle.LiveData.dispatchingValue(SourceFile:149)
at androidx.lifecycle.LiveData.setValue(SourceFile:307)
at androidx.lifecycle.LiveData$1.run(SourceFile:91)
Caused by: android.os.DeadObjectException
*/
}
}
}

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/widget_background"
android:padding="6dp">
<ImageView
android:id="@+id/ivSync"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/title_widget_title_sync"
android:padding="3dp"
android:scaleType="fitXY"
android:src="@drawable/twotone_sync_disabled_24"
android:tint="@color/colorWidgetForeground" />
</RelativeLayout>

@ -1511,6 +1511,7 @@
<string name="title_widget_title_count">New message count</string>
<string name="title_widget_title_list">Message list</string>
<string name="title_widget_title_sync">Sync on/off</string>
<string name="title_widget_account">Account</string>
<string name="title_widget_account_all">All</string>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_sync"
android:minWidth="40dp"
android:minHeight="40dp"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen" />

@ -412,6 +412,20 @@
</intent-filter>
</receiver>
<receiver
android:name=".WidgetSync"
android:exported="true"
android:label="@string/title_widget_title_sync">
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_sync" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>
<service
android:name="WidgetUnifiedService"
android:exported="true"

Loading…
Cancel
Save