DatbaseUtils class
This commit is contained in:
@@ -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}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user