The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Romain Guy | d93a7d1 | 2009-03-24 21:17:50 -0700 | [diff] [blame] | 22 | import android.widget.Toast; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 23 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 24 | import com.android.launcher.R; |
| 25 | |
Winson Chung | e428e29 | 2012-01-10 16:20:33 -0800 | [diff] [blame] | 26 | import java.util.ArrayList; |
| 27 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | public class InstallShortcutReceiver extends BroadcastReceiver { |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 29 | public static final String ACTION_INSTALL_SHORTCUT = |
Romain Guy | 51ed5b9 | 2009-06-17 10:20:34 -0700 | [diff] [blame] | 30 | "com.android.launcher.action.INSTALL_SHORTCUT"; |
| 31 | |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 32 | // A mime-type representing shortcut data |
| 33 | public static final String SHORTCUT_MIMETYPE = |
| 34 | "com.android.launcher/shortcut"; |
| 35 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | private final int[] mCoordinates = new int[2]; |
| 37 | |
| 38 | public void onReceive(Context context, Intent data) { |
Romain Guy | 51ed5b9 | 2009-06-17 10:20:34 -0700 | [diff] [blame] | 39 | if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) { |
| 40 | return; |
| 41 | } |
| 42 | |
Winson Chung | b7bea81 | 2012-02-13 12:53:54 -0800 | [diff] [blame^] | 43 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 54 | // The target screen is full, let's try the other screens |
| 55 | for (int i = 0; i < Launcher.SCREEN_COUNT; i++) { |
Winson Chung | b7bea81 | 2012-02-13 12:53:54 -0800 | [diff] [blame^] | 56 | if (i != screen && installShortcut(context, data, items, name, intent, i, |
| 57 | shortcutExists, errorMsgs)) break; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
Winson Chung | e428e29 | 2012-01-10 16:20:33 -0800 | [diff] [blame] | 60 | |
| 61 | if (!errorMsgs[0].isEmpty()) { |
| 62 | Toast.makeText(context, errorMsgs[0], |
| 63 | Toast.LENGTH_SHORT).show(); |
| 64 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Winson Chung | b7bea81 | 2012-02-13 12:53:54 -0800 | [diff] [blame^] | 67 | 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 Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 70 | if (intent != null) { |
| 71 | if (intent.getAction() == null) { |
| 72 | intent.setAction(Intent.ACTION_VIEW); |
| 73 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 74 | |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 75 | // By default, we allow for duplicate entries (located in |
| 76 | // different places) |
| 77 | boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true); |
Winson Chung | b7bea81 | 2012-02-13 12:53:54 -0800 | [diff] [blame^] | 78 | if (duplicate || !shortcutExists) { |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 79 | LauncherApplication app = (LauncherApplication) context.getApplicationContext(); |
Adam Cohen | d919882 | 2011-11-22 16:42:47 -0800 | [diff] [blame] | 80 | ShortcutInfo info = app.getModel().addShortcut(context, data, |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 81 | LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, mCoordinates[0], |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 82 | mCoordinates[1], true); |
Adam Cohen | d919882 | 2011-11-22 16:42:47 -0800 | [diff] [blame] | 83 | if (info != null) { |
Winson Chung | e428e29 | 2012-01-10 16:20:33 -0800 | [diff] [blame] | 84 | errorMsgs[0] = context.getString(R.string.shortcut_installed, name); |
Adam Cohen | d919882 | 2011-11-22 16:42:47 -0800 | [diff] [blame] | 85 | } else { |
| 86 | return false; |
| 87 | } |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 88 | } else { |
Winson Chung | e428e29 | 2012-01-10 16:20:33 -0800 | [diff] [blame] | 89 | errorMsgs[0] = context.getString(R.string.shortcut_duplicate, name); |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | } |
Romain Guy | d93a7d1 | 2009-03-24 21:17:50 -0700 | [diff] [blame] | 94 | } else { |
Winson Chung | e428e29 | 2012-01-10 16:20:33 -0800 | [diff] [blame] | 95 | errorMsgs[0] = context.getString(R.string.out_of_space); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
Winson Chung | b7bea81 | 2012-02-13 12:53:54 -0800 | [diff] [blame^] | 101 | private static boolean findEmptyCell(Context context, ArrayList<ItemInfo> items, int[] xy, |
| 102 | int screen) { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 103 | final int xCount = LauncherModel.getCellCountX(); |
| 104 | final int yCount = LauncherModel.getCellCountY(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 105 | boolean[][] occupied = new boolean[xCount][yCount]; |
| 106 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 107 | ItemInfo item = null; |
| 108 | int cellX, cellY, spanX, spanY; |
| 109 | for (int i = 0; i < items.size(); ++i) { |
| 110 | item = items.get(i); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 111 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied); |
| 127 | } |
| 128 | } |