Merge branch 'widget'
This commit is contained in:
@@ -43,6 +43,18 @@
|
||||
android:name="net.headlezz.notificationlogger.logger.NotificationProvider"
|
||||
android:exported="true"
|
||||
/>
|
||||
|
||||
<!-- widget stuff -->
|
||||
<receiver android:name=".widget.ListWidgetProvider">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||
</intent-filter>
|
||||
<meta-data android:name="android.appwidget.provider"
|
||||
android:resource="@xml/widget_info" />
|
||||
</receiver>
|
||||
<service android:name=".widget.ListWidgetService"
|
||||
android:permission="android.permission.BIND_REMOTEVIEWS"
|
||||
/>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
||||
@@ -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) {
|
||||
@@ -81,5 +84,6 @@ public class DatabaseUtils {
|
||||
Logged_notificationTable.FIELD__ID + " = ?",
|
||||
new String[]{String.valueOf(id)}
|
||||
);
|
||||
WidgetUtils.notifyWidgetsUpdate(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package net.headlezz.notificationlogger.widget;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
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.MainActivity;
|
||||
import net.headlezz.notificationlogger.R;
|
||||
|
||||
public class ListWidgetProvider extends AppWidgetProvider {
|
||||
|
||||
public static final String SHOW_NOTIFICATION_ACTION = "net.headlezz.notificationlogger.listwidget.SHOW_NOTIFICATION_ACTION";
|
||||
|
||||
@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)));
|
||||
|
||||
Intent showNotificationIntent = new Intent(context, ListWidgetProvider.class);
|
||||
showNotificationIntent.setAction(ListWidgetProvider.SHOW_NOTIFICATION_ACTION);
|
||||
PendingIntent pi = PendingIntent.getBroadcast(context, 0, showNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
rvs.setPendingIntentTemplate(R.id.list_widget_listView, pi);
|
||||
|
||||
rvs.setRemoteAdapter(R.id.list_widget_listView, serviceIntent);
|
||||
rvs.setEmptyView(R.id.list_widget_listView, R.id.list_widget_empty_view);
|
||||
|
||||
return rvs;
|
||||
}
|
||||
|
||||
// receives the intent broadcast from list items
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(SHOW_NOTIFICATION_ACTION)) {
|
||||
Intent mainActivityIntent = new Intent(context, MainActivity.class);
|
||||
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(mainActivityIntent);
|
||||
}
|
||||
super.onReceive(context, intent);
|
||||
}
|
||||
}
|
||||
@@ -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,100 @@
|
||||
package net.headlezz.notificationlogger.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Typeface;
|
||||
import android.text.SpannableString;
|
||||
import android.text.format.DateUtils;
|
||||
import android.text.style.StyleSpan;
|
||||
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);
|
||||
|
||||
String title = notification.title + ":";
|
||||
SpannableString titleSpan = new SpannableString(title + " " + notification.message);
|
||||
titleSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, title.length(), 0);
|
||||
|
||||
rv.setTextViewText(R.id.widget_list_item_notification, titleSpan);
|
||||
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));
|
||||
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(Logged_notificationTable.FIELD__ID, notification.id);
|
||||
rv.setOnClickFillInIntent(R.id.widget_list_item, intent);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
BIN
app/src/main/res/drawable/widget_list_preview.png
Normal file
BIN
app/src/main/res/drawable/widget_list_preview.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
37
app/src/main/res/layout/widget_list.xml
Normal file
37
app/src/main/res/layout/widget_list.xml
Normal file
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:ellipsize="end"
|
||||
android:maxLines="1"
|
||||
android:padding="16dp"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Title"
|
||||
android:background="@color/colorPrimary"
|
||||
android:text="@string/widget_list_title"
|
||||
android:textColor="@android:color/white"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<!-- ListView to be shown on widget -->
|
||||
<ListView
|
||||
android:visibility="gone"
|
||||
android:dividerHeight="4dp"
|
||||
android:id="@+id/list_widget_listView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<!-- Empty view is show if list items are empty -->
|
||||
<TextView
|
||||
android:background="@android:color/white"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:id="@+id/list_widget_empty_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
android:text="@string/widget_list_empty"
|
||||
android:textColor="@android:color/black" />
|
||||
|
||||
</LinearLayout>
|
||||
51
app/src/main/res/layout/widget_list_item.xml
Normal file
51
app/src/main/res/layout/widget_list_item.xml
Normal file
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:background="@android:color/white"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/widget_list_item"
|
||||
android:padding="8dp">
|
||||
|
||||
<TextView
|
||||
android:layout_alignParentStart="true"
|
||||
android:maxLines="1"
|
||||
android:ellipsize="end"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Title"
|
||||
android:id="@+id/widget_list_item_title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:clickable="false"
|
||||
android:textColor="@android:color/black"
|
||||
android:textStyle="bold"
|
||||
tools:text="appname asd asdasqweqwe qw q asd asd aweq"
|
||||
android:layout_toStartOf="@+id/widget_list_item_date" />
|
||||
|
||||
<TextView
|
||||
android:ellipsize="end"
|
||||
android:maxLines="2"
|
||||
android:layout_alignParentStart="true"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:layout_below="@+id/widget_list_item_title"
|
||||
android:id="@+id/widget_list_item_notification"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:clickable="false"
|
||||
android:textColor="@android:color/black"
|
||||
tools:text="notification" />
|
||||
|
||||
|
||||
<TextView
|
||||
android:maxLines="2"
|
||||
android:ellipsize="end"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
|
||||
android:id="@+id/widget_list_item_date"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:clickable="false"
|
||||
android:textColor="@color/colorAccent"
|
||||
tools:text="date"
|
||||
android:layout_alignTop="@+id/widget_list_item_title"
|
||||
android:layout_alignParentEnd="true" />
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -83,6 +83,8 @@
|
||||
<string name="pref_export_snack_action">Show</string>
|
||||
<string name="pref_export_snack_show_chooser_title">Open folder</string>
|
||||
<string name="pref_export_snack_show_activity_not_found">No file browser installed.</string>
|
||||
<string name="widget_list_title">Latest Notifications</string>
|
||||
<string name="widget_list_empty">No notifications logged.</string>
|
||||
|
||||
<string-array name="notification_list_options">
|
||||
<item>Show more info</item>
|
||||
|
||||
9
app/src/main/res/xml/widget_info.xml
Normal file
9
app/src/main/res/xml/widget_info.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:minWidth="110dp"
|
||||
android:minHeight="110dp"
|
||||
android:previewImage="@drawable/widget_list_preview"
|
||||
android:initialLayout="@layout/widget_list"
|
||||
android:resizeMode="horizontal|vertical"
|
||||
android:widgetCategory="home_screen">
|
||||
</appwidget-provider>
|
||||
Reference in New Issue
Block a user