From b3c7bd1429b7c265a21ef16588a060132f7d7d1f Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 21 Oct 2018 18:29:28 +0000 Subject: [PATCH] Added widget --- app/src/main/AndroidManifest.xml | 15 ++++++ .../eu/faircode/email/ServiceSynchronize.java | 4 ++ .../main/java/eu/faircode/email/Widget.java | 53 +++++++++++++++++++ app/src/main/res/layout/widget.xml | 26 +++++++++ app/src/main/res/values/colors.xml | 3 ++ app/src/main/res/xml/widget.xml | 8 +++ 6 files changed, 109 insertions(+) create mode 100644 app/src/main/java/eu/faircode/email/Widget.java create mode 100644 app/src/main/res/layout/widget.xml create mode 100644 app/src/main/res/xml/widget.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0d8048b2..521ee2fc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,6 +11,10 @@ + + + + + + + + + diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index a50a771b..2ba4cc0b 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -215,6 +215,8 @@ public class ServiceSynchronize extends LifecycleService { serviceManager.onLost(null); + Widget.update(this, -1); + stopForeground(true); NotificationManager nm = getSystemService(NotificationManager.class); @@ -339,6 +341,8 @@ public class ServiceSynchronize extends LifecycleService { // https://developer.android.com/training/notify-user/group List notifications = new ArrayList<>(); + Widget.update(this, messages.size()); + if (messages.size() == 0) return notifications; diff --git a/app/src/main/java/eu/faircode/email/Widget.java b/app/src/main/java/eu/faircode/email/Widget.java new file mode 100644 index 00000000..2d1bb9f7 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/Widget.java @@ -0,0 +1,53 @@ +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. + + NetGuard 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 NetGuard. If not, see . + + Copyright 2018 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.widget.RemoteViews; + +public class Widget extends AppWidgetProvider { + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + update(appWidgetIds, appWidgetManager, context, -1); + } + + static void update(Context context, int count) { + AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); + int appWidgetIds[] = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, Widget.class)); + update(appWidgetIds, appWidgetManager, context, count); + } + + private static void update(int[] appWidgetIds, AppWidgetManager appWidgetManager, Context context, int count) { + Intent intent = new Intent(context, ActivityView.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + PendingIntent pi = PendingIntent.getActivity(context, ActivityView.REQUEST_UNIFIED, intent, PendingIntent.FLAG_UPDATE_CURRENT); + for (int id : appWidgetIds) { + RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); + views.setOnClickPendingIntent(R.id.widget, pi); + views.setTextViewText(R.id.tvCount, count < 0 ? "?" : Integer.toString(count)); + appWidgetManager.updateAppWidget(id, views); + } + } +} diff --git a/app/src/main/res/layout/widget.xml b/app/src/main/res/layout/widget.xml new file mode 100644 index 00000000..bf75d6ef --- /dev/null +++ b/app/src/main/res/layout/widget.xml @@ -0,0 +1,26 @@ + + + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index cbafc39c..55449864 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -7,6 +7,9 @@ #fff #ccc + #fff + #3f000000 + #000 #ccc #aaa diff --git a/app/src/main/res/xml/widget.xml b/app/src/main/res/xml/widget.xml new file mode 100644 index 00000000..e9727198 --- /dev/null +++ b/app/src/main/res/xml/widget.xml @@ -0,0 +1,8 @@ + + \ No newline at end of file