Files
FairEmail/app/src/main/java/eu/faircode/email/Widget.java

80 lines
3.0 KiB
Java
Raw Normal View History

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
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 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;
import android.widget.RemoteViews;
2019-06-23 14:05:30 +02:00
import java.text.NumberFormat;
2019-07-25 12:33:06 +02:00
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
2019-05-06 22:34:18 +02:00
2018-10-21 18:29:28 +00:00
public class Widget extends AppWidgetProvider {
2019-07-25 12:33:06 +02:00
private static final ExecutorService executor =
Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
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() {
DB db = DB.getInstance(context);
2019-07-30 11:31:32 +02:00
update(context, appWidgetManager, appWidgetIds, db.message().getUnseenUnified());
2018-11-05 15:28:30 +00:00
}
2019-07-25 12:33:06 +02:00
});
2018-10-21 18:29:28 +00:00
}
static void update(Context context, int count) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
2019-06-23 14:05:30 +02:00
int[] appWidgetIds = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, Widget.class));
2019-07-30 11:31:32 +02:00
update(context, appWidgetManager, appWidgetIds, count);
2018-10-21 18:29:28 +00:00
}
2019-07-30 11:31:32 +02:00
private static void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, int count) {
2019-09-20 15:14:04 +02:00
NumberFormat nf = NumberFormat.getIntegerInstance();
2018-12-06 15:27:30 +01:00
Intent view = new Intent(context, ActivityView.class);
view.setAction("unified");
2019-08-12 15:18:35 +02:00
view.putExtra("refresh", true);
2018-12-06 15:27:30 +01:00
view.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(context, ActivityView.REQUEST_UNIFIED, view, PendingIntent.FLAG_UPDATE_CURRENT);
2019-06-23 14:05:30 +02:00
2019-09-20 15:14:04 +02:00
for (int appWidgetId : appWidgetIds) {
2018-10-21 18:29:28 +00:00
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
2019-06-23 14:05:30 +02:00
2018-10-21 18:29:28 +00:00
views.setOnClickPendingIntent(R.id.widget, pi);
2019-06-23 14:05:30 +02:00
if (count < 0)
views.setTextViewText(R.id.tvCount, "?");
2019-06-23 16:14:30 +02:00
else if (count > 99)
2019-06-23 14:05:30 +02:00
views.setTextViewText(R.id.tvCount, "");
else
views.setTextViewText(R.id.tvCount, nf.format(count));
2019-09-20 15:14:04 +02:00
appWidgetManager.updateAppWidget(appWidgetId, views);
2018-10-21 18:29:28 +00:00
}
}
}