auto updating list widget
This commit is contained in:
@@ -8,6 +8,7 @@ import net.headlezz.notificationlogger.logger.BlacklistItem;
|
||||
import net.headlezz.notificationlogger.logger.BlacklistTable;
|
||||
import net.headlezz.notificationlogger.logger.LoggedNotification;
|
||||
import net.headlezz.notificationlogger.logger.Logged_notificationTable;
|
||||
import net.headlezz.notificationlogger.widget.WidgetUtils;
|
||||
|
||||
public class DatabaseUtils {
|
||||
|
||||
@@ -16,6 +17,7 @@ public class DatabaseUtils {
|
||||
Logged_notificationTable.CONTENT_URI,
|
||||
Logged_notificationTable.getContentValues(ln, false)
|
||||
);
|
||||
WidgetUtils.notifyWidgetsUpdate(context);
|
||||
}
|
||||
|
||||
public static LoggedNotification getNotificationById(Context context, long id) {
|
||||
@@ -31,6 +33,7 @@ public class DatabaseUtils {
|
||||
// there are no notifications with negative ids, so we use this to select all rows
|
||||
String whereClause = Logged_notificationTable.FIELD_NOTIFICATION_ID + " != -1";
|
||||
resolver.delete(Logged_notificationTable.CONTENT_URI, whereClause, new String[0]);
|
||||
WidgetUtils.notifyWidgetsUpdate(context);
|
||||
}
|
||||
|
||||
public static boolean isPackageBlacklisted(Context context, String packageName) {
|
||||
@@ -71,7 +74,7 @@ public class DatabaseUtils {
|
||||
context.getContentResolver().delete(
|
||||
BlacklistTable.CONTENT_URI,
|
||||
BlacklistTable.FIELD_PACKAGE_NAME + " = ?",
|
||||
new String[] {packageName}
|
||||
new String[]{packageName}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -79,7 +82,8 @@ public class DatabaseUtils {
|
||||
context.getContentResolver().delete(
|
||||
Logged_notificationTable.CONTENT_URI,
|
||||
Logged_notificationTable.FIELD__ID + " = ?",
|
||||
new String[] {String.valueOf(id)}
|
||||
new String[]{String.valueOf(id)}
|
||||
);
|
||||
WidgetUtils.notifyWidgetsUpdate(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package net.headlezz.notificationlogger.widget;
|
||||
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProvider;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import net.headlezz.notificationlogger.R;
|
||||
|
||||
public class ListWidgetProvider extends AppWidgetProvider {
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
for(int widgetId : appWidgetIds) {
|
||||
RemoteViews rvs = updateWidgetListView(context, widgetId);
|
||||
appWidgetManager.updateAppWidget(appWidgetIds, rvs);
|
||||
}
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||
}
|
||||
|
||||
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
|
||||
RemoteViews rvs = new RemoteViews(context.getPackageName(), R.layout.widget_list);
|
||||
|
||||
Intent serviceIntent = new Intent(context, ListWidgetService.class);
|
||||
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
|
||||
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
|
||||
|
||||
rvs.setRemoteAdapter(R.id.list_widget_listView, serviceIntent);
|
||||
rvs.setEmptyView(R.id.list_widget_listView, R.id.list_widget_empty_view);
|
||||
|
||||
|
||||
|
||||
return rvs;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package net.headlezz.notificationlogger.widget;
|
||||
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.Intent;
|
||||
import android.widget.RemoteViewsService;
|
||||
|
||||
public class ListWidgetService extends RemoteViewsService {
|
||||
@Override
|
||||
public RemoteViewsFactory onGetViewFactory(Intent intent) {
|
||||
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||
return new ListWidgetViewFactory(getApplicationContext(), appWidgetId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package net.headlezz.notificationlogger.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.text.format.DateUtils;
|
||||
import android.widget.RemoteViews;
|
||||
import android.widget.RemoteViewsService;
|
||||
|
||||
import net.headlezz.notificationlogger.R;
|
||||
import net.headlezz.notificationlogger.logger.LoggedNotification;
|
||||
import net.headlezz.notificationlogger.logger.Logged_notificationTable;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
public class ListWidgetViewFactory implements RemoteViewsService.RemoteViewsFactory {
|
||||
|
||||
int mAppWidgetId;
|
||||
Context mContext;
|
||||
|
||||
Cursor mCursor;
|
||||
|
||||
public ListWidgetViewFactory(Context context, int appWidgetId) {
|
||||
mContext = context;
|
||||
this.mAppWidgetId = appWidgetId;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate() { /** ignored **/ }
|
||||
|
||||
@Override
|
||||
public void onDataSetChanged() {
|
||||
Timber.d("ON DATA SET CHANGED");
|
||||
if(mCursor != null) {
|
||||
mCursor.close();
|
||||
}
|
||||
mCursor = mContext.getContentResolver().query(Logged_notificationTable.CONTENT_URI, null, null, null, Logged_notificationTable.FIELD_DATE + " DESC");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if(mCursor != null)
|
||||
mCursor.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
if(mCursor == null) {
|
||||
Timber.d("cursor is null");
|
||||
return 0;
|
||||
}
|
||||
return mCursor.getCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteViews getViewAt(int position) {
|
||||
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_list_item);
|
||||
mCursor.moveToPosition(position);
|
||||
|
||||
LoggedNotification notification = Logged_notificationTable.getRow(mCursor, false);
|
||||
|
||||
rv.setTextViewText(R.id.widget_list_item_title, notification.appName);
|
||||
rv.setTextViewText(R.id.widget_list_item_notification, notification.title + ": " + notification.message);
|
||||
rv.setTextViewText(R.id.widget_list_item_date, DateUtils.formatDateTime(mContext, notification.date, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE));
|
||||
return rv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteViews getLoadingView() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getViewTypeCount() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasStableIds() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package net.headlezz.notificationlogger.widget;
|
||||
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
|
||||
import net.headlezz.notificationlogger.R;
|
||||
|
||||
public class WidgetUtils {
|
||||
|
||||
public static void notifyWidgetsUpdate(Context context) {
|
||||
AppWidgetManager manager = AppWidgetManager.getInstance(context);
|
||||
int[] appWidgetIds = manager.getAppWidgetIds(new ComponentName(context, ListWidgetProvider.class));
|
||||
manager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.list_widget_listView);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user