Fixed/simplified unified widget update

pull/159/head
M66B 6 years ago
parent 58943f1291
commit 92332a7c78

@ -273,7 +273,7 @@ public interface DaoMessage {
" ORDER BY message.received")
LiveData<List<TupleMessageEx>> liveUnseenNotify();
@Query("SELECT message.*" +
String widget = "SELECT message.*" +
" FROM message" +
" JOIN account ON account.id = message.account" +
" JOIN folder ON folder.id = message.folder" +
@ -281,9 +281,14 @@ public interface DaoMessage {
" AND folder.unified" +
" AND message.ui_hide = 0" +
" AND message.ui_snoozed IS NULL" +
" ORDER BY message.received DESC")
" ORDER BY message.received DESC";
@Query(widget)
LiveData<List<EntityMessage>> liveWidgetUnified();
@Query(widget)
List<EntityMessage> getWidgetUnified();
@Query("SELECT COUNT(message.id) FROM message" +
" JOIN account ON account.id = message.account" +
" JOIN folder ON folder.id = message.folder" +

@ -59,6 +59,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ExecutorService;
@ -184,9 +185,39 @@ public class ServiceSynchronize extends LifecycleService {
}
});
WorkerCleanup.queue(this);
db.message().liveWidgetUnified().observe(cowner, new Observer<List<EntityMessage>>() {
private List<EntityMessage> last = null;
@Override
public void onChanged(List<EntityMessage> messages) {
if (messages == null)
messages = new ArrayList<>();
boolean changed = false;
if (last != null && last.size() == messages.size()) {
for (int i = 0; i < last.size(); i++) {
EntityMessage m1 = last.get(i);
EntityMessage m2 = messages.get(i);
if (!m1.id.equals(m2.id) ||
!MessageHelper.equal(m1.from, m2.from) ||
!m1.received.equals(m2.received) ||
!Objects.equals(m1.subject, m2.subject) ||
m1.ui_seen != m2.ui_seen) {
changed = true;
break;
}
}
} else
changed = true;
last = messages;
if (changed)
WidgetUnified.update(ServiceSynchronize.this);
}
});
WorkerCleanup.queue(this);
}
@Override

@ -22,6 +22,7 @@ package eu.faircode.email;
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.net.Uri;
@ -57,4 +58,12 @@ public class WidgetUnified extends AppWidgetProvider {
appWidgetManager.updateAppWidget(id, views);
}
}
static void update(Context context) {
Log.i("Widget unified update");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = AppWidgetManager.getInstance(context).getAppWidgetIds(
new ComponentName(context, WidgetUnified.class));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.lv);
}
}

@ -19,11 +19,9 @@ package eu.faircode.email;
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
@ -31,94 +29,35 @@ import android.text.style.StyleSpan;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import androidx.lifecycle.Observer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static android.os.Looper.getMainLooper;
public class WidgetUnifiedRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private Context context;
private int appWidgetId;
private Handler handler;
private TwoStateOwner owner;
private List<EntityMessage> messages = new ArrayList<>();
WidgetUnifiedRemoteViewsFactory(final Context context, final int appWidgetId) {
this.appWidgetId = appWidgetId;
this.context = context;
this.handler = new Handler(getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
DB db = DB.getInstance(context);
owner = new TwoStateOwner("WidgetUnified:" + appWidgetId);
db.message().liveWidgetUnified().observe(owner, new Observer<List<EntityMessage>>() {
@Override
public void onChanged(List<EntityMessage> messages) {
if (messages == null)
messages = new ArrayList<>();
boolean changed = false;
if (WidgetUnifiedRemoteViewsFactory.this.messages.size() == messages.size()) {
for (int i = 0; i < messages.size(); i++) {
EntityMessage m1 = messages.get(i);
EntityMessage m2 = WidgetUnifiedRemoteViewsFactory.this.messages.get(i);
if (!m1.id.equals(m2.id) ||
!MessageHelper.equal(m1.from, m2.from) ||
!m1.received.equals(m2.received) ||
!Objects.equals(m1.subject, m2.subject) ||
m1.ui_seen != m2.ui_seen) {
changed = true;
break;
}
}
} else
changed = true;
WidgetUnifiedRemoteViewsFactory.this.messages = messages;
if (changed) {
Log.i("Widget factory notify changed id=" + appWidgetId);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.notifyAppWidgetViewDataChanged(new int[]{appWidgetId}, R.id.lv);
}
}
});
}
});
}
@Override
public void onCreate() {
Log.i("Widget factory create id=" + appWidgetId);
handler.post(new Runnable() {
@Override
public void run() {
owner.start();
}
});
}
@Override
public void onDataSetChanged() {
Log.i("Widget factory changed id=" + appWidgetId);
DB db = DB.getInstance(context);
messages = db.message().getWidgetUnified();
}
@Override
public void onDestroy() {
Log.i("Widget factory destroy id=" + appWidgetId);
handler.post(new Runnable() {
@Override
public void run() {
owner.destroy();
}
});
}
@Override

Loading…
Cancel
Save