diff --git a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/AppDatabase.java b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/AppDatabase.java index 4d0f2e33..f1710340 100644 --- a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/AppDatabase.java +++ b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/AppDatabase.java @@ -22,6 +22,7 @@ package org.kde.kdeconnect.Plugins.NotificationsPlugin; import android.content.ContentValues; import android.content.Context; +import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; @@ -31,23 +32,27 @@ import java.util.HashSet; class AppDatabase { static final private HashSet disabledByDefault = new HashSet<>(); - static { disabledByDefault.add("com.google.android.googlequicksearchbox"); //Google Now notifications re-spawn every few minutes } + private static final String SETTINGS_NAME = "app_database"; + private static final String SETTINGS_KEY_ALL_ENABLED = "all_enabled"; + + private static final int DATABASE_VERSION = 4; + private static final String DATABASE_NAME = "Applications"; + private static final String DATABASE_TABLE = "Applications"; private static final String KEY_PACKAGE_NAME = "packageName"; private static final String KEY_IS_ENABLED = "isEnabled"; - private static final String DATABASE_NAME = "Applications"; - private static final String DATABASE_TABLE = "Applications"; - private static final int DATABASE_VERSION = 2; private SQLiteDatabase ourDatabase; private DbHelper ourHelper; + private SharedPreferences prefs; AppDatabase(Context context, boolean readonly) { ourHelper = new DbHelper(context); + prefs = context.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE); if (readonly) { ourDatabase = ourHelper.getReadableDatabase(); } else { @@ -69,7 +74,7 @@ class AppDatabase { @Override public void onCreate(SQLiteDatabase db) { - db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" + KEY_PACKAGE_NAME + " TEXT PRIMARY KEY NOT NULL, " + KEY_IS_ENABLED + " TEXT NOT NULL); "); + db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" + KEY_PACKAGE_NAME + " TEXT PRIMARY KEY NOT NULL, " + KEY_IS_ENABLED + " INTEGER NOT NULL); "); } @Override @@ -84,7 +89,7 @@ class AppDatabase { String[] columns = new String[]{KEY_IS_ENABLED}; try (Cursor res = ourDatabase.query(DATABASE_TABLE, columns, KEY_PACKAGE_NAME + " =? ", new String[]{packageName}, null, null, null)) { ContentValues cv = new ContentValues(); - cv.put(KEY_IS_ENABLED, isEnabled ? "true" : "false"); + cv.put(KEY_IS_ENABLED, isEnabled ? 1 : 0); if (res.getCount() > 0) { ourDatabase.update(DATABASE_TABLE, cv, KEY_PACKAGE_NAME + "=?", new String[]{packageName}); } else { @@ -94,8 +99,13 @@ class AppDatabase { } } + boolean getAllEnabled() { + return prefs.getBoolean(SETTINGS_KEY_ALL_ENABLED, true); + } + void setAllEnabled(boolean enabled) { - ourDatabase.execSQL("UPDATE " + DATABASE_TABLE + " SET " + KEY_IS_ENABLED + "='" + enabled + "'"); + prefs.edit().putBoolean(SETTINGS_KEY_ALL_ENABLED, enabled).apply(); + ourDatabase.execSQL("UPDATE " + DATABASE_TABLE + " SET " + KEY_IS_ENABLED + "=" + (enabled? "1" : "0")); } boolean isEnabled(String packageName) { @@ -104,7 +114,7 @@ class AppDatabase { boolean result; if (res.getCount() > 0) { res.moveToFirst(); - result = (res.getString(res.getColumnIndex(KEY_IS_ENABLED))).equals("true"); + result = (res.getInt(res.getColumnIndex(KEY_IS_ENABLED)) != 0); } else { result = getDefaultStatus(packageName); } @@ -113,7 +123,10 @@ class AppDatabase { } private boolean getDefaultStatus(String packageName) { - return !disabledByDefault.contains(packageName); + if (disabledByDefault.contains(packageName)) { + return false; + } + return getAllEnabled(); } } diff --git a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationFilterActivity.java b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationFilterActivity.java index 0cb8bbb1..193b0b56 100644 --- a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationFilterActivity.java +++ b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationFilterActivity.java @@ -148,6 +148,7 @@ public class NotificationFilterActivity extends AppCompatActivity { } }); + listView.setItemChecked(0, appDatabase.getAllEnabled()); //"Select all" button for (int i = 0; i < apps.length; i++) { listView.setItemChecked(i + 1, apps[i].isEnabled); }