blob: c67e90ecf4bfeab9032331147d0d7e51c49b7f32 [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
Winson Chungaafa03c2010-06-11 17:34:16 -070019import java.util.ArrayList;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
Romain Guyd93a7d12009-03-24 21:17:50 -070024import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025
Romain Guyedcce092010-03-04 13:03:17 -080026import com.android.launcher.R;
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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 int screen = Launcher.getScreen();
44
45 if (!installShortcut(context, data, screen)) {
46 // The target screen is full, let's try the other screens
47 for (int i = 0; i < Launcher.SCREEN_COUNT; i++) {
48 if (i != screen && installShortcut(context, data, i)) break;
49 }
50 }
51 }
52
53 private boolean installShortcut(Context context, Intent data, int screen) {
Romain Guyd93a7d12009-03-24 21:17:50 -070054 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
55
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056 if (findEmptyCell(context, mCoordinates, screen)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
Winson Chungb3781d92011-01-27 15:21:41 -080058 if (intent != null) {
59 if (intent.getAction() == null) {
60 intent.setAction(Intent.ACTION_VIEW);
61 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
Winson Chungb3781d92011-01-27 15:21:41 -080063 // By default, we allow for duplicate entries (located in
64 // different places)
65 boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
66 if (duplicate || !LauncherModel.shortcutExists(context, name, intent)) {
67 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
68 app.getModel().addShortcut(context, data, screen, mCoordinates[0],
69 mCoordinates[1], true);
70 Toast.makeText(context, context.getString(R.string.shortcut_installed, name),
71 Toast.LENGTH_SHORT).show();
72 } else {
73 Toast.makeText(context, context.getString(R.string.shortcut_duplicate, name),
74 Toast.LENGTH_SHORT).show();
75 }
76
77 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 }
Romain Guyd93a7d12009-03-24 21:17:50 -070079 } else {
80 Toast.makeText(context, context.getString(R.string.out_of_space),
81 Toast.LENGTH_SHORT).show();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 }
83
84 return false;
85 }
86
87 private static boolean findEmptyCell(Context context, int[] xy, int screen) {
Adam Cohend22015c2010-07-26 22:02:18 -070088 final int xCount = LauncherModel.getCellCountX();
89 final int yCount = LauncherModel.getCellCountY();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 boolean[][] occupied = new boolean[xCount][yCount];
91
Winson Chungaafa03c2010-06-11 17:34:16 -070092 ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
93 ItemInfo item = null;
94 int cellX, cellY, spanX, spanY;
95 for (int i = 0; i < items.size(); ++i) {
96 item = items.get(i);
97 if (item.screen == screen) {
98 cellX = item.cellX;
99 cellY = item.cellY;
100 spanX = item.spanX;
101 spanY = item.spanY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 for (int x = cellX; x < cellX + spanX && x < xCount; x++) {
103 for (int y = cellY; y < cellY + spanY && y < yCount; y++) {
104 occupied[x][y] = true;
105 }
106 }
107 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 }
109
110 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
111 }
112}