Clean up code
This commit is contained in:
parent
800bfead67
commit
13922f0ae6
2 changed files with 111 additions and 104 deletions
|
@ -11,6 +11,7 @@ import android.content.Intent;
|
|||
import android.database.Cursor;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Typeface;
|
||||
import android.net.Uri;
|
||||
import android.os.Binder;
|
||||
import android.text.SpannableString;
|
||||
import android.text.style.StyleSpan;
|
||||
|
@ -25,7 +26,6 @@ import com.fsck.k9.provider.MessageProvider;
|
|||
|
||||
public class MailListRemoteViewFactory implements RemoteViewsService.RemoteViewsFactory {
|
||||
private static String[] MAIL_LIST_PROJECTIONS = {
|
||||
MessageProvider.MessageColumns._ID,
|
||||
MessageProvider.MessageColumns.SENDER,
|
||||
MessageProvider.MessageColumns.SEND_DATE,
|
||||
MessageProvider.MessageColumns.SUBJECT,
|
||||
|
@ -36,47 +36,57 @@ public class MailListRemoteViewFactory implements RemoteViewsService.RemoteViews
|
|||
};
|
||||
|
||||
|
||||
private Context context;
|
||||
private ArrayList<MailItem> mailItems;
|
||||
private int count;
|
||||
private final Context context;
|
||||
private final Calendar calendar;
|
||||
private final ArrayList<MailItem> mailItems = new ArrayList<>(25);
|
||||
private boolean senderAboveSubject;
|
||||
|
||||
|
||||
public MailListRemoteViewFactory(Context context) {
|
||||
this.context = context;
|
||||
calendar = Calendar.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
mailItems = new ArrayList<>(25);
|
||||
senderAboveSubject = K9.messageListSenderAboveSubject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataSetChanged() {
|
||||
final long identityToken = Binder.clearCallingIdentity();
|
||||
mailItems.clear();
|
||||
Cursor cursor = context.getContentResolver().query(
|
||||
MessageProvider.CONTENT_URI.buildUpon().appendPath("inbox_messages").build(),
|
||||
MAIL_LIST_PROJECTIONS,
|
||||
null,
|
||||
null,
|
||||
MessageProvider.MessageColumns.SEND_DATE + " DESC");
|
||||
while (cursor.moveToNext()) {
|
||||
final String id = cursor.getString(0);
|
||||
final String sender = cursor.getString(1);
|
||||
final String date = cursor.getString(2);
|
||||
final String subject = cursor.getString(3);
|
||||
final String preview = cursor.getString(4);
|
||||
final String unread = cursor.getString(5);
|
||||
final String hasAttachment = cursor.getString(6);
|
||||
final String uri = cursor.getString(7);
|
||||
mailItems.add(new MailItem(id, sender, date, subject, preview, unread, hasAttachment, uri));
|
||||
long identityToken = Binder.clearCallingIdentity();
|
||||
try {
|
||||
loadMessageList();
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(identityToken);
|
||||
}
|
||||
count = cursor.getCount();
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
Binder.restoreCallingIdentity(identityToken);
|
||||
private void loadMessageList() {
|
||||
mailItems.clear();
|
||||
|
||||
Uri unifiedInboxUri = MessageProvider.CONTENT_URI.buildUpon().appendPath("inbox_messages").build();
|
||||
Cursor cursor = context.getContentResolver().query(unifiedInboxUri, MAIL_LIST_PROJECTIONS, null, null, null);
|
||||
|
||||
if (cursor == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
while (cursor.moveToNext()) {
|
||||
String sender = cursor.getString(0);
|
||||
long date = cursor.isNull(1) ? 0L : cursor.getLong(1);
|
||||
String subject = cursor.getString(2);
|
||||
String preview = cursor.getString(3);
|
||||
boolean unread = toBoolean(cursor.getString(4));
|
||||
boolean hasAttachment = toBoolean(cursor.getString(5));
|
||||
Uri viewUri = Uri.parse(cursor.getString(6));
|
||||
|
||||
mailItems.add(new MailItem(sender, date, subject, preview, unread, hasAttachment, viewUri));
|
||||
}
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -85,7 +95,7 @@ public class MailListRemoteViewFactory implements RemoteViewsService.RemoteViews
|
|||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return count;
|
||||
return mailItems.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -93,10 +103,9 @@ public class MailListRemoteViewFactory implements RemoteViewsService.RemoteViews
|
|||
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.mail_list_item);
|
||||
MailItem item = mailItems.get(position);
|
||||
|
||||
CharSequence sender = Boolean.valueOf(item.unread) ? bold(item.sender) : item.sender;
|
||||
CharSequence subject = Boolean.valueOf(item.unread) ? bold(item.subject) : item.subject;
|
||||
CharSequence sender = item.unread ? bold(item.sender) : item.sender;
|
||||
CharSequence subject = item.unread ? bold(item.subject) : item.subject;
|
||||
|
||||
/* Populate the views from the mailItem object */
|
||||
if (senderAboveSubject) {
|
||||
remoteView.setTextViewText(R.id.sender, sender);
|
||||
remoteView.setTextViewText(R.id.mail_subject, subject);
|
||||
|
@ -113,7 +122,7 @@ public class MailListRemoteViewFactory implements RemoteViewsService.RemoteViews
|
|||
remoteView.setTextColor(R.id.mail_date, textColor);
|
||||
remoteView.setTextColor(R.id.mail_preview, textColor);
|
||||
|
||||
if (item.hasAttachment()) {
|
||||
if (item.hasAttachment) {
|
||||
remoteView.setInt(R.id.attachment, "setVisibility", View.VISIBLE);
|
||||
} else {
|
||||
remoteView.setInt(R.id.attachment, "setVisibility", View.GONE);
|
||||
|
@ -154,54 +163,42 @@ public class MailListRemoteViewFactory implements RemoteViewsService.RemoteViews
|
|||
return spannableString;
|
||||
}
|
||||
|
||||
private static class MailItem {
|
||||
private static Calendar cl = Calendar.getInstance();
|
||||
private boolean toBoolean(String value) {
|
||||
return Boolean.valueOf(value);
|
||||
}
|
||||
|
||||
|
||||
private String id;
|
||||
private String date;
|
||||
private String sender;
|
||||
private String preview;
|
||||
private String subject;
|
||||
private String unread;
|
||||
private String hasAttachment;
|
||||
private String uri;
|
||||
private class MailItem {
|
||||
final long date;
|
||||
final String sender;
|
||||
final String preview;
|
||||
final String subject;
|
||||
final boolean unread;
|
||||
final boolean hasAttachment;
|
||||
final Uri uri;
|
||||
|
||||
|
||||
public MailItem(String id, String sender, String date, String subject, String preview, String unread,
|
||||
String hasAttachment, String uri) {
|
||||
this.id = id;
|
||||
MailItem(String sender, long date, String subject, String preview, boolean unread, boolean hasAttachment,
|
||||
Uri viewUri) {
|
||||
this.sender = sender;
|
||||
this.date = date;
|
||||
this.preview = preview;
|
||||
this.subject = subject;
|
||||
this.unread = unread;
|
||||
this.uri = uri;
|
||||
this.uri = viewUri;
|
||||
this.hasAttachment = hasAttachment;
|
||||
}
|
||||
|
||||
public int getColor() {
|
||||
if (Boolean.valueOf(unread)) {
|
||||
return Color.BLACK;
|
||||
} else {
|
||||
/* light_black */
|
||||
return Color.parseColor("#444444");
|
||||
}
|
||||
int getColor() {
|
||||
return unread ? Color.BLACK : Color.parseColor("#444444");
|
||||
}
|
||||
|
||||
public String getDateFormatted(String format) {
|
||||
// set default format if null is passed
|
||||
if (format.isEmpty()) {
|
||||
format = "%d %s";
|
||||
}
|
||||
cl.setTimeInMillis(Long.valueOf(date));
|
||||
String getDateFormatted(String format) {
|
||||
calendar.setTimeInMillis(date);
|
||||
|
||||
return String.format(format,
|
||||
cl.get(Calendar.DAY_OF_MONTH),
|
||||
cl.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault()));
|
||||
}
|
||||
|
||||
public boolean hasAttachment() {
|
||||
return Boolean.valueOf(hasAttachment);
|
||||
calendar.get(Calendar.DAY_OF_MONTH),
|
||||
calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,10 +16,9 @@ import com.fsck.k9.activity.MessageList;
|
|||
|
||||
|
||||
public class MailListViewWidgetProvider extends AppWidgetProvider {
|
||||
public static final String PACKAGE_NAME = "com.fsck.k9";
|
||||
public static String ACTION_VIEW_MAIL_ITEM = PACKAGE_NAME + ".provider.ACTION_VIEW_MAIL_ITEM";
|
||||
public static String ACTION_COMPOSE_EMAIL = PACKAGE_NAME + ".provider.ACTION_COMPOSE_EMAIL";
|
||||
public static String ACTION_UPDATE_MESSAGE_LIST = PACKAGE_NAME + ".provider.ACTION_UPDATE_MESSAGE_LIST";
|
||||
private static String ACTION_VIEW_MAIL_ITEM = "VIEW_MAIL_ITEM";
|
||||
private static String ACTION_COMPOSE_EMAIL = "COMPOSE_EMAIL";
|
||||
private static String ACTION_UPDATE_MESSAGE_LIST = "UPDATE_MESSAGE_LIST";
|
||||
|
||||
|
||||
public static void updateMailViewList(Context context) {
|
||||
|
@ -34,7 +33,14 @@ public class MailListViewWidgetProvider extends AppWidgetProvider {
|
|||
context.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
for (int appWidgetId : appWidgetIds) {
|
||||
updateAppWidget(context, appWidgetManager, appWidgetId);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
|
||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.mail_list_view_widget_layout);
|
||||
|
||||
views.setTextViewText(R.id.folder, context.getString(R.string.integrated_inbox_title));
|
||||
|
@ -44,50 +50,54 @@ public class MailListViewWidgetProvider extends AppWidgetProvider {
|
|||
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
|
||||
views.setRemoteAdapter(R.id.listView, intent);
|
||||
|
||||
Intent clickIntent = new Intent(context, MailListViewWidgetProvider.class);
|
||||
clickIntent.setPackage(PACKAGE_NAME);
|
||||
clickIntent.setAction(ACTION_VIEW_MAIL_ITEM);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, MessageList.REQUEST_MASK_PENDING_INTENT,
|
||||
clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
views.setPendingIntentTemplate(R.id.listView, pendingIntent);
|
||||
PendingIntent viewAction = viewActionTemplatePendingIntent(context);
|
||||
views.setPendingIntentTemplate(R.id.listView, viewAction);
|
||||
|
||||
Intent composeIntent = new Intent(context, MailListViewWidgetProvider.class);
|
||||
composeIntent.setPackage(PACKAGE_NAME);
|
||||
composeIntent.setAction(ACTION_COMPOSE_EMAIL);
|
||||
|
||||
PendingIntent newMailIntent = PendingIntent.getBroadcast(context, 0, composeIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
views.setOnClickPendingIntent(R.id.new_message, newMailIntent);
|
||||
PendingIntent composeAction = composeActionPendingIntent(context);
|
||||
views.setOnClickPendingIntent(R.id.new_message, composeAction);
|
||||
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
for (int appWidgetId : appWidgetIds) {
|
||||
updateAppWidget(context, appWidgetManager, appWidgetId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
super.onReceive(context, intent);
|
||||
|
||||
if (intent.getAction().equals(ACTION_VIEW_MAIL_ITEM)) {
|
||||
Intent viewMailIntent = new Intent(context, MessageList.class);
|
||||
viewMailIntent.setAction(Intent.ACTION_VIEW);
|
||||
viewMailIntent.setData(Uri.parse(intent.getStringExtra(AppWidgetManager.EXTRA_CUSTOM_INFO)));
|
||||
viewMailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(viewMailIntent);
|
||||
} else if (intent.getAction().equals(ACTION_UPDATE_MESSAGE_LIST)) {
|
||||
AppWidgetManager.getInstance(context)
|
||||
.notifyAppWidgetViewDataChanged(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS),
|
||||
R.id.listView);
|
||||
} else if (intent.getAction().equals(ACTION_COMPOSE_EMAIL)) {
|
||||
Intent newMessage = new Intent(context, MessageCompose.class)
|
||||
.setAction(MessageCompose.ACTION_COMPOSE)
|
||||
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(newMessage);
|
||||
String action = intent.getAction();
|
||||
if (action.equals(ACTION_UPDATE_MESSAGE_LIST)) {
|
||||
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||
int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
|
||||
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listView);
|
||||
} else if (action.equals(ACTION_VIEW_MAIL_ITEM)) {
|
||||
String messageUri = intent.getStringExtra(AppWidgetManager.EXTRA_CUSTOM_INFO);
|
||||
Intent viewMessageIntent = new Intent(context, MessageList.class);
|
||||
viewMessageIntent.setAction(Intent.ACTION_VIEW);
|
||||
viewMessageIntent.setData(Uri.parse(messageUri));
|
||||
startActivity(context, viewMessageIntent);
|
||||
} else if (action.equals(ACTION_COMPOSE_EMAIL)) {
|
||||
Intent composeIntent = new Intent(context, MessageCompose.class);
|
||||
composeIntent.setAction(MessageCompose.ACTION_COMPOSE);
|
||||
startActivity(context, composeIntent);
|
||||
}
|
||||
}
|
||||
|
||||
private void startActivity(Context context, Intent intent) {
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
|
||||
private PendingIntent viewActionTemplatePendingIntent(Context context) {
|
||||
Intent intent = new Intent(context, MailListViewWidgetProvider.class);
|
||||
intent.setAction(ACTION_VIEW_MAIL_ITEM);
|
||||
|
||||
return PendingIntent.getBroadcast(context, MessageList.REQUEST_MASK_PENDING_INTENT, intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
|
||||
private PendingIntent composeActionPendingIntent(Context context) {
|
||||
Intent intent = new Intent(context, MailListViewWidgetProvider.class);
|
||||
intent.setAction(ACTION_COMPOSE_EMAIL);
|
||||
|
||||
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue