DatbaseUtils class

This commit is contained in:
danijoo
2016-01-08 01:02:14 +01:00
parent e22c4d8cc1
commit 481253abe0
5 changed files with 95 additions and 62 deletions

View File

@@ -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}
);
}
}

View File

@@ -4,13 +4,13 @@ import android.app.Notification;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.service.notification.NotificationListenerService; import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification;
import net.headlezz.notificationlogger.DatabaseUtils;
import net.headlezz.notificationlogger.PackageUtils; import net.headlezz.notificationlogger.PackageUtils;
import timber.log.Timber; import timber.log.Timber;
@@ -60,27 +60,18 @@ public class NotificationLoggerService extends NotificationListenerService imple
if (isBlacklisted(ln)) { if (isBlacklisted(ln)) {
Timber.d("App blacklisted. Notification not logged"); Timber.d("App blacklisted. Notification not logged");
} else { } else {
getApplicationContext().getContentResolver().insert( DatabaseUtils.insertNotification(getApplicationContext(), ln);
Logged_notificationTable.CONTENT_URI,
Logged_notificationTable.getContentValues(ln, false)
);
Timber.d(String.format("Notificaton logged (id: %d)", ln.notificationId)); Timber.d(String.format("Notificaton logged (id: %d)", ln.notificationId));
} }
} }
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
}
private boolean isBlacklisted(LoggedNotification ln) { private boolean isBlacklisted(LoggedNotification ln) {
Cursor cursor = getApplicationContext().getContentResolver().query( return DatabaseUtils.isPackageBlacklisted(getApplicationContext(), ln.packageName);
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;
} }
private LoggedNotification buildLoggedNotification(StatusBarNotification sbn) { private LoggedNotification buildLoggedNotification(StatusBarNotification sbn) {

View File

@@ -13,10 +13,10 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import net.headlezz.notificationlogger.DatabaseUtils;
import net.headlezz.notificationlogger.NotificationInfoDialog; import net.headlezz.notificationlogger.NotificationInfoDialog;
import net.headlezz.notificationlogger.R; import net.headlezz.notificationlogger.R;
import net.headlezz.notificationlogger.logger.LoggedNotification; import net.headlezz.notificationlogger.logger.LoggedNotification;
import net.headlezz.notificationlogger.logger.Logged_notificationTable;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@@ -107,10 +107,7 @@ public class NotificationListFragment extends Fragment implements NotificationLi
@Override @Override
public void onNotificationClick(long id) { public void onNotificationClick(long id) {
String qry = Logged_notificationTable.FIELD__ID + " = ? "; LoggedNotification notification = DatabaseUtils.getNotificationById(getContext(), 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);
Timber.e(notification.title); Timber.e(notification.title);
new NotificationInfoDialog(getContext(), notification).show(); new NotificationInfoDialog(getContext(), notification).show();

View File

@@ -1,6 +1,5 @@
package net.headlezz.notificationlogger.preferences; package net.headlezz.notificationlogger.preferences;
import android.content.ContentResolver;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@@ -20,6 +19,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import net.headlezz.notificationlogger.DatabaseUtils;
import net.headlezz.notificationlogger.PackageUtils; import net.headlezz.notificationlogger.PackageUtils;
import net.headlezz.notificationlogger.R; import net.headlezz.notificationlogger.R;
import net.headlezz.notificationlogger.logger.BlacklistItem; import net.headlezz.notificationlogger.logger.BlacklistItem;
@@ -147,24 +147,9 @@ public class BlacklistFragment extends Fragment implements LoaderManager.LoaderC
String packageName = packageNames.get(which); String packageName = packageNames.get(which);
BlacklistItem item = new BlacklistItem(); BlacklistItem item = new BlacklistItem();
item.packageName = packageName; item.packageName = packageName;
ContentResolver cr = getContext().getContentResolver(); if(!DatabaseUtils.isPackageBlacklisted(getContext(), packageName)) {
Cursor cursor = cr.query( DatabaseUtils.insertBlacklistItem(getContext(), item);
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)
);
} }
cursor.close();
} }
}) })
.create().show(); .create().show();
@@ -186,11 +171,7 @@ public class BlacklistFragment extends Fragment implements LoaderManager.LoaderC
.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() { .setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
getContext().getContentResolver().delete( DatabaseUtils.deleteBlacklistItem(getContext(), item.packageName);
BlacklistTable.CONTENT_URI,
BlacklistTable.FIELD_PACKAGE_NAME + " = ?",
new String[] {String.valueOf(item.packageName)}
);
} }
}) })
.setNegativeButton(getContext().getString(R.string.cancel), null) .setNegativeButton(getContext().getString(R.string.cancel), null)

View File

@@ -1,9 +1,7 @@
package net.headlezz.notificationlogger.preferences; package net.headlezz.notificationlogger.preferences;
import android.app.Activity; import android.app.Activity;
import android.content.ContentResolver;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.database.Cursor;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
@@ -12,9 +10,8 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat; import android.support.v7.preference.PreferenceFragmentCompat;
import android.text.Html; import android.text.Html;
import net.headlezz.notificationlogger.DatabaseUtils;
import net.headlezz.notificationlogger.R; 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 { public class PreferenceFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceClickListener {
@@ -46,15 +43,7 @@ public class PreferenceFragment extends PreferenceFragmentCompat implements Pref
@Override @Override
protected Integer doInBackground(Void... params) { protected Integer doInBackground(Void... params) {
Cursor cursor = getContext().getContentResolver().query( return DatabaseUtils.getBlacklistItemCount(getContext());
BlacklistTable.CONTENT_URI,
null,
null,
null,
null
);
cursor.moveToFirst();
return cursor.getCount();
} }
@Override @Override
@@ -101,10 +90,7 @@ public class PreferenceFragment extends PreferenceFragmentCompat implements Pref
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
ContentResolver resolver = getContext().getContentResolver(); DatabaseUtils.removeAllNotifications(getContext());
// 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]);
return null; return null;
} }