|
|
|
@ -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
|
|
|
|
|