2018-10-21 18:29:28 +00:00
|
|
|
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.
|
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-10-21 18:29:28 +00:00
|
|
|
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
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-10-21 18:29:28 +00:00
|
|
|
|
2020-01-05 18:32:53 +01:00
|
|
|
Copyright 2018-2020 by Marcel Bokhorst (M66B)
|
2018-10-21 18:29:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
|
import android.appwidget.AppWidgetProvider;
|
|
|
|
|
import android.content.ComponentName;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
2019-09-23 10:57:02 +02:00
|
|
|
import android.content.SharedPreferences;
|
2020-04-08 12:08:50 +02:00
|
|
|
import android.graphics.Color;
|
2020-01-01 20:18:54 +01:00
|
|
|
import android.text.TextUtils;
|
2020-04-10 16:46:43 +02:00
|
|
|
import android.view.View;
|
2018-10-21 18:29:28 +00:00
|
|
|
import android.widget.RemoteViews;
|
|
|
|
|
|
2019-09-23 10:57:02 +02:00
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
|
2019-06-23 14:05:30 +02:00
|
|
|
import java.text.NumberFormat;
|
2020-03-28 10:20:52 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
2019-07-25 12:33:06 +02:00
|
|
|
import java.util.concurrent.ExecutorService;
|
2019-05-06 22:34:18 +02:00
|
|
|
|
2019-11-15 13:58:34 +01:00
|
|
|
public class Widget extends AppWidgetProvider {
|
2019-07-25 12:33:06 +02:00
|
|
|
private static final ExecutorService executor =
|
2019-10-10 13:26:44 +02:00
|
|
|
Helper.getBackgroundExecutor(1, "widget");
|
2019-07-25 12:33:06 +02:00
|
|
|
|
2018-10-21 18:29:28 +00:00
|
|
|
@Override
|
2018-11-05 15:28:30 +00:00
|
|
|
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
|
2019-07-25 12:33:06 +02:00
|
|
|
executor.submit(new Runnable() {
|
2018-11-05 15:28:30 +00:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2019-09-23 10:57:02 +02:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
boolean unseen_ignored = prefs.getBoolean("unseen_ignored", false);
|
|
|
|
|
|
2020-01-01 20:18:54 +01:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
NumberFormat nf = NumberFormat.getIntegerInstance();
|
|
|
|
|
|
|
|
|
|
for (int appWidgetId : appWidgetIds) {
|
|
|
|
|
String name = prefs.getString("widget." + appWidgetId + ".name", null);
|
2020-04-08 12:24:51 +02:00
|
|
|
long account = prefs.getLong("widget." + appWidgetId + ".account", -1L);
|
2020-04-08 12:08:50 +02:00
|
|
|
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
|
2020-04-08 14:28:57 +02:00
|
|
|
int layout = prefs.getInt("widget." + appWidgetId + ".layout", 0);
|
2020-01-01 20:18:54 +01:00
|
|
|
|
2020-03-28 10:20:52 +01:00
|
|
|
List<EntityFolder> folders = db.folder().getNotifyingFolders(account);
|
|
|
|
|
if (folders == null)
|
|
|
|
|
folders = new ArrayList<>();
|
2020-01-06 09:29:19 +01:00
|
|
|
|
2020-03-14 14:43:29 +01:00
|
|
|
PendingIntent pi;
|
2020-03-28 10:20:52 +01:00
|
|
|
if (folders.size() == 1) {
|
2020-03-14 14:43:29 +01:00
|
|
|
Intent view = new Intent(context, ActivityView.class);
|
2020-03-28 10:20:52 +01:00
|
|
|
view.setAction("folder:" + folders.get(0).id);
|
2020-01-06 09:29:19 +01:00
|
|
|
view.putExtra("account", account);
|
2020-03-28 10:20:52 +01:00
|
|
|
view.putExtra("type", folders.get(0).type);
|
2020-01-06 09:29:19 +01:00
|
|
|
view.putExtra("refresh", true);
|
2020-03-14 14:43:29 +01:00
|
|
|
view.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
|
|
|
pi = PendingIntent.getActivity(context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT);
|
2020-03-28 10:20:52 +01:00
|
|
|
} else {
|
|
|
|
|
if (account < 0) {
|
|
|
|
|
Intent view = new Intent(context, ActivityView.class);
|
|
|
|
|
view.setAction("unified");
|
|
|
|
|
view.putExtra("refresh", true);
|
|
|
|
|
view.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
|
|
|
pi = PendingIntent.getActivity(context, ActivityView.REQUEST_UNIFIED, view, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
} else {
|
|
|
|
|
Intent view = new Intent(context, ActivityView.class);
|
|
|
|
|
view.setAction("folders:" + account);
|
|
|
|
|
view.putExtra("refresh", true);
|
|
|
|
|
view.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
|
|
|
pi = PendingIntent.getActivity(context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
}
|
2020-01-06 09:29:19 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 10:02:00 +01:00
|
|
|
TupleMessageStats stats = db.message().getWidgetUnseen(account < 0 ? null : account);
|
2020-01-01 20:18:54 +01:00
|
|
|
Integer unseen = (unseen_ignored ? stats.notifying : stats.unseen);
|
|
|
|
|
if (unseen == null)
|
|
|
|
|
unseen = 0;
|
|
|
|
|
|
2020-04-08 14:28:57 +02:00
|
|
|
RemoteViews views = new RemoteViews(context.getPackageName(),
|
|
|
|
|
layout == 0 ? R.layout.widget : R.layout.widget_new);
|
2020-01-01 20:18:54 +01:00
|
|
|
|
|
|
|
|
views.setOnClickPendingIntent(R.id.widget, pi);
|
|
|
|
|
|
2020-04-08 12:08:50 +02:00
|
|
|
if (!semi)
|
|
|
|
|
views.setInt(R.id.widget, "setBackgroundColor", Color.TRANSPARENT);
|
|
|
|
|
|
2020-04-11 10:53:31 +02:00
|
|
|
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.baseline_mail_outline_24
|
|
|
|
|
: R.drawable.baseline_mail_24);
|
2020-04-10 16:46:43 +02:00
|
|
|
views.setTextViewText(R.id.tvCount, unseen < 100 ? nf.format(unseen) : "99+");
|
2020-04-10 17:21:57 +02:00
|
|
|
views.setViewVisibility(R.id.tvCount, layout == 1 && unseen == 0 ? View.GONE : View.VISIBLE);
|
2020-01-01 20:18:54 +01:00
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(name)) {
|
|
|
|
|
views.setTextViewText(R.id.tvAccount, name);
|
|
|
|
|
views.setViewVisibility(R.id.tvAccount, ViewStripe.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
appWidgetManager.updateAppWidget(appWidgetId, views);
|
|
|
|
|
}
|
2018-11-05 15:28:30 +00:00
|
|
|
}
|
2019-07-25 12:33:06 +02:00
|
|
|
});
|
2018-10-21 18:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-01 20:18:54 +01:00
|
|
|
static void init(Context context, int appWidgetId) {
|
|
|
|
|
update(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void update(Context context) {
|
2018-10-21 18:29:28 +00:00
|
|
|
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
2019-10-06 11:28:38 +02:00
|
|
|
if (appWidgetManager == null) {
|
|
|
|
|
Log.w("No app widget manager"); // Fairphone FP2
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 13:58:34 +01:00
|
|
|
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, Widget.class));
|
2019-09-20 15:14:04 +02:00
|
|
|
|
2020-01-01 20:18:54 +01:00
|
|
|
Intent intent = new Intent(context, Widget.class);
|
|
|
|
|
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
|
|
|
|
|
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
|
|
|
|
|
context.sendBroadcast(intent);
|
2018-10-21 18:29:28 +00:00
|
|
|
}
|
|
|
|
|
}
|