From 481253abe0f56e83fc2dda551742e532aba71f94 Mon Sep 17 00:00:00 2001 From: danijoo Date: Fri, 8 Jan 2016 01:02:14 +0100 Subject: [PATCH] DatbaseUtils class --- .../notificationlogger/DatabaseUtils.java | 78 +++++++++++++++++++ .../logger/NotificationLoggerService.java | 25 ++---- .../NotificationListFragment.java | 7 +- .../preferences/BlacklistFragment.java | 27 +------ .../preferences/PreferenceFragment.java | 20 +---- 5 files changed, 95 insertions(+), 62 deletions(-) create mode 100644 app/src/main/java/net/headlezz/notificationlogger/DatabaseUtils.java diff --git a/app/src/main/java/net/headlezz/notificationlogger/DatabaseUtils.java b/app/src/main/java/net/headlezz/notificationlogger/DatabaseUtils.java new file mode 100644 index 0000000..7baddf8 --- /dev/null +++ b/app/src/main/java/net/headlezz/notificationlogger/DatabaseUtils.java @@ -0,0 +1,78 @@ +package net.headlezz.notificationlogger; + +import android.content.ContentResolver; +import android.content.Context; +import android.database.Cursor; + +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; + +public class DatabaseUtils { + + public static void insertNotification(Context context, LoggedNotification ln) { + context.getContentResolver().insert( + Logged_notificationTable.CONTENT_URI, + Logged_notificationTable.getContentValues(ln, false) + ); + } + + public static LoggedNotification getNotificationById(Context context, long id) { + String qry = Logged_notificationTable.FIELD__ID + " = ? "; + String[] args = {String.valueOf(id)}; + Cursor cursor = context.getContentResolver().query(Logged_notificationTable.CONTENT_URI, null, qry, args, null); + return Logged_notificationTable.getRow(cursor, true); + } + + + public static void removeAllNotifications(Context context) { + ContentResolver resolver = context.getContentResolver(); + // 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]); + } + + public static boolean isPackageBlacklisted(Context context, String packageName) { + Cursor cursor = context.getContentResolver().query( + BlacklistTable.CONTENT_URI, + null, + BlacklistTable.FIELD_PACKAGE_NAME + " = ?", + new String[]{String.valueOf(packageName)}, + null, + null + ); + boolean isBlacklisted = cursor != null && cursor.getCount() > 0; + if (cursor != null) + cursor.close(); + return isBlacklisted; + } + + public static int getBlacklistItemCount(Context context) { + Cursor cursor = context.getContentResolver().query( + BlacklistTable.CONTENT_URI, + null, + null, + null, + null + ); + cursor.moveToFirst(); + return cursor.getCount(); + } + + public static void insertBlacklistItem(Context context, BlacklistItem item) { + context.getContentResolver().insert( + BlacklistTable.CONTENT_URI, + BlacklistTable.getContentValues(item, false) + ); + } + + public static void deleteBlacklistItem(Context context, String packageName) { + context.getContentResolver().delete( + BlacklistTable.CONTENT_URI, + BlacklistTable.FIELD_PACKAGE_NAME + " = ?", + new String[] {packageName} + ); + } + +} diff --git a/app/src/main/java/net/headlezz/notificationlogger/logger/NotificationLoggerService.java b/app/src/main/java/net/headlezz/notificationlogger/logger/NotificationLoggerService.java index 8539713..5349322 100644 --- a/app/src/main/java/net/headlezz/notificationlogger/logger/NotificationLoggerService.java +++ b/app/src/main/java/net/headlezz/notificationlogger/logger/NotificationLoggerService.java @@ -4,13 +4,13 @@ import android.app.Notification; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; -import android.database.Cursor; import android.os.Bundle; import android.os.IBinder; import android.preference.PreferenceManager; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; +import net.headlezz.notificationlogger.DatabaseUtils; import net.headlezz.notificationlogger.PackageUtils; import timber.log.Timber; @@ -60,27 +60,18 @@ public class NotificationLoggerService extends NotificationListenerService imple if (isBlacklisted(ln)) { Timber.d("App blacklisted. Notification not logged"); } else { - getApplicationContext().getContentResolver().insert( - Logged_notificationTable.CONTENT_URI, - Logged_notificationTable.getContentValues(ln, false) - ); + DatabaseUtils.insertNotification(getApplicationContext(), ln); Timber.d(String.format("Notificaton logged (id: %d)", ln.notificationId)); } } + @Override + public void onNotificationRemoved(StatusBarNotification sbn) { + super.onNotificationRemoved(sbn); + } + private boolean isBlacklisted(LoggedNotification ln) { - Cursor cursor = getApplicationContext().getContentResolver().query( - BlacklistTable.CONTENT_URI, - null, - BlacklistTable.FIELD_PACKAGE_NAME + " = ?", - new String[] { String.valueOf(ln.packageName) }, - null, - null - ); - boolean isBlacklisted = cursor != null && cursor.getCount() > 0; - if(cursor != null) - cursor.close(); - return isBlacklisted; + return DatabaseUtils.isPackageBlacklisted(getApplicationContext(), ln.packageName); } private LoggedNotification buildLoggedNotification(StatusBarNotification sbn) { diff --git a/app/src/main/java/net/headlezz/notificationlogger/notificationlist/NotificationListFragment.java b/app/src/main/java/net/headlezz/notificationlogger/notificationlist/NotificationListFragment.java index 27deeea..05c8e5d 100644 --- a/app/src/main/java/net/headlezz/notificationlogger/notificationlist/NotificationListFragment.java +++ b/app/src/main/java/net/headlezz/notificationlogger/notificationlist/NotificationListFragment.java @@ -13,10 +13,10 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import net.headlezz.notificationlogger.DatabaseUtils; import net.headlezz.notificationlogger.NotificationInfoDialog; import net.headlezz.notificationlogger.R; import net.headlezz.notificationlogger.logger.LoggedNotification; -import net.headlezz.notificationlogger.logger.Logged_notificationTable; import butterknife.Bind; import butterknife.ButterKnife; @@ -107,10 +107,7 @@ public class NotificationListFragment extends Fragment implements NotificationLi @Override public void onNotificationClick(long id) { - String qry = Logged_notificationTable.FIELD__ID + " = ? "; - String[] args = {String.valueOf(id)}; - Cursor cursor = getContext().getContentResolver().query(Logged_notificationTable.CONTENT_URI, null, qry, args, null); - LoggedNotification notification = Logged_notificationTable.getRow(cursor, true); + LoggedNotification notification = DatabaseUtils.getNotificationById(getContext(), id); Timber.e(notification.title); new NotificationInfoDialog(getContext(), notification).show(); diff --git a/app/src/main/java/net/headlezz/notificationlogger/preferences/BlacklistFragment.java b/app/src/main/java/net/headlezz/notificationlogger/preferences/BlacklistFragment.java index 923793b..8c7bf47 100644 --- a/app/src/main/java/net/headlezz/notificationlogger/preferences/BlacklistFragment.java +++ b/app/src/main/java/net/headlezz/notificationlogger/preferences/BlacklistFragment.java @@ -1,6 +1,5 @@ package net.headlezz.notificationlogger.preferences; -import android.content.ContentResolver; import android.content.DialogInterface; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; @@ -20,6 +19,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import net.headlezz.notificationlogger.DatabaseUtils; import net.headlezz.notificationlogger.PackageUtils; import net.headlezz.notificationlogger.R; import net.headlezz.notificationlogger.logger.BlacklistItem; @@ -147,24 +147,9 @@ public class BlacklistFragment extends Fragment implements LoaderManager.LoaderC String packageName = packageNames.get(which); BlacklistItem item = new BlacklistItem(); item.packageName = packageName; - ContentResolver cr = getContext().getContentResolver(); - Cursor cursor = cr.query( - BlacklistTable.CONTENT_URI, - null, - BlacklistTable.FIELD_PACKAGE_NAME + " = ?", - new String[] { packageName }, - null, - null - ); - boolean blacklisted = cursor.getCount() > 0; - cursor.close(); - if(!blacklisted) { - getContext().getContentResolver().insert( - BlacklistTable.CONTENT_URI, - BlacklistTable.getContentValues(item, false) - ); + if(!DatabaseUtils.isPackageBlacklisted(getContext(), packageName)) { + DatabaseUtils.insertBlacklistItem(getContext(), item); } - cursor.close(); } }) .create().show(); @@ -186,11 +171,7 @@ public class BlacklistFragment extends Fragment implements LoaderManager.LoaderC .setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - getContext().getContentResolver().delete( - BlacklistTable.CONTENT_URI, - BlacklistTable.FIELD_PACKAGE_NAME + " = ?", - new String[] {String.valueOf(item.packageName)} - ); + DatabaseUtils.deleteBlacklistItem(getContext(), item.packageName); } }) .setNegativeButton(getContext().getString(R.string.cancel), null) diff --git a/app/src/main/java/net/headlezz/notificationlogger/preferences/PreferenceFragment.java b/app/src/main/java/net/headlezz/notificationlogger/preferences/PreferenceFragment.java index 1c69db2..6a7aafd 100644 --- a/app/src/main/java/net/headlezz/notificationlogger/preferences/PreferenceFragment.java +++ b/app/src/main/java/net/headlezz/notificationlogger/preferences/PreferenceFragment.java @@ -1,9 +1,7 @@ package net.headlezz.notificationlogger.preferences; import android.app.Activity; -import android.content.ContentResolver; import android.content.DialogInterface; -import android.database.Cursor; import android.os.AsyncTask; import android.os.Bundle; import android.support.design.widget.Snackbar; @@ -12,9 +10,8 @@ import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceFragmentCompat; import android.text.Html; +import net.headlezz.notificationlogger.DatabaseUtils; import net.headlezz.notificationlogger.R; -import net.headlezz.notificationlogger.logger.BlacklistTable; -import net.headlezz.notificationlogger.logger.Logged_notificationTable; public class PreferenceFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceClickListener { @@ -46,15 +43,7 @@ public class PreferenceFragment extends PreferenceFragmentCompat implements Pref @Override protected Integer doInBackground(Void... params) { - Cursor cursor = getContext().getContentResolver().query( - BlacklistTable.CONTENT_URI, - null, - null, - null, - null - ); - cursor.moveToFirst(); - return cursor.getCount(); + return DatabaseUtils.getBlacklistItemCount(getContext()); } @Override @@ -101,10 +90,7 @@ public class PreferenceFragment extends PreferenceFragmentCompat implements Pref @Override protected Void doInBackground(Void... params) { - ContentResolver resolver = getContext().getContentResolver(); - // 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]); + DatabaseUtils.removeAllNotifications(getContext()); return null; }