From e4601102773141088ea63fbe348f81f7b93654fd Mon Sep 17 00:00:00 2001 From: ilmich Date: Tue, 24 Nov 2015 22:14:30 +0100 Subject: [PATCH 1/3] Shortcuts database code moved from ShortcutProvider fix #333 --- .../main/java/fr/neamar/kiss/DataHandler.java | 33 ++++++++++++++++++ .../broadcast/InstallShortcutHandler.java | 2 +- .../broadcast/UninstallShortcutHandler.java | 2 +- .../kiss/dataprovider/ShortcutProvider.java | 34 ------------------- .../main/java/fr/neamar/kiss/db/DBHelper.java | 25 +++++++++++++- .../fr/neamar/kiss/result/ShortcutResult.java | 2 +- 6 files changed, 60 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/fr/neamar/kiss/DataHandler.java b/app/src/main/java/fr/neamar/kiss/DataHandler.java index 4b3f2ca8e..28a11ed9d 100644 --- a/app/src/main/java/fr/neamar/kiss/DataHandler.java +++ b/app/src/main/java/fr/neamar/kiss/DataHandler.java @@ -1,10 +1,17 @@ package fr.neamar.kiss; +import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; + import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; +import android.graphics.Bitmap.CompressFormat; import android.preference.PreferenceManager; import java.util.ArrayList; @@ -23,9 +30,11 @@ import fr.neamar.kiss.dataprovider.ShortcutProvider; import fr.neamar.kiss.dataprovider.ToggleProvider; import fr.neamar.kiss.db.DBHelper; +import fr.neamar.kiss.db.ShortcutRecord; import fr.neamar.kiss.db.ValuedHistoryRecord; import fr.neamar.kiss.pojo.Pojo; import fr.neamar.kiss.pojo.PojoComparator; +import fr.neamar.kiss.pojo.ShortcutPojo; public class DataHandler extends BroadcastReceiver { @@ -160,6 +169,30 @@ public ArrayList getHistory(Context context, int itemCount) { public int getHistoryLength(Context context) { return DBHelper.getHistoryLength(context); } + + public void addShortcut(Context ctx, ShortcutPojo shortcut) { + ShortcutRecord record = new ShortcutRecord(); + record.name = shortcut.name; + record.iconResource = shortcut.resourceName; + record.packageName = shortcut.packageName; + record.intentUri = shortcut.intentUri; + + if (shortcut.icon != null) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + shortcut.icon.compress(CompressFormat.PNG,100,baos); + record.icon_blob = baos.toByteArray(); + } + + DBHelper.insertShortcut(ctx, record); + } + + public void removeShortcut(Context ctx, ShortcutPojo shortcut) { + DBHelper.removeShortcut(ctx, shortcut.name); + } + + public void removeShortcuts(Context ctx, String packageName) { + DBHelper.removeShortcuts(ctx, packageName); + } /** * Return all applications diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java index bfb886205..3c7b1baf8 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java @@ -61,7 +61,7 @@ public void onReceive(Context context, Intent data) { } } - dh.getShortcutProvider().addShortcut(pojo); + dh.addShortcut(context, pojo); } diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java index c6a4dcc21..3531e827f 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java @@ -29,7 +29,7 @@ public void onReceive(Context context, Intent data) { return; } - dh.getShortcutProvider().removeShortcut(pojo); + dh.removeShortcut(context, pojo); } diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java index a49e90fe5..74bc36404 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java @@ -59,41 +59,7 @@ else if ((matchPositionStart = shortcutNameLowerCased.indexOf(query)) > -1) { return results; } - - public void addShortcut(ShortcutPojo shortcut) { - ShortcutRecord record = new ShortcutRecord(); - record.name = shortcut.name; - record.iconResource = shortcut.resourceName; - record.packageName = shortcut.packageName; - record.intentUri = shortcut.intentUri; - - if (shortcut.icon != null) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - shortcut.icon.compress(CompressFormat.PNG,100,baos); - record.icon_blob = baos.toByteArray(); - } - - DBHelper.insertShortcut(this.context, record); - this.pojos.add(shortcut); - } - public void removeShortcut(ShortcutPojo shortcut) { - DBHelper.removeShortcut(context, shortcut.name); - this.pojos.remove(shortcut); - } - - public void removeShortcuts(String packageName) { - Iterator iter = this.pojos.iterator(); - - while (iter.hasNext()) { - ShortcutPojo current = iter.next(); - if (current.intentUri.contains(packageName)) { - DBHelper.removeShortcut(context, current.name); - DBHelper.removeFromHistory(context, current.id); - iter.remove(); - } - } - } public Pojo findById(String id) { diff --git a/app/src/main/java/fr/neamar/kiss/db/DBHelper.java b/app/src/main/java/fr/neamar/kiss/db/DBHelper.java index ace014e4b..1629f833c 100644 --- a/app/src/main/java/fr/neamar/kiss/db/DBHelper.java +++ b/app/src/main/java/fr/neamar/kiss/db/DBHelper.java @@ -4,6 +4,7 @@ import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import fr.neamar.kiss.pojo.ShortcutPojo; import java.util.ArrayList; @@ -157,7 +158,7 @@ public static ArrayList getShortcuts(Context context) { cursor.moveToFirst(); while (!cursor.isAfterLast()) { - ShortcutRecord entry = new ShortcutRecord(); + ShortcutRecord entry = new ShortcutRecord(); entry.name = cursor.getString(0); entry.packageName = cursor.getString(1); @@ -174,4 +175,26 @@ public static ArrayList getShortcuts(Context context) { return records; } + public static void removeShortcuts(Context context, String packageName) { + SQLiteDatabase db = getDatabase(context); + + // Cursor query (String table, String[] columns, String selection, + // String[] selectionArgs, String groupBy, String having, String + // orderBy) + Cursor cursor = db.query("shortcuts", new String[]{"name", "package", "icon", "intent_uri", "icon_blob"}, + "intent_uri LIKE ?", new String[]{"%"+packageName+"%"}, null, null, null); + + cursor.moveToFirst(); + while (!cursor.isAfterLast()) { // remove from history + db.delete("history", "record = ?", new String[]{ShortcutPojo.SCHEME + cursor.getString(0).toLowerCase()}); + cursor.moveToNext(); + } + cursor.close(); + + //remove shortcuts + db.delete("shortcuts", "intent_uri LIKE ?", new String[]{"%" + packageName + "%"}); + + db.close(); + } + } diff --git a/app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java b/app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java index 4ae7e68d3..3016e9ff5 100644 --- a/app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java @@ -103,7 +103,7 @@ Boolean popupMenuClickHandler(Context context, RecordAdapter parent, MenuItem it private void launchUninstall(Context context, ShortcutPojo shortcutPojo) { DataHandler dh = KissApplication.getDataHandler(context); if (dh != null) { - dh.getShortcutProvider().removeShortcut(shortcutPojo); + dh.removeShortcut(context, shortcutPojo); } } From 42721b840bd4d545748b7b70f997d85725768418 Mon Sep 17 00:00:00 2001 From: Alexander Schlarb Date: Sat, 21 Nov 2015 23:38:29 +0100 Subject: [PATCH 2/3] Refactored data providers into Android services that can run independently of the main activity Fixes #269. --- app/src/main/AndroidManifest.xml | 9 + .../main/java/fr/neamar/kiss/DataHandler.java | 210 ++++++++++++++---- .../java/fr/neamar/kiss/KissApplication.java | 4 - .../java/fr/neamar/kiss/MainActivity.java | 4 +- .../java/fr/neamar/kiss/SettingsActivity.java | 4 +- .../kiss/broadcast/IncomingCallHandler.java | 2 +- .../kiss/broadcast/IncomingSmsHandler.java | 2 +- .../broadcast/InstallShortcutHandler.java | 2 +- .../broadcast/NewAppInstalledHandler.java | 16 +- .../broadcast/UninstallShortcutHandler.java | 2 +- .../kiss/dataprovider/AliasProvider.java | 40 +++- .../neamar/kiss/dataprovider/AppProvider.java | 7 +- .../kiss/dataprovider/ContactProvider.java | 7 +- .../neamar/kiss/dataprovider/IProvider.java | 53 +++++ .../kiss/dataprovider/PhoneProvider.java | 8 +- .../fr/neamar/kiss/dataprovider/Provider.java | 88 +++++++- .../kiss/dataprovider/SearchProvider.java | 7 +- .../kiss/dataprovider/SettingProvider.java | 12 +- .../kiss/dataprovider/ShortcutProvider.java | 15 +- .../kiss/dataprovider/ToggleProvider.java | 12 +- .../java/fr/neamar/kiss/loader/LoadPojos.java | 2 - .../ResetExcludedAppsPreference.java | 2 +- .../preference/ResetFavoritesPreference.java | 2 +- .../kiss/preference/ResetPreference.java | 2 +- .../java/fr/neamar/kiss/result/Result.java | 2 +- .../fr/neamar/kiss/result/ShortcutResult.java | 2 +- 26 files changed, 392 insertions(+), 124 deletions(-) create mode 100644 app/src/main/java/fr/neamar/kiss/dataprovider/IProvider.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4f0cd3551..27112d04e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -154,6 +154,15 @@ + + + + + + + + + diff --git a/app/src/main/java/fr/neamar/kiss/DataHandler.java b/app/src/main/java/fr/neamar/kiss/DataHandler.java index 28a11ed9d..de3e7adf1 100644 --- a/app/src/main/java/fr/neamar/kiss/DataHandler.java +++ b/app/src/main/java/fr/neamar/kiss/DataHandler.java @@ -5,13 +5,17 @@ import java.util.Collections; import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; +import android.content.ServiceConnection; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.Bitmap.CompressFormat; +import android.os.IBinder; import android.preference.PreferenceManager; import java.util.ArrayList; @@ -23,6 +27,7 @@ import fr.neamar.kiss.dataprovider.AliasProvider; import fr.neamar.kiss.dataprovider.AppProvider; import fr.neamar.kiss.dataprovider.ContactProvider; +import fr.neamar.kiss.dataprovider.IProvider; import fr.neamar.kiss.dataprovider.PhoneProvider; import fr.neamar.kiss.dataprovider.Provider; import fr.neamar.kiss.dataprovider.SearchProvider; @@ -41,58 +46,98 @@ public class DataHandler extends BroadcastReceiver { /** * List all known providers */ - private final ArrayList> providers = new ArrayList<>(); - private final AppProvider appProvider; - private final ContactProvider contactProvider; - private final ShortcutProvider shortcutProvider; + private Map providers = new HashMap<>(); + private AppProvider appProvider = null; + private ContactProvider contactProvider = null; + private ShortcutProvider shortcutProvider = null; private String currentQuery; - private int providersLoaded = 0; + private final Context context; + + private Map providersNew; + private int providersNewCount; + /** * Initialize all providers */ public DataHandler(Context context) { + // Make sure we are in the context of the main activity + // (otherwise we might receive an exception about broadcast listeners not being able + // to bind to services) + this.context = context.getApplicationContext(); + IntentFilter intentFilter = new IntentFilter(MainActivity.LOAD_OVER); - context.getApplicationContext().registerReceiver(this, intentFilter); + this.context.getApplicationContext().registerReceiver(this, intentFilter); Intent i = new Intent(MainActivity.START_LOAD); - context.sendBroadcast(i); + this.context.sendBroadcast(i); + + this.reload(); + } + + /** + * Reconnect to all provider services + */ + public void reload() { + this.providersNew = new HashMap<>(); + this.providersNewCount = 0; + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); // Initialize providers - appProvider = new AppProvider(context); - providers.add(appProvider); + this.connect(new Intent(this.context, AppProvider.class)); if (prefs.getBoolean("enable-contacts", true)) { - contactProvider = new ContactProvider(context); - providers.add(contactProvider); - } else { - contactProvider = null; + this.connect(new Intent(this.context, ContactProvider.class)); } if (prefs.getBoolean("enable-search", true)) { - providers.add(new SearchProvider(context)); + this.connect(new Intent(this.context, SearchProvider.class)); } if (prefs.getBoolean("enable-phone", true)) { - providers.add(new PhoneProvider(context)); + this.connect(new Intent(this.context, PhoneProvider.class)); } if (prefs.getBoolean("enable-toggles", true)) { - providers.add(new ToggleProvider(context)); + this.connect(new Intent(this.context, ToggleProvider.class)); } if (prefs.getBoolean("enable-settings", true)) { - providers.add(new SettingProvider(context)); + this.connect(new Intent(this.context, SettingProvider.class)); } if (prefs.getBoolean("enable-alias", true)) { - providers.add(new AliasProvider(context, appProvider)); + this.connect(new Intent(this.context, AliasProvider.class)); } - if (prefs.getBoolean("enable-shortcuts", true)) { - shortcutProvider = new ShortcutProvider(context); - providers.add(shortcutProvider); - } else { - shortcutProvider = null; + this.connect(new Intent(this.context, ShortcutProvider.class)); } + } + private void connect(Intent intent) { + // Count the number of providers that we expect to be initialized to make sure that + // `handleProviderLoaded(Context)` will not emit a `FULL_LOAD_OVER` event before all + // providers have been actually added to the `providers` list + this.providersNewCount++; + + // Send "start service" command first so that the service can run independently + // of the activity + this.context.startService(intent); + + // Connect and bind to provider service + this.context.bindService(intent, new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName className, IBinder service) { + // We've bound to LocalService, cast the IBinder and get LocalService instance + Provider.LocalBinder binder = (Provider.LocalBinder) service; + IProvider provider = binder.getService(); + providersNew.put(provider, this); + + if (provider.isLoaded()) { + handleProviderLoaded(); + } + } + + @Override + public void onServiceDisconnected(ComponentName arg0) {} + }, Context.BIND_AUTO_CREATE); } /** @@ -118,7 +163,7 @@ public ArrayList getResults(Context context, String query) { // Ask all providers for data ArrayList allPojos = new ArrayList<>(); - for (Provider provider : providers) { + for (IProvider provider : providers.keySet()) { // Retrieve results for query: ArrayList pojos = provider.getResults(query); @@ -166,11 +211,11 @@ public ArrayList getHistory(Context context, int itemCount) { return history; } - public int getHistoryLength(Context context) { - return DBHelper.getHistoryLength(context); + public int getHistoryLength() { + return DBHelper.getHistoryLength(this.context); } - public void addShortcut(Context ctx, ShortcutPojo shortcut) { + public void addShortcut(ShortcutPojo shortcut) { ShortcutRecord record = new ShortcutRecord(); record.name = shortcut.name; record.iconResource = shortcut.resourceName; @@ -183,15 +228,27 @@ public void addShortcut(Context ctx, ShortcutPojo shortcut) { record.icon_blob = baos.toByteArray(); } - DBHelper.insertShortcut(ctx, record); + DBHelper.insertShortcut(this.context, record); + + if(this.shortcutProvider != null) { + this.shortcutProvider.reload(); + } } - public void removeShortcut(Context ctx, ShortcutPojo shortcut) { - DBHelper.removeShortcut(ctx, shortcut.name); + public void removeShortcut(ShortcutPojo shortcut) { + DBHelper.removeShortcut(this.context, shortcut.name); + + if(this.shortcutProvider != null) { + this.shortcutProvider.reload(); + } } - public void removeShortcuts(Context ctx, String packageName) { - DBHelper.removeShortcuts(ctx, packageName); + public void removeShortcuts(String packageName) { + DBHelper.removeShortcuts(this.context, packageName); + + if(this.shortcutProvider != null) { + this.shortcutProvider.reload(); + } } /** @@ -224,14 +281,13 @@ public AppProvider getAppProvider() { * @param limit max number of items to retrieve. You may end with less items if favorites contains non existing items. * @return favorites' pojo */ - ArrayList getFavorites(Context context, int limit) { + public ArrayList getFavorites(int limit) { ArrayList favorites = new ArrayList<>(limit); - String favApps = PreferenceManager.getDefaultSharedPreferences(context). + String favApps = PreferenceManager.getDefaultSharedPreferences(this.context). getString("favorite-apps-list", ""); List favAppsList = Arrays.asList(favApps.split(";")); - // Find associated items for (int i = 0; i < favAppsList.size(); i++) { Pojo pojo = getPojo(favAppsList.get(i)); @@ -273,28 +329,86 @@ public boolean addToFavorites(MainActivity context, String id) { * @param context android context * @param id pojo.id of item to record */ - public void addToHistory(Context context, String id) { - DBHelper.insertHistory(context, currentQuery, id); + public void addToHistory(String id) { + DBHelper.insertHistory(this.context, currentQuery, id); } @Override public void onReceive(Context context, Intent intent) { - providersLoaded++; - if (providersLoaded == providers.size()) { - try { - context.unregisterReceiver(this); - Intent i = new Intent(MainActivity.FULL_LOAD_OVER); - context.sendBroadcast(i); - providersLoaded = 0; - } catch (IllegalArgumentException e) { - // Nothing + this.handleProviderLoaded(); + } + + private void handleProviderLoaded() { + // Do not continue if not all providers are connected yet + if(this.providersNewCount != this.providersNew.size()) { + return; + } + + // Make sure all providers have completely loaded + for(IProvider provider : this.providersNew.keySet()) { + if(!provider.isLoaded()) { + return; + } + } + + // Obtain reference to old and new providers list + final Map providersOld = this.providers; + final Map providersNew = this.providersNew; + + // Store references to important services + for(IProvider provider : this.providersNew.keySet()) { + final String providerName = provider.getClass().getName(); + if(providerName.equals(AppProvider.class.getName())) { + this.appProvider = (AppProvider) provider; + } else if(providerName.equals(ContactProvider.class.getName())) { + this.contactProvider = (ContactProvider) provider; + } else if(providerName.equals(ShortcutProvider.class.getName())) { + this.shortcutProvider = (ShortcutProvider) provider; + } + } + + // Switch to the new providers list + this.providers = this.providersNew; + + // Broadcast the fact that the new providers list is ready + try { + this.context.unregisterReceiver(this); + Intent i = new Intent(MainActivity.FULL_LOAD_OVER); + this.context.sendBroadcast(i); + } catch (IllegalArgumentException e) { + // Nothing + } + + // Stop all providers that were previously used but are not used anymore + for(Map.Entry entryOld : providersOld.entrySet()) { + final IProvider providerOld = entryOld.getKey(); + final ServiceConnection connectionOld = entryOld.getValue(); + + boolean used = false; + for(IProvider providerNew : providersNew.keySet()) { + if(providerOld.getClass().getName() == providerNew.getClass().getName()) { + used = true; + break; + } + } + + if(!used) { + // Disconnect from provider service + this.context.unbindService(connectionOld); + + // Stop provider service + this.context.stopService(new Intent(this.context, providerOld.getClass())); } } + + // Clean up + this.providersNew = null; + this.providersNewCount = 0; } private Pojo getPojo(String id) { // Ask all providers if they know this id - for (Provider provider : providers) { + for (IProvider provider : providers.keySet()) { if (provider.mayFindById(id)) { return provider.findById(id); } diff --git a/app/src/main/java/fr/neamar/kiss/KissApplication.java b/app/src/main/java/fr/neamar/kiss/KissApplication.java index 71cfa0f26..9c277fd29 100644 --- a/app/src/main/java/fr/neamar/kiss/KissApplication.java +++ b/app/src/main/java/fr/neamar/kiss/KissApplication.java @@ -43,8 +43,4 @@ public static void initDataHandler(Context ctx) { dataHandler = new DataHandler(ctx); } } - - public static void resetDataHandler(Context ctx) { - dataHandler = new DataHandler(ctx); - } } diff --git a/app/src/main/java/fr/neamar/kiss/MainActivity.java b/app/src/main/java/fr/neamar/kiss/MainActivity.java index 92ccd02e4..b746c50c7 100644 --- a/app/src/main/java/fr/neamar/kiss/MainActivity.java +++ b/app/src/main/java/fr/neamar/kiss/MainActivity.java @@ -443,7 +443,7 @@ public void onLauncherButtonClicked(View launcherButton) { public void onFavoriteButtonClicked(View favorite) { // Favorites handling - Pojo pojo = KissApplication.getDataHandler(MainActivity.this).getFavorites(MainActivity.this, tryToRetrieve) + Pojo pojo = KissApplication.getDataHandler(MainActivity.this).getFavorites(tryToRetrieve) .get(Integer.parseInt((String) favorite.getTag())); final Result result = Result.fromPojo(MainActivity.this, pojo); @@ -558,7 +558,7 @@ public void onAnimationEnd(Animator animation) { public void retrieveFavorites() { ArrayList favoritesPojo = KissApplication.getDataHandler(MainActivity.this) - .getFavorites(MainActivity.this, tryToRetrieve); + .getFavorites(tryToRetrieve); if (favoritesPojo.size() == 0) { Toast toast = Toast.makeText(MainActivity.this, getString(R.string.no_favorites), Toast.LENGTH_SHORT); diff --git a/app/src/main/java/fr/neamar/kiss/SettingsActivity.java b/app/src/main/java/fr/neamar/kiss/SettingsActivity.java index 373d560a7..b0dc60f6b 100644 --- a/app/src/main/java/fr/neamar/kiss/SettingsActivity.java +++ b/app/src/main/java/fr/neamar/kiss/SettingsActivity.java @@ -65,7 +65,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin if (!Arrays.asList(safeSettings.split(" ")).contains(key)) { // Reload the DataHandler since Providers preferences have changed - KissApplication.resetDataHandler(this); + KissApplication.getDataHandler(this).reload(); } if("enable-sms-history".equals(key) || "enable-phone-history".equals(key)) { @@ -95,7 +95,7 @@ public void onPause() { } private void fixSummaries(SharedPreferences prefs) { - int historyLength = KissApplication.getDataHandler(this).getHistoryLength(this); + int historyLength = KissApplication.getDataHandler(this).getHistoryLength(); if (historyLength > 5) { findPreference("reset").setSummary(getString(R.string.reset_desc) + " (" + historyLength + " items)"); } diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/IncomingCallHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/IncomingCallHandler.java index ccb0553a5..2a9d23ea3 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/IncomingCallHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/IncomingCallHandler.java @@ -35,7 +35,7 @@ public void onReceive(final Context context, Intent intent) { ContactPojo contactPojo = contactProvider.findByPhone(phoneNumber); if (contactPojo != null) { - dataHandler.addToHistory(context, contactPojo.id); + dataHandler.addToHistory(contactPojo.id); } } } catch (Exception e) { diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/IncomingSmsHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/IncomingSmsHandler.java index f7e52e995..68f4551ee 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/IncomingSmsHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/IncomingSmsHandler.java @@ -43,7 +43,7 @@ public void onReceive(Context context, Intent intent) { ContactPojo contactPojo = contactProvider.findByPhone(msg.getOriginatingAddress()); if (contactPojo != null) { // We have a match! - dataHandler.addToHistory(context, contactPojo.id); + dataHandler.addToHistory(contactPojo.id); } } } \ No newline at end of file diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java index 3c7b1baf8..5bc14134d 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java @@ -61,7 +61,7 @@ public void onReceive(Context context, Intent data) { } } - dh.addShortcut(context, pojo); + dh.addShortcut(pojo); } diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/NewAppInstalledHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/NewAppInstalledHandler.java index 3eb5b9552..133aaff5a 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/NewAppInstalledHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/NewAppInstalledHandler.java @@ -6,7 +6,7 @@ import android.preference.PreferenceManager; import fr.neamar.kiss.KissApplication; -import fr.neamar.kiss.dataprovider.ShortcutProvider; +import fr.neamar.kiss.dataprovider.AppProvider; /** * This class gets called when an application is created or removed on the @@ -34,20 +34,22 @@ public void onReceive(Context ctx, Intent intent) { String className = launchIntent.getComponent().getClassName(); if (className != null) { - KissApplication.getDataHandler(ctx).addToHistory(ctx, "app://" + packageName + "/" + className); + KissApplication.getDataHandler(ctx) + .addToHistory("app://" + packageName + "/" + className); } } if ("android.intent.action.PACKAGE_REMOVED".equals(intent.getAction())) { // Removed all installed shortcuts String packageName = intent.getData().getSchemeSpecificPart(); - ShortcutProvider shortcutProvider = KissApplication.getDataHandler(ctx).getShortcutProvider(); - if(shortcutProvider != null) { - shortcutProvider.removeShortcuts(packageName); - } + KissApplication.getDataHandler(ctx).removeShortcuts(packageName); } - KissApplication.resetDataHandler(ctx); + // Reload application list + final AppProvider provider = KissApplication.getDataHandler(ctx).getAppProvider(); + if(provider != null) { + provider.reload(); + } } } diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java index 3531e827f..24531f98e 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java @@ -29,7 +29,7 @@ public void onReceive(Context context, Intent data) { return; } - dh.removeShortcut(context, pojo); + dh.removeShortcut(pojo); } diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/AliasProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/AliasProvider.java index d0d7547f5..0deab1dba 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/AliasProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/AliasProvider.java @@ -1,6 +1,10 @@ package fr.neamar.kiss.dataprovider; +import android.content.ComponentName; import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.IBinder; import java.util.ArrayList; import java.util.regex.Pattern; @@ -11,11 +15,39 @@ import fr.neamar.kiss.pojo.Pojo; public class AliasProvider extends Provider { - private final AppProvider appProvider; + private AppProvider appProvider = null; - public AliasProvider(final Context context, AppProvider appProvider) { - super(new LoadAliasPojos(context)); - this.appProvider = appProvider; + /** + * Handler for connection to the required application provider + */ + private final ServiceConnection appConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName className, IBinder service) { + // We've bound to LocalService, cast the IBinder and get LocalService instance + Provider.LocalBinder binder = (Provider.LocalBinder) service; + appProvider = (AppProvider) binder.getService(); + } + + @Override + public void onServiceDisconnected(ComponentName arg0) {} + }; + + + @Override + public void onCreate() { + // Connect to the required application provider + this.bindService( + new Intent(this, AppProvider.class), + this.appConnection, + Context.BIND_AUTO_CREATE + ); + + super.onCreate(); + } + + @Override + public void reload() { + this.initialize(new LoadAliasPojos(this)); } public ArrayList getResults(String query) { diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/AppProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/AppProvider.java index 62f0dcb8c..2cf952ce7 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/AppProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/AppProvider.java @@ -1,7 +1,5 @@ package fr.neamar.kiss.dataprovider; -import android.content.Context; - import java.util.ArrayList; import fr.neamar.kiss.loader.LoadAppPojos; @@ -11,8 +9,9 @@ public class AppProvider extends Provider { - public AppProvider(Context context) { - super(new LoadAppPojos(context)); + @Override + public void reload() { + this.initialize(new LoadAppPojos(this)); } public ArrayList getResults(String query) { diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/ContactProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/ContactProvider.java index 390fa9314..d4766850f 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/ContactProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/ContactProvider.java @@ -1,7 +1,5 @@ package fr.neamar.kiss.dataprovider; -import android.content.Context; - import java.util.ArrayList; import fr.neamar.kiss.loader.LoadContactPojos; @@ -12,8 +10,9 @@ public class ContactProvider extends Provider { - public ContactProvider(final Context context) { - super(new LoadContactPojos(context)); + @Override + public void reload() { + this.initialize(new LoadContactPojos(this)); } public ArrayList getResults(String query) { diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/IProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/IProvider.java new file mode 100644 index 000000000..15ffc4241 --- /dev/null +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/IProvider.java @@ -0,0 +1,53 @@ +package fr.neamar.kiss.dataprovider; + +import java.util.ArrayList; + +import fr.neamar.kiss.pojo.Pojo; + +/** + * Public interface exposed by every KISS data provider + */ +public interface IProvider { + /** + * Synchronously retrieve list of search results for the given query string + * + * @param s Some string query (usually provided by an user) + */ + ArrayList getResults(String s); + + /** + * Reload the data stored in this provider + * + * `"fr.neamar.summon.LOAD_OVER"` will be emitted once the reload is complete. The data provider + * will stay usable (using it's old data) during the reload. + */ + void reload(); + + /** + * Indicate whether this provider has already loaded it's data + * + * If this method returns `false` then the client may listen for the + * `"fr.neamar.summon.LOAD_OVER"` intent for notification of when the provider is ready. + * + * @return Is the provider ready to process search results? + */ + boolean isLoaded(); + + /** + * Tells whether or not this provider may be able to find the pojo with + * specified id + * + * @param id id we're looking for + * @return true if the provider can handle the query ; does not guarantee it + * will! + */ + boolean mayFindById(String id); + + /** + * Try to find a record by its id + * + * @param id id we're looking for + * @return null if not found + */ + Pojo findById(String id); +} diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/PhoneProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/PhoneProvider.java index 376cfe03e..892bb4ab6 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/PhoneProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/PhoneProvider.java @@ -1,6 +1,5 @@ package fr.neamar.kiss.dataprovider; -import android.content.Context; import android.content.pm.PackageManager; import java.util.ArrayList; @@ -14,10 +13,11 @@ public class PhoneProvider extends Provider { public static final String PHONE_SCHEME = "phone://"; private boolean deviceIsPhone = false; - public PhoneProvider(Context context) { - super(new LoadPhonePojos(context)); + @Override + public void reload() { + this.initialize(new LoadPhonePojos(this)); - PackageManager pm = context.getPackageManager(); + PackageManager pm = this.getPackageManager(); deviceIsPhone = pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY); } diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/Provider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/Provider.java index d7e58678c..40623e332 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/Provider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/Provider.java @@ -1,28 +1,80 @@ package fr.neamar.kiss.dataprovider; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.Binder; +import android.os.IBinder; +import android.util.Log; + +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import fr.neamar.kiss.MainActivity; import fr.neamar.kiss.loader.LoadPojos; import fr.neamar.kiss.pojo.Pojo; -public abstract class Provider { - ArrayList pojos = new ArrayList<>(); +public abstract class Provider extends Service implements IProvider { + /** + * Storage for search items used by this provider + */ + protected ArrayList pojos = new ArrayList<>(); + private boolean loaded = false; + /** * Scheme used to build ids for the pojos created by this provider */ private String pojoScheme = "(none)://"; - Provider(LoadPojos loader) { - super(); + /** + * Binder given to clients + */ + private final IBinder binder = new LocalBinder(); + + + /** + * (Re-)load the providers resources when the provider has been completely initialized + * by the Android system + */ + @Override + public void onCreate() { + super.onCreate(); + + this.reload(); + } + + + protected void initialize(LoadPojos loader) { + Log.i("Provider.initialize", "Starting provider: " + this.getClass().getSimpleName()); + loader.setProvider(this); this.pojoScheme = loader.getPojoScheme(); loader.execute(); } + /** + * Synchronously retrieve list of search results for the given query string + * + * @param s Some string query (usually provided by an user) + */ public abstract ArrayList getResults(String s); + public abstract void reload(); + + public boolean isLoaded() { + return this.loaded; + } + public void loadOver(ArrayList results) { - pojos = results; + Log.i("Provider.loadOver", "Done loading provider: " + this.getClass().getSimpleName()); + + // Store results + this.pojos = results; + this.loaded = true; + + // Broadcast this event + Intent i = new Intent(MainActivity.LOAD_OVER); + this.sendBroadcast(i); } /** @@ -33,7 +85,7 @@ public void loadOver(ArrayList results) { * @return true if the provider can handle the query ; does not guarantee it * will! */ - public Boolean mayFindById(String id) { + public boolean mayFindById(String id) { return id.startsWith(pojoScheme); } @@ -46,4 +98,28 @@ public Boolean mayFindById(String id) { public Pojo findById(String id) { return null; } + + + /** + * Class used for the client Binder. Because we know this service always + * runs in the same process as its clients, we don't need to deal with IPC. + */ + public class LocalBinder extends Binder { + public IProvider getService() { + // Return this instance of the provider so that clients can call public methods + return Provider.this; + } + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + // We want this service to continue running until it is explicitly + // stopped, so return sticky. + return START_STICKY; + } + + @Override + public IBinder onBind(Intent intent) { + return this.binder; + } } diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/SearchProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/SearchProvider.java index a23bd9a6b..8f183ff90 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/SearchProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/SearchProvider.java @@ -1,7 +1,5 @@ package fr.neamar.kiss.dataprovider; -import android.content.Context; - import java.util.ArrayList; import fr.neamar.kiss.loader.LoadSearchPojos; @@ -10,8 +8,9 @@ public class SearchProvider extends Provider { - public SearchProvider(Context context) { - super(new LoadSearchPojos(context)); + @Override + public void reload() { + this.initialize(new LoadSearchPojos(this)); } public ArrayList getResults(String query) { diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/SettingProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/SettingProvider.java index 082ae8563..846639d54 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/SettingProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/SettingProvider.java @@ -1,7 +1,5 @@ package fr.neamar.kiss.dataprovider; -import android.content.Context; - import java.util.ArrayList; import java.util.regex.Pattern; @@ -11,11 +9,13 @@ import fr.neamar.kiss.pojo.SettingPojo; public class SettingProvider extends Provider { - private final String settingName; + private String settingName; + + @Override + public void reload() { + this.initialize(new LoadSettingPojos(this)); - public SettingProvider(Context context) { - super(new LoadSettingPojos(context)); - settingName = context.getString(R.string.settings_prefix).toLowerCase(); + settingName = this.getString(R.string.settings_prefix).toLowerCase(); } public ArrayList getResults(String query) { diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java index 74bc36404..22a50bffe 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java @@ -1,24 +1,16 @@ package fr.neamar.kiss.dataprovider; -import java.io.ByteArrayOutputStream; import java.util.ArrayList; -import java.util.Iterator; -import android.content.Context; -import android.graphics.Bitmap.CompressFormat; -import fr.neamar.kiss.db.DBHelper; -import fr.neamar.kiss.db.ShortcutRecord; import fr.neamar.kiss.loader.LoadShortcutPojos; import fr.neamar.kiss.pojo.Pojo; import fr.neamar.kiss.pojo.ShortcutPojo; public class ShortcutProvider extends Provider { - - Context context = null; - public ShortcutProvider(Context context) { - super(new LoadShortcutPojos(context)); - this.context = context; + @Override + public void reload() { + this.initialize(new LoadShortcutPojos(this)); } @Override @@ -59,7 +51,6 @@ else if ((matchPositionStart = shortcutNameLowerCased.indexOf(query)) > -1) { return results; } - public Pojo findById(String id) { diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/ToggleProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/ToggleProvider.java index a540749b9..847ba39f2 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/ToggleProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/ToggleProvider.java @@ -1,7 +1,5 @@ package fr.neamar.kiss.dataprovider; -import android.content.Context; - import java.util.ArrayList; import java.util.regex.Pattern; @@ -11,11 +9,13 @@ import fr.neamar.kiss.pojo.TogglePojo; public class ToggleProvider extends Provider { - private final String toggleName; + private String toggleName; + + @Override + public void reload() { + this.initialize(new LoadTogglePojos(this)); - public ToggleProvider(Context context) { - super(new LoadTogglePojos(context)); - toggleName = context.getString(R.string.toggles_prefix).toLowerCase(); + toggleName = this.getString(R.string.toggles_prefix).toLowerCase(); } public ArrayList getResults(String query) { diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadPojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadPojos.java index bc18da73c..d40556113 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadPojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadPojos.java @@ -34,8 +34,6 @@ public String getPojoScheme() { protected void onPostExecute(ArrayList result) { super.onPostExecute(result); provider.loadOver(result); - Intent i = new Intent(MainActivity.LOAD_OVER); - context.sendBroadcast(i); } } diff --git a/app/src/main/java/fr/neamar/kiss/preference/ResetExcludedAppsPreference.java b/app/src/main/java/fr/neamar/kiss/preference/ResetExcludedAppsPreference.java index 6f34b4b39..d5e1e4660 100644 --- a/app/src/main/java/fr/neamar/kiss/preference/ResetExcludedAppsPreference.java +++ b/app/src/main/java/fr/neamar/kiss/preference/ResetExcludedAppsPreference.java @@ -23,7 +23,7 @@ public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { PreferenceManager.getDefaultSharedPreferences(getContext()).edit() .putString("excluded-apps-list", getContext().getPackageName() + ";").commit(); - KissApplication.resetDataHandler(getContext()); + KissApplication.getDataHandler(getContext()).getAppProvider().reload(); Toast.makeText(getContext(), R.string.excluded_app_list_erased, Toast.LENGTH_LONG).show(); } diff --git a/app/src/main/java/fr/neamar/kiss/preference/ResetFavoritesPreference.java b/app/src/main/java/fr/neamar/kiss/preference/ResetFavoritesPreference.java index 32181987c..db5ee134b 100644 --- a/app/src/main/java/fr/neamar/kiss/preference/ResetFavoritesPreference.java +++ b/app/src/main/java/fr/neamar/kiss/preference/ResetFavoritesPreference.java @@ -23,7 +23,7 @@ public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { PreferenceManager.getDefaultSharedPreferences(getContext()).edit() .putString("favorite-apps-list", "").commit(); - KissApplication.resetDataHandler(getContext()); + KissApplication.getDataHandler(getContext()).getAppProvider().reload(); Toast.makeText(getContext(), R.string.favorites_erased, Toast.LENGTH_LONG).show(); } diff --git a/app/src/main/java/fr/neamar/kiss/preference/ResetPreference.java b/app/src/main/java/fr/neamar/kiss/preference/ResetPreference.java index 37a8a222e..425c578b5 100644 --- a/app/src/main/java/fr/neamar/kiss/preference/ResetPreference.java +++ b/app/src/main/java/fr/neamar/kiss/preference/ResetPreference.java @@ -22,7 +22,7 @@ public void onClick(DialogInterface dialog, int which) { super.onClick(dialog, which); if (which == DialogInterface.BUTTON_POSITIVE) { getContext().deleteDatabase(DB.DB_NAME); - KissApplication.resetDataHandler(getContext()); + KissApplication.getDataHandler(getContext()).reload(); PreferenceManager.getDefaultSharedPreferences(getContext()).edit() .putBoolean("layout-updated", true).commit(); diff --git a/app/src/main/java/fr/neamar/kiss/result/Result.java b/app/src/main/java/fr/neamar/kiss/result/Result.java index 02d7cbc95..c6fba7eb4 100644 --- a/app/src/main/java/fr/neamar/kiss/result/Result.java +++ b/app/src/main/java/fr/neamar/kiss/result/Result.java @@ -198,7 +198,7 @@ Spanned enrichText(String text) { */ void recordLaunch(Context context) { // Save in history - KissApplication.getDataHandler(context).addToHistory(context, pojo.id); + KissApplication.getDataHandler(context).addToHistory(pojo.id); } public void deleteRecord(Context context) { diff --git a/app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java b/app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java index 3016e9ff5..58c0eb928 100644 --- a/app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java @@ -103,7 +103,7 @@ Boolean popupMenuClickHandler(Context context, RecordAdapter parent, MenuItem it private void launchUninstall(Context context, ShortcutPojo shortcutPojo) { DataHandler dh = KissApplication.getDataHandler(context); if (dh != null) { - dh.removeShortcut(context, shortcutPojo); + dh.removeShortcut(shortcutPojo); } } From 44ea18be4d55eab9fe371ddaf3cf53d0261b916e Mon Sep 17 00:00:00 2001 From: Alexander Schlarb Date: Sat, 28 Nov 2015 02:01:47 +0100 Subject: [PATCH 3/3] Incrementally start and stop services in DataHandler --- app/src/main/AndroidManifest.xml | 10 +- .../main/java/fr/neamar/kiss/DataHandler.java | 343 ++++++++++-------- .../java/fr/neamar/kiss/SettingsActivity.java | 12 +- .../fr/neamar/kiss/adapter/RecordAdapter.java | 16 +- .../kiss/broadcast/IncomingCallHandler.java | 10 +- .../kiss/broadcast/IncomingSmsHandler.java | 12 +- .../broadcast/InstallShortcutHandler.java | 16 +- .../broadcast/UninstallShortcutHandler.java | 8 +- .../kiss/dataprovider/AliasProvider.java | 8 + ...actProvider.java => ContactsProvider.java} | 18 +- ...ingProvider.java => SettingsProvider.java} | 10 +- ...utProvider.java => ShortcutsProvider.java} | 10 +- ...ggleProvider.java => TogglesProvider.java} | 10 +- .../main/java/fr/neamar/kiss/db/DBHelper.java | 4 +- ...ntactPojos.java => LoadContactsPojos.java} | 22 +- ...ttingPojos.java => LoadSettingsPojos.java} | 14 +- ...tcutPojos.java => LoadShortcutsPojos.java} | 20 +- ...TogglePojos.java => LoadTogglesPojos.java} | 15 +- .../{ContactPojo.java => ContactsPojo.java} | 2 +- .../{TogglePojo.java => SettingsPojo.java} | 2 +- .../{ShortcutPojo.java => ShortcutsPojo.java} | 2 +- .../{SettingPojo.java => TogglesPojo.java} | 2 +- .../kiss/preference/ResetPreference.java | 2 +- ...ContactResult.java => ContactsResult.java} | 16 +- .../java/fr/neamar/kiss/result/Result.java | 24 +- ...SettingResult.java => SettingsResult.java} | 8 +- ...ortcutResult.java => ShortcutsResult.java} | 10 +- .../{ToggleResult.java => TogglesResult.java} | 8 +- .../neamar/kiss/toggles/TogglesHandler.java | 6 +- 29 files changed, 333 insertions(+), 307 deletions(-) rename app/src/main/java/fr/neamar/kiss/dataprovider/{ContactProvider.java => ContactsProvider.java} (90%) rename app/src/main/java/fr/neamar/kiss/dataprovider/{SettingProvider.java => SettingsProvider.java} (85%) rename app/src/main/java/fr/neamar/kiss/dataprovider/{ShortcutProvider.java => ShortcutsProvider.java} (88%) rename app/src/main/java/fr/neamar/kiss/dataprovider/{ToggleProvider.java => TogglesProvider.java} (86%) rename app/src/main/java/fr/neamar/kiss/loader/{LoadContactPojos.java => LoadContactsPojos.java} (87%) rename app/src/main/java/fr/neamar/kiss/loader/{LoadSettingPojos.java => LoadSettingsPojos.java} (77%) rename app/src/main/java/fr/neamar/kiss/loader/{LoadShortcutPojos.java => LoadShortcutsPojos.java} (60%) rename app/src/main/java/fr/neamar/kiss/loader/{LoadTogglePojos.java => LoadTogglesPojos.java} (82%) rename app/src/main/java/fr/neamar/kiss/pojo/{ContactPojo.java => ContactsPojo.java} (92%) rename app/src/main/java/fr/neamar/kiss/pojo/{TogglePojo.java => SettingsPojo.java} (68%) rename app/src/main/java/fr/neamar/kiss/pojo/{ShortcutPojo.java => ShortcutsPojo.java} (85%) rename app/src/main/java/fr/neamar/kiss/pojo/{SettingPojo.java => TogglesPojo.java} (68%) rename app/src/main/java/fr/neamar/kiss/result/{ContactResult.java => ContactsResult.java} (93%) rename app/src/main/java/fr/neamar/kiss/result/{SettingResult.java => SettingsResult.java} (91%) rename app/src/main/java/fr/neamar/kiss/result/{ShortcutResult.java => ShortcutsResult.java} (92%) rename app/src/main/java/fr/neamar/kiss/result/{ToggleResult.java => TogglesResult.java} (96%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 27112d04e..3c33ec639 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -157,12 +157,12 @@ - + - - - + + + - + \ No newline at end of file diff --git a/app/src/main/java/fr/neamar/kiss/DataHandler.java b/app/src/main/java/fr/neamar/kiss/DataHandler.java index de3e7adf1..34c240eaa 100644 --- a/app/src/main/java/fr/neamar/kiss/DataHandler.java +++ b/app/src/main/java/fr/neamar/kiss/DataHandler.java @@ -2,9 +2,10 @@ import java.io.ByteArrayOutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; +import java.util.List; import java.util.Map; import android.content.BroadcastReceiver; @@ -18,43 +19,39 @@ import android.os.IBinder; import android.preference.PreferenceManager; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; - -import fr.neamar.kiss.dataprovider.AliasProvider; import fr.neamar.kiss.dataprovider.AppProvider; -import fr.neamar.kiss.dataprovider.ContactProvider; +import fr.neamar.kiss.dataprovider.ContactsProvider; import fr.neamar.kiss.dataprovider.IProvider; -import fr.neamar.kiss.dataprovider.PhoneProvider; import fr.neamar.kiss.dataprovider.Provider; -import fr.neamar.kiss.dataprovider.SearchProvider; -import fr.neamar.kiss.dataprovider.SettingProvider; -import fr.neamar.kiss.dataprovider.ShortcutProvider; -import fr.neamar.kiss.dataprovider.ToggleProvider; +import fr.neamar.kiss.dataprovider.ShortcutsProvider; import fr.neamar.kiss.db.DBHelper; import fr.neamar.kiss.db.ShortcutRecord; import fr.neamar.kiss.db.ValuedHistoryRecord; import fr.neamar.kiss.pojo.Pojo; import fr.neamar.kiss.pojo.PojoComparator; -import fr.neamar.kiss.pojo.ShortcutPojo; - -public class DataHandler extends BroadcastReceiver { +import fr.neamar.kiss.pojo.ShortcutsPojo; +public class DataHandler extends BroadcastReceiver + implements SharedPreferences.OnSharedPreferenceChangeListener { /** * List all known providers */ - private Map providers = new HashMap<>(); - private AppProvider appProvider = null; - private ContactProvider contactProvider = null; - private ShortcutProvider shortcutProvider = null; + final static private List PROVIDER_NAMES = Arrays.asList( + "alias", "app", "contacts", "phone", "search", "settings", "shortcuts", "toggles" + ); + + final private SharedPreferences prefs; + final private Context context; private String currentQuery; - private final Context context; - private Map providersNew; - private int providersNewCount; + private Map providers = new HashMap<>(); + private boolean providersReady = false; + + + protected class ProviderEntry { + public IProvider provider = null; + public ServiceConnection connection = null; + } /** @@ -72,55 +69,78 @@ public DataHandler(Context context) { Intent i = new Intent(MainActivity.START_LOAD); this.context.sendBroadcast(i); - this.reload(); + // Monitor changes for service preferences (to automatically start and stop services) + this.prefs = PreferenceManager.getDefaultSharedPreferences(context); + this.prefs.registerOnSharedPreferenceChangeListener(this); + + // Connect to initial providers + for(String providerName : PROVIDER_NAMES) { + if(this.prefs.getBoolean("enable-" + providerName, true)) { + this.connectToProvider(providerName); + } + } + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if(key.startsWith("enable-")) { + String providerName = key.substring(7); + if(PROVIDER_NAMES.contains(providerName)) { + if(sharedPreferences.getBoolean(key, true)) { + this.connectToProvider(providerName); + } else { + this.disconnectFromProvider(providerName); + } + } + } } /** - * Reconnect to all provider services + * Generate an intent that can be used to start or stop the given provider + * + * @param name The name of the provider + * @return Android intent for this provider */ - public void reload() { - this.providersNew = new HashMap<>(); - this.providersNewCount = 0; - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); - - // Initialize providers - this.connect(new Intent(this.context, AppProvider.class)); - - if (prefs.getBoolean("enable-contacts", true)) { - this.connect(new Intent(this.context, ContactProvider.class)); + protected Intent providerName2Intent(String name) { + // Build expected fully-qualified provider class name + StringBuilder className = new StringBuilder(50); + className.append("fr.neamar.kiss.dataprovider."); + className.append(Character.toUpperCase(name.charAt(0))); + className.append(name.substring(1).toLowerCase()); + className.append("Provider"); + + // Try to create reflection class instance for class name + try { + return new Intent(this.context, Class.forName(className.toString())); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return null; } + } - if (prefs.getBoolean("enable-search", true)) { - this.connect(new Intent(this.context, SearchProvider.class)); - } - if (prefs.getBoolean("enable-phone", true)) { - this.connect(new Intent(this.context, PhoneProvider.class)); - } - if (prefs.getBoolean("enable-toggles", true)) { - this.connect(new Intent(this.context, ToggleProvider.class)); - } - if (prefs.getBoolean("enable-settings", true)) { - this.connect(new Intent(this.context, SettingProvider.class)); - } - if (prefs.getBoolean("enable-alias", true)) { - this.connect(new Intent(this.context, AliasProvider.class)); - } - if (prefs.getBoolean("enable-shortcuts", true)) { - this.connect(new Intent(this.context, ShortcutProvider.class)); + /** + * Require the data handler to be connected to the data provider with the given name + * + * @param name Data provider name (i.e.: `AliasProvider` → `"alias"`) + */ + protected void connectToProvider(final String name) { + // Do not continue if this provider has already been connected to + if(this.providers.containsKey(name)) { + return; } - } - private void connect(Intent intent) { - // Count the number of providers that we expect to be initialized to make sure that - // `handleProviderLoaded(Context)` will not emit a `FULL_LOAD_OVER` event before all - // providers have been actually added to the `providers` list - this.providersNewCount++; + // Find provider class for the given service name + Intent intent = this.providerName2Intent(name); + if(intent == null) { + return; + } // Send "start service" command first so that the service can run independently // of the activity this.context.startService(intent); + final ProviderEntry entry = new ProviderEntry(); + // Connect and bind to provider service this.context.bindService(intent, new ServiceConnection() { @Override @@ -128,7 +148,10 @@ public void onServiceConnected(ComponentName className, IBinder service) { // We've bound to LocalService, cast the IBinder and get LocalService instance Provider.LocalBinder binder = (Provider.LocalBinder) service; IProvider provider = binder.getService(); - providersNew.put(provider, this); + + // Update provider info so that it contains something useful + entry.provider = provider; + entry.connection = this; if (provider.isLoaded()) { handleProviderLoaded(); @@ -136,10 +159,84 @@ public void onServiceConnected(ComponentName className, IBinder service) { } @Override - public void onServiceDisconnected(ComponentName arg0) {} + public void onServiceDisconnected(ComponentName arg0) { + } }, Context.BIND_AUTO_CREATE); + + // Add empty provider object to list of providers + this.providers.put(name, entry); + } + + /** + * Terminate any connection between the data handler and the data provider with the given name + * + * @param name Data provider name (i.e.: `AppProvider` → `"app"`) + */ + protected void disconnectFromProvider(String name) { + // Skip already disconnected services + ProviderEntry entry = this.providers.get(name); + if(entry == null) { + return; + } + + // Disconnect from provider service + this.context.unbindService(entry.connection); + + // Stop provider service + this.context.stopService(new Intent(this.context, entry.provider.getClass())); + + // Remove provider from list + this.providers.remove(name); + } + + + /** + * Called when some event occurred that makes us believe that all data providers + * might be ready now + */ + private void handleProviderLoaded() { + if(this.providersReady) { + return; + } + + // Make sure that all providers are fully connected + for (ProviderEntry entry : this.providers.values()) { + if(entry.provider == null || !entry.provider.isLoaded()) { + return; + } + } + + // Broadcast the fact that the new providers list is ready + try { + this.context.unregisterReceiver(this); + Intent i = new Intent(MainActivity.FULL_LOAD_OVER); + this.context.sendBroadcast(i); + } catch (IllegalArgumentException e) { + // Nothing + } + + this.providersReady = true; + } + + + @Override + public void onReceive(Context context, Intent intent) { + this.handleProviderLoaded(); } + + /** + * Reload all currently used data providers + */ + public void reloadAll() { + for(ProviderEntry entry : this.providers.values()) { + if(entry.provider != null && entry.provider.isLoaded()) { + entry.provider.reload(); + } + } + } + + /** * Get records for this query. * @@ -163,9 +260,9 @@ public ArrayList getResults(Context context, String query) { // Ask all providers for data ArrayList allPojos = new ArrayList<>(); - for (IProvider provider : providers.keySet()) { + for (ProviderEntry entry : this.providers.values()) { // Retrieve results for query: - ArrayList pojos = provider.getResults(query); + ArrayList pojos = entry.provider.getResults(query); // Add results to list for (Pojo pojo : pojos) { @@ -215,7 +312,7 @@ public int getHistoryLength() { return DBHelper.getHistoryLength(this.context); } - public void addShortcut(ShortcutPojo shortcut) { + public void addShortcut(ShortcutsPojo shortcut) { ShortcutRecord record = new ShortcutRecord(); record.name = shortcut.name; record.iconResource = shortcut.resourceName; @@ -230,24 +327,24 @@ public void addShortcut(ShortcutPojo shortcut) { DBHelper.insertShortcut(this.context, record); - if(this.shortcutProvider != null) { - this.shortcutProvider.reload(); + if(this.getShortcutsProvider() != null) { + this.getShortcutsProvider().reload(); } } - public void removeShortcut(ShortcutPojo shortcut) { + public void removeShortcut(ShortcutsPojo shortcut) { DBHelper.removeShortcut(this.context, shortcut.name); - if(this.shortcutProvider != null) { - this.shortcutProvider.reload(); + if(this.getShortcutsProvider() != null) { + this.getShortcutsProvider().reload(); } } public void removeShortcuts(String packageName) { DBHelper.removeShortcuts(this.context, packageName); - if(this.shortcutProvider != null) { - this.shortcutProvider.reload(); + if(this.getShortcutsProvider() != null) { + this.getShortcutsProvider().reload(); } } @@ -257,19 +354,22 @@ public void removeShortcuts(String packageName) { * @return pojos for all applications */ public ArrayList getApplications() { - return appProvider.getAllApps(); + return this.getAppProvider().getAllApps(); } - public ContactProvider getContactProvider() { - return contactProvider; + public ContactsProvider getContactsProvider() { + ProviderEntry entry = this.providers.get("contacts"); + return (entry != null) ? ((ContactsProvider) entry.provider) : null; } - - public ShortcutProvider getShortcutProvider() { - return shortcutProvider; + + public ShortcutsProvider getShortcutsProvider() { + ProviderEntry entry = this.providers.get("shortcuts"); + return (entry != null) ? ((ShortcutsProvider) entry.provider) : null; } public AppProvider getAppProvider() { - return appProvider; + ProviderEntry entry = this.providers.get("app"); + return (entry != null) ? ((AppProvider) entry.provider) : null; } @@ -277,8 +377,7 @@ public AppProvider getAppProvider() { * Return most used items.
* May return null if no items were ever selected (app first use) * - * @param context android context - * @param limit max number of items to retrieve. You may end with less items if favorites contains non existing items. + * @param limit max number of items to retrieve. You may end with less items if favorites contains non existing items. * @return favorites' pojo */ public ArrayList getFavorites(int limit) { @@ -326,91 +425,17 @@ public boolean addToFavorites(MainActivity context, String id) { /** * Insert specified ID (probably a pojo.id) into history * - * @param context android context - * @param id pojo.id of item to record + * @param id pojo.id of item to record */ public void addToHistory(String id) { DBHelper.insertHistory(this.context, currentQuery, id); } - @Override - public void onReceive(Context context, Intent intent) { - this.handleProviderLoaded(); - } - - private void handleProviderLoaded() { - // Do not continue if not all providers are connected yet - if(this.providersNewCount != this.providersNew.size()) { - return; - } - - // Make sure all providers have completely loaded - for(IProvider provider : this.providersNew.keySet()) { - if(!provider.isLoaded()) { - return; - } - } - - // Obtain reference to old and new providers list - final Map providersOld = this.providers; - final Map providersNew = this.providersNew; - - // Store references to important services - for(IProvider provider : this.providersNew.keySet()) { - final String providerName = provider.getClass().getName(); - if(providerName.equals(AppProvider.class.getName())) { - this.appProvider = (AppProvider) provider; - } else if(providerName.equals(ContactProvider.class.getName())) { - this.contactProvider = (ContactProvider) provider; - } else if(providerName.equals(ShortcutProvider.class.getName())) { - this.shortcutProvider = (ShortcutProvider) provider; - } - } - - // Switch to the new providers list - this.providers = this.providersNew; - - // Broadcast the fact that the new providers list is ready - try { - this.context.unregisterReceiver(this); - Intent i = new Intent(MainActivity.FULL_LOAD_OVER); - this.context.sendBroadcast(i); - } catch (IllegalArgumentException e) { - // Nothing - } - - // Stop all providers that were previously used but are not used anymore - for(Map.Entry entryOld : providersOld.entrySet()) { - final IProvider providerOld = entryOld.getKey(); - final ServiceConnection connectionOld = entryOld.getValue(); - - boolean used = false; - for(IProvider providerNew : providersNew.keySet()) { - if(providerOld.getClass().getName() == providerNew.getClass().getName()) { - used = true; - break; - } - } - - if(!used) { - // Disconnect from provider service - this.context.unbindService(connectionOld); - - // Stop provider service - this.context.stopService(new Intent(this.context, providerOld.getClass())); - } - } - - // Clean up - this.providersNew = null; - this.providersNewCount = 0; - } - private Pojo getPojo(String id) { // Ask all providers if they know this id - for (IProvider provider : providers.keySet()) { - if (provider.mayFindById(id)) { - return provider.findById(id); + for (ProviderEntry entry : this.providers.values()) { + if (entry.provider != null && entry.provider.mayFindById(id)) { + return entry.provider.findById(id); } } diff --git a/app/src/main/java/fr/neamar/kiss/SettingsActivity.java b/app/src/main/java/fr/neamar/kiss/SettingsActivity.java index b0dc60f6b..2bdd950bb 100644 --- a/app/src/main/java/fr/neamar/kiss/SettingsActivity.java +++ b/app/src/main/java/fr/neamar/kiss/SettingsActivity.java @@ -16,10 +16,9 @@ public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { - // Those settings can be set without resetting the DataHandler - private String safeSettings = "theme enable-spellcheck display-keyboard root-mode require-layout-update icons-hide enable-sms-history enable-phone-history enable-app-history"; // Those settings require the app to restart - private String requireRestartSettings = "theme enable-spellcheck force-portrait"; + final static private String requireRestartSettings = "theme enable-spellcheck force-portrait"; + private SharedPreferences prefs; @SuppressWarnings("deprecation") @@ -63,12 +62,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin return; } - if (!Arrays.asList(safeSettings.split(" ")).contains(key)) { - // Reload the DataHandler since Providers preferences have changed - KissApplication.getDataHandler(this).reload(); - } - - if("enable-sms-history".equals(key) || "enable-phone-history".equals(key)) { + if("enable-sms".equals(key) || "enable-phone".equals(key)) { ComponentName receiver; if("enable-sms-history".equals(key)) { diff --git a/app/src/main/java/fr/neamar/kiss/adapter/RecordAdapter.java b/app/src/main/java/fr/neamar/kiss/adapter/RecordAdapter.java index 5520baf10..6b4ea411c 100644 --- a/app/src/main/java/fr/neamar/kiss/adapter/RecordAdapter.java +++ b/app/src/main/java/fr/neamar/kiss/adapter/RecordAdapter.java @@ -13,13 +13,13 @@ import fr.neamar.kiss.KissApplication; import fr.neamar.kiss.result.AppResult; -import fr.neamar.kiss.result.ContactResult; +import fr.neamar.kiss.result.ContactsResult; import fr.neamar.kiss.result.PhoneResult; import fr.neamar.kiss.result.Result; import fr.neamar.kiss.result.SearchResult; -import fr.neamar.kiss.result.SettingResult; -import fr.neamar.kiss.result.ShortcutResult; -import fr.neamar.kiss.result.ToggleResult; +import fr.neamar.kiss.result.SettingsResult; +import fr.neamar.kiss.result.ShortcutsResult; +import fr.neamar.kiss.result.TogglesResult; import fr.neamar.kiss.searcher.QueryInterface; public class RecordAdapter extends ArrayAdapter { @@ -47,15 +47,15 @@ public int getItemViewType(int position) { return 0; else if (results.get(position) instanceof SearchResult) return 1; - else if (results.get(position) instanceof ContactResult) + else if (results.get(position) instanceof ContactsResult) return 2; - else if (results.get(position) instanceof ToggleResult) + else if (results.get(position) instanceof TogglesResult) return 3; - else if (results.get(position) instanceof SettingResult) + else if (results.get(position) instanceof SettingsResult) return 4; else if (results.get(position) instanceof PhoneResult) return 5; - else if (results.get(position) instanceof ShortcutResult) + else if (results.get(position) instanceof ShortcutsResult) return 6; else return -1; diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/IncomingCallHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/IncomingCallHandler.java index 2a9d23ea3..c6115510a 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/IncomingCallHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/IncomingCallHandler.java @@ -8,8 +8,8 @@ import fr.neamar.kiss.DataHandler; import fr.neamar.kiss.KissApplication; -import fr.neamar.kiss.dataprovider.ContactProvider; -import fr.neamar.kiss.pojo.ContactPojo; +import fr.neamar.kiss.dataprovider.ContactsProvider; +import fr.neamar.kiss.pojo.ContactsPojo; public class IncomingCallHandler extends BroadcastReceiver { @@ -18,10 +18,10 @@ public void onReceive(final Context context, Intent intent) { try { DataHandler dataHandler = KissApplication.getDataHandler(context); - ContactProvider contactProvider = dataHandler.getContactProvider(); + ContactsProvider contactsProvider = dataHandler.getContactsProvider(); // Stop if contacts are not enabled - if (contactProvider == null) { + if (contactsProvider == null) { return; } @@ -33,7 +33,7 @@ public void onReceive(final Context context, Intent intent) { return; } - ContactPojo contactPojo = contactProvider.findByPhone(phoneNumber); + ContactsPojo contactPojo = contactsProvider.findByPhone(phoneNumber); if (contactPojo != null) { dataHandler.addToHistory(contactPojo.id); } diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/IncomingSmsHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/IncomingSmsHandler.java index 68f4551ee..039e8e5bc 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/IncomingSmsHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/IncomingSmsHandler.java @@ -8,8 +8,8 @@ import fr.neamar.kiss.DataHandler; import fr.neamar.kiss.KissApplication; -import fr.neamar.kiss.dataprovider.ContactProvider; -import fr.neamar.kiss.pojo.ContactPojo; +import fr.neamar.kiss.dataprovider.ContactsProvider; +import fr.neamar.kiss.pojo.ContactsPojo; public class IncomingSmsHandler extends BroadcastReceiver { @Override @@ -21,8 +21,8 @@ public void onReceive(Context context, Intent intent) { // Stop if contacts are not enabled DataHandler dataHandler = KissApplication.getDataHandler(context); - ContactProvider contactProvider = dataHandler.getContactProvider(); - if (contactProvider == null) { + ContactsProvider contactsProvider = dataHandler.getContactsProvider(); + if (contactsProvider == null) { // Contacts have been disabled from settings return; } @@ -39,8 +39,8 @@ public void onReceive(Context context, Intent intent) { Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]); - // Now, retrieve the contact by its lookup key on our contactProvider - ContactPojo contactPojo = contactProvider.findByPhone(msg.getOriginatingAddress()); + // Now, retrieve the contact by its lookup key on our contactsProvider + ContactsPojo contactPojo = contactsProvider.findByPhone(msg.getOriginatingAddress()); if (contactPojo != null) { // We have a match! dataHandler.addToHistory(contactPojo.id); diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java index 5bc14134d..aa325365e 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/InstallShortcutHandler.java @@ -8,8 +8,8 @@ import android.util.Log; import fr.neamar.kiss.DataHandler; import fr.neamar.kiss.KissApplication; -import fr.neamar.kiss.dataprovider.ShortcutProvider; -import fr.neamar.kiss.pojo.ShortcutPojo; +import fr.neamar.kiss.dataprovider.ShortcutsProvider; +import fr.neamar.kiss.pojo.ShortcutsPojo; public class InstallShortcutHandler extends BroadcastReceiver { @@ -17,7 +17,7 @@ public class InstallShortcutHandler extends BroadcastReceiver { public void onReceive(Context context, Intent data) { DataHandler dh = KissApplication.getDataHandler(context); - ShortcutProvider sp = dh.getShortcutProvider(); + ShortcutsProvider sp = dh.getShortcutsProvider(); if (sp == null) return; @@ -26,7 +26,7 @@ public void onReceive(Context context, Intent data) { Log.d("onReceive", "Received shortcut " + name); //avoid duplicates - if (sp != null && sp.findByName(name) != null || dh.getContactProvider().findByName(name) != null || dh.getAppProvider().findByName(name) != null ) { + if (sp != null && sp.findByName(name) != null || dh.getContactsProvider().findByName(name) != null || dh.getAppProvider().findByName(name) != null ) { Log.d("onReceive", "Duplicated shortcut " + name + ", ignoring"); return; } @@ -36,7 +36,7 @@ public void onReceive(Context context, Intent data) { target.setAction(Intent.ACTION_VIEW); } - ShortcutPojo pojo = createPojo(name); + ShortcutsPojo pojo = createPojo(name); // convert target intent to parsable uri pojo.intentUri = target.toUri(0); @@ -65,10 +65,10 @@ public void onReceive(Context context, Intent data) { } - public ShortcutPojo createPojo(String name) { - ShortcutPojo pojo = new ShortcutPojo(); + public ShortcutsPojo createPojo(String name) { + ShortcutsPojo pojo = new ShortcutsPojo(); - pojo.id = ShortcutPojo.SCHEME + name.toLowerCase(); + pojo.id = ShortcutsPojo.SCHEME + name.toLowerCase(); pojo.setName(name); return pojo; diff --git a/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java b/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java index 24531f98e..05e09d563 100644 --- a/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java +++ b/app/src/main/java/fr/neamar/kiss/broadcast/UninstallShortcutHandler.java @@ -6,8 +6,8 @@ import android.util.Log; import fr.neamar.kiss.DataHandler; import fr.neamar.kiss.KissApplication; -import fr.neamar.kiss.dataprovider.ShortcutProvider; -import fr.neamar.kiss.pojo.ShortcutPojo; +import fr.neamar.kiss.dataprovider.ShortcutsProvider; +import fr.neamar.kiss.pojo.ShortcutsPojo; public class UninstallShortcutHandler extends BroadcastReceiver { @@ -15,7 +15,7 @@ public class UninstallShortcutHandler extends BroadcastReceiver { public void onReceive(Context context, Intent data) { DataHandler dh = KissApplication.getDataHandler(context); - ShortcutProvider sp = dh.getShortcutProvider(); + ShortcutsProvider sp = dh.getShortcutsProvider(); if (sp == null) return; @@ -23,7 +23,7 @@ public void onReceive(Context context, Intent data) { String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); Log.d("onReceive", "Uninstall shortcut " + name); - ShortcutPojo pojo = (ShortcutPojo) sp.findByName(name); + ShortcutsPojo pojo = (ShortcutsPojo) sp.findByName(name); if (pojo == null) { Log.d("onReceive", "Shortcut " + name + " not found"); return; diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/AliasProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/AliasProvider.java index 0deab1dba..9cca68602 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/AliasProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/AliasProvider.java @@ -45,6 +45,14 @@ public void onCreate() { super.onCreate(); } + @Override + public void onDestroy() { + // Disconnect from application provider + this.unbindService(this.appConnection); + + super.onDestroy(); + } + @Override public void reload() { this.initialize(new LoadAliasPojos(this)); diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/ContactProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/ContactsProvider.java similarity index 90% rename from app/src/main/java/fr/neamar/kiss/dataprovider/ContactProvider.java rename to app/src/main/java/fr/neamar/kiss/dataprovider/ContactsProvider.java index d4766850f..24cc14c94 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/ContactProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/ContactsProvider.java @@ -2,17 +2,17 @@ import java.util.ArrayList; -import fr.neamar.kiss.loader.LoadContactPojos; +import fr.neamar.kiss.loader.LoadContactsPojos; import fr.neamar.kiss.normalizer.PhoneNormalizer; import fr.neamar.kiss.normalizer.StringNormalizer; -import fr.neamar.kiss.pojo.ContactPojo; +import fr.neamar.kiss.pojo.ContactsPojo; import fr.neamar.kiss.pojo.Pojo; -public class ContactProvider extends Provider { +public class ContactsProvider extends Provider { @Override public void reload() { - this.initialize(new LoadContactPojos(this)); + this.initialize(new LoadContactsPojos(this)); } public ArrayList getResults(String query) { @@ -29,7 +29,7 @@ public ArrayList getResults(String query) { String contactNameNormalized; final String queryWithSpace = " " + query; - for (ContactPojo contact : pojos) { + for (ContactsPojo contact : pojos) { relevance = 0; contactNameNormalized = contact.nameNormalized; @@ -68,7 +68,7 @@ public ArrayList getResults(String query) { // Circuit-breaker to avoid spending too much time // building results - // Important: this is made possible because LoadContactPojos already + // Important: this is made possible because LoadContactsPojos already // returns contacts sorted by popularity, so the first items should be the most useful ones. // (short queries, e.g. "a" with thousands of contacts, // can return hundreds of results which are then slow to sort and display) @@ -102,20 +102,20 @@ public Pojo findByName(String name) { /** - * Find a ContactPojo from a phoneNumber + * Find a ContactsPojo from a phoneNumber * If many contacts match, the one most often contacted will be returned * * @param phoneNumber phone number to find (will be normalized) * @return a contactpojo, or null. */ - public ContactPojo findByPhone(String phoneNumber) { + public ContactsPojo findByPhone(String phoneNumber) { // We need to normalize, since the phone can be without any formatting and with international code, // Contacts are stored with formatting and sometimes without code // Thus, normalizing them allow for simpler comparison // (contact phone number are already normalized at build time) String normalizedPhoneNumber = PhoneNormalizer.normalizePhone(phoneNumber); - for (ContactPojo pojo : pojos) { + for (ContactsPojo pojo : pojos) { if (pojo.phone.equals(normalizedPhoneNumber)) { return pojo; } diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/SettingProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/SettingsProvider.java similarity index 85% rename from app/src/main/java/fr/neamar/kiss/dataprovider/SettingProvider.java rename to app/src/main/java/fr/neamar/kiss/dataprovider/SettingsProvider.java index 846639d54..e45546959 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/SettingProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/SettingsProvider.java @@ -4,16 +4,16 @@ import java.util.regex.Pattern; import fr.neamar.kiss.R; -import fr.neamar.kiss.loader.LoadSettingPojos; +import fr.neamar.kiss.loader.LoadSettingsPojos; import fr.neamar.kiss.pojo.Pojo; -import fr.neamar.kiss.pojo.SettingPojo; +import fr.neamar.kiss.pojo.SettingsPojo; -public class SettingProvider extends Provider { +public class SettingsProvider extends Provider { private String settingName; @Override public void reload() { - this.initialize(new LoadSettingPojos(this)); + this.initialize(new LoadSettingsPojos(this)); settingName = this.getString(R.string.settings_prefix).toLowerCase(); } @@ -23,7 +23,7 @@ public ArrayList getResults(String query) { int relevance; String settingNameLowerCased; - for (SettingPojo setting : pojos) { + for (SettingsPojo setting : pojos) { relevance = 0; settingNameLowerCased = setting.nameNormalized; if (settingNameLowerCased.startsWith(query)) diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutsProvider.java similarity index 88% rename from app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java rename to app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutsProvider.java index 22a50bffe..43703497a 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/ShortcutsProvider.java @@ -2,15 +2,15 @@ import java.util.ArrayList; -import fr.neamar.kiss.loader.LoadShortcutPojos; +import fr.neamar.kiss.loader.LoadShortcutsPojos; import fr.neamar.kiss.pojo.Pojo; -import fr.neamar.kiss.pojo.ShortcutPojo; +import fr.neamar.kiss.pojo.ShortcutsPojo; -public class ShortcutProvider extends Provider { +public class ShortcutsProvider extends Provider { @Override public void reload() { - this.initialize(new LoadShortcutPojos(this)); + this.initialize(new LoadShortcutsPojos(this)); } @Override @@ -23,7 +23,7 @@ public ArrayList getResults(String query) { String shortcutNameLowerCased; final String queryWithSpace = " " + query; - for (ShortcutPojo shortcut : pojos) { + for (ShortcutsPojo shortcut : pojos) { relevance = 0; shortcutNameLowerCased = shortcut.nameNormalized; diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/ToggleProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/TogglesProvider.java similarity index 86% rename from app/src/main/java/fr/neamar/kiss/dataprovider/ToggleProvider.java rename to app/src/main/java/fr/neamar/kiss/dataprovider/TogglesProvider.java index 847ba39f2..8cfe810d9 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/ToggleProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/TogglesProvider.java @@ -4,16 +4,16 @@ import java.util.regex.Pattern; import fr.neamar.kiss.R; -import fr.neamar.kiss.loader.LoadTogglePojos; +import fr.neamar.kiss.loader.LoadTogglesPojos; import fr.neamar.kiss.pojo.Pojo; -import fr.neamar.kiss.pojo.TogglePojo; +import fr.neamar.kiss.pojo.TogglesPojo; -public class ToggleProvider extends Provider { +public class TogglesProvider extends Provider { private String toggleName; @Override public void reload() { - this.initialize(new LoadTogglePojos(this)); + this.initialize(new LoadTogglesPojos(this)); toggleName = this.getString(R.string.toggles_prefix).toLowerCase(); } @@ -23,7 +23,7 @@ public ArrayList getResults(String query) { int relevance; String toggleNameLowerCased; - for (TogglePojo toggle : pojos) { + for (TogglesPojo toggle : pojos) { relevance = 0; toggleNameLowerCased = toggle.nameNormalized; if (toggleNameLowerCased.startsWith(query)) diff --git a/app/src/main/java/fr/neamar/kiss/db/DBHelper.java b/app/src/main/java/fr/neamar/kiss/db/DBHelper.java index 1629f833c..6a05d6150 100644 --- a/app/src/main/java/fr/neamar/kiss/db/DBHelper.java +++ b/app/src/main/java/fr/neamar/kiss/db/DBHelper.java @@ -4,7 +4,7 @@ import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; -import fr.neamar.kiss.pojo.ShortcutPojo; +import fr.neamar.kiss.pojo.ShortcutsPojo; import java.util.ArrayList; @@ -186,7 +186,7 @@ public static void removeShortcuts(Context context, String packageName) { cursor.moveToFirst(); while (!cursor.isAfterLast()) { // remove from history - db.delete("history", "record = ?", new String[]{ShortcutPojo.SCHEME + cursor.getString(0).toLowerCase()}); + db.delete("history", "record = ?", new String[]{ShortcutsPojo.SCHEME + cursor.getString(0).toLowerCase()}); cursor.moveToNext(); } cursor.close(); diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadContactPojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadContactsPojos.java similarity index 87% rename from app/src/main/java/fr/neamar/kiss/loader/LoadContactPojos.java rename to app/src/main/java/fr/neamar/kiss/loader/LoadContactsPojos.java index 78dcbfdbd..5f82a9126 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadContactPojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadContactsPojos.java @@ -12,16 +12,16 @@ import fr.neamar.kiss.normalizer.PhoneNormalizer; import fr.neamar.kiss.normalizer.StringNormalizer; -import fr.neamar.kiss.pojo.ContactPojo; +import fr.neamar.kiss.pojo.ContactsPojo; -public class LoadContactPojos extends LoadPojos { +public class LoadContactsPojos extends LoadPojos { - public LoadContactPojos(Context context) { + public LoadContactsPojos(Context context) { super(context, "contact://"); } @Override - protected ArrayList doInBackground(Void... params) { + protected ArrayList doInBackground(Void... params) { Pattern homePattern = Pattern.compile("^\\+33\\s?[1-5]"); long start = System.nanoTime(); @@ -40,12 +40,12 @@ protected ArrayList doInBackground(Void... params) { // Prevent duplicates by keeping in memory encountered phones. // The string key is "phone" + "|" + "name" (so if two contacts // with distinct name share same number, they both get displayed) - HashMap> mapContacts = new HashMap<>(); + HashMap> mapContacts = new HashMap<>(); if (cur != null) { if (cur.getCount() > 0) { while (cur.moveToNext()) { - ContactPojo contact = new ContactPojo(); + ContactsPojo contact = new ContactsPojo(); contact.lookupKey = cur.getString(cur .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); @@ -81,7 +81,7 @@ protected ArrayList doInBackground(Void... params) { if (mapContacts.containsKey(contact.lookupKey)) mapContacts.get(contact.lookupKey).add(contact); else { - ArrayList phones = new ArrayList<>(); + ArrayList phones = new ArrayList<>(); phones.add(contact); mapContacts.put(contact.lookupKey, phones); } @@ -92,13 +92,13 @@ protected ArrayList doInBackground(Void... params) { } - ArrayList contacts = new ArrayList<>(); + ArrayList contacts = new ArrayList<>(); Pattern phoneFormatter = Pattern.compile("[ \\.\\(\\)]"); - for (ArrayList phones : mapContacts.values()) { + for (ArrayList phones : mapContacts.values()) { // Find primary phone and add this one. Boolean hasPrimary = false; - for (ContactPojo contact : phones) { + for (ContactsPojo contact : phones) { if (contact.primary) { contacts.add(contact); hasPrimary = true; @@ -109,7 +109,7 @@ protected ArrayList doInBackground(Void... params) { // If not available, add all (excluding duplicates). if (!hasPrimary) { HashMap added = new HashMap<>(); - for (ContactPojo contact : phones) { + for (ContactsPojo contact : phones) { String uniqueKey = phoneFormatter.matcher(contact.phone).replaceAll(""); uniqueKey = uniqueKey.replaceAll("^\\+33", "0"); uniqueKey = uniqueKey.replaceAll("^\\+1", "0"); diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadSettingPojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadSettingsPojos.java similarity index 77% rename from app/src/main/java/fr/neamar/kiss/loader/LoadSettingPojos.java rename to app/src/main/java/fr/neamar/kiss/loader/LoadSettingsPojos.java index 86f35cec3..b6f259572 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadSettingPojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadSettingsPojos.java @@ -7,17 +7,17 @@ import java.util.Locale; import fr.neamar.kiss.R; -import fr.neamar.kiss.pojo.SettingPojo; +import fr.neamar.kiss.pojo.SettingsPojo; -public class LoadSettingPojos extends LoadPojos { +public class LoadSettingsPojos extends LoadPojos { - public LoadSettingPojos(Context context) { + public LoadSettingsPojos(Context context) { super(context, "setting://"); } @Override - protected ArrayList doInBackground(Void... params) { - ArrayList settings = new ArrayList<>(); + protected ArrayList doInBackground(Void... params) { + ArrayList settings = new ArrayList<>(); settings.add(createPojo(context.getString(R.string.settings_airplane), android.provider.Settings.ACTION_AIRPLANE_MODE_SETTINGS, R.drawable.setting_airplane)); settings.add(createPojo(context.getString(R.string.settings_device_info), @@ -31,8 +31,8 @@ protected ArrayList doInBackground(Void... params) { return settings; } - private SettingPojo createPojo(String name, String settingName, int resId) { - SettingPojo pojo = new SettingPojo(); + private SettingsPojo createPojo(String name, String settingName, int resId) { + SettingsPojo pojo = new SettingsPojo(); pojo.id = pojoScheme + settingName.toLowerCase(Locale.ENGLISH); pojo.name = name; pojo.nameNormalized = pojo.name.toLowerCase(Locale.ENGLISH); diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutPojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java similarity index 60% rename from app/src/main/java/fr/neamar/kiss/loader/LoadShortcutPojos.java rename to app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java index e8c7cab16..c7b254b32 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutPojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java @@ -6,20 +6,20 @@ import android.graphics.BitmapFactory; import fr.neamar.kiss.db.DBHelper; import fr.neamar.kiss.db.ShortcutRecord; -import fr.neamar.kiss.pojo.ShortcutPojo; +import fr.neamar.kiss.pojo.ShortcutsPojo; -public class LoadShortcutPojos extends LoadPojos { +public class LoadShortcutsPojos extends LoadPojos { - public LoadShortcutPojos(Context context) { - super(context, ShortcutPojo.SCHEME); + public LoadShortcutsPojos(Context context) { + super(context, ShortcutsPojo.SCHEME); } @Override - protected ArrayList doInBackground(Void... arg0) { + protected ArrayList doInBackground(Void... arg0) { ArrayList records = DBHelper.getShortcuts(context); - ArrayList pojos = new ArrayList<>(); + ArrayList pojos = new ArrayList<>(); for (ShortcutRecord shortcutRecord : records) { - ShortcutPojo pojo = createPojo(shortcutRecord.name); + ShortcutsPojo pojo = createPojo(shortcutRecord.name); pojo.packageName = shortcutRecord.packageName; pojo.resourceName = shortcutRecord.iconResource; pojo.intentUri = shortcutRecord.intentUri; @@ -33,10 +33,10 @@ protected ArrayList doInBackground(Void... arg0) { return pojos; } - public ShortcutPojo createPojo(String name) { - ShortcutPojo pojo = new ShortcutPojo(); + public ShortcutsPojo createPojo(String name) { + ShortcutsPojo pojo = new ShortcutsPojo(); - pojo.id = ShortcutPojo.SCHEME + name.toLowerCase(); + pojo.id = ShortcutsPojo.SCHEME + name.toLowerCase(); pojo.setName(name); return pojo; diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadTogglePojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadTogglesPojos.java similarity index 82% rename from app/src/main/java/fr/neamar/kiss/loader/LoadTogglePojos.java rename to app/src/main/java/fr/neamar/kiss/loader/LoadTogglesPojos.java index 1b509d110..d766120ed 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadTogglePojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadTogglesPojos.java @@ -6,19 +6,18 @@ import java.util.ArrayList; -import fr.neamar.kiss.KissApplication; import fr.neamar.kiss.R; -import fr.neamar.kiss.pojo.TogglePojo; +import fr.neamar.kiss.pojo.TogglesPojo; -public class LoadTogglePojos extends LoadPojos { +public class LoadTogglesPojos extends LoadPojos { - public LoadTogglePojos(Context context) { + public LoadTogglesPojos(Context context) { super(context, "toggle://"); } @Override - protected ArrayList doInBackground(Void... params) { - ArrayList toggles = new ArrayList<>(); + protected ArrayList doInBackground(Void... params) { + ArrayList toggles = new ArrayList<>(); PackageManager pm = context.getPackageManager(); if (pm.hasSystemFeature(PackageManager.FEATURE_WIFI)) { toggles.add(createPojo(context.getString(R.string.toggle_wifi), "wifi", R.drawable.toggle_wifi)); @@ -45,8 +44,8 @@ protected ArrayList doInBackground(Void... params) { return toggles; } - private TogglePojo createPojo(String name, String settingName, int resId) { - TogglePojo pojo = new TogglePojo(); + private TogglesPojo createPojo(String name, String settingName, int resId) { + TogglesPojo pojo = new TogglesPojo(); pojo.id = pojoScheme + name.toLowerCase(); pojo.name = name; pojo.nameNormalized = pojo.name.toLowerCase(); diff --git a/app/src/main/java/fr/neamar/kiss/pojo/ContactPojo.java b/app/src/main/java/fr/neamar/kiss/pojo/ContactsPojo.java similarity index 92% rename from app/src/main/java/fr/neamar/kiss/pojo/ContactPojo.java rename to app/src/main/java/fr/neamar/kiss/pojo/ContactsPojo.java index fe5a87b8c..4f37cc213 100644 --- a/app/src/main/java/fr/neamar/kiss/pojo/ContactPojo.java +++ b/app/src/main/java/fr/neamar/kiss/pojo/ContactsPojo.java @@ -2,7 +2,7 @@ import android.net.Uri; -public class ContactPojo extends Pojo { +public class ContactsPojo extends Pojo { public String lookupKey = ""; public String phone = ""; diff --git a/app/src/main/java/fr/neamar/kiss/pojo/TogglePojo.java b/app/src/main/java/fr/neamar/kiss/pojo/SettingsPojo.java similarity index 68% rename from app/src/main/java/fr/neamar/kiss/pojo/TogglePojo.java rename to app/src/main/java/fr/neamar/kiss/pojo/SettingsPojo.java index 99d0dab3c..d03556838 100644 --- a/app/src/main/java/fr/neamar/kiss/pojo/TogglePojo.java +++ b/app/src/main/java/fr/neamar/kiss/pojo/SettingsPojo.java @@ -1,6 +1,6 @@ package fr.neamar.kiss.pojo; -public class TogglePojo extends Pojo { +public class SettingsPojo extends Pojo { public String settingName; public int icon = -1; } diff --git a/app/src/main/java/fr/neamar/kiss/pojo/ShortcutPojo.java b/app/src/main/java/fr/neamar/kiss/pojo/ShortcutsPojo.java similarity index 85% rename from app/src/main/java/fr/neamar/kiss/pojo/ShortcutPojo.java rename to app/src/main/java/fr/neamar/kiss/pojo/ShortcutsPojo.java index b97710133..98c11c20f 100644 --- a/app/src/main/java/fr/neamar/kiss/pojo/ShortcutPojo.java +++ b/app/src/main/java/fr/neamar/kiss/pojo/ShortcutsPojo.java @@ -2,7 +2,7 @@ import android.graphics.Bitmap; -public class ShortcutPojo extends Pojo { +public class ShortcutsPojo extends Pojo { public static final String SCHEME = "shortcut://"; diff --git a/app/src/main/java/fr/neamar/kiss/pojo/SettingPojo.java b/app/src/main/java/fr/neamar/kiss/pojo/TogglesPojo.java similarity index 68% rename from app/src/main/java/fr/neamar/kiss/pojo/SettingPojo.java rename to app/src/main/java/fr/neamar/kiss/pojo/TogglesPojo.java index a7d37376d..f902d9e2c 100644 --- a/app/src/main/java/fr/neamar/kiss/pojo/SettingPojo.java +++ b/app/src/main/java/fr/neamar/kiss/pojo/TogglesPojo.java @@ -1,6 +1,6 @@ package fr.neamar.kiss.pojo; -public class SettingPojo extends Pojo { +public class TogglesPojo extends Pojo { public String settingName; public int icon = -1; } diff --git a/app/src/main/java/fr/neamar/kiss/preference/ResetPreference.java b/app/src/main/java/fr/neamar/kiss/preference/ResetPreference.java index 425c578b5..5b5efa72e 100644 --- a/app/src/main/java/fr/neamar/kiss/preference/ResetPreference.java +++ b/app/src/main/java/fr/neamar/kiss/preference/ResetPreference.java @@ -22,7 +22,7 @@ public void onClick(DialogInterface dialog, int which) { super.onClick(dialog, which); if (which == DialogInterface.BUTTON_POSITIVE) { getContext().deleteDatabase(DB.DB_NAME); - KissApplication.getDataHandler(getContext()).reload(); + KissApplication.getDataHandler(getContext()).reloadAll(); PreferenceManager.getDefaultSharedPreferences(getContext()).edit() .putBoolean("layout-updated", true).commit(); diff --git a/app/src/main/java/fr/neamar/kiss/result/ContactResult.java b/app/src/main/java/fr/neamar/kiss/result/ContactsResult.java similarity index 93% rename from app/src/main/java/fr/neamar/kiss/result/ContactResult.java rename to app/src/main/java/fr/neamar/kiss/result/ContactsResult.java index 513c2c82c..1a99ae7ca 100644 --- a/app/src/main/java/fr/neamar/kiss/result/ContactResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/ContactsResult.java @@ -23,15 +23,15 @@ import fr.neamar.kiss.KissApplication; import fr.neamar.kiss.R; import fr.neamar.kiss.adapter.RecordAdapter; -import fr.neamar.kiss.pojo.ContactPojo; +import fr.neamar.kiss.pojo.ContactsPojo; import fr.neamar.kiss.searcher.QueryInterface; import fr.neamar.kiss.ui.ImprovedQuickContactBadge; -public class ContactResult extends Result { - private final ContactPojo contactPojo; +public class ContactsResult extends Result { + private final ContactsPojo contactPojo; private final QueryInterface queryInterface; - public ContactResult(QueryInterface queryInterface, ContactPojo contactPojo) { + public ContactsResult(QueryInterface queryInterface, ContactsPojo contactPojo) { super(); this.pojo = this.contactPojo = contactPojo; this.queryInterface = queryInterface; @@ -63,7 +63,7 @@ public View display(Context context, int position, View v) { @Override public void onClick(View v) { recordLaunch(v.getContext()); - queryInterface.launchOccurred(-1, ContactResult.this); + queryInterface.launchOccurred(-1, ContactsResult.this); } }); @@ -127,7 +127,7 @@ protected Boolean popupMenuClickHandler(Context context, RecordAdapter parent, M } @SuppressWarnings("deprecation") - private void copyPhone(Context context, ContactPojo contactPojo) { + private void copyPhone(Context context, ContactsPojo contactPojo) { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); @@ -187,7 +187,7 @@ private void launchMessaging(final Context context) { @Override public void run() { recordLaunch(context); - queryInterface.launchOccurred(-1, ContactResult.this); + queryInterface.launchOccurred(-1, ContactsResult.this); } }, KissApplication.TOUCH_DELAY); @@ -204,7 +204,7 @@ private void launchCall(final Context context) { @Override public void run() { recordLaunch(context); - queryInterface.launchOccurred(-1, ContactResult.this); + queryInterface.launchOccurred(-1, ContactsResult.this); } }, KissApplication.TOUCH_DELAY); diff --git a/app/src/main/java/fr/neamar/kiss/result/Result.java b/app/src/main/java/fr/neamar/kiss/result/Result.java index c6fba7eb4..c8d0c197c 100644 --- a/app/src/main/java/fr/neamar/kiss/result/Result.java +++ b/app/src/main/java/fr/neamar/kiss/result/Result.java @@ -21,13 +21,13 @@ import fr.neamar.kiss.adapter.RecordAdapter; import fr.neamar.kiss.db.DBHelper; import fr.neamar.kiss.pojo.AppPojo; -import fr.neamar.kiss.pojo.ContactPojo; +import fr.neamar.kiss.pojo.ContactsPojo; import fr.neamar.kiss.pojo.PhonePojo; import fr.neamar.kiss.pojo.Pojo; import fr.neamar.kiss.pojo.SearchPojo; -import fr.neamar.kiss.pojo.SettingPojo; -import fr.neamar.kiss.pojo.ShortcutPojo; -import fr.neamar.kiss.pojo.TogglePojo; +import fr.neamar.kiss.pojo.SettingsPojo; +import fr.neamar.kiss.pojo.ShortcutsPojo; +import fr.neamar.kiss.pojo.TogglesPojo; import fr.neamar.kiss.searcher.QueryInterface; public abstract class Result { @@ -39,18 +39,18 @@ public abstract class Result { public static Result fromPojo(QueryInterface parent, Pojo pojo) { if (pojo instanceof AppPojo) return new AppResult((AppPojo) pojo); - else if (pojo instanceof ContactPojo) - return new ContactResult(parent, (ContactPojo) pojo); + else if (pojo instanceof ContactsPojo) + return new ContactsResult(parent, (ContactsPojo) pojo); else if (pojo instanceof SearchPojo) return new SearchResult((SearchPojo) pojo); - else if (pojo instanceof SettingPojo) - return new SettingResult((SettingPojo) pojo); - else if (pojo instanceof TogglePojo) - return new ToggleResult((TogglePojo) pojo); + else if (pojo instanceof SettingsPojo) + return new SettingsResult((SettingsPojo) pojo); + else if (pojo instanceof TogglesPojo) + return new TogglesResult((TogglesPojo) pojo); else if (pojo instanceof PhonePojo) return new PhoneResult((PhonePojo) pojo); - else if (pojo instanceof ShortcutPojo) - return new ShortcutResult((ShortcutPojo) pojo); + else if (pojo instanceof ShortcutsPojo) + return new ShortcutsResult((ShortcutsPojo) pojo); throw new RuntimeException("Unable to create a result from POJO"); diff --git a/app/src/main/java/fr/neamar/kiss/result/SettingResult.java b/app/src/main/java/fr/neamar/kiss/result/SettingsResult.java similarity index 91% rename from app/src/main/java/fr/neamar/kiss/result/SettingResult.java rename to app/src/main/java/fr/neamar/kiss/result/SettingsResult.java index e54127bb9..d61bc51dd 100644 --- a/app/src/main/java/fr/neamar/kiss/result/SettingResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/SettingsResult.java @@ -14,12 +14,12 @@ import android.widget.TextView; import fr.neamar.kiss.R; import fr.neamar.kiss.adapter.RecordAdapter; -import fr.neamar.kiss.pojo.SettingPojo; +import fr.neamar.kiss.pojo.SettingsPojo; -public class SettingResult extends Result { - private final SettingPojo settingPojo; +public class SettingsResult extends Result { + private final SettingsPojo settingPojo; - public SettingResult(SettingPojo settingPojo) { + public SettingsResult(SettingsPojo settingPojo) { super(); this.pojo = this.settingPojo = settingPojo; } diff --git a/app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java b/app/src/main/java/fr/neamar/kiss/result/ShortcutsResult.java similarity index 92% rename from app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java rename to app/src/main/java/fr/neamar/kiss/result/ShortcutsResult.java index 58c0eb928..14da45577 100644 --- a/app/src/main/java/fr/neamar/kiss/result/ShortcutResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/ShortcutsResult.java @@ -19,12 +19,12 @@ import fr.neamar.kiss.KissApplication; import fr.neamar.kiss.R; import fr.neamar.kiss.adapter.RecordAdapter; -import fr.neamar.kiss.pojo.ShortcutPojo; +import fr.neamar.kiss.pojo.ShortcutsPojo; -public class ShortcutResult extends Result { - private final ShortcutPojo shortcutPojo; +public class ShortcutsResult extends Result { + private final ShortcutsPojo shortcutPojo; - public ShortcutResult(ShortcutPojo shortcutPojo) { + public ShortcutsResult(ShortcutsPojo shortcutPojo) { super(); this.pojo = this.shortcutPojo = shortcutPojo; } @@ -100,7 +100,7 @@ Boolean popupMenuClickHandler(Context context, RecordAdapter parent, MenuItem it return super.popupMenuClickHandler(context, parent, item); } - private void launchUninstall(Context context, ShortcutPojo shortcutPojo) { + private void launchUninstall(Context context, ShortcutsPojo shortcutPojo) { DataHandler dh = KissApplication.getDataHandler(context); if (dh != null) { dh.removeShortcut(shortcutPojo); diff --git a/app/src/main/java/fr/neamar/kiss/result/ToggleResult.java b/app/src/main/java/fr/neamar/kiss/result/TogglesResult.java similarity index 96% rename from app/src/main/java/fr/neamar/kiss/result/ToggleResult.java rename to app/src/main/java/fr/neamar/kiss/result/TogglesResult.java index 9570412c1..d37987027 100644 --- a/app/src/main/java/fr/neamar/kiss/result/ToggleResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/TogglesResult.java @@ -19,18 +19,18 @@ import fr.neamar.kiss.MainActivity; import fr.neamar.kiss.R; import fr.neamar.kiss.adapter.RecordAdapter; -import fr.neamar.kiss.pojo.TogglePojo; +import fr.neamar.kiss.pojo.TogglesPojo; import fr.neamar.kiss.toggles.TogglesHandler; -public class ToggleResult extends Result { - private final TogglePojo togglePojo; +public class TogglesResult extends Result { + private final TogglesPojo togglePojo; /** * Handler for all toggle-related queries */ private TogglesHandler togglesHandler = null; - public ToggleResult(TogglePojo togglePojo) { + public TogglesResult(TogglesPojo togglePojo) { super(); this.pojo = this.togglePojo = togglePojo; } diff --git a/app/src/main/java/fr/neamar/kiss/toggles/TogglesHandler.java b/app/src/main/java/fr/neamar/kiss/toggles/TogglesHandler.java index 495366e64..2e58a2212 100644 --- a/app/src/main/java/fr/neamar/kiss/toggles/TogglesHandler.java +++ b/app/src/main/java/fr/neamar/kiss/toggles/TogglesHandler.java @@ -11,7 +11,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import fr.neamar.kiss.KissApplication; -import fr.neamar.kiss.pojo.TogglePojo; +import fr.neamar.kiss.pojo.TogglesPojo; public class TogglesHandler { private final ConnectivityManager connectivityManager; @@ -40,7 +40,7 @@ public TogglesHandler(Context context) { * @param pojo item to look for * @return item state */ - public Boolean getState(TogglePojo pojo) { + public Boolean getState(TogglesPojo pojo) { try { switch (pojo.settingName) { case "wifi": @@ -68,7 +68,7 @@ public Boolean getState(TogglePojo pojo) { } } - public void setState(TogglePojo pojo, Boolean state) { + public void setState(TogglesPojo pojo, Boolean state) { try { switch (pojo.settingName) { case "wifi":