blob: 9626a2ec2314b13b83b99a647effbf5eb434d8d6 [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;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070049import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Bundle;
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;
Winson Chung7ad01412010-09-27 11:33:03 -0700108 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
109 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
111 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700112 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 private static final int REQUEST_CREATE_SHORTCUT = 1;
115 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700116 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 private static final int REQUEST_PICK_APPLICATION = 6;
118 private static final int REQUEST_PICK_SHORTCUT = 7;
119 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700120 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700121 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
123 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
124
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800125 static final int SCREEN_COUNT = 5;
126 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700128 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Joe Onorato7c312c12009-08-13 21:36:53 -0700130 static final int DIALOG_CREATE_SHORTCUT = 1;
131 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Romain Guy98d01652009-06-30 16:21:04 -0700133 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 // Type: int
136 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
137 // Type: boolean
138 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
139 // Type: long
140 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
155 // Type: int[]
156 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
157 // Type: boolean
158 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
159 // Type: long
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
161
Joe Onorato9c1289c2009-08-17 11:03:03 -0400162 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800167 private final BroadcastReceiver mCloseSystemDialogsReceiver
168 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800169 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private LayoutInflater mInflater;
172
Joe Onorato00acb122009-08-04 16:04:30 -0400173 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700175
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private AppWidgetManager mAppWidgetManager;
177 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private CellLayout.CellInfo mAddItemCellInfo;
180 private CellLayout.CellInfo mMenuAddInfo;
181 private final int[] mCellCoordinates = new int[2];
182 private FolderInfo mFolderInfo;
183
Joe Onorato7c312c12009-08-13 21:36:53 -0700184 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700185 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700186 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private Bundle mSavedState;
189
190 private SpannableStringBuilder mDefaultKeySsb = null;
191
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 private boolean mWorkspaceLoading = true;
193
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800194 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private boolean mRestoring;
196 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
198 private Bundle mSavedInstanceState;
199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800201 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700203 private static LocaleConfiguration sLocaleConfiguration = null;
204
Romain Guy84f296c2009-11-04 15:00:44 -0800205 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700206 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700207
Romain Guy1fbc1c82009-11-09 20:43:08 -0800208 private ImageView mPreviousView;
209 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500210
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400211 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400212 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400213 private String[] mHotseatConfig = null;
214 private Intent[] mHotseats = null;
215 private Drawable[] mHotseatIcons = null;
216 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 @Override
219 protected void onCreate(Bundle savedInstanceState) {
220 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700221
Joe Onorato0589f0f2010-02-08 13:44:00 -0800222 LauncherApplication app = ((LauncherApplication)getApplication());
223 mModel = app.setLauncher(this);
224 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400225 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700227
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700228 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700229 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
230 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 if (PROFILE_STARTUP) {
233 android.os.Debug.startMethodTracing("/sdcard/launcher");
234 }
235
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400236 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 checkForLocaleChange();
238 setWallpaperDimension();
239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 setContentView(R.layout.launcher);
241 setupViews();
242
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800243 registerContentObservers();
244
Joe Onorato7c312c12009-08-13 21:36:53 -0700245 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 mSavedState = savedInstanceState;
248 restoreState(mSavedState);
249
250 if (PROFILE_STARTUP) {
251 android.os.Debug.stopMethodTracing();
252 }
253
254 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 }
257
258 // For handling default keys
259 mDefaultKeySsb = new SpannableStringBuilder();
260 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800261
262 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
263 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 }
Romain Guycbb89e42009-06-08 15:52:54 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700267 if (sLocaleConfiguration == null) {
268 new AsyncTask<Void, Void, LocaleConfiguration>() {
269 @Override
270 protected LocaleConfiguration doInBackground(Void... unused) {
271 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
272 readConfiguration(Launcher.this, localeConfiguration);
273 return localeConfiguration;
274 }
275
276 @Override
277 protected void onPostExecute(LocaleConfiguration result) {
278 sLocaleConfiguration = result;
279 checkForLocaleChange(); // recursive, but now with a locale configuration
280 }
281 }.execute();
282 return;
283 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 final Configuration configuration = getResources().getConfiguration();
286
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700287 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 final String locale = configuration.locale.toString();
289
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700290 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 final int mcc = configuration.mcc;
292
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700293 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 final int mnc = configuration.mnc;
295
Romain Guy84f296c2009-11-04 15:00:44 -0800296 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297
Romain Guy84f296c2009-11-04 15:00:44 -0800298 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700299 sLocaleConfiguration.locale = locale;
300 sLocaleConfiguration.mcc = mcc;
301 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700302
Joe Onorato0589f0f2010-02-08 13:44:00 -0800303 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400304 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700305
306 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
307 new Thread("WriteLocaleConfiguration") {
308 public void run() {
309 writeConfiguration(Launcher.this, localeConfiguration);
310 }
311 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700312 }
313 }
314
315 private static class LocaleConfiguration {
316 public String locale;
317 public int mcc = -1;
318 public int mnc = -1;
319 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700320
Romain Guy98d01652009-06-30 16:21:04 -0700321 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
322 DataInputStream in = null;
323 try {
324 in = new DataInputStream(context.openFileInput(PREFERENCES));
325 configuration.locale = in.readUTF();
326 configuration.mcc = in.readInt();
327 configuration.mnc = in.readInt();
328 } catch (FileNotFoundException e) {
329 // Ignore
330 } catch (IOException e) {
331 // Ignore
332 } finally {
333 if (in != null) {
334 try {
335 in.close();
336 } catch (IOException e) {
337 // Ignore
338 }
339 }
340 }
341 }
342
343 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
344 DataOutputStream out = null;
345 try {
346 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
347 out.writeUTF(configuration.locale);
348 out.writeInt(configuration.mcc);
349 out.writeInt(configuration.mnc);
350 out.flush();
351 } catch (FileNotFoundException e) {
352 // Ignore
353 } catch (IOException e) {
354 //noinspection ResultOfMethodCallIgnored
355 context.getFileStreamPath(PREFERENCES).delete();
356 } finally {
357 if (out != null) {
358 try {
359 out.close();
360 } catch (IOException e) {
361 // Ignore
362 }
363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 }
365 }
366
367 static int getScreen() {
368 synchronized (sLock) {
369 return sScreen;
370 }
371 }
372
373 static void setScreen(int screen) {
374 synchronized (sLock) {
375 sScreen = screen;
376 }
377 }
378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700380 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381
382 Display display = getWindowManager().getDefaultDisplay();
383 boolean isPortrait = display.getWidth() < display.getHeight();
384
385 final int width = isPortrait ? display.getWidth() : display.getHeight();
386 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700387 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
389
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400390 // Note: This doesn't do all the client-id magic that BrowserProvider does
391 // in Browser. (http://b/2425179)
392 private Uri getDefaultBrowserUri() {
393 String url = getString(R.string.default_browser_url);
394 if (url.indexOf("{CID}") != -1) {
395 url = url.replace("{CID}", "android-google");
396 }
397 return Uri.parse(url);
398 }
399
400 // Load the Intent templates from arrays.xml to populate the hotseats. For
401 // each Intent, if it resolves to a single app, use that as the launch
402 // intent & use that app's label as the contentDescription. Otherwise,
403 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400404 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400405 if (mHotseatConfig == null) {
406 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
407 if (mHotseatConfig.length > 0) {
408 mHotseats = new Intent[mHotseatConfig.length];
409 mHotseatLabels = new CharSequence[mHotseatConfig.length];
410 mHotseatIcons = new Drawable[mHotseatConfig.length];
411 } else {
412 mHotseats = null;
413 mHotseatIcons = null;
414 mHotseatLabels = null;
415 }
416
417 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
418 for (int i=0; i<mHotseatConfig.length; i++) {
419 // load icon for this slot; currently unrelated to the actual activity
420 try {
421 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
422 } catch (ArrayIndexOutOfBoundsException ex) {
423 Log.w(TAG, "Missing hotseat_icons array item #" + i);
424 mHotseatIcons[i] = null;
425 }
426 }
427 hotseatIconDrawables.recycle();
428 }
429
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400430 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400431 for (int i=0; i<mHotseatConfig.length; i++) {
432 Intent intent = null;
433 if (mHotseatConfig[i].equals("*BROWSER*")) {
434 // magic value meaning "launch user's default web browser"
435 // replace it with a generic web request so we can see if there is indeed a default
436 String defaultUri = getString(R.string.default_browser_url);
437 intent = new Intent(
438 Intent.ACTION_VIEW,
439 ((defaultUri != null)
440 ? Uri.parse(defaultUri)
441 : getDefaultBrowserUri())
442 ).addCategory(Intent.CATEGORY_BROWSABLE);
443 // note: if the user launches this without a default set, she
444 // will always be taken to the default URL above; this is
445 // unavoidable as we must specify a valid URL in order for the
446 // chooser to appear, and once the user selects something, that
447 // URL is unavoidably sent to the chosen app.
448 } else {
449 try {
450 intent = Intent.parseUri(mHotseatConfig[i], 0);
451 } catch (java.net.URISyntaxException ex) {
452 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
453 // bogus; leave intent=null
454 }
455 }
456
457 if (intent == null) {
458 mHotseats[i] = null;
459 mHotseatLabels[i] = getText(R.string.activity_not_found);
460 continue;
461 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400462
463 if (LOGD) {
464 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400465 + " initial intent=["
466 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400467 + "]");
468 }
469
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400470 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
471 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
472 if (LOGD) {
473 Log.d(TAG, "Best match for intent: " + bestMatch);
474 Log.d(TAG, "All matches: ");
475 for (ResolveInfo ri : allMatches) {
476 Log.d(TAG, " --> " + ri);
477 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400478 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400479 // did this resolve to a single app, or the resolver?
480 if (allMatches.size() == 0 || bestMatch == null) {
481 // can't find any activity to handle this. let's leave the
482 // intent as-is and let Launcher show a toast when it fails
483 // to launch.
484 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400485
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400486 // set accessibility text to "Not installed"
487 mHotseatLabels[i] = getText(R.string.activity_not_found);
488 } else {
489 boolean found = false;
490 for (ResolveInfo ri : allMatches) {
491 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
492 && bestMatch.activityInfo.applicationInfo.packageName
493 .equals(ri.activityInfo.applicationInfo.packageName)) {
494 found = true;
495 break;
496 }
497 }
498
499 if (!found) {
500 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
501 // the bestMatch is probably the ResolveActivity, meaning the
502 // user has not yet selected a default
503 // so: we'll keep the original intent for now
504 mHotseats[i] = intent;
505
506 // set the accessibility text to "Select shortcut"
507 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
508 } else {
509 // we have an app!
510 // now reconstruct the intent to launch it through the front
511 // door
512 ComponentName com = new ComponentName(
513 bestMatch.activityInfo.applicationInfo.packageName,
514 bestMatch.activityInfo.name);
515 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
516
517 // load the app label for accessibility
518 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
519 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400520 }
521
522 if (LOGD) {
523 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400524 + " final intent=["
525 + ((mHotseats[i] == null)
526 ? "null"
527 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400528 + "] label=[" + mHotseatLabels[i]
529 + "]"
530 );
531 }
532 }
533 }
534
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 @Override
536 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700537 mWaitingForResult = false;
538
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 // The pattern used here is that a user PICKs a specific application,
540 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700541
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
543 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700544
Romain Guy94dabf12009-07-21 10:55:43 -0700545 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 switch (requestCode) {
547 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400548 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 break;
550 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800551 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 break;
553 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400554 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 break;
556 case REQUEST_PICK_LIVE_FOLDER:
557 addLiveFolder(data);
558 break;
559 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700562 case REQUEST_PICK_APPWIDGET:
563 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700565 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700568 case REQUEST_PICK_WALLPAPER:
569 // We just wanted the activity result here so we can clear mWaitingForResult
570 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 }
Romain Guy18042c82009-11-06 11:44:55 -0800572 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
573 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
574 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700575 // Clean up the appWidgetId if we canceled
576 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
577 if (appWidgetId != -1) {
578 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
580 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 }
582
583 @Override
584 protected void onResume() {
585 super.onResume();
586
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800587 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800588
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400590 mWorkspaceLoading = true;
591 mModel.startLoader(this, true);
592 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 }
594 }
595
596 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700597 protected void onPause() {
598 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800599 dismissPreview(mPreviousView);
600 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800601 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700602 }
Romain Guycbb89e42009-06-08 15:52:54 -0700603
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700604 @Override
605 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400606 // Flag the loader to stop early before switching
607 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800608 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800609 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700610 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700611
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800612 // We can't hide the IME if it was forced open. So don't bother
613 /*
614 @Override
615 public void onWindowFocusChanged(boolean hasFocus) {
616 super.onWindowFocusChanged(hasFocus);
617
618 if (hasFocus) {
619 final InputMethodManager inputManager = (InputMethodManager)
620 getSystemService(Context.INPUT_METHOD_SERVICE);
621 WindowManager.LayoutParams lp = getWindow().getAttributes();
622 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
623 android.os.Handler()) {
624 protected void onReceiveResult(int resultCode, Bundle resultData) {
625 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
626 }
627 });
628 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
629 }
630 }
631 */
632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 private boolean acceptFilter() {
634 final InputMethodManager inputManager = (InputMethodManager)
635 getSystemService(Context.INPUT_METHOD_SERVICE);
636 return !inputManager.isFullscreenMode();
637 }
638
639 @Override
640 public boolean onKeyDown(int keyCode, KeyEvent event) {
641 boolean handled = super.onKeyDown(keyCode, event);
642 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
643 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
644 keyCode, event);
645 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700646 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700647 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700648 // showSearchDialog()
649 // If there are multiple keystrokes before the search dialog takes focus,
650 // onSearchRequested() will be called for every keystroke,
651 // but it is idempotent, so it's fine.
652 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
654 }
655
Joe Onorato8a9625e2010-01-28 15:55:35 -0800656 // Eat the long press event so the keyboard doesn't come up.
657 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
658 return true;
659 }
660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 return handled;
662 }
663
Karl Rosaen138a0412009-04-23 19:00:21 -0700664 private String getTypedText() {
665 return mDefaultKeySsb.toString();
666 }
667
668 private void clearTypedText() {
669 mDefaultKeySsb.clear();
670 mDefaultKeySsb.clearSpans();
671 Selection.setSelection(mDefaultKeySsb, 0);
672 }
673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 /**
675 * Restores the previous state, if it exists.
676 *
677 * @param savedState The previous state.
678 */
679 private void restoreState(Bundle savedState) {
680 if (savedState == null) {
681 return;
682 }
683
Joe Onorato3a8820b2009-11-10 15:06:42 -0800684 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
685 if (allApps) {
686 showAllApps(false);
687 }
688
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
690 if (currentScreen > -1) {
691 mWorkspace.setCurrentScreen(currentScreen);
692 }
693
694 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
695 if (addScreen > -1) {
696 mAddItemCellInfo = new CellLayout.CellInfo();
697 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
698 addItemCellInfo.valid = true;
699 addItemCellInfo.screen = addScreen;
700 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
701 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
702 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
703 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
704 addItemCellInfo.findVacantCellsFromOccupied(
705 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
706 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
707 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
708 mRestoring = true;
709 }
710
711 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
712 if (renameFolder) {
713 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700714 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 mRestoring = true;
716 }
717 }
718
719 /**
720 * Finds all the views we need and configure them properly.
721 */
722 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400723 DragController dragController = mDragController;
724
Romain Guyb1b69f52009-08-10 15:10:15 -0700725 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400726 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727
Joe Onorato7c312c12009-08-13 21:36:53 -0700728 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700729 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700730 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800731 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700732 // Manage focusability manually since this thing is always visible
Romain Guyc16fea72010-03-12 17:17:56 -0800733 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700734
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
736 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500737 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738
Joe Onorato7c312c12009-08-13 21:36:53 -0700739 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
740 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741
Joe Onorato7c312c12009-08-13 21:36:53 -0700742 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700744 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800745 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800746
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400747 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
748 hotseatLeft.setContentDescription(mHotseatLabels[0]);
749 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
750 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
751 hotseatRight.setContentDescription(mHotseatLabels[1]);
752 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400753
Romain Guy1fbc1c82009-11-09 20:43:08 -0800754 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
755 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
756
757 Drawable previous = mPreviousView.getDrawable();
758 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800759 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760
Joe Onorato0d44e942009-11-16 18:20:51 -0800761 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800762 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800763 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800764 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400767 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769
770 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400771 deleteZone.setDragController(dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400772 deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773
Joe Onorato00acb122009-08-04 16:04:30 -0400774 dragController.setDragScoller(workspace);
775 dragController.setDragListener(deleteZone);
776 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800777 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700778
Joe Onorato00acb122009-08-04 16:04:30 -0400779 // The order here is bottom to top.
780 dragController.addDropTarget(workspace);
781 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 }
783
Romain Guy8a73c512009-11-09 19:19:59 -0800784 @SuppressWarnings({"UnusedDeclaration"})
785 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800786 if (!isAllAppsVisible()) {
787 mWorkspace.scrollLeft();
788 }
Romain Guy8a73c512009-11-09 19:19:59 -0800789 }
790
791 @SuppressWarnings({"UnusedDeclaration"})
792 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800793 if (!isAllAppsVisible()) {
794 mWorkspace.scrollRight();
795 }
Romain Guy8a73c512009-11-09 19:19:59 -0800796 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400797
798 @SuppressWarnings({"UnusedDeclaration"})
799 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400800 if (isAllAppsVisible()) return;
801
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400802 int index = -1;
803 if (v.getId() == R.id.hotseat_left) {
804 index = 0;
805 } else if (v.getId() == R.id.hotseat_right) {
806 index = 1;
807 }
808
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400809 // reload these every tap; you never know when they might change
810 loadHotseats();
811 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
812 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400813 startActivitySafely(
814 mHotseats[index],
815 "hotseat"
816 );
817 }
818 }
Romain Guy8a73c512009-11-09 19:19:59 -0800819
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 /**
821 * Creates a view representing a shortcut.
822 *
823 * @param info The data structure describing the shortcut.
824 *
825 * @return A View inflated from R.layout.application.
826 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800827 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 return createShortcut(R.layout.application,
829 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
830 }
831
832 /**
833 * Creates a view representing a shortcut inflated from the specified resource.
834 *
835 * @param layoutResId The id of the XML layout used to create the shortcut.
836 * @param parent The group the shortcut belongs to.
837 * @param info The data structure describing the shortcut.
838 *
839 * @return A View inflated from layoutResId.
840 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800841 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
843
Joe Onorato0589f0f2010-02-08 13:44:00 -0800844 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
845 new FastBitmapDrawable(info.getIcon(mIconCache)),
846 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 favorite.setText(info.title);
848 favorite.setTag(info);
849 favorite.setOnClickListener(this);
850
851 return favorite;
852 }
853
854 /**
855 * Add an application shortcut to the workspace.
856 *
857 * @param data The intent describing the application.
858 * @param cellInfo The position on screen where to create the shortcut.
859 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400860 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 cellInfo.screen = mWorkspace.getCurrentScreen();
862 if (!findSingleSlot(cellInfo)) return;
863
Joe Onorato0589f0f2010-02-08 13:44:00 -0800864 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
865 data, context);
866
Romain Guy73b979d2009-06-09 12:57:21 -0700867 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800868 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800870 info.container = ItemInfo.NO_ID;
871 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
872 } else {
873 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 }
875 }
Romain Guycbb89e42009-06-08 15:52:54 -0700876
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 /**
878 * Add a shortcut to the workspace.
879 *
880 * @param data The intent describing the shortcut.
881 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 cellInfo.screen = mWorkspace.getCurrentScreen();
885 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700886
Joe Onorato0589f0f2010-02-08 13:44:00 -0800887 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888
889 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
892 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894 }
895
Romain Guycbb89e42009-06-08 15:52:54 -0700896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700898 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700900 * @param data The intent describing the appWidgetId.
901 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700905 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700906
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800907 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700908
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700909 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700910
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700911 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700913 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 // Try finding open space on Launcher screen
916 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800917 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
918 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
919 return;
920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700922 // Build Launcher-specific widget info and save to database
923 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 launcherInfo.spanX = spans[0];
925 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700926
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 LauncherModel.addItemToDatabase(this, launcherInfo,
928 LauncherSettings.Favorites.CONTAINER_DESKTOP,
929 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
930
931 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700933
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700935 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700936
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700937 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700939
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943 }
Romain Guycbb89e42009-06-08 15:52:54 -0700944
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
946 mDesktopItems.remove(launcherInfo);
947 launcherInfo.hostView = null;
948 }
949
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700950 public LauncherAppWidgetHost getAppWidgetHost() {
951 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 }
Romain Guycbb89e42009-06-08 15:52:54 -0700953
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800954 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800955 getWindow().closeAllPanels();
956
957 try {
958 dismissDialog(DIALOG_CREATE_SHORTCUT);
959 // Unlock the workspace if the dialog was showing
960 } catch (Exception e) {
961 // An exception is thrown if the dialog is not visible, which is fine
962 }
963
964 try {
965 dismissDialog(DIALOG_RENAME_FOLDER);
966 // Unlock the workspace if the dialog was showing
967 } catch (Exception e) {
968 // An exception is thrown if the dialog is not visible, which is fine
969 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800970
971 // Whatever we were doing is hereby canceled.
972 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800973 }
974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 @Override
976 protected void onNewIntent(Intent intent) {
977 super.onNewIntent(intent);
978
979 // Close the menu
980 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800981 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800982 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700983
Joe Onorato14f122b2009-11-19 14:06:36 -0800984 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
985 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
986 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800987 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800988 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800989 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800990 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700991
Joe Onorato3a8820b2009-11-10 15:06:42 -0800992 final View v = getWindow().peekDecorView();
993 if (v != null && v.getWindowToken() != null) {
994 InputMethodManager imm = (InputMethodManager)getSystemService(
995 INPUT_METHOD_SERVICE);
996 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 }
998 }
999 }
1000
1001 @Override
1002 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1003 // Do not call super here
1004 mSavedInstanceState = savedInstanceState;
1005 }
1006
1007 @Override
1008 protected void onSaveInstanceState(Bundle outState) {
1009 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1010
1011 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1012 if (folders.size() > 0) {
1013 final int count = folders.size();
1014 long[] ids = new long[count];
1015 for (int i = 0; i < count; i++) {
1016 final FolderInfo info = folders.get(i).getInfo();
1017 ids[i] = info.id;
1018 }
1019 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1020 } else {
1021 super.onSaveInstanceState(outState);
1022 }
1023
Joe Onoratofb0ca672009-09-14 17:55:46 -04001024 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001025 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028
1029 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1030 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1031 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1032
1033 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1034 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1035 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1036 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1037 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1038 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1039 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1040 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1041 layout.getOccupiedCells());
1042 }
1043
1044 if (mFolderInfo != null && mWaitingForResult) {
1045 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1046 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1047 }
1048 }
1049
1050 @Override
1051 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001057 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 }
1059
1060 TextKeyListener.getInstance().release();
1061
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001065
1066 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001067
1068 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001069 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001070
1071 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
1073
1074 @Override
1075 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001076 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 super.startActivityForResult(intent, requestCode);
1078 }
1079
1080 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001081 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001083
Joe Onorato7bb17492009-09-24 17:51:01 -07001084 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001085
Karl Rosaen138a0412009-04-23 19:00:21 -07001086 if (initialQuery == null) {
1087 // Use any text typed in the launcher as the initial query
1088 initialQuery = getTypedText();
1089 clearTypedText();
1090 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 if (appSearchData == null) {
1092 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001093 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
Romain Guycbb89e42009-06-08 15:52:54 -07001095
Karl Rosaen138a0412009-04-23 19:00:21 -07001096 final SearchManager searchManager =
1097 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001098 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001099 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 }
1101
1102 @Override
1103 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001104 if (isWorkspaceLocked()) {
1105 return false;
1106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107
1108 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1111 .setIcon(android.R.drawable.ic_menu_add)
1112 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001113 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1114 .setIcon(android.R.drawable.ic_menu_manage)
1115 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001116 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 .setIcon(android.R.drawable.ic_menu_gallery)
1118 .setAlphabeticShortcut('W');
1119 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1120 .setIcon(android.R.drawable.ic_search_category_default)
1121 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1122 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1123 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1124 .setAlphabeticShortcut('N');
1125
1126 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001127 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1128 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129
1130 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1131 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1132 .setIntent(settings);
1133
1134 return true;
1135 }
1136
1137 @Override
1138 public boolean onPrepareOptionsMenu(Menu menu) {
1139 super.onPrepareOptionsMenu(menu);
1140
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001141 // If all apps is animating, don't show the menu, because we don't know
1142 // which one to show.
1143 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1144 return false;
1145 }
1146
1147 // Only show the add and wallpaper options when we're not in all apps.
1148 boolean visible = !mAllAppsGrid.isOpaque();
1149 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1150 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1151
1152 // Disable add if the workspace is full.
1153 if (visible) {
1154 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1155 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157
1158 return true;
1159 }
1160
1161 @Override
1162 public boolean onOptionsItemSelected(MenuItem item) {
1163 switch (item.getItemId()) {
1164 case MENU_ADD:
1165 addItems();
1166 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001167 case MENU_MANAGE_APPS:
1168 manageApps();
1169 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 case MENU_WALLPAPER_SETTINGS:
1171 startWallpaper();
1172 return true;
1173 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001174 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 return true;
1176 case MENU_NOTIFICATIONS:
1177 showNotifications();
1178 return true;
1179 }
1180
1181 return super.onOptionsItemSelected(item);
1182 }
Romain Guycbb89e42009-06-08 15:52:54 -07001183
Karl Rosaen138a0412009-04-23 19:00:21 -07001184 /**
1185 * Indicates that we want global search for this activity by setting the globalSearch
1186 * argument for {@link #startSearch} to true.
1187 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001189 @Override
1190 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001191 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001192 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001193 }
1194
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 public boolean isWorkspaceLocked() {
1196 return mWorkspaceLoading || mWaitingForResult;
1197 }
1198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001200 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 showAddDialog(mMenuAddInfo);
1202 }
1203
Winson Chung7ad01412010-09-27 11:33:03 -07001204 private void manageApps() {
1205 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1206 }
1207
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001208 void addAppWidget(Intent data) {
1209 // TODO: catch bad widget exception when sent
1210 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001211 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212
Bjorn Bringert7984c942009-12-09 15:38:25 +00001213 if (appWidget.configure != null) {
1214 // Launch over to configure widget, if needed
1215 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1216 intent.setComponent(appWidget.configure);
1217 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1218
Romain Guy8e633c52010-03-04 12:51:36 -08001219 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001221 // Otherwise just add it
1222 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
1224 }
Romain Guycbb89e42009-06-08 15:52:54 -07001225
Joe Onoratodeb98af2010-02-19 14:59:39 -08001226 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001227 // Handle case where user selected "Applications"
1228 String applicationName = getResources().getString(R.string.group_applications);
1229 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001230
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001231 if (applicationName != null && applicationName.equals(shortcutName)) {
1232 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1233 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001234
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001235 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1236 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001237 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001238 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001239 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001240 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243
1244 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001245 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001246 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001247 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001248
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001249 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001250 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001251 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001252 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001253 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 UserFolderInfo folderInfo = new UserFolderInfo();
1258 folderInfo.title = getText(R.string.folder_name);
1259
1260 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1261 cellInfo.screen = mWorkspace.getCurrentScreen();
1262 if (!findSingleSlot(cellInfo)) return;
1263
1264 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001265 LauncherModel.addItemToDatabase(this, folderInfo,
1266 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001268 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269
1270 // Create the view
1271 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1272 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1273 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001274 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
Romain Guycbb89e42009-06-08 15:52:54 -07001276
Joe Onorato9c1289c2009-08-17 11:03:03 -04001277 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001278 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279 }
1280
1281 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 cellInfo.screen = mWorkspace.getCurrentScreen();
1283 if (!findSingleSlot(cellInfo)) return;
1284
1285 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1286
1287 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1289 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001290 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1291 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 }
1293 }
1294
1295 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1296 CellLayout.CellInfo cellInfo, boolean notify) {
1297
1298 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1299 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1300
1301 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 Intent.ShortcutIconResource iconResource = null;
1303
1304 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1305 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1306 try {
1307 iconResource = (Intent.ShortcutIconResource) extra;
1308 final PackageManager packageManager = context.getPackageManager();
1309 Resources resources = packageManager.getResourcesForApplication(
1310 iconResource.packageName);
1311 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1312 icon = resources.getDrawable(id);
1313 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001314 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
1316 }
1317
1318 if (icon == null) {
1319 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1320 }
1321
1322 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001323 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 info.title = name;
1325 info.iconResource = iconResource;
1326 info.uri = data.getData();
1327 info.baseIntent = baseIntent;
1328 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1329 LiveFolders.DISPLAY_MODE_GRID);
1330
1331 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1332 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001333 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334
1335 return info;
1336 }
1337
1338 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1339 final int[] xy = new int[2];
1340 if (findSlot(cellInfo, xy, 1, 1)) {
1341 cellInfo.cellX = xy[0];
1342 cellInfo.cellY = xy[1];
1343 return true;
1344 }
1345 return false;
1346 }
1347
1348 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1349 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1350 boolean[] occupied = mSavedState != null ?
1351 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1352 cellInfo = mWorkspace.findAllVacantCells(occupied);
1353 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1354 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1355 return false;
1356 }
1357 }
1358 return true;
1359 }
1360
1361 private void showNotifications() {
1362 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1363 if (statusBar != null) {
1364 statusBar.expand();
1365 }
1366 }
1367
1368 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001369 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001371 Intent chooser = Intent.createChooser(pickWallpaper,
1372 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001373 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1374 // Removed in Eclair MR1
1375// WallpaperManager wm = (WallpaperManager)
1376// getSystemService(Context.WALLPAPER_SERVICE);
1377// WallpaperInfo wi = wm.getWallpaperInfo();
1378// if (wi != null && wi.getSettingsActivity() != null) {
1379// LabeledIntent li = new LabeledIntent(getPackageName(),
1380// R.string.configure_wallpaper, 0);
1381// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1382// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1383// }
Mike Clerona0618e42009-10-22 13:55:21 -07001384 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
1386
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001387 /**
1388 * Registers various content observers. The current implementation registers
1389 * only a favorites observer to keep track of the favorites applications.
1390 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001391 private void registerContentObservers() {
1392 ContentResolver resolver = getContentResolver();
1393 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1394 true, mWidgetObserver);
1395 }
1396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 @Override
1398 public boolean dispatchKeyEvent(KeyEvent event) {
1399 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1400 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001402 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001403 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001404 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001405 dumpState();
1406 return true;
1407 }
1408 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001409 }
1410 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1411 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001412 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 return true;
1414 }
1415 }
1416
1417 return super.dispatchKeyEvent(event);
1418 }
1419
Joe Onorato88ec0992009-11-19 13:16:06 -08001420 @Override
1421 public void onBackPressed() {
1422 if (isAllAppsVisible()) {
1423 closeAllApps(true);
1424 } else {
1425 closeFolder();
1426 }
1427 dismissPreview(mPreviousView);
1428 dismissPreview(mNextView);
1429 }
1430
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 private void closeFolder() {
1432 Folder folder = mWorkspace.getOpenFolder();
1433 if (folder != null) {
1434 closeFolder(folder);
1435 }
1436 }
1437
1438 void closeFolder(Folder folder) {
1439 folder.getInfo().opened = false;
1440 ViewGroup parent = (ViewGroup) folder.getParent();
1441 if (parent != null) {
1442 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001443 if (folder instanceof DropTarget) {
1444 // Live folders aren't DropTargets.
1445 mDragController.removeDropTarget((DropTarget)folder);
1446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
1448 folder.onClose();
1449 }
1450
1451 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001452 * Re-listen when widgets are reset.
1453 */
1454 private void onAppWidgetReset() {
1455 mAppWidgetHost.startListening();
1456 }
1457
1458 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001459 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1460 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001462 private void unbindDesktopItems() {
1463 for (ItemInfo item: mDesktopItems) {
1464 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
1466 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 /**
1469 * Launches the intent referred by the clicked shortcut.
1470 *
1471 * @param v The view representing the clicked shortcut.
1472 */
1473 public void onClick(View v) {
1474 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001475 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001477 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001478 int[] pos = new int[2];
1479 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001480 intent.setSourceBounds(new Rect(pos[0], pos[1],
1481 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001482 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 } else if (tag instanceof FolderInfo) {
1484 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001485 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001486 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001487 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001488 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001489 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001490 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492 }
1493
Joe Onoratof984e852010-03-25 09:47:45 -07001494 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1496 try {
1497 startActivity(intent);
1498 } catch (ActivityNotFoundException e) {
1499 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001500 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 } catch (SecurityException e) {
1502 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001503 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001505 "or use the exported attribute for this activity. "
1506 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
1508 }
Romain Guy8e633c52010-03-04 12:51:36 -08001509
1510 void startActivityForResultSafely(Intent intent, int requestCode) {
1511 try {
1512 startActivityForResult(intent, requestCode);
1513 } catch (ActivityNotFoundException e) {
1514 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1515 } catch (SecurityException e) {
1516 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1517 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1518 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1519 "or use the exported attribute for this activity.", e);
1520 }
1521 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522
1523 private void handleFolderClick(FolderInfo folderInfo) {
1524 if (!folderInfo.opened) {
1525 // Close any open folder
1526 closeFolder();
1527 // Open the requested folder
1528 openFolder(folderInfo);
1529 } else {
1530 // Find the open folder...
1531 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1532 int folderScreen;
1533 if (openFolder != null) {
1534 folderScreen = mWorkspace.getScreenForView(openFolder);
1535 // .. and close it
1536 closeFolder(openFolder);
1537 if (folderScreen != mWorkspace.getCurrentScreen()) {
1538 // Close any folder open on the current screen
1539 closeFolder();
1540 // Pull the folder onto this screen
1541 openFolder(folderInfo);
1542 }
1543 }
1544 }
1545 }
1546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 /**
1548 * Opens the user fodler described by the specified tag. The opening of the folder
1549 * is animated relative to the specified View. If the View is null, no animation
1550 * is played.
1551 *
1552 * @param folderInfo The FolderInfo describing the folder to open.
1553 */
1554 private void openFolder(FolderInfo folderInfo) {
1555 Folder openFolder;
1556
1557 if (folderInfo instanceof UserFolderInfo) {
1558 openFolder = UserFolder.fromXml(this);
1559 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001560 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 } else {
1562 return;
1563 }
1564
Joe Onorato00acb122009-08-04 16:04:30 -04001565 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 openFolder.setLauncher(this);
1567
1568 openFolder.bind(folderInfo);
1569 folderInfo.opened = true;
1570
1571 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1572 openFolder.onOpen();
1573 }
1574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001576 switch (v.getId()) {
1577 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001578 if (!isAllAppsVisible()) {
1579 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1580 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001581 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001582 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001583 return true;
1584 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001585 if (!isAllAppsVisible()) {
1586 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1587 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001588 showPreviews(v);
1589 }
1590 return true;
1591 case R.id.all_apps_button:
1592 if (!isAllAppsVisible()) {
1593 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1594 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1595 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001596 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001597 return true;
1598 }
1599
Joe Onorato9c1289c2009-08-17 11:03:03 -04001600 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 return false;
1602 }
1603
1604 if (!(v instanceof CellLayout)) {
1605 v = (View) v.getParent();
1606 }
1607
1608 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1609
1610 // This happens when long clicking an item with the dpad/trackball
1611 if (cellInfo == null) {
1612 return true;
1613 }
1614
1615 if (mWorkspace.allowLongPress()) {
1616 if (cellInfo.cell == null) {
1617 if (cellInfo.valid) {
1618 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001619 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001620 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1621 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 showAddDialog(cellInfo);
1623 }
1624 } else {
1625 if (!(cellInfo.cell instanceof Folder)) {
1626 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001627 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1628 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 mWorkspace.startDrag(cellInfo);
1630 }
1631 }
1632 }
1633 return true;
1634 }
1635
Romain Guye6b8e2f2009-11-10 11:56:55 -08001636 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001637 private void dismissPreview(final View v) {
1638 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001639 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001640 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1641 public void onDismiss() {
1642 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1643 int count = group.getChildCount();
1644 for (int i = 0; i < count; i++) {
1645 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1646 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001647 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1648 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1649
1650 v.setTag(R.id.workspace, null);
1651 v.setTag(R.id.icon, null);
1652 window.setOnDismissListener(null);
1653 }
1654 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001655 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001656 }
1657 v.setTag(null);
1658 }
1659
Romain Guyf8e6a802009-12-07 17:48:02 -08001660 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001661 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001662 }
1663
Romain Guya6abce82009-11-10 02:54:41 -08001664 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001665 final Resources resources = getResources();
1666 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001667
Romain Guya6abce82009-11-10 02:54:41 -08001668 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001669
1670 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001671
Romain Guyf8e6a802009-12-07 17:48:02 -08001672 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001673 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001674 int extraW = (int) ((r.left + r.right) * max);
1675 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001676
1677 int aW = cell.getWidth() - extraW;
1678 float w = aW / max;
1679
1680 int width = cell.getWidth();
1681 int height = cell.getHeight();
1682 int x = cell.getLeftPadding();
1683 int y = cell.getTopPadding();
1684 width -= (x + cell.getRightPadding());
1685 height -= (y + cell.getBottomPadding());
1686
1687 float scale = w / width;
1688
1689 int count = end - start;
1690
1691 final float sWidth = width * scale;
1692 float sHeight = height * scale;
1693
Romain Guye6b8e2f2009-11-10 11:56:55 -08001694 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001695
Romain Guye6b8e2f2009-11-10 11:56:55 -08001696 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1697 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001698
1699 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001700 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001701 cell = (CellLayout) workspace.getChildAt(i);
1702
Romain Guyf8e6a802009-12-07 17:48:02 -08001703 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001704 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001705
1706 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001707 c.scale(scale, scale);
1708 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1709 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001710
Romain Guy9d31e9f2009-11-11 18:54:28 -08001711 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001712 image.setImageBitmap(bitmap);
1713 image.setTag(i);
1714 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001715 image.setOnFocusChangeListener(handler);
1716 image.setFocusable(true);
1717 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001718
1719 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001720 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1721
1722 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001723 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001724
1725 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001726 p.setContentView(preview);
1727 p.setWidth((int) (sWidth * count + extraW));
1728 p.setHeight((int) (sHeight + extraH));
1729 p.setAnimationStyle(R.style.AnimationPreview);
1730 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001731 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001732 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001733 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001734
Romain Guye6b8e2f2009-11-10 11:56:55 -08001735 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1736 public void onDismiss() {
1737 dismissPreview(anchor);
1738 }
1739 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001740
1741 anchor.setTag(p);
1742 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001743 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001744 }
1745
Romain Guy9d31e9f2009-11-11 18:54:28 -08001746 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001747 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001748
Romain Guye6b8e2f2009-11-10 11:56:55 -08001749 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001750 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001751 }
1752
1753 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001754 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001755 v.post(this);
1756 }
1757
1758 public void run() {
1759 dismissPreview(mAnchor);
1760 }
1761
1762 public void onFocusChange(View v, boolean hasFocus) {
1763 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001764 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001765 }
Romain Guya6abce82009-11-10 02:54:41 -08001766 }
1767 }
1768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 Workspace getWorkspace() {
1770 return mWorkspace;
1771 }
1772
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 @Override
1774 protected Dialog onCreateDialog(int id) {
1775 switch (id) {
1776 case DIALOG_CREATE_SHORTCUT:
1777 return new CreateShortcut().createDialog();
1778 case DIALOG_RENAME_FOLDER:
1779 return new RenameFolder().createDialog();
1780 }
1781
1782 return super.onCreateDialog(id);
1783 }
1784
1785 @Override
1786 protected void onPrepareDialog(int id, Dialog dialog) {
1787 switch (id) {
1788 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 break;
1790 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001791 if (mFolderInfo != null) {
1792 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1793 final CharSequence text = mFolderInfo.title;
1794 input.setText(text);
1795 input.setSelection(0, text.length());
1796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 break;
1798 }
1799 }
1800
1801 void showRenameDialog(FolderInfo info) {
1802 mFolderInfo = info;
1803 mWaitingForResult = true;
1804 showDialog(DIALOG_RENAME_FOLDER);
1805 }
1806
1807 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1808 mAddItemCellInfo = cellInfo;
1809 mWaitingForResult = true;
1810 showDialog(DIALOG_CREATE_SHORTCUT);
1811 }
1812
Joe Onoratodeb98af2010-02-19 14:59:39 -08001813 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001814 Bundle bundle = new Bundle();
1815
1816 ArrayList<String> shortcutNames = new ArrayList<String>();
1817 shortcutNames.add(getString(R.string.group_applications));
1818 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1819
1820 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1821 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1822 R.drawable.ic_launcher_application));
1823 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1824
1825 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1826 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001827 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001828 pickIntent.putExtras(bundle);
1829
Joe Onoratodeb98af2010-02-19 14:59:39 -08001830 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001831 }
1832
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 private class RenameFolder {
1834 private EditText mInput;
1835
1836 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1838 mInput = (EditText) layout.findViewById(R.id.folder_name);
1839
1840 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1841 builder.setIcon(0);
1842 builder.setTitle(getString(R.string.rename_folder_title));
1843 builder.setCancelable(true);
1844 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1845 public void onCancel(DialogInterface dialog) {
1846 cleanup();
1847 }
1848 });
1849 builder.setNegativeButton(getString(R.string.cancel_action),
1850 new Dialog.OnClickListener() {
1851 public void onClick(DialogInterface dialog, int which) {
1852 cleanup();
1853 }
1854 }
1855 );
1856 builder.setPositiveButton(getString(R.string.rename_action),
1857 new Dialog.OnClickListener() {
1858 public void onClick(DialogInterface dialog, int which) {
1859 changeFolderName();
1860 }
1861 }
1862 );
1863 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001864
1865 final AlertDialog dialog = builder.create();
1866 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1867 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001868 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001869 mInput.requestFocus();
1870 InputMethodManager inputManager = (InputMethodManager)
1871 getSystemService(Context.INPUT_METHOD_SERVICE);
1872 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001873 }
1874 });
1875
1876 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 }
1878
1879 private void changeFolderName() {
1880 final String name = mInput.getText().toString();
1881 if (!TextUtils.isEmpty(name)) {
1882 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001883 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 mFolderInfo.title = name;
1885 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1886
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001888 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001889 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 } else {
1891 final FolderIcon folderIcon = (FolderIcon)
1892 mWorkspace.getViewForTag(mFolderInfo);
1893 if (folderIcon != null) {
1894 folderIcon.setText(name);
1895 getWorkspace().requestLayout();
1896 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001897 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001898 mWorkspaceLoading = true;
1899 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 }
1901 }
1902 }
1903 cleanup();
1904 }
1905
1906 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 dismissDialog(DIALOG_RENAME_FOLDER);
1908 mWaitingForResult = false;
1909 mFolderInfo = null;
1910 }
1911 }
1912
Daniel Sandler843e8602010-06-07 14:59:01 -04001913 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1914 public boolean isAllAppsVisible() {
1915 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04001916 }
1917
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001918 // AllAppsView.Watcher
1919 public void zoomed(float zoom) {
1920 if (zoom == 1.0f) {
1921 mWorkspace.setVisibility(View.GONE);
1922 }
1923 }
1924
Joe Onorato3a8820b2009-11-10 15:06:42 -08001925 void showAllApps(boolean animated) {
1926 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001927
Romain Guyc16fea72010-03-12 17:17:56 -08001928 ((View) mAllAppsGrid).setFocusable(true);
1929 ((View) mAllAppsGrid).requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001930
Joe Onorato7c312c12009-08-13 21:36:53 -07001931 // TODO: fade these two too
1932 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001933 }
1934
Joe Onoratob2061212009-11-24 16:13:54 -05001935 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001936 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001937 * - Home from workspace
1938 * - from center screen
1939 * - from other screens
1940 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001941 * - from center screen
1942 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001943 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001944 * - from center screen
1945 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001946 * - Launch app from workspace and quit
1947 * - with back
1948 * - with home
1949 * - Launch app from all apps and quit
1950 * - with back
1951 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001952 * - Go to a screen that's not the default, then all
1953 * apps, and launch and app, and go back
1954 * - with back
1955 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001956 * - On workspace, long press power and go back
1957 * - with back
1958 * - with home
1959 * - On all apps, long press power and go back
1960 * - with back
1961 * - with home
1962 * - On workspace, power off
1963 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001964 * - Launch an app and turn off the screen while in that app
1965 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001966 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001967 * - From all apps
1968 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07001969 * - Enter and exit car mode (becuase it causes an extra configuration changed)
1970 * - From all apps
1971 * - From the center workspace
1972 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001973 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001974 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001975 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001976 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001977 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001978 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001979 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001980 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001981 }
1982
Joe Onorato7c312c12009-08-13 21:36:53 -07001983 void lockAllApps() {
1984 // TODO
1985 }
1986
1987 void unlockAllApps() {
1988 // TODO
1989 }
1990
Joe Onorato7404ee42009-07-31 11:54:44 -07001991 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 * Displays the shortcut creation dialog and launches, if necessary, the
1993 * appropriate activity.
1994 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001995 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001996 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1997 DialogInterface.OnShowListener {
1998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2005 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002006 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 builder.setInverseBackgroundForced(true);
2009
2010 AlertDialog dialog = builder.create();
2011 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002012 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002013 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 return dialog;
2016 }
2017
2018 public void onCancel(DialogInterface dialog) {
2019 mWaitingForResult = false;
2020 cleanup();
2021 }
2022
Romain Guycbb89e42009-06-08 15:52:54 -07002023 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002024 }
2025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002027 try {
2028 dismissDialog(DIALOG_CREATE_SHORTCUT);
2029 } catch (Exception e) {
2030 // An exception is thrown if the dialog is not visible, which is fine
2031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
2033
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002034 /**
2035 * Handle the action clicked in the "Add to home" dialog.
2036 */
2037 public void onClick(DialogInterface dialog, int which) {
2038 Resources res = getResources();
2039 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002040
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002041 switch (which) {
2042 case AddAdapter.ITEM_SHORTCUT: {
2043 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08002044 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002045 break;
2046 }
Romain Guycbb89e42009-06-08 15:52:54 -07002047
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002048 case AddAdapter.ITEM_APPWIDGET: {
2049 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002050
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002051 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2052 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002053 // start the pick activity
2054 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2055 break;
2056 }
Romain Guycbb89e42009-06-08 15:52:54 -07002057
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002058 case AddAdapter.ITEM_LIVE_FOLDER: {
2059 // Insert extra item to handle inserting folder
2060 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002061
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002062 ArrayList<String> shortcutNames = new ArrayList<String>();
2063 shortcutNames.add(res.getString(R.string.group_folder));
2064 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002065
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002066 ArrayList<ShortcutIconResource> shortcutIcons =
2067 new ArrayList<ShortcutIconResource>();
2068 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2069 R.drawable.ic_launcher_folder));
2070 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2071
2072 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2073 pickIntent.putExtra(Intent.EXTRA_INTENT,
2074 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2075 pickIntent.putExtra(Intent.EXTRA_TITLE,
2076 getText(R.string.title_select_live_folder));
2077 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002078
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002079 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2080 break;
2081 }
2082
2083 case AddAdapter.ITEM_WALLPAPER: {
2084 startWallpaper();
2085 break;
2086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002089
2090 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08002091 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 }
2094
2095 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002096 * Receives notifications when applications are added/removed.
2097 */
2098 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2099 @Override
2100 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002101 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002102 String reason = intent.getStringExtra("reason");
2103 if (!"homekey".equals(reason)) {
2104 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002105 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002106 animate = false;
2107 }
Joe Onoratob2061212009-11-24 16:13:54 -05002108 closeAllApps(animate);
2109 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002110 }
2111 }
2112
2113 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002114 * Receives notifications whenever the appwidgets are reset.
2115 */
2116 private class AppWidgetResetObserver extends ContentObserver {
2117 public AppWidgetResetObserver() {
2118 super(new Handler());
2119 }
2120
2121 @Override
2122 public void onChange(boolean selfChange) {
2123 onAppWidgetReset();
2124 }
2125 }
2126
2127 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002128 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002130 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002131 if (mWorkspace != null) {
2132 return mWorkspace.getCurrentScreen();
2133 } else {
2134 return SCREEN_COUNT / 2;
2135 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002136 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002137
Joe Onorato9c1289c2009-08-17 11:03:03 -04002138 /**
2139 * Refreshes the shortcuts shown on the workspace.
2140 *
2141 * Implementation of the method from LauncherModel.Callbacks.
2142 */
2143 public void startBinding() {
2144 final Workspace workspace = mWorkspace;
2145 int count = workspace.getChildCount();
2146 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002147 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2149 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002150
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151 if (DEBUG_USER_INTERFACE) {
2152 android.widget.Button finishButton = new android.widget.Button(this);
2153 finishButton.setText("Finish");
2154 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2155
2156 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2157 public void onClick(View v) {
2158 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002159 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 }
2162 }
2163
2164 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 * Bind the items start-end from the list.
2166 *
2167 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002169 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2170
2171 final Workspace workspace = mWorkspace;
2172
2173 for (int i=start; i<end; i++) {
2174 final ItemInfo item = shortcuts.get(i);
2175 mDesktopItems.add(item);
2176 switch (item.itemType) {
2177 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2178 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002179 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2181 false);
2182 break;
2183 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2184 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2185 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2186 (UserFolderInfo) item);
2187 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2188 false);
2189 break;
2190 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2191 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2192 R.layout.live_folder_icon, this,
2193 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2194 (LiveFolderInfo) item);
2195 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2196 false);
2197 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 }
2200
Joe Onorato9c1289c2009-08-17 11:03:03 -04002201 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 }
2203
Joe Onorato9c1289c2009-08-17 11:03:03 -04002204 /**
2205 * Implementation of the method from LauncherModel.Callbacks.
2206 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002207 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002208 sFolders.clear();
2209 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002211
2212 /**
2213 * Add the views for a widget to the workspace.
2214 *
2215 * Implementation of the method from LauncherModel.Callbacks.
2216 */
2217 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002218 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2219 if (DEBUG_WIDGETS) {
2220 Log.d(TAG, "bindAppWidget: " + item);
2221 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002222 final Workspace workspace = mWorkspace;
2223
2224 final int appWidgetId = item.appWidgetId;
2225 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002226 if (DEBUG_WIDGETS) {
2227 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2228 }
2229
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2231
Joe Onorato9c1289c2009-08-17 11:03:03 -04002232 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2233 item.hostView.setTag(item);
2234
2235 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2236 item.cellY, item.spanX, item.spanY, false);
2237
2238 workspace.requestLayout();
2239
2240 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002241
2242 if (DEBUG_WIDGETS) {
2243 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2244 + (SystemClock.uptimeMillis()-start) + "ms");
2245 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002246 }
2247
2248 /**
2249 * Callback saying that there aren't any more items to bind.
2250 *
2251 * Implementation of the method from LauncherModel.Callbacks.
2252 */
2253 public void finishBindingItems() {
2254 if (mSavedState != null) {
2255 if (!mWorkspace.hasFocus()) {
2256 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2257 }
2258
2259 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2260 if (userFolders != null) {
2261 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002262 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002263 if (info != null) {
2264 openFolder(info);
2265 }
2266 }
2267 final Folder openFolder = mWorkspace.getOpenFolder();
2268 if (openFolder != null) {
2269 openFolder.requestFocus();
2270 }
2271 }
2272
Joe Onorato9c1289c2009-08-17 11:03:03 -04002273 mSavedState = null;
2274 }
2275
2276 if (mSavedInstanceState != null) {
2277 super.onRestoreInstanceState(mSavedInstanceState);
2278 mSavedInstanceState = null;
2279 }
2280
Joe Onorato9c1289c2009-08-17 11:03:03 -04002281 mWorkspaceLoading = false;
2282 }
2283
2284 /**
2285 * Add the icons for all apps.
2286 *
2287 * Implementation of the method from LauncherModel.Callbacks.
2288 */
2289 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002290 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002291 }
2292
2293 /**
2294 * A package was installed.
2295 *
2296 * Implementation of the method from LauncherModel.Callbacks.
2297 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002298 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002299 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002300 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002301 }
2302
2303 /**
2304 * A package was updated.
2305 *
2306 * Implementation of the method from LauncherModel.Callbacks.
2307 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002308 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002309 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002310 mWorkspace.updateShortcuts(apps);
2311 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002312 }
2313
2314 /**
2315 * A package was uninstalled.
2316 *
2317 * Implementation of the method from LauncherModel.Callbacks.
2318 */
Joe Onorato36115782010-06-17 13:28:48 -04002319 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002320 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002321 if (permanent) {
2322 mWorkspace.removeItems(apps);
2323 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002324 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002325 }
Joe Onoratobe386092009-11-17 17:32:16 -08002326
2327 /**
2328 * Prints out out state for debugging.
2329 */
2330 public void dumpState() {
2331 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002332 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002333 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2334 Log.d(TAG, "mRestoring=" + mRestoring);
2335 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2336 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2337 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002338 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002339 mModel.dumpState();
2340 mAllAppsGrid.dumpState();
2341 Log.d(TAG, "END launcher2 dump state");
2342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343}