blob: 2742f2f971e611d0ccf2f23836d0bf16b56e550c [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
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000019import com.android.common.Search;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Activity;
22import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080028import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040029import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080030import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.IntentFilter;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040036import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040038import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070040import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040041import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080042import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080044import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080045import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080047import android.graphics.drawable.ColorDrawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040048import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020050import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040053import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080054import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070055import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.text.Selection;
57import android.text.SpannableStringBuilder;
58import android.text.TextUtils;
59import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070060import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080062import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.KeyEvent;
64import android.view.LayoutInflater;
65import android.view.Menu;
66import android.view.MenuItem;
67import android.view.View;
68import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.view.View.OnLongClickListener;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.widget.TextView;
73import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080074import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080075import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080076import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070077import android.appwidget.AppWidgetManager;
78import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import java.util.ArrayList;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040081import java.util.List;
Joe Onorato9c1289c2009-08-17 11:03:03 -040082import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070083import java.io.DataOutputStream;
84import java.io.FileNotFoundException;
85import java.io.IOException;
86import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087
Romain Guyedcce092010-03-04 13:03:17 -080088import com.android.launcher.R;
89
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090/**
91 * Default launcher application.
92 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040093public final class Launcher extends Activity
Daniel Sandlerc351eb82010-03-03 15:05:19 -050094 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080095 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -070096 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Joe Onorato9c1289c2009-08-17 11:03:03 -040098 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -040099 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 private static final int WALLPAPER_SCREENS_SPAN = 2;
103
104 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800105 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int MENU_ADD = Menu.FIRST + 1;
108 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
109 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
110 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700111 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
113 private static final int REQUEST_CREATE_SHORTCUT = 1;
114 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700115 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 private static final int REQUEST_PICK_APPLICATION = 6;
117 private static final int REQUEST_PICK_SHORTCUT = 7;
118 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700119 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700120 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
122 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
123
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800124 static final int SCREEN_COUNT = 5;
125 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700127 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Joe Onorato7c312c12009-08-13 21:36:53 -0700129 static final int DIALOG_CREATE_SHORTCUT = 1;
130 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Romain Guy98d01652009-06-30 16:21:04 -0700132 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 // Type: int
135 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
136 // Type: boolean
137 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
138 // Type: long
139 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
148 // Type: int
149 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
150 // Type: int
151 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
152 // Type: int
153 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
154 // Type: int[]
155 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
156 // Type: boolean
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
158 // Type: long
159 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
160
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800164 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800166 private final BroadcastReceiver mCloseSystemDialogsReceiver
167 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800168 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private LayoutInflater mInflater;
171
Joe Onorato00acb122009-08-04 16:04:30 -0400172 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700174
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700175 private AppWidgetManager mAppWidgetManager;
176 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private CellLayout.CellInfo mAddItemCellInfo;
179 private CellLayout.CellInfo mMenuAddInfo;
180 private final int[] mCellCoordinates = new int[2];
181 private FolderInfo mFolderInfo;
182
Joe Onorato7c312c12009-08-13 21:36:53 -0700183 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700184 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700185 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private Bundle mSavedState;
188
189 private SpannableStringBuilder mDefaultKeySsb = null;
190
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 private boolean mWorkspaceLoading = true;
192
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800193 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private boolean mRestoring;
195 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
197 private Bundle mSavedInstanceState;
198
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800200 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201
Romain Guy84f296c2009-11-04 15:00:44 -0800202 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
203 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700204
Romain Guy1fbc1c82009-11-09 20:43:08 -0800205 private ImageView mPreviousView;
206 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500207
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400208 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400209 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400210 private String[] mHotseatConfig = null;
211 private Intent[] mHotseats = null;
212 private Drawable[] mHotseatIcons = null;
213 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 @Override
216 protected void onCreate(Bundle savedInstanceState) {
217 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700218
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219 LauncherApplication app = ((LauncherApplication)getApplication());
220 mModel = app.setLauncher(this);
221 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400222 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700224
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700226 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
227 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200230 android.os.Debug.startMethodTracing(
231 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 }
233
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400234 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 checkForLocaleChange();
236 setWallpaperDimension();
237
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 setContentView(R.layout.launcher);
239 setupViews();
240
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800241 registerContentObservers();
242
Joe Onorato7c312c12009-08-13 21:36:53 -0700243 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 mSavedState = savedInstanceState;
246 restoreState(mSavedState);
247
248 if (PROFILE_STARTUP) {
249 android.os.Debug.stopMethodTracing();
250 }
251
252 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 }
255
256 // For handling default keys
257 mDefaultKeySsb = new SpannableStringBuilder();
258 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800259
260 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
261 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 }
Romain Guycbb89e42009-06-08 15:52:54 -0700263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700265 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
266 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 final Configuration configuration = getResources().getConfiguration();
269
Romain Guy98d01652009-06-30 16:21:04 -0700270 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 final String locale = configuration.locale.toString();
272
Romain Guy98d01652009-06-30 16:21:04 -0700273 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 final int mcc = configuration.mcc;
275
Romain Guy98d01652009-06-30 16:21:04 -0700276 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 final int mnc = configuration.mnc;
278
Romain Guy84f296c2009-11-04 15:00:44 -0800279 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
Romain Guy84f296c2009-11-04 15:00:44 -0800281 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700282 localeConfiguration.locale = locale;
283 localeConfiguration.mcc = mcc;
284 localeConfiguration.mnc = mnc;
285
286 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800287 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400288
289 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700290 }
291 }
292
293 private static class LocaleConfiguration {
294 public String locale;
295 public int mcc = -1;
296 public int mnc = -1;
297 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700298
Romain Guy98d01652009-06-30 16:21:04 -0700299 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
300 DataInputStream in = null;
301 try {
302 in = new DataInputStream(context.openFileInput(PREFERENCES));
303 configuration.locale = in.readUTF();
304 configuration.mcc = in.readInt();
305 configuration.mnc = in.readInt();
306 } catch (FileNotFoundException e) {
307 // Ignore
308 } catch (IOException e) {
309 // Ignore
310 } finally {
311 if (in != null) {
312 try {
313 in.close();
314 } catch (IOException e) {
315 // Ignore
316 }
317 }
318 }
319 }
320
321 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
322 DataOutputStream out = null;
323 try {
324 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
325 out.writeUTF(configuration.locale);
326 out.writeInt(configuration.mcc);
327 out.writeInt(configuration.mnc);
328 out.flush();
329 } catch (FileNotFoundException e) {
330 // Ignore
331 } catch (IOException e) {
332 //noinspection ResultOfMethodCallIgnored
333 context.getFileStreamPath(PREFERENCES).delete();
334 } finally {
335 if (out != null) {
336 try {
337 out.close();
338 } catch (IOException e) {
339 // Ignore
340 }
341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 }
343 }
344
345 static int getScreen() {
346 synchronized (sLock) {
347 return sScreen;
348 }
349 }
350
351 static void setScreen(int screen) {
352 synchronized (sLock) {
353 sScreen = screen;
354 }
355 }
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700358 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359
360 Display display = getWindowManager().getDefaultDisplay();
361 boolean isPortrait = display.getWidth() < display.getHeight();
362
363 final int width = isPortrait ? display.getWidth() : display.getHeight();
364 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700365 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 }
367
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400368 // Note: This doesn't do all the client-id magic that BrowserProvider does
369 // in Browser. (http://b/2425179)
370 private Uri getDefaultBrowserUri() {
371 String url = getString(R.string.default_browser_url);
372 if (url.indexOf("{CID}") != -1) {
373 url = url.replace("{CID}", "android-google");
374 }
375 return Uri.parse(url);
376 }
377
378 // Load the Intent templates from arrays.xml to populate the hotseats. For
379 // each Intent, if it resolves to a single app, use that as the launch
380 // intent & use that app's label as the contentDescription. Otherwise,
381 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400382 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400383 if (mHotseatConfig == null) {
384 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
385 if (mHotseatConfig.length > 0) {
386 mHotseats = new Intent[mHotseatConfig.length];
387 mHotseatLabels = new CharSequence[mHotseatConfig.length];
388 mHotseatIcons = new Drawable[mHotseatConfig.length];
389 } else {
390 mHotseats = null;
391 mHotseatIcons = null;
392 mHotseatLabels = null;
393 }
394
395 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
396 for (int i=0; i<mHotseatConfig.length; i++) {
397 // load icon for this slot; currently unrelated to the actual activity
398 try {
399 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
400 } catch (ArrayIndexOutOfBoundsException ex) {
401 Log.w(TAG, "Missing hotseat_icons array item #" + i);
402 mHotseatIcons[i] = null;
403 }
404 }
405 hotseatIconDrawables.recycle();
406 }
407
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400408 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400409 for (int i=0; i<mHotseatConfig.length; i++) {
410 Intent intent = null;
411 if (mHotseatConfig[i].equals("*BROWSER*")) {
412 // magic value meaning "launch user's default web browser"
413 // replace it with a generic web request so we can see if there is indeed a default
414 String defaultUri = getString(R.string.default_browser_url);
415 intent = new Intent(
416 Intent.ACTION_VIEW,
417 ((defaultUri != null)
418 ? Uri.parse(defaultUri)
419 : getDefaultBrowserUri())
420 ).addCategory(Intent.CATEGORY_BROWSABLE);
421 // note: if the user launches this without a default set, she
422 // will always be taken to the default URL above; this is
423 // unavoidable as we must specify a valid URL in order for the
424 // chooser to appear, and once the user selects something, that
425 // URL is unavoidably sent to the chosen app.
426 } else {
427 try {
428 intent = Intent.parseUri(mHotseatConfig[i], 0);
429 } catch (java.net.URISyntaxException ex) {
430 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
431 // bogus; leave intent=null
432 }
433 }
434
435 if (intent == null) {
436 mHotseats[i] = null;
437 mHotseatLabels[i] = getText(R.string.activity_not_found);
438 continue;
439 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400440
441 if (LOGD) {
442 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400443 + " initial intent=["
444 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400445 + "]");
446 }
447
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400448 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
449 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
450 if (LOGD) {
451 Log.d(TAG, "Best match for intent: " + bestMatch);
452 Log.d(TAG, "All matches: ");
453 for (ResolveInfo ri : allMatches) {
454 Log.d(TAG, " --> " + ri);
455 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400456 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400457 // did this resolve to a single app, or the resolver?
458 if (allMatches.size() == 0 || bestMatch == null) {
459 // can't find any activity to handle this. let's leave the
460 // intent as-is and let Launcher show a toast when it fails
461 // to launch.
462 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400463
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400464 // set accessibility text to "Not installed"
465 mHotseatLabels[i] = getText(R.string.activity_not_found);
466 } else {
467 boolean found = false;
468 for (ResolveInfo ri : allMatches) {
469 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
470 && bestMatch.activityInfo.applicationInfo.packageName
471 .equals(ri.activityInfo.applicationInfo.packageName)) {
472 found = true;
473 break;
474 }
475 }
476
477 if (!found) {
478 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
479 // the bestMatch is probably the ResolveActivity, meaning the
480 // user has not yet selected a default
481 // so: we'll keep the original intent for now
482 mHotseats[i] = intent;
483
484 // set the accessibility text to "Select shortcut"
485 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
486 } else {
487 // we have an app!
488 // now reconstruct the intent to launch it through the front
489 // door
490 ComponentName com = new ComponentName(
491 bestMatch.activityInfo.applicationInfo.packageName,
492 bestMatch.activityInfo.name);
493 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
494
495 // load the app label for accessibility
496 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
497 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400498 }
499
500 if (LOGD) {
501 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400502 + " final intent=["
503 + ((mHotseats[i] == null)
504 ? "null"
505 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400506 + "] label=[" + mHotseatLabels[i]
507 + "]"
508 );
509 }
510 }
511 }
512
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 @Override
514 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700515 mWaitingForResult = false;
516
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 // The pattern used here is that a user PICKs a specific application,
518 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700519
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
521 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700522
Romain Guy94dabf12009-07-21 10:55:43 -0700523 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 switch (requestCode) {
525 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400526 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 break;
528 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800529 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 break;
531 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400532 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 break;
534 case REQUEST_PICK_LIVE_FOLDER:
535 addLiveFolder(data);
536 break;
537 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700540 case REQUEST_PICK_APPWIDGET:
541 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700543 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700546 case REQUEST_PICK_WALLPAPER:
547 // We just wanted the activity result here so we can clear mWaitingForResult
548 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 }
Romain Guy18042c82009-11-06 11:44:55 -0800550 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
551 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
552 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700553 // Clean up the appWidgetId if we canceled
554 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
555 if (appWidgetId != -1) {
556 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 }
558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 }
560
561 @Override
562 protected void onResume() {
563 super.onResume();
564
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800565 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800566
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400568 mWorkspaceLoading = true;
569 mModel.startLoader(this, true);
570 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 }
572 }
573
574 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700575 protected void onPause() {
576 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800577 dismissPreview(mPreviousView);
578 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800579 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700580 }
Romain Guycbb89e42009-06-08 15:52:54 -0700581
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700582 @Override
583 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400584 // Flag the loader to stop early before switching
585 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800586 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800587 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700588 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700589
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800590 // We can't hide the IME if it was forced open. So don't bother
591 /*
592 @Override
593 public void onWindowFocusChanged(boolean hasFocus) {
594 super.onWindowFocusChanged(hasFocus);
595
596 if (hasFocus) {
597 final InputMethodManager inputManager = (InputMethodManager)
598 getSystemService(Context.INPUT_METHOD_SERVICE);
599 WindowManager.LayoutParams lp = getWindow().getAttributes();
600 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
601 android.os.Handler()) {
602 protected void onReceiveResult(int resultCode, Bundle resultData) {
603 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
604 }
605 });
606 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
607 }
608 }
609 */
610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 private boolean acceptFilter() {
612 final InputMethodManager inputManager = (InputMethodManager)
613 getSystemService(Context.INPUT_METHOD_SERVICE);
614 return !inputManager.isFullscreenMode();
615 }
616
617 @Override
618 public boolean onKeyDown(int keyCode, KeyEvent event) {
619 boolean handled = super.onKeyDown(keyCode, event);
620 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
621 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
622 keyCode, event);
623 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700624 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700625 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700626 // showSearchDialog()
627 // If there are multiple keystrokes before the search dialog takes focus,
628 // onSearchRequested() will be called for every keystroke,
629 // but it is idempotent, so it's fine.
630 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 }
632 }
633
Joe Onorato8a9625e2010-01-28 15:55:35 -0800634 // Eat the long press event so the keyboard doesn't come up.
635 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
636 return true;
637 }
638
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 return handled;
640 }
641
Karl Rosaen138a0412009-04-23 19:00:21 -0700642 private String getTypedText() {
643 return mDefaultKeySsb.toString();
644 }
645
646 private void clearTypedText() {
647 mDefaultKeySsb.clear();
648 mDefaultKeySsb.clearSpans();
649 Selection.setSelection(mDefaultKeySsb, 0);
650 }
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 /**
653 * Restores the previous state, if it exists.
654 *
655 * @param savedState The previous state.
656 */
657 private void restoreState(Bundle savedState) {
658 if (savedState == null) {
659 return;
660 }
661
Joe Onorato3a8820b2009-11-10 15:06:42 -0800662 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
663 if (allApps) {
664 showAllApps(false);
665 }
666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
668 if (currentScreen > -1) {
669 mWorkspace.setCurrentScreen(currentScreen);
670 }
671
672 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
673 if (addScreen > -1) {
674 mAddItemCellInfo = new CellLayout.CellInfo();
675 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
676 addItemCellInfo.valid = true;
677 addItemCellInfo.screen = addScreen;
678 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
679 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
680 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
681 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
682 addItemCellInfo.findVacantCellsFromOccupied(
683 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
684 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
685 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
686 mRestoring = true;
687 }
688
689 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
690 if (renameFolder) {
691 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 mRestoring = true;
694 }
695 }
696
697 /**
698 * Finds all the views we need and configure them properly.
699 */
700 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400701 DragController dragController = mDragController;
702
Romain Guyb1b69f52009-08-10 15:10:15 -0700703 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400704 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705
Joe Onorato7c312c12009-08-13 21:36:53 -0700706 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700707 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700708 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800709 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700710 // Manage focusability manually since this thing is always visible
Romain Guyc16fea72010-03-12 17:17:56 -0800711 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700712
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
714 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500715 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716
Joe Onorato7c312c12009-08-13 21:36:53 -0700717 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
718 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719
Joe Onorato7c312c12009-08-13 21:36:53 -0700720 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700721 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700722 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800723 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800724
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400725 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
726 hotseatLeft.setContentDescription(mHotseatLabels[0]);
727 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
728 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
729 hotseatRight.setContentDescription(mHotseatLabels[1]);
730 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400731
Romain Guy1fbc1c82009-11-09 20:43:08 -0800732 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
733 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
734
735 Drawable previous = mPreviousView.getDrawable();
736 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800737 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738
Joe Onorato0d44e942009-11-16 18:20:51 -0800739 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800740 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800741 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800742 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800743
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400745 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747
748 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400749 deleteZone.setDragController(dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400750 deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751
Joe Onorato00acb122009-08-04 16:04:30 -0400752 dragController.setDragScoller(workspace);
753 dragController.setDragListener(deleteZone);
754 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800755 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700756
Joe Onorato00acb122009-08-04 16:04:30 -0400757 // The order here is bottom to top.
758 dragController.addDropTarget(workspace);
759 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761
Romain Guy8a73c512009-11-09 19:19:59 -0800762 @SuppressWarnings({"UnusedDeclaration"})
763 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800764 if (!isAllAppsVisible()) {
765 mWorkspace.scrollLeft();
766 }
Romain Guy8a73c512009-11-09 19:19:59 -0800767 }
768
769 @SuppressWarnings({"UnusedDeclaration"})
770 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800771 if (!isAllAppsVisible()) {
772 mWorkspace.scrollRight();
773 }
Romain Guy8a73c512009-11-09 19:19:59 -0800774 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400775
776 @SuppressWarnings({"UnusedDeclaration"})
777 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400778 if (isAllAppsVisible()) return;
779
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400780 int index = -1;
781 if (v.getId() == R.id.hotseat_left) {
782 index = 0;
783 } else if (v.getId() == R.id.hotseat_right) {
784 index = 1;
785 }
786
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400787 // reload these every tap; you never know when they might change
788 loadHotseats();
789 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
790 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400791 startActivitySafely(
792 mHotseats[index],
793 "hotseat"
794 );
795 }
796 }
Romain Guy8a73c512009-11-09 19:19:59 -0800797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 /**
799 * Creates a view representing a shortcut.
800 *
801 * @param info The data structure describing the shortcut.
802 *
803 * @return A View inflated from R.layout.application.
804 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800805 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 return createShortcut(R.layout.application,
807 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
808 }
809
810 /**
811 * Creates a view representing a shortcut inflated from the specified resource.
812 *
813 * @param layoutResId The id of the XML layout used to create the shortcut.
814 * @param parent The group the shortcut belongs to.
815 * @param info The data structure describing the shortcut.
816 *
817 * @return A View inflated from layoutResId.
818 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800819 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
821
Joe Onorato0589f0f2010-02-08 13:44:00 -0800822 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
823 new FastBitmapDrawable(info.getIcon(mIconCache)),
824 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 favorite.setText(info.title);
826 favorite.setTag(info);
827 favorite.setOnClickListener(this);
828
829 return favorite;
830 }
831
832 /**
833 * Add an application shortcut to the workspace.
834 *
835 * @param data The intent describing the application.
836 * @param cellInfo The position on screen where to create the shortcut.
837 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 cellInfo.screen = mWorkspace.getCurrentScreen();
840 if (!findSingleSlot(cellInfo)) return;
841
Joe Onorato0589f0f2010-02-08 13:44:00 -0800842 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
843 data, context);
844
Romain Guy73b979d2009-06-09 12:57:21 -0700845 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800846 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800848 info.container = ItemInfo.NO_ID;
849 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
850 } else {
851 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 }
853 }
Romain Guycbb89e42009-06-08 15:52:54 -0700854
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 /**
856 * Add a shortcut to the workspace.
857 *
858 * @param data The intent describing the shortcut.
859 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 cellInfo.screen = mWorkspace.getCurrentScreen();
863 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700864
Joe Onorato0589f0f2010-02-08 13:44:00 -0800865 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866
867 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
870 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 }
872 }
873
Romain Guycbb89e42009-06-08 15:52:54 -0700874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700876 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700878 * @param data The intent describing the appWidgetId.
879 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700883 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700884
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800885 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700886
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700887 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700888
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700889 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700891 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 // Try finding open space on Launcher screen
894 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800895 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
896 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
897 return;
898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700900 // Build Launcher-specific widget info and save to database
901 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 launcherInfo.spanX = spans[0];
903 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 LauncherModel.addItemToDatabase(this, launcherInfo,
906 LauncherSettings.Favorites.CONTAINER_DESKTOP,
907 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
908
909 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700911
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700913 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700914
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700915 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700917
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 }
921 }
Romain Guycbb89e42009-06-08 15:52:54 -0700922
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
924 mDesktopItems.remove(launcherInfo);
925 launcherInfo.hostView = null;
926 }
927
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700928 public LauncherAppWidgetHost getAppWidgetHost() {
929 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 }
Romain Guycbb89e42009-06-08 15:52:54 -0700931
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800932 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800933 getWindow().closeAllPanels();
934
935 try {
936 dismissDialog(DIALOG_CREATE_SHORTCUT);
937 // Unlock the workspace if the dialog was showing
938 } catch (Exception e) {
939 // An exception is thrown if the dialog is not visible, which is fine
940 }
941
942 try {
943 dismissDialog(DIALOG_RENAME_FOLDER);
944 // Unlock the workspace if the dialog was showing
945 } catch (Exception e) {
946 // An exception is thrown if the dialog is not visible, which is fine
947 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800948
949 // Whatever we were doing is hereby canceled.
950 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800951 }
952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 @Override
954 protected void onNewIntent(Intent intent) {
955 super.onNewIntent(intent);
956
957 // Close the menu
958 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800959 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800960 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700961
Joe Onorato14f122b2009-11-19 14:06:36 -0800962 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
963 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
964 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800965 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800966 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800967 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800968 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700969
Joe Onorato3a8820b2009-11-10 15:06:42 -0800970 final View v = getWindow().peekDecorView();
971 if (v != null && v.getWindowToken() != null) {
972 InputMethodManager imm = (InputMethodManager)getSystemService(
973 INPUT_METHOD_SERVICE);
974 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976 }
977 }
978
979 @Override
980 protected void onRestoreInstanceState(Bundle savedInstanceState) {
981 // Do not call super here
982 mSavedInstanceState = savedInstanceState;
983 }
984
985 @Override
986 protected void onSaveInstanceState(Bundle outState) {
987 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
988
989 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
990 if (folders.size() > 0) {
991 final int count = folders.size();
992 long[] ids = new long[count];
993 for (int i = 0; i < count; i++) {
994 final FolderInfo info = folders.get(i).getInfo();
995 ids[i] = info.id;
996 }
997 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
998 } else {
999 super.onSaveInstanceState(outState);
1000 }
1001
Joe Onoratofb0ca672009-09-14 17:55:46 -04001002 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001003 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006
1007 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1008 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1009 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1010
1011 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1012 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1013 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1014 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1015 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1016 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1017 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1018 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1019 layout.getOccupiedCells());
1020 }
1021
1022 if (mFolderInfo != null && mWaitingForResult) {
1023 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1024 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1025 }
1026 }
1027
1028 @Override
1029 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001033 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001035 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037
1038 TextKeyListener.getInstance().release();
1039
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001043
1044 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001045
1046 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001047 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001048
1049 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
1051
1052 @Override
1053 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001054 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 super.startActivityForResult(intent, requestCode);
1056 }
1057
1058 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001059 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001061
Joe Onorato7bb17492009-09-24 17:51:01 -07001062 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001063
Karl Rosaen138a0412009-04-23 19:00:21 -07001064 if (initialQuery == null) {
1065 // Use any text typed in the launcher as the initial query
1066 initialQuery = getTypedText();
1067 clearTypedText();
1068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 if (appSearchData == null) {
1070 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001071 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
Romain Guycbb89e42009-06-08 15:52:54 -07001073
Karl Rosaen138a0412009-04-23 19:00:21 -07001074 final SearchManager searchManager =
1075 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001076 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001077 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
1079
1080 @Override
1081 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 if (isWorkspaceLocked()) {
1083 return false;
1084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085
1086 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1089 .setIcon(android.R.drawable.ic_menu_add)
1090 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001091 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 .setIcon(android.R.drawable.ic_menu_gallery)
1093 .setAlphabeticShortcut('W');
1094 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1095 .setIcon(android.R.drawable.ic_search_category_default)
1096 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1097 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1098 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1099 .setAlphabeticShortcut('N');
1100
1101 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001102 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1103 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104
1105 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1106 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1107 .setIntent(settings);
1108
1109 return true;
1110 }
1111
1112 @Override
1113 public boolean onPrepareOptionsMenu(Menu menu) {
1114 super.onPrepareOptionsMenu(menu);
1115
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001116 // If all apps is animating, don't show the menu, because we don't know
1117 // which one to show.
1118 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1119 return false;
1120 }
1121
1122 // Only show the add and wallpaper options when we're not in all apps.
1123 boolean visible = !mAllAppsGrid.isOpaque();
1124 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1125 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1126
1127 // Disable add if the workspace is full.
1128 if (visible) {
1129 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1130 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132
1133 return true;
1134 }
1135
1136 @Override
1137 public boolean onOptionsItemSelected(MenuItem item) {
1138 switch (item.getItemId()) {
1139 case MENU_ADD:
1140 addItems();
1141 return true;
1142 case MENU_WALLPAPER_SETTINGS:
1143 startWallpaper();
1144 return true;
1145 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001146 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 return true;
1148 case MENU_NOTIFICATIONS:
1149 showNotifications();
1150 return true;
1151 }
1152
1153 return super.onOptionsItemSelected(item);
1154 }
Romain Guycbb89e42009-06-08 15:52:54 -07001155
Karl Rosaen138a0412009-04-23 19:00:21 -07001156 /**
1157 * Indicates that we want global search for this activity by setting the globalSearch
1158 * argument for {@link #startSearch} to true.
1159 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001161 @Override
1162 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001163 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001164 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001165 }
1166
Joe Onorato9c1289c2009-08-17 11:03:03 -04001167 public boolean isWorkspaceLocked() {
1168 return mWorkspaceLoading || mWaitingForResult;
1169 }
1170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001172 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 showAddDialog(mMenuAddInfo);
1174 }
1175
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001176 void addAppWidget(Intent data) {
1177 // TODO: catch bad widget exception when sent
1178 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001179 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180
Bjorn Bringert7984c942009-12-09 15:38:25 +00001181 if (appWidget.configure != null) {
1182 // Launch over to configure widget, if needed
1183 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1184 intent.setComponent(appWidget.configure);
1185 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1186
Romain Guy8e633c52010-03-04 12:51:36 -08001187 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001189 // Otherwise just add it
1190 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
1192 }
Romain Guycbb89e42009-06-08 15:52:54 -07001193
Joe Onoratodeb98af2010-02-19 14:59:39 -08001194 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001195 // Handle case where user selected "Applications"
1196 String applicationName = getResources().getString(R.string.group_applications);
1197 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001198
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001199 if (applicationName != null && applicationName.equals(shortcutName)) {
1200 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1201 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001202
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001203 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1204 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001205 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001206 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001207 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
1210
1211 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001212 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001213 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001214 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001215
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001216 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001218 } else {
1219 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 }
1222
Joe Onorato9c1289c2009-08-17 11:03:03 -04001223 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 UserFolderInfo folderInfo = new UserFolderInfo();
1225 folderInfo.title = getText(R.string.folder_name);
1226
1227 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1228 cellInfo.screen = mWorkspace.getCurrentScreen();
1229 if (!findSingleSlot(cellInfo)) return;
1230
1231 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 LauncherModel.addItemToDatabase(this, folderInfo,
1233 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001235 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236
1237 // Create the view
1238 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1239 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1240 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
Romain Guycbb89e42009-06-08 15:52:54 -07001243
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 void removeFolder(FolderInfo folder) {
1245 mFolders.remove(folder.id);
1246 }
1247
1248 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 cellInfo.screen = mWorkspace.getCurrentScreen();
1250 if (!findSingleSlot(cellInfo)) return;
1251
1252 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1253
1254 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1256 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1258 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260 }
1261
1262 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1263 CellLayout.CellInfo cellInfo, boolean notify) {
1264
1265 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1266 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1267
1268 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 Intent.ShortcutIconResource iconResource = null;
1270
1271 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1272 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1273 try {
1274 iconResource = (Intent.ShortcutIconResource) extra;
1275 final PackageManager packageManager = context.getPackageManager();
1276 Resources resources = packageManager.getResourcesForApplication(
1277 iconResource.packageName);
1278 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1279 icon = resources.getDrawable(id);
1280 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001281 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
1283 }
1284
1285 if (icon == null) {
1286 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1287 }
1288
1289 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001290 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 info.title = name;
1292 info.iconResource = iconResource;
1293 info.uri = data.getData();
1294 info.baseIntent = baseIntent;
1295 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1296 LiveFolders.DISPLAY_MODE_GRID);
1297
1298 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1299 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001300 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301
1302 return info;
1303 }
1304
1305 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1306 final int[] xy = new int[2];
1307 if (findSlot(cellInfo, xy, 1, 1)) {
1308 cellInfo.cellX = xy[0];
1309 cellInfo.cellY = xy[1];
1310 return true;
1311 }
1312 return false;
1313 }
1314
1315 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1316 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1317 boolean[] occupied = mSavedState != null ?
1318 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1319 cellInfo = mWorkspace.findAllVacantCells(occupied);
1320 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1321 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1322 return false;
1323 }
1324 }
1325 return true;
1326 }
1327
1328 private void showNotifications() {
1329 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1330 if (statusBar != null) {
1331 statusBar.expand();
1332 }
1333 }
1334
1335 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001336 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001338 Intent chooser = Intent.createChooser(pickWallpaper,
1339 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001340 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1341 // Removed in Eclair MR1
1342// WallpaperManager wm = (WallpaperManager)
1343// getSystemService(Context.WALLPAPER_SERVICE);
1344// WallpaperInfo wi = wm.getWallpaperInfo();
1345// if (wi != null && wi.getSettingsActivity() != null) {
1346// LabeledIntent li = new LabeledIntent(getPackageName(),
1347// R.string.configure_wallpaper, 0);
1348// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1349// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1350// }
Mike Clerona0618e42009-10-22 13:55:21 -07001351 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001354 /**
1355 * Registers various content observers. The current implementation registers
1356 * only a favorites observer to keep track of the favorites applications.
1357 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001358 private void registerContentObservers() {
1359 ContentResolver resolver = getContentResolver();
1360 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1361 true, mWidgetObserver);
1362 }
1363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 @Override
1365 public boolean dispatchKeyEvent(KeyEvent event) {
1366 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1367 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001369 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001370 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001371 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001372 dumpState();
1373 return true;
1374 }
1375 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001376 }
1377 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1378 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001379 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 return true;
1381 }
1382 }
1383
1384 return super.dispatchKeyEvent(event);
1385 }
1386
Joe Onorato88ec0992009-11-19 13:16:06 -08001387 @Override
1388 public void onBackPressed() {
1389 if (isAllAppsVisible()) {
1390 closeAllApps(true);
1391 } else {
1392 closeFolder();
1393 }
1394 dismissPreview(mPreviousView);
1395 dismissPreview(mNextView);
1396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 private void closeFolder() {
1399 Folder folder = mWorkspace.getOpenFolder();
1400 if (folder != null) {
1401 closeFolder(folder);
1402 }
1403 }
1404
1405 void closeFolder(Folder folder) {
1406 folder.getInfo().opened = false;
1407 ViewGroup parent = (ViewGroup) folder.getParent();
1408 if (parent != null) {
1409 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001410 if (folder instanceof DropTarget) {
1411 // Live folders aren't DropTargets.
1412 mDragController.removeDropTarget((DropTarget)folder);
1413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415 folder.onClose();
1416 }
1417
1418 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001419 * Re-listen when widgets are reset.
1420 */
1421 private void onAppWidgetReset() {
1422 mAppWidgetHost.startListening();
1423 }
1424
1425 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001426 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1427 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429 private void unbindDesktopItems() {
1430 for (ItemInfo item: mDesktopItems) {
1431 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
1433 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001434
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 /**
1436 * Launches the intent referred by the clicked shortcut.
1437 *
1438 * @param v The view representing the clicked shortcut.
1439 */
1440 public void onClick(View v) {
1441 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001442 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001444 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001445 int[] pos = new int[2];
1446 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001447 intent.setSourceBounds(new Rect(pos[0], pos[1],
1448 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001449 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 } else if (tag instanceof FolderInfo) {
1451 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001452 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001453 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001454 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001455 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001456 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
1459 }
1460
Joe Onoratof984e852010-03-25 09:47:45 -07001461 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1463 try {
1464 startActivity(intent);
1465 } catch (ActivityNotFoundException e) {
1466 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001467 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 } catch (SecurityException e) {
1469 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001470 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001472 "or use the exported attribute for this activity. "
1473 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475 }
Romain Guy8e633c52010-03-04 12:51:36 -08001476
1477 void startActivityForResultSafely(Intent intent, int requestCode) {
1478 try {
1479 startActivityForResult(intent, requestCode);
1480 } catch (ActivityNotFoundException e) {
1481 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1482 } catch (SecurityException e) {
1483 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1484 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1485 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1486 "or use the exported attribute for this activity.", e);
1487 }
1488 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489
1490 private void handleFolderClick(FolderInfo folderInfo) {
1491 if (!folderInfo.opened) {
1492 // Close any open folder
1493 closeFolder();
1494 // Open the requested folder
1495 openFolder(folderInfo);
1496 } else {
1497 // Find the open folder...
1498 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1499 int folderScreen;
1500 if (openFolder != null) {
1501 folderScreen = mWorkspace.getScreenForView(openFolder);
1502 // .. and close it
1503 closeFolder(openFolder);
1504 if (folderScreen != mWorkspace.getCurrentScreen()) {
1505 // Close any folder open on the current screen
1506 closeFolder();
1507 // Pull the folder onto this screen
1508 openFolder(folderInfo);
1509 }
1510 }
1511 }
1512 }
1513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 /**
1515 * Opens the user fodler described by the specified tag. The opening of the folder
1516 * is animated relative to the specified View. If the View is null, no animation
1517 * is played.
1518 *
1519 * @param folderInfo The FolderInfo describing the folder to open.
1520 */
1521 private void openFolder(FolderInfo folderInfo) {
1522 Folder openFolder;
1523
1524 if (folderInfo instanceof UserFolderInfo) {
1525 openFolder = UserFolder.fromXml(this);
1526 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001527 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 } else {
1529 return;
1530 }
1531
Joe Onorato00acb122009-08-04 16:04:30 -04001532 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 openFolder.setLauncher(this);
1534
1535 openFolder.bind(folderInfo);
1536 folderInfo.opened = true;
1537
1538 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1539 openFolder.onOpen();
1540 }
1541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001543 switch (v.getId()) {
1544 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001545 if (!isAllAppsVisible()) {
1546 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1547 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001548 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001549 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001550 return true;
1551 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001552 if (!isAllAppsVisible()) {
1553 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1554 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001555 showPreviews(v);
1556 }
1557 return true;
1558 case R.id.all_apps_button:
1559 if (!isAllAppsVisible()) {
1560 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1561 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1562 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001563 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001564 return true;
1565 }
1566
Joe Onorato9c1289c2009-08-17 11:03:03 -04001567 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 return false;
1569 }
1570
1571 if (!(v instanceof CellLayout)) {
1572 v = (View) v.getParent();
1573 }
1574
1575 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1576
1577 // This happens when long clicking an item with the dpad/trackball
1578 if (cellInfo == null) {
1579 return true;
1580 }
1581
1582 if (mWorkspace.allowLongPress()) {
1583 if (cellInfo.cell == null) {
1584 if (cellInfo.valid) {
1585 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001586 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001587 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1588 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 showAddDialog(cellInfo);
1590 }
1591 } else {
1592 if (!(cellInfo.cell instanceof Folder)) {
1593 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001594 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1595 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 mWorkspace.startDrag(cellInfo);
1597 }
1598 }
1599 }
1600 return true;
1601 }
1602
Romain Guye6b8e2f2009-11-10 11:56:55 -08001603 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001604 private void dismissPreview(final View v) {
1605 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001606 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001607 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1608 public void onDismiss() {
1609 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1610 int count = group.getChildCount();
1611 for (int i = 0; i < count; i++) {
1612 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1613 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001614 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1615 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1616
1617 v.setTag(R.id.workspace, null);
1618 v.setTag(R.id.icon, null);
1619 window.setOnDismissListener(null);
1620 }
1621 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001622 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001623 }
1624 v.setTag(null);
1625 }
1626
Romain Guyf8e6a802009-12-07 17:48:02 -08001627 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001628 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001629 }
1630
Romain Guya6abce82009-11-10 02:54:41 -08001631 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001632 final Resources resources = getResources();
1633 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001634
Romain Guya6abce82009-11-10 02:54:41 -08001635 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001636
1637 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001638
Romain Guyf8e6a802009-12-07 17:48:02 -08001639 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001640 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001641 int extraW = (int) ((r.left + r.right) * max);
1642 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001643
1644 int aW = cell.getWidth() - extraW;
1645 float w = aW / max;
1646
1647 int width = cell.getWidth();
1648 int height = cell.getHeight();
1649 int x = cell.getLeftPadding();
1650 int y = cell.getTopPadding();
1651 width -= (x + cell.getRightPadding());
1652 height -= (y + cell.getBottomPadding());
1653
1654 float scale = w / width;
1655
1656 int count = end - start;
1657
1658 final float sWidth = width * scale;
1659 float sHeight = height * scale;
1660
Romain Guye6b8e2f2009-11-10 11:56:55 -08001661 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001662
Romain Guye6b8e2f2009-11-10 11:56:55 -08001663 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1664 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001665
1666 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001667 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001668 cell = (CellLayout) workspace.getChildAt(i);
1669
Romain Guyf8e6a802009-12-07 17:48:02 -08001670 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001671 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001672
1673 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001674 c.scale(scale, scale);
1675 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1676 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001677
Romain Guy9d31e9f2009-11-11 18:54:28 -08001678 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001679 image.setImageBitmap(bitmap);
1680 image.setTag(i);
1681 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001682 image.setOnFocusChangeListener(handler);
1683 image.setFocusable(true);
1684 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001685
1686 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001687 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1688
1689 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001690 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001691
1692 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001693 p.setContentView(preview);
1694 p.setWidth((int) (sWidth * count + extraW));
1695 p.setHeight((int) (sHeight + extraH));
1696 p.setAnimationStyle(R.style.AnimationPreview);
1697 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001698 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001699 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001700 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001701
Romain Guye6b8e2f2009-11-10 11:56:55 -08001702 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1703 public void onDismiss() {
1704 dismissPreview(anchor);
1705 }
1706 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001707
1708 anchor.setTag(p);
1709 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001710 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001711 }
1712
Romain Guy9d31e9f2009-11-11 18:54:28 -08001713 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001714 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001715
Romain Guye6b8e2f2009-11-10 11:56:55 -08001716 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001717 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001718 }
1719
1720 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001721 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001722 v.post(this);
1723 }
1724
1725 public void run() {
1726 dismissPreview(mAnchor);
1727 }
1728
1729 public void onFocusChange(View v, boolean hasFocus) {
1730 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001731 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001732 }
Romain Guya6abce82009-11-10 02:54:41 -08001733 }
1734 }
1735
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 Workspace getWorkspace() {
1737 return mWorkspace;
1738 }
1739
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 @Override
1741 protected Dialog onCreateDialog(int id) {
1742 switch (id) {
1743 case DIALOG_CREATE_SHORTCUT:
1744 return new CreateShortcut().createDialog();
1745 case DIALOG_RENAME_FOLDER:
1746 return new RenameFolder().createDialog();
1747 }
1748
1749 return super.onCreateDialog(id);
1750 }
1751
1752 @Override
1753 protected void onPrepareDialog(int id, Dialog dialog) {
1754 switch (id) {
1755 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 break;
1757 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001758 if (mFolderInfo != null) {
1759 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1760 final CharSequence text = mFolderInfo.title;
1761 input.setText(text);
1762 input.setSelection(0, text.length());
1763 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 break;
1765 }
1766 }
1767
1768 void showRenameDialog(FolderInfo info) {
1769 mFolderInfo = info;
1770 mWaitingForResult = true;
1771 showDialog(DIALOG_RENAME_FOLDER);
1772 }
1773
1774 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1775 mAddItemCellInfo = cellInfo;
1776 mWaitingForResult = true;
1777 showDialog(DIALOG_CREATE_SHORTCUT);
1778 }
1779
Joe Onoratodeb98af2010-02-19 14:59:39 -08001780 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001781 Bundle bundle = new Bundle();
1782
1783 ArrayList<String> shortcutNames = new ArrayList<String>();
1784 shortcutNames.add(getString(R.string.group_applications));
1785 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1786
1787 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1788 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1789 R.drawable.ic_launcher_application));
1790 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1791
1792 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1793 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001794 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001795 pickIntent.putExtras(bundle);
1796
Joe Onoratodeb98af2010-02-19 14:59:39 -08001797 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001798 }
1799
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 private class RenameFolder {
1801 private EditText mInput;
1802
1803 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1805 mInput = (EditText) layout.findViewById(R.id.folder_name);
1806
1807 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1808 builder.setIcon(0);
1809 builder.setTitle(getString(R.string.rename_folder_title));
1810 builder.setCancelable(true);
1811 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1812 public void onCancel(DialogInterface dialog) {
1813 cleanup();
1814 }
1815 });
1816 builder.setNegativeButton(getString(R.string.cancel_action),
1817 new Dialog.OnClickListener() {
1818 public void onClick(DialogInterface dialog, int which) {
1819 cleanup();
1820 }
1821 }
1822 );
1823 builder.setPositiveButton(getString(R.string.rename_action),
1824 new Dialog.OnClickListener() {
1825 public void onClick(DialogInterface dialog, int which) {
1826 changeFolderName();
1827 }
1828 }
1829 );
1830 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001831
1832 final AlertDialog dialog = builder.create();
1833 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1834 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001835 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001836 mInput.requestFocus();
1837 InputMethodManager inputManager = (InputMethodManager)
1838 getSystemService(Context.INPUT_METHOD_SERVICE);
1839 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001840 }
1841 });
1842
1843 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
1845
1846 private void changeFolderName() {
1847 final String name = mInput.getText().toString();
1848 if (!TextUtils.isEmpty(name)) {
1849 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001850 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 mFolderInfo.title = name;
1852 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1853
Joe Onorato9c1289c2009-08-17 11:03:03 -04001854 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001855 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001856 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 } else {
1858 final FolderIcon folderIcon = (FolderIcon)
1859 mWorkspace.getViewForTag(mFolderInfo);
1860 if (folderIcon != null) {
1861 folderIcon.setText(name);
1862 getWorkspace().requestLayout();
1863 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001864 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001865 mWorkspaceLoading = true;
1866 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 }
1868 }
1869 }
1870 cleanup();
1871 }
1872
1873 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 dismissDialog(DIALOG_RENAME_FOLDER);
1875 mWaitingForResult = false;
1876 mFolderInfo = null;
1877 }
1878 }
1879
Daniel Sandler843e8602010-06-07 14:59:01 -04001880 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1881 public boolean isAllAppsVisible() {
1882 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04001883 }
1884
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001885 // AllAppsView.Watcher
1886 public void zoomed(float zoom) {
1887 if (zoom == 1.0f) {
1888 mWorkspace.setVisibility(View.GONE);
1889 }
1890 }
1891
Joe Onorato3a8820b2009-11-10 15:06:42 -08001892 void showAllApps(boolean animated) {
1893 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001894
Romain Guyc16fea72010-03-12 17:17:56 -08001895 ((View) mAllAppsGrid).setFocusable(true);
1896 ((View) mAllAppsGrid).requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001897
Joe Onorato7c312c12009-08-13 21:36:53 -07001898 // TODO: fade these two too
1899 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001900 }
1901
Joe Onoratob2061212009-11-24 16:13:54 -05001902 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001903 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001904 * - Home from workspace
1905 * - from center screen
1906 * - from other screens
1907 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001908 * - from center screen
1909 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001910 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001911 * - from center screen
1912 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001913 * - Launch app from workspace and quit
1914 * - with back
1915 * - with home
1916 * - Launch app from all apps and quit
1917 * - with back
1918 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001919 * - Go to a screen that's not the default, then all
1920 * apps, and launch and app, and go back
1921 * - with back
1922 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001923 * - On workspace, long press power and go back
1924 * - with back
1925 * - with home
1926 * - On all apps, long press power and go back
1927 * - with back
1928 * - with home
1929 * - On workspace, power off
1930 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001931 * - Launch an app and turn off the screen while in that app
1932 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001933 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001934 * - From all apps
1935 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07001936 * - Enter and exit car mode (becuase it causes an extra configuration changed)
1937 * - From all apps
1938 * - From the center workspace
1939 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001940 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001941 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001942 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001943 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001944 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001945 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001946 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001947 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001948 }
1949
Joe Onorato7c312c12009-08-13 21:36:53 -07001950 void lockAllApps() {
1951 // TODO
1952 }
1953
1954 void unlockAllApps() {
1955 // TODO
1956 }
1957
Joe Onorato7404ee42009-07-31 11:54:44 -07001958 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 * Displays the shortcut creation dialog and launches, if necessary, the
1960 * appropriate activity.
1961 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001962 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001963 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1964 DialogInterface.OnShowListener {
1965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1972 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001973 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 builder.setInverseBackgroundForced(true);
1976
1977 AlertDialog dialog = builder.create();
1978 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001979 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001980 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 return dialog;
1983 }
1984
1985 public void onCancel(DialogInterface dialog) {
1986 mWaitingForResult = false;
1987 cleanup();
1988 }
1989
Romain Guycbb89e42009-06-08 15:52:54 -07001990 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001994 try {
1995 dismissDialog(DIALOG_CREATE_SHORTCUT);
1996 } catch (Exception e) {
1997 // An exception is thrown if the dialog is not visible, which is fine
1998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
2000
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002001 /**
2002 * Handle the action clicked in the "Add to home" dialog.
2003 */
2004 public void onClick(DialogInterface dialog, int which) {
2005 Resources res = getResources();
2006 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002007
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002008 switch (which) {
2009 case AddAdapter.ITEM_SHORTCUT: {
2010 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08002011 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002012 break;
2013 }
Romain Guycbb89e42009-06-08 15:52:54 -07002014
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002015 case AddAdapter.ITEM_APPWIDGET: {
2016 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002017
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002018 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2019 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002020 // start the pick activity
2021 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2022 break;
2023 }
Romain Guycbb89e42009-06-08 15:52:54 -07002024
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002025 case AddAdapter.ITEM_LIVE_FOLDER: {
2026 // Insert extra item to handle inserting folder
2027 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002028
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002029 ArrayList<String> shortcutNames = new ArrayList<String>();
2030 shortcutNames.add(res.getString(R.string.group_folder));
2031 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002032
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002033 ArrayList<ShortcutIconResource> shortcutIcons =
2034 new ArrayList<ShortcutIconResource>();
2035 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2036 R.drawable.ic_launcher_folder));
2037 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2038
2039 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2040 pickIntent.putExtra(Intent.EXTRA_INTENT,
2041 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2042 pickIntent.putExtra(Intent.EXTRA_TITLE,
2043 getText(R.string.title_select_live_folder));
2044 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002045
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002046 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2047 break;
2048 }
2049
2050 case AddAdapter.ITEM_WALLPAPER: {
2051 startWallpaper();
2052 break;
2053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
2055 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002056
2057 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08002058 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
2061
2062 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002063 * Receives notifications when applications are added/removed.
2064 */
2065 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2066 @Override
2067 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002068 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002069 String reason = intent.getStringExtra("reason");
2070 if (!"homekey".equals(reason)) {
2071 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002072 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002073 animate = false;
2074 }
Joe Onoratob2061212009-11-24 16:13:54 -05002075 closeAllApps(animate);
2076 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002077 }
2078 }
2079
2080 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002081 * Receives notifications whenever the appwidgets are reset.
2082 */
2083 private class AppWidgetResetObserver extends ContentObserver {
2084 public AppWidgetResetObserver() {
2085 super(new Handler());
2086 }
2087
2088 @Override
2089 public void onChange(boolean selfChange) {
2090 onAppWidgetReset();
2091 }
2092 }
2093
2094 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002097 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002098 if (mWorkspace != null) {
2099 return mWorkspace.getCurrentScreen();
2100 } else {
2101 return SCREEN_COUNT / 2;
2102 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002103 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002104
Joe Onorato9c1289c2009-08-17 11:03:03 -04002105 /**
2106 * Refreshes the shortcuts shown on the workspace.
2107 *
2108 * Implementation of the method from LauncherModel.Callbacks.
2109 */
2110 public void startBinding() {
2111 final Workspace workspace = mWorkspace;
2112 int count = workspace.getChildCount();
2113 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002114 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002115 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2116 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002117
Joe Onorato9c1289c2009-08-17 11:03:03 -04002118 if (DEBUG_USER_INTERFACE) {
2119 android.widget.Button finishButton = new android.widget.Button(this);
2120 finishButton.setText("Finish");
2121 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2122
2123 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2124 public void onClick(View v) {
2125 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002126 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002127 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 }
2129 }
2130
2131 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002132 * Bind the items start-end from the list.
2133 *
2134 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002136 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2137
2138 final Workspace workspace = mWorkspace;
2139
2140 for (int i=start; i<end; i++) {
2141 final ItemInfo item = shortcuts.get(i);
2142 mDesktopItems.add(item);
2143 switch (item.itemType) {
2144 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2145 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002146 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2148 false);
2149 break;
2150 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2151 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2152 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2153 (UserFolderInfo) item);
2154 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2155 false);
2156 break;
2157 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2158 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2159 R.layout.live_folder_icon, this,
2160 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2161 (LiveFolderInfo) item);
2162 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2163 false);
2164 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 }
2167
Joe Onorato9c1289c2009-08-17 11:03:03 -04002168 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 }
2170
Joe Onorato9c1289c2009-08-17 11:03:03 -04002171 /**
2172 * Implementation of the method from LauncherModel.Callbacks.
2173 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002174 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2175 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002176 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002178
2179 /**
2180 * Add the views for a widget to the workspace.
2181 *
2182 * Implementation of the method from LauncherModel.Callbacks.
2183 */
2184 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002185 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2186 if (DEBUG_WIDGETS) {
2187 Log.d(TAG, "bindAppWidget: " + item);
2188 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002189 final Workspace workspace = mWorkspace;
2190
2191 final int appWidgetId = item.appWidgetId;
2192 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002193 if (DEBUG_WIDGETS) {
2194 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2195 }
2196
Joe Onorato9c1289c2009-08-17 11:03:03 -04002197 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2198
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2200 item.hostView.setTag(item);
2201
2202 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2203 item.cellY, item.spanX, item.spanY, false);
2204
2205 workspace.requestLayout();
2206
2207 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002208
2209 if (DEBUG_WIDGETS) {
2210 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2211 + (SystemClock.uptimeMillis()-start) + "ms");
2212 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002213 }
2214
2215 /**
2216 * Callback saying that there aren't any more items to bind.
2217 *
2218 * Implementation of the method from LauncherModel.Callbacks.
2219 */
2220 public void finishBindingItems() {
2221 if (mSavedState != null) {
2222 if (!mWorkspace.hasFocus()) {
2223 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2224 }
2225
2226 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2227 if (userFolders != null) {
2228 for (long folderId : userFolders) {
2229 final FolderInfo info = mFolders.get(folderId);
2230 if (info != null) {
2231 openFolder(info);
2232 }
2233 }
2234 final Folder openFolder = mWorkspace.getOpenFolder();
2235 if (openFolder != null) {
2236 openFolder.requestFocus();
2237 }
2238 }
2239
Joe Onorato9c1289c2009-08-17 11:03:03 -04002240 mSavedState = null;
2241 }
2242
2243 if (mSavedInstanceState != null) {
2244 super.onRestoreInstanceState(mSavedInstanceState);
2245 mSavedInstanceState = null;
2246 }
2247
Joe Onorato9c1289c2009-08-17 11:03:03 -04002248 mWorkspaceLoading = false;
2249 }
2250
2251 /**
2252 * Add the icons for all apps.
2253 *
2254 * Implementation of the method from LauncherModel.Callbacks.
2255 */
2256 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002257 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002258 }
2259
2260 /**
2261 * A package was installed.
2262 *
2263 * Implementation of the method from LauncherModel.Callbacks.
2264 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002265 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002266 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002267 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002268 }
2269
2270 /**
2271 * A package was updated.
2272 *
2273 * Implementation of the method from LauncherModel.Callbacks.
2274 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002275 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002276 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002277 mWorkspace.updateShortcuts(apps);
2278 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002279 }
2280
2281 /**
2282 * A package was uninstalled.
2283 *
2284 * Implementation of the method from LauncherModel.Callbacks.
2285 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002286 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002287 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002288 mWorkspace.removeItems(apps);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002289 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002290 }
Joe Onoratobe386092009-11-17 17:32:16 -08002291
2292 /**
2293 * Prints out out state for debugging.
2294 */
2295 public void dumpState() {
2296 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002297 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002298 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2299 Log.d(TAG, "mRestoring=" + mRestoring);
2300 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2301 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2302 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2303 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002304 mModel.dumpState();
2305 mAllAppsGrid.dumpState();
2306 Log.d(TAG, "END launcher2 dump state");
2307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308}