summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod
diff options
context:
space:
mode:
authorMichael Jurka <mikejurka@google.com>2013-05-14 20:17:58 +0200
committerSteve Kondik <shade@chemlab.org>2013-08-17 02:35:26 -0700
commit3bc0399cda899e1bca2a7bc3e9f10914edd0f248 (patch)
tree5a69621fe7efce32cc0015718a60baeed016a6bf /src/com/cyanogenmod
parentf2147c1591b63dcf1f727cf81c70bb79dcf26959 (diff)
downloadandroid_packages_apps_Trebuchet-3bc0399cda899e1bca2a7bc3e9f10914edd0f248.tar.gz
android_packages_apps_Trebuchet-3bc0399cda899e1bca2a7bc3e9f10914edd0f248.tar.bz2
android_packages_apps_Trebuchet-3bc0399cda899e1bca2a7bc3e9f10914edd0f248.zip
Persist list of shortcuts to install on home workspace
Bug: 8707110 Change-Id: I6740350040dceb0edaf4295d6a5af62a088a3055
Diffstat (limited to 'src/com/cyanogenmod')
-rwxr-xr-xsrc/com/cyanogenmod/trebuchet/InstallShortcutReceiver.java113
1 files changed, 106 insertions, 7 deletions
diff --git a/src/com/cyanogenmod/trebuchet/InstallShortcutReceiver.java b/src/com/cyanogenmod/trebuchet/InstallShortcutReceiver.java
index 27253fd1e..f2d42ba23 100755
--- a/src/com/cyanogenmod/trebuchet/InstallShortcutReceiver.java
+++ b/src/com/cyanogenmod/trebuchet/InstallShortcutReceiver.java
@@ -27,6 +27,10 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.util.Base64;
+import android.util.Log;
import android.widget.Toast;
import com.cyanogenmod.trebuchet.preference.PreferencesProvider;
@@ -36,12 +40,23 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
+import org.json.*;
+
public class InstallShortcutReceiver extends BroadcastReceiver {
public static final String ACTION_INSTALL_SHORTCUT =
"com.android.launcher.action.INSTALL_SHORTCUT";
public static final String NEW_APPS_PAGE_KEY = "apps.new.page";
public static final String NEW_APPS_LIST_KEY = "apps.new.list";
+ public static final String DATA_INTENT_KEY = "intent.data";
+ public static final String LAUNCH_INTENT_KEY = "intent.launch";
+ public static final String NAME_KEY = "name";
+ public static final String ICON_KEY = "icon";
+ public static final String ICON_RESOURCE_NAME_KEY = "iconResource";
+ public static final String ICON_RESOURCE_PACKAGE_NAME_KEY = "iconResourcePackage";
+ // The set of shortcuts that are pending install
+ public static final String APPS_PENDING_INSTALL = "apps_to_install";
+
public static final int NEW_SHORTCUT_BOUNCE_DURATION = 450;
public static final int NEW_SHORTCUT_STAGGER_DELAY = 75;
@@ -60,10 +75,6 @@ public class InstallShortcutReceiver extends BroadcastReceiver {
private static Object sLock = new Object();
- // The set of shortcuts that are pending install
- private static ArrayList<PendingInstallShortcutInfo> mInstallQueue =
- new ArrayList<PendingInstallShortcutInfo>();
-
private static void addToStringSet(SharedPreferences sharedPrefs,
SharedPreferences.Editor editor, String key, String value) {
Set<String> strings = sharedPrefs.getStringSet(key, null);
@@ -75,6 +86,82 @@ public class InstallShortcutReceiver extends BroadcastReceiver {
strings.add(value);
editor.putStringSet(key, strings);
}
+
+ private static void addToInstallQueue(
+ SharedPreferences sharedPrefs, PendingInstallShortcutInfo info) {
+ synchronized(sLock) {
+ try {
+ JSONStringer json = new JSONStringer()
+ .object()
+ .key(DATA_INTENT_KEY).value(info.data.toUri(0))
+ .key(LAUNCH_INTENT_KEY).value(info.launchIntent.toUri(0))
+ .key(NAME_KEY).value(info.name);
+ if (info.icon != null) {
+ byte[] iconByteArray = ItemInfo.flattenBitmap(info.icon);
+ json = json.key(ICON_KEY).value(
+ Base64.encodeToString(
+ iconByteArray, 0, iconByteArray.length, Base64.DEFAULT));
+ }
+ if (info.iconResource != null) {
+ json = json.key(ICON_RESOURCE_NAME_KEY).value(info.iconResource.resourceName);
+ json = json.key(ICON_RESOURCE_PACKAGE_NAME_KEY)
+ .value(info.iconResource.packageName);
+ }
+ json = json.endObject();
+ SharedPreferences.Editor editor = sharedPrefs.edit();
+ addToStringSet(sharedPrefs, editor, APPS_PENDING_INSTALL, json.toString());
+ editor.commit();
+ } catch (org.json.JSONException e) {
+ Log.d("InstallShortcutReceiver", "Exception when adding shortcut: " + e);
+ }
+ }
+ }
+
+ private static ArrayList<PendingInstallShortcutInfo> getAndClearInstallQueue(
+ SharedPreferences sharedPrefs) {
+ synchronized(sLock) {
+ Set<String> strings = sharedPrefs.getStringSet(APPS_PENDING_INSTALL, null);
+ if (strings == null) {
+ return new ArrayList<PendingInstallShortcutInfo>();
+ }
+ ArrayList<PendingInstallShortcutInfo> infos =
+ new ArrayList<PendingInstallShortcutInfo>();
+ for (String json : strings) {
+ try {
+ JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
+ Intent data = Intent.parseUri(object.getString(DATA_INTENT_KEY), 0);
+ Intent launchIntent = Intent.parseUri(object.getString(LAUNCH_INTENT_KEY), 0);
+ String name = object.getString(NAME_KEY);
+ String iconBase64 = object.optString(ICON_KEY);
+ String iconResourceName = object.optString(ICON_RESOURCE_NAME_KEY);
+ String iconResourcePackageName =
+ object.optString(ICON_RESOURCE_PACKAGE_NAME_KEY);
+ if (iconBase64 != null && !iconBase64.isEmpty()) {
+ byte[] iconArray = Base64.decode(iconBase64, Base64.DEFAULT);
+ Bitmap b = BitmapFactory.decodeByteArray(iconArray, 0, iconArray.length);
+ data.putExtra(Intent.EXTRA_SHORTCUT_ICON, b);
+ } else if (iconResourceName != null && !iconResourceName.isEmpty()) {
+ Intent.ShortcutIconResource iconResource =
+ new Intent.ShortcutIconResource();
+ iconResource.resourceName = iconResourceName;
+ iconResource.packageName = iconResourcePackageName;
+ data.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
+ }
+ data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
+ PendingInstallShortcutInfo info =
+ new PendingInstallShortcutInfo(data, name, launchIntent);
+ infos.add(info);
+ } catch (org.json.JSONException e) {
+ Log.d("InstallShortcutReceiver", "Exception reading shortcut to add: " + e);
+ } catch (java.net.URISyntaxException e) {
+ Log.d("InstallShortcutReceiver", "Exception reading shortcut to add: " + e);
+ }
+ }
+ sharedPrefs.edit().putStringSet(APPS_PENDING_INSTALL, new HashSet<String>()).commit();
+ return infos;
+ }
+ }
+
// Determines whether to defer installing shortcuts immediately until
// processAllPendingInstalls() is called.
private static boolean mUseInstallQueue = false;
@@ -83,6 +170,8 @@ public class InstallShortcutReceiver extends BroadcastReceiver {
Intent data;
Intent launchIntent;
String name;
+ Bitmap icon;
+ Intent.ShortcutIconResource iconResource;
public PendingInstallShortcutInfo(Intent rawData, String shortcutName,
Intent shortcutIntent) {
@@ -113,13 +202,21 @@ public class InstallShortcutReceiver extends BroadcastReceiver {
return;
}
}
+ Bitmap icon = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
+ Intent.ShortcutIconResource iconResource =
+ data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
+
// Queue the item up for adding if launcher has not loaded properly yet
boolean launcherNotLoaded = LauncherModel.getWorkspaceCellCountX() <= 0 ||
LauncherModel.getWorkspaceCellCountY() <= 0;
PendingInstallShortcutInfo info = new PendingInstallShortcutInfo(data, name, intent);
+ info.icon = icon;
+ info.iconResource = iconResource;
if (mUseInstallQueue || launcherNotLoaded) {
- mInstallQueue.add(info);
+ String spKey = LauncherApplication.getSharedPreferencesKey();
+ SharedPreferences sp = context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
+ addToInstallQueue(sp, info);
} else {
processInstallShortcut(context, info);
}
@@ -133,10 +230,12 @@ public class InstallShortcutReceiver extends BroadcastReceiver {
flushInstallQueue(context);
}
static void flushInstallQueue(Context context) {
- Iterator<PendingInstallShortcutInfo> iter = mInstallQueue.iterator();
+ String spKey = LauncherApplication.getSharedPreferencesKey();
+ SharedPreferences sp = context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
+ ArrayList<PendingInstallShortcutInfo> installQueue = getAndClearInstallQueue(sp);
+ Iterator<PendingInstallShortcutInfo> iter = installQueue.iterator();
while (iter.hasNext()) {
processInstallShortcut(context, iter.next());
- iter.remove();
}
}