blob: 9e2067d346e5714c25aa3d9f9c97dae4c181f680 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
Romain Guyd93a7d12009-03-24 21:17:50 -070022import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023
Romain Guyedcce092010-03-04 13:03:17 -080024import com.android.launcher.R;
25
Winson Chunge428e292012-01-10 16:20:33 -080026import java.util.ArrayList;
27
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028public class InstallShortcutReceiver extends BroadcastReceiver {
Winson Chunga9abd0e2010-10-27 17:18:37 -070029 public static final String ACTION_INSTALL_SHORTCUT =
Romain Guy51ed5b92009-06-17 10:20:34 -070030 "com.android.launcher.action.INSTALL_SHORTCUT";
31
Winson Chung68846fd2010-10-29 11:00:27 -070032 // A mime-type representing shortcut data
33 public static final String SHORTCUT_MIMETYPE =
34 "com.android.launcher/shortcut";
35
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036 private final int[] mCoordinates = new int[2];
37
38 public void onReceive(Context context, Intent data) {
Romain Guy51ed5b92009-06-17 10:20:34 -070039 if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) {
40 return;
41 }
42
Winson Chungb7bea812012-02-13 12:53:54 -080043 final int screen = Launcher.getScreen();
44 final String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
45 final Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
46
47 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
48 final boolean shortcutExists = LauncherModel.shortcutExists(context, name, intent);
49
50 final String[] errorMsgs = {""};
51
52 if (!installShortcut(context, data, items, name, intent, screen, shortcutExists,
53 errorMsgs)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 // The target screen is full, let's try the other screens
55 for (int i = 0; i < Launcher.SCREEN_COUNT; i++) {
Winson Chungb7bea812012-02-13 12:53:54 -080056 if (i != screen && installShortcut(context, data, items, name, intent, i,
57 shortcutExists, errorMsgs)) break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 }
59 }
Winson Chunge428e292012-01-10 16:20:33 -080060
61 if (!errorMsgs[0].isEmpty()) {
62 Toast.makeText(context, errorMsgs[0],
63 Toast.LENGTH_SHORT).show();
64 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 }
66
Winson Chungb7bea812012-02-13 12:53:54 -080067 private boolean installShortcut(Context context, Intent data, ArrayList<ItemInfo> items,
68 String name, Intent intent, int screen, boolean shortcutExists, String[] errorMsgs) {
69 if (findEmptyCell(context, items, mCoordinates, screen)) {
Winson Chungb3781d92011-01-27 15:21:41 -080070 if (intent != null) {
71 if (intent.getAction() == null) {
72 intent.setAction(Intent.ACTION_VIEW);
73 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
Winson Chungb3781d92011-01-27 15:21:41 -080075 // By default, we allow for duplicate entries (located in
76 // different places)
77 boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
Winson Chungb7bea812012-02-13 12:53:54 -080078 if (duplicate || !shortcutExists) {
Winson Chungb3781d92011-01-27 15:21:41 -080079 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Adam Cohend9198822011-11-22 16:42:47 -080080 ShortcutInfo info = app.getModel().addShortcut(context, data,
Winson Chung3d503fb2011-07-13 17:25:49 -070081 LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, mCoordinates[0],
Winson Chungb3781d92011-01-27 15:21:41 -080082 mCoordinates[1], true);
Adam Cohend9198822011-11-22 16:42:47 -080083 if (info != null) {
Winson Chunge428e292012-01-10 16:20:33 -080084 errorMsgs[0] = context.getString(R.string.shortcut_installed, name);
Adam Cohend9198822011-11-22 16:42:47 -080085 } else {
86 return false;
87 }
Winson Chungb3781d92011-01-27 15:21:41 -080088 } else {
Winson Chunge428e292012-01-10 16:20:33 -080089 errorMsgs[0] = context.getString(R.string.shortcut_duplicate, name);
Winson Chungb3781d92011-01-27 15:21:41 -080090 }
91
92 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 }
Romain Guyd93a7d12009-03-24 21:17:50 -070094 } else {
Winson Chunge428e292012-01-10 16:20:33 -080095 errorMsgs[0] = context.getString(R.string.out_of_space);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 }
97
98 return false;
99 }
100
Winson Chungb7bea812012-02-13 12:53:54 -0800101 private static boolean findEmptyCell(Context context, ArrayList<ItemInfo> items, int[] xy,
102 int screen) {
Adam Cohend22015c2010-07-26 22:02:18 -0700103 final int xCount = LauncherModel.getCellCountX();
104 final int yCount = LauncherModel.getCellCountY();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 boolean[][] occupied = new boolean[xCount][yCount];
106
Winson Chungaafa03c2010-06-11 17:34:16 -0700107 ItemInfo item = null;
108 int cellX, cellY, spanX, spanY;
109 for (int i = 0; i < items.size(); ++i) {
110 item = items.get(i);
Winson Chung3d503fb2011-07-13 17:25:49 -0700111 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
112 if (item.screen == screen) {
113 cellX = item.cellX;
114 cellY = item.cellY;
115 spanX = item.spanX;
116 spanY = item.spanY;
117 for (int x = cellX; x < cellX + spanX && x < xCount; x++) {
118 for (int y = cellY; y < cellY + spanY && y < yCount; y++) {
119 occupied[x][y] = true;
120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 }
122 }
123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 }
125
126 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
127 }
128}