diff options
Diffstat (limited to 'src/com/android/launcher2/InstallShortcutReceiver.java')
-rw-r--r-- | src/com/android/launcher2/InstallShortcutReceiver.java | 63 |
1 files changed, 44 insertions, 19 deletions
diff --git a/src/com/android/launcher2/InstallShortcutReceiver.java b/src/com/android/launcher2/InstallShortcutReceiver.java index ed8f29942..e04ce6419 100644 --- a/src/com/android/launcher2/InstallShortcutReceiver.java +++ b/src/com/android/launcher2/InstallShortcutReceiver.java @@ -16,15 +16,18 @@ package com.android.launcher2; -import java.util.ArrayList; - import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageManager; import android.widget.Toast; import com.android.launcher.R; +import java.util.ArrayList; + public class InstallShortcutReceiver extends BroadcastReceiver { public static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT"; @@ -40,21 +43,46 @@ public class InstallShortcutReceiver extends BroadcastReceiver { return; } - int screen = Launcher.getScreen(); + final int screen = Launcher.getScreen(); + final Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); + if (intent == null) { + return; + } + // This name is only used for comparisons and notifications, so fall back to activity name + // if not supplied + String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); + if (name == null) { + try { + PackageManager pm = context.getPackageManager(); + ActivityInfo info = pm.getActivityInfo(intent.getComponent(), 0); + name = info.loadLabel(pm).toString(); + } catch (PackageManager.NameNotFoundException nnfe) { + return; + } + } + + final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context); + final boolean shortcutExists = LauncherModel.shortcutExists(context, name, intent); + final String[] errorMsgs = {""}; - if (!installShortcut(context, data, screen)) { + if (!installShortcut(context, data, items, name, intent, screen, shortcutExists, + errorMsgs)) { // The target screen is full, let's try the other screens for (int i = 0; i < Launcher.SCREEN_COUNT; i++) { - if (i != screen && installShortcut(context, data, i)) break; + if (i != screen && installShortcut(context, data, items, name, intent, i, + shortcutExists, errorMsgs)) break; } } - } - private boolean installShortcut(Context context, Intent data, int screen) { - String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); + if (!errorMsgs[0].isEmpty()) { + Toast.makeText(context, errorMsgs[0], + Toast.LENGTH_SHORT).show(); + } + } - if (findEmptyCell(context, mCoordinates, screen)) { - Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); + private boolean installShortcut(Context context, Intent data, ArrayList<ItemInfo> items, + String name, Intent intent, int screen, boolean shortcutExists, String[] errorMsgs) { + if (findEmptyCell(context, items, mCoordinates, screen)) { if (intent != null) { if (intent.getAction() == null) { intent.setAction(Intent.ACTION_VIEW); @@ -63,38 +91,35 @@ public class InstallShortcutReceiver extends BroadcastReceiver { // By default, we allow for duplicate entries (located in // different places) boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true); - if (duplicate || !LauncherModel.shortcutExists(context, name, intent)) { + if (duplicate || !shortcutExists) { LauncherApplication app = (LauncherApplication) context.getApplicationContext(); ShortcutInfo info = app.getModel().addShortcut(context, data, LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, mCoordinates[0], mCoordinates[1], true); if (info != null) { - Toast.makeText(context, context.getString(R.string.shortcut_installed, name), - Toast.LENGTH_SHORT).show(); + errorMsgs[0] = context.getString(R.string.shortcut_installed, name); } else { return false; } } else { - Toast.makeText(context, context.getString(R.string.shortcut_duplicate, name), - Toast.LENGTH_SHORT).show(); + errorMsgs[0] = context.getString(R.string.shortcut_duplicate, name); } return true; } } else { - Toast.makeText(context, context.getString(R.string.out_of_space), - Toast.LENGTH_SHORT).show(); + errorMsgs[0] = context.getString(R.string.out_of_space); } return false; } - private static boolean findEmptyCell(Context context, int[] xy, int screen) { + private static boolean findEmptyCell(Context context, ArrayList<ItemInfo> items, int[] xy, + int screen) { final int xCount = LauncherModel.getCellCountX(); final int yCount = LauncherModel.getCellCountY(); boolean[][] occupied = new boolean[xCount][yCount]; - ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context); ItemInfo item = null; int cellX, cellY, spanX, spanY; for (int i = 0; i < items.size(); ++i) { |