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 17 package com.android.launcher2; 18 19 import java.util.ArrayList; 20 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.widget.Toast; 25 26 import com.android.launcher.R; 27 28 public class InstallShortcutReceiver extends BroadcastReceiver { 29 public static final String ACTION_INSTALL_SHORTCUT = 30 "com.android.launcher.action.INSTALL_SHORTCUT"; 31 32 // A mime-type representing shortcut data 33 public static final String SHORTCUT_MIMETYPE = 34 "com.android.launcher/shortcut"; 35 36 private final int[] mCoordinates = new int[2]; 37 onReceive(Context context, Intent data)38 public void onReceive(Context context, Intent data) { 39 if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) { 40 return; 41 } 42 43 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 installShortcut(Context context, Intent data, int screen)53 private boolean installShortcut(Context context, Intent data, int screen) { 54 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); 55 56 if (findEmptyCell(context, mCoordinates, screen)) { 57 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); 58 if (intent != null) { 59 if (intent.getAction() == null) { 60 intent.setAction(Intent.ACTION_VIEW); 61 } 62 63 // 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, 69 LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, mCoordinates[0], 70 mCoordinates[1], true); 71 Toast.makeText(context, context.getString(R.string.shortcut_installed, name), 72 Toast.LENGTH_SHORT).show(); 73 } else { 74 Toast.makeText(context, context.getString(R.string.shortcut_duplicate, name), 75 Toast.LENGTH_SHORT).show(); 76 } 77 78 return true; 79 } 80 } else { 81 Toast.makeText(context, context.getString(R.string.out_of_space), 82 Toast.LENGTH_SHORT).show(); 83 } 84 85 return false; 86 } 87 findEmptyCell(Context context, int[] xy, int screen)88 private static boolean findEmptyCell(Context context, int[] xy, int screen) { 89 final int xCount = LauncherModel.getCellCountX(); 90 final int yCount = LauncherModel.getCellCountY(); 91 boolean[][] occupied = new boolean[xCount][yCount]; 92 93 ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context); 94 ItemInfo item = null; 95 int cellX, cellY, spanX, spanY; 96 for (int i = 0; i < items.size(); ++i) { 97 item = items.get(i); 98 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { 99 if (item.screen == screen) { 100 cellX = item.cellX; 101 cellY = item.cellY; 102 spanX = item.spanX; 103 spanY = item.spanY; 104 for (int x = cellX; x < cellX + spanX && x < xCount; x++) { 105 for (int y = cellY; y < cellY + spanY && y < yCount; y++) { 106 occupied[x][y] = true; 107 } 108 } 109 } 110 } 111 } 112 113 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied); 114 } 115 } 116