2009-03-03 19:32:27 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-07-30 13:37:37 -07:00
|
|
|
package com.android.launcher2;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
2012-03-21 16:10:31 -07:00
|
|
|
import android.content.SharedPreferences;
|
2012-02-29 10:56:19 -08:00
|
|
|
import android.content.pm.ActivityInfo;
|
|
|
|
|
import android.content.pm.PackageManager;
|
2009-03-24 21:17:50 -07:00
|
|
|
import android.widget.Toast;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-03-04 13:03:17 -08:00
|
|
|
import com.android.launcher.R;
|
|
|
|
|
|
2012-01-10 16:20:33 -08:00
|
|
|
import java.util.ArrayList;
|
2012-03-21 16:10:31 -07:00
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Set;
|
2012-01-10 16:20:33 -08:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
public class InstallShortcutReceiver extends BroadcastReceiver {
|
2010-10-27 17:18:37 -07:00
|
|
|
public static final String ACTION_INSTALL_SHORTCUT =
|
2009-06-17 10:20:34 -07:00
|
|
|
"com.android.launcher.action.INSTALL_SHORTCUT";
|
2012-03-21 16:10:31 -07:00
|
|
|
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 int NEW_SHORTCUT_BOUNCE_DURATION = 450;
|
|
|
|
|
public static final int NEW_SHORTCUT_STAGGER_DELAY = 75;
|
|
|
|
|
|
|
|
|
|
private static final int INSTALL_SHORTCUT_SUCCESSFUL = 0;
|
|
|
|
|
private static final int INSTALL_SHORTCUT_IS_DUPLICATE = -1;
|
|
|
|
|
private static final int INSTALL_SHORTCUT_NO_SPACE = -2;
|
2009-06-17 10:20:34 -07:00
|
|
|
|
2010-10-29 11:00:27 -07:00
|
|
|
// A mime-type representing shortcut data
|
|
|
|
|
public static final String SHORTCUT_MIMETYPE =
|
|
|
|
|
"com.android.launcher/shortcut";
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
private final int[] mCoordinates = new int[2];
|
|
|
|
|
|
|
|
|
|
public void onReceive(Context context, Intent data) {
|
2009-06-17 10:20:34 -07:00
|
|
|
if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-03-21 16:10:31 -07:00
|
|
|
String spKey = LauncherApplication.getSharedPreferencesKey();
|
|
|
|
|
SharedPreferences sp = context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
|
2009-06-17 10:20:34 -07:00
|
|
|
|
2012-02-13 12:53:54 -08:00
|
|
|
final Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
|
2012-02-29 10:56:19 -08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-13 12:53:54 -08:00
|
|
|
|
2012-04-03 14:22:34 -07:00
|
|
|
// Lock on the app so that we don't try and get the items while apps are being added
|
|
|
|
|
LauncherApplication app = (LauncherApplication) context.getApplicationContext();
|
2012-03-21 16:10:31 -07:00
|
|
|
final int[] result = {INSTALL_SHORTCUT_SUCCESSFUL};
|
|
|
|
|
boolean found = false;
|
2012-04-03 14:22:34 -07:00
|
|
|
synchronized (app) {
|
|
|
|
|
final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
|
|
|
|
|
final boolean exists = LauncherModel.shortcutExists(context, name, intent);
|
|
|
|
|
|
|
|
|
|
// Try adding to the workspace screens incrementally, starting at the default or center
|
|
|
|
|
// screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
|
|
|
|
|
final int screen = Launcher.DEFAULT_SCREEN;
|
|
|
|
|
for (int i = 0; i < (2 * Launcher.SCREEN_COUNT) + 1 && !found; ++i) {
|
|
|
|
|
int si = screen + (int) ((i / 2f) + 0.5f) * ((i % 2 == 1) ? 1 : -1);
|
|
|
|
|
if (0 <= si && si < Launcher.SCREEN_COUNT) {
|
|
|
|
|
found = installShortcut(context, data, items, name, intent, si, exists, sp,
|
|
|
|
|
result);
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
2012-01-10 16:20:33 -08:00
|
|
|
|
2012-03-21 16:10:31 -07:00
|
|
|
// We only report error messages (duplicate shortcut or out of space) as the add-animation
|
|
|
|
|
// will provide feedback otherwise
|
|
|
|
|
if (!found) {
|
|
|
|
|
if (result[0] == INSTALL_SHORTCUT_NO_SPACE) {
|
2012-04-24 14:43:54 -07:00
|
|
|
Toast.makeText(context, context.getString(R.string.completely_out_of_space),
|
2012-03-21 16:10:31 -07:00
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
|
} else if (result[0] == INSTALL_SHORTCUT_IS_DUPLICATE) {
|
|
|
|
|
Toast.makeText(context, context.getString(R.string.shortcut_duplicate, name),
|
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
2012-01-10 16:20:33 -08:00
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2012-02-13 12:53:54 -08:00
|
|
|
private boolean installShortcut(Context context, Intent data, ArrayList<ItemInfo> items,
|
2012-04-03 14:22:34 -07:00
|
|
|
String name, Intent intent, final int screen, boolean shortcutExists,
|
|
|
|
|
final SharedPreferences sharedPrefs, int[] result) {
|
2012-02-13 12:53:54 -08:00
|
|
|
if (findEmptyCell(context, items, mCoordinates, screen)) {
|
2011-01-27 15:21:41 -08:00
|
|
|
if (intent != null) {
|
|
|
|
|
if (intent.getAction() == null) {
|
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
2012-03-22 05:54:33 -07:00
|
|
|
} else if (intent.getAction().equals(Intent.ACTION_MAIN) &&
|
|
|
|
|
intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
|
|
|
|
|
intent.addFlags(
|
|
|
|
|
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
|
2011-01-27 15:21:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// By default, we allow for duplicate entries (located in
|
|
|
|
|
// different places)
|
|
|
|
|
boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
|
2012-02-13 12:53:54 -08:00
|
|
|
if (duplicate || !shortcutExists) {
|
2012-03-21 16:10:31 -07:00
|
|
|
// If the new app is going to fall into the same page as before, then just
|
|
|
|
|
// continue adding to the current page
|
|
|
|
|
int newAppsScreen = sharedPrefs.getInt(NEW_APPS_PAGE_KEY, screen);
|
|
|
|
|
Set<String> newApps = new HashSet<String>();
|
|
|
|
|
if (newAppsScreen == screen) {
|
|
|
|
|
newApps = sharedPrefs.getStringSet(NEW_APPS_LIST_KEY, newApps);
|
|
|
|
|
}
|
|
|
|
|
newApps.add(intent.toUri(0).toString());
|
2012-04-03 14:22:34 -07:00
|
|
|
final Set<String> savedNewApps = newApps;
|
|
|
|
|
new Thread("setNewAppsThread") {
|
|
|
|
|
public void run() {
|
|
|
|
|
sharedPrefs.edit()
|
|
|
|
|
.putInt(NEW_APPS_PAGE_KEY, screen)
|
|
|
|
|
.putStringSet(NEW_APPS_LIST_KEY, savedNewApps)
|
|
|
|
|
.commit();
|
|
|
|
|
}
|
|
|
|
|
}.start();
|
2012-03-21 16:10:31 -07:00
|
|
|
|
|
|
|
|
// Update the Launcher db
|
2011-01-27 15:21:41 -08:00
|
|
|
LauncherApplication app = (LauncherApplication) context.getApplicationContext();
|
2011-11-22 16:42:47 -08:00
|
|
|
ShortcutInfo info = app.getModel().addShortcut(context, data,
|
2012-03-21 16:10:31 -07:00
|
|
|
LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
|
|
|
|
|
mCoordinates[0], mCoordinates[1], true);
|
|
|
|
|
if (info == null) {
|
2011-11-22 16:42:47 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
2011-01-27 15:21:41 -08:00
|
|
|
} else {
|
2012-03-21 16:10:31 -07:00
|
|
|
result[0] = INSTALL_SHORTCUT_IS_DUPLICATE;
|
2011-01-27 15:21:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2009-03-24 21:17:50 -07:00
|
|
|
} else {
|
2012-03-21 16:10:31 -07:00
|
|
|
result[0] = INSTALL_SHORTCUT_NO_SPACE;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-13 12:53:54 -08:00
|
|
|
private static boolean findEmptyCell(Context context, ArrayList<ItemInfo> items, int[] xy,
|
|
|
|
|
int screen) {
|
2010-07-26 22:02:18 -07:00
|
|
|
final int xCount = LauncherModel.getCellCountX();
|
|
|
|
|
final int yCount = LauncherModel.getCellCountY();
|
2009-03-03 19:32:27 -08:00
|
|
|
boolean[][] occupied = new boolean[xCount][yCount];
|
|
|
|
|
|
2010-06-11 17:34:16 -07:00
|
|
|
ItemInfo item = null;
|
|
|
|
|
int cellX, cellY, spanX, spanY;
|
|
|
|
|
for (int i = 0; i < items.size(); ++i) {
|
|
|
|
|
item = items.get(i);
|
2011-07-13 17:25:49 -07:00
|
|
|
if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
|
|
|
|
|
if (item.screen == screen) {
|
|
|
|
|
cellX = item.cellX;
|
|
|
|
|
cellY = item.cellY;
|
|
|
|
|
spanX = item.spanX;
|
|
|
|
|
spanY = item.spanY;
|
2012-04-24 14:49:03 -07:00
|
|
|
for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
|
|
|
|
|
for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
|
2011-07-13 17:25:49 -07:00
|
|
|
occupied[x][y] = true;
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
|
|
|
|
|
}
|
|
|
|
|
}
|