blob: b9c2f0956ec033a5324298425b74c28f57fde794 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
41import android.content.DialogInterface;
42import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080043import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080044import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080047import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040051import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080054import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080065import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
70import android.text.TextUtils;
71import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080073import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080074import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070079import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080080import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.View;
82import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070083import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080084import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080085import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080087import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070089import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070091import android.widget.ImageView;
92import android.widget.LinearLayout;
93import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070094import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -070095import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Adam Cohendf2cc412011-04-27 16:56:57 -070098import com.android.common.Search;
99import com.android.launcher.R;
100import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
104import java.io.FileNotFoundException;
105import java.io.IOException;
106import java.util.ArrayList;
107import java.util.HashMap;
108import java.util.List;
109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700127 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
128 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
130 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700131 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
141
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800142 static final int SCREEN_COUNT = 5;
143 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato7c312c12009-08-13 21:36:53 -0700145 static final int DIALOG_CREATE_SHORTCUT = 1;
146 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
150 // Type: int
151 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700152 // Type: int
153 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: long
Adam Cohendf2cc412011-04-27 16:56:57 -0700155 private static final String RUNTIME_STATE_FOLDERS = "launcher.folder";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: boolean
163 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
164 // Type: long
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
166
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700167 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
168
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700169 /** The different states that Launcher can be in. */
Winson Chung785d2eb2011-04-14 16:08:02 -0700170 private enum State { WORKSPACE, APPS_CUSTOMIZE, ALL_APPS, CUSTOMIZE,
Winson Chungb26f3d62011-06-02 10:49:29 -0700171 APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700172 private State mState = State.WORKSPACE;
173 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700174
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800178 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800180 private final BroadcastReceiver mCloseSystemDialogsReceiver
181 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800182 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private LayoutInflater mInflater;
185
Joe Onorato00acb122009-08-04 16:04:30 -0400186 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700188
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700189 private AppWidgetManager mAppWidgetManager;
190 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700191
Michael Jurka0280c3b2010-09-17 15:00:07 -0700192 private int mAddScreen = -1;
193 private int mAddIntersectCellX = -1;
194 private int mAddIntersectCellY = -1;
195 private int[] mAddDropPosition;
196 private int[] mTmpAddItemCellCoordinates = new int[2];
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private FolderInfo mFolderInfo;
199
Joe Onorato7c312c12009-08-13 21:36:53 -0700200 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700201 private HandleView mHandleView;
Winson Chung4c98d922011-05-31 16:50:48 -0700202 private SearchDropTargetBar mSearchDeleteBar;
Joe Onorato7c312c12009-08-13 21:36:53 -0700203 private AllAppsView mAllAppsGrid;
Winson Chung785d2eb2011-04-14 16:08:02 -0700204 private AppsCustomizeTabHost mAppsCustomizeTabHost;
205 private AppsCustomizePagedView mAppsCustomizeContent;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800206 private CustomizeTrayTabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700207 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700208
Winson Chung97d85d22011-04-13 11:27:36 -0700209 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800210 private View mAllAppsButton;
211 private View mDivider;
212 private View mConfigureButton;
213
Winson Chunga12a2502010-12-20 14:41:35 -0800214 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700215 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 private boolean mWorkspaceLoading = true;
222
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800223 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private boolean mRestoring;
225 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700226 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
228 private Bundle mSavedInstanceState;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800232 private boolean mUserPresent = true;
233 private boolean mVisible = false;
234 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700236 private static LocaleConfiguration sLocaleConfiguration = null;
237
Romain Guy84f296c2009-11-04 15:00:44 -0800238 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700239
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700240 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800242 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800243 private ImageView mPreviousView;
244 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500245
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400246 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400247 private String[] mHotseatConfig = null;
248 private Intent[] mHotseats = null;
249 private Drawable[] mHotseatIcons = null;
250 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400251
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700252 private Intent mAppMarketIntent = null;
253
Adam Cohended9f8d2010-11-03 13:25:16 -0700254 // Related to the auto-advancing of widgets
255 private final int ADVANCE_MSG = 1;
256 private final int mAdvanceInterval = 20000;
257 private final int mAdvanceStagger = 250;
258 private long mAutoAdvanceSentTime;
259 private long mAutoAdvanceTimeLeft = -1;
260 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
261 new HashMap<View, AppWidgetProviderInfo>();
262
Winson Chung400438b2011-01-16 17:53:48 -0800263 // Determines how long to wait after a rotation before restoring the screen orientation to
264 // match the sensor state.
265 private final int mRestoreScreenOrientationDelay = 500;
266
Michael Jurka4ef207b2010-11-29 17:05:45 -0800267 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800268 private static Drawable.ConstantState sGlobalSearchIcon;
269 private static Drawable.ConstantState sVoiceSearchIcon;
270 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800271
Adam Cohen2801caf2011-05-13 20:57:39 -0700272 private DragLayer mDragLayer;
273
Michael Jurkaddd62e92011-02-16 17:49:14 -0800274 private BubbleTextView mWaitingForResume;
275
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800276 private static ArrayList<PendingAddArguments> sPendingAddList
277 = new ArrayList<PendingAddArguments>();
278
279 private static class PendingAddArguments {
280 int requestCode;
281 Intent intent;
282 int screen;
283 int cellX;
284 int cellY;
285 }
286
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 @Override
288 protected void onCreate(Bundle savedInstanceState) {
289 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700290
Winson Chungaafa03c2010-06-11 17:34:16 -0700291 if (LauncherApplication.isInPlaceRotationEnabled()) {
292 // hide the status bar (temporary until we get the status bar design figured out)
293 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
294 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
295 }
296
Joe Onorato0589f0f2010-02-08 13:44:00 -0800297 LauncherApplication app = ((LauncherApplication)getApplication());
298 mModel = app.setLauncher(this);
299 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400300 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700302
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700303 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700304 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
305 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200308 android.os.Debug.startMethodTracing(
309 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 }
311
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400312 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 setContentView(R.layout.launcher);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800315 mHomeCustomizationDrawer = (CustomizeTrayTabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700316 if (mHomeCustomizationDrawer != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700317 // share the same customization workspace across all the tabs
Michael Jurka7ef959b2011-02-23 11:48:32 -0800318 mCustomizePagedView = (CustomizePagedView) findViewById(
319 R.id.customization_drawer_tab_contents);
Michael Jurka946ad472010-07-09 18:05:18 -0700320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 setupViews();
322
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800323 registerContentObservers();
324
Joe Onorato7c312c12009-08-13 21:36:53 -0700325 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700326
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 mSavedState = savedInstanceState;
328 restoreState(mSavedState);
329
Winson Chunga12a2502010-12-20 14:41:35 -0800330 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800331 if (mCustomizePagedView != null) {
332 mCustomizePagedView.update();
333 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700334 if (mAppsCustomizeContent != null) {
335 mAppsCustomizeContent.onPackagesUpdated();
336 }
Winson Chunga12a2502010-12-20 14:41:35 -0800337
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 if (PROFILE_STARTUP) {
339 android.os.Debug.stopMethodTracing();
340 }
341
342 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400343 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
345
346 // For handling default keys
347 mDefaultKeySsb = new SpannableStringBuilder();
348 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800349
350 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
351 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800352
353 // If we have a saved version of these external icons, we load them up immediately
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700354 if (LauncherApplication.isScreenLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800355 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
356 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800357 }
Michael Jurka1c3f8ec2011-03-07 16:41:21 -0800358 if (sGlobalSearchIcon != null) {
359 updateGlobalSearchIcon(sGlobalSearchIcon);
360 }
361 if (sVoiceSearchIcon != null) {
362 updateVoiceSearchIcon(sVoiceSearchIcon);
363 }
364 if (sAppMarketIcon != null) {
365 updateAppMarketIcon(sAppMarketIcon);
366 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 }
Romain Guycbb89e42009-06-08 15:52:54 -0700369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700371 if (sLocaleConfiguration == null) {
372 new AsyncTask<Void, Void, LocaleConfiguration>() {
373 @Override
374 protected LocaleConfiguration doInBackground(Void... unused) {
375 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
376 readConfiguration(Launcher.this, localeConfiguration);
377 return localeConfiguration;
378 }
379
380 @Override
381 protected void onPostExecute(LocaleConfiguration result) {
382 sLocaleConfiguration = result;
383 checkForLocaleChange(); // recursive, but now with a locale configuration
384 }
385 }.execute();
386 return;
387 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 final Configuration configuration = getResources().getConfiguration();
390
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700391 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 final String locale = configuration.locale.toString();
393
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700394 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 final int mcc = configuration.mcc;
396
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700397 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 final int mnc = configuration.mnc;
399
Romain Guy84f296c2009-11-04 15:00:44 -0800400 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401
Romain Guy84f296c2009-11-04 15:00:44 -0800402 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700403 sLocaleConfiguration.locale = locale;
404 sLocaleConfiguration.mcc = mcc;
405 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700406
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400408 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700409
410 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
411 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700412 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700413 public void run() {
414 writeConfiguration(Launcher.this, localeConfiguration);
415 }
416 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700417 }
418 }
419
420 private static class LocaleConfiguration {
421 public String locale;
422 public int mcc = -1;
423 public int mnc = -1;
424 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700425
Romain Guy98d01652009-06-30 16:21:04 -0700426 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
427 DataInputStream in = null;
428 try {
429 in = new DataInputStream(context.openFileInput(PREFERENCES));
430 configuration.locale = in.readUTF();
431 configuration.mcc = in.readInt();
432 configuration.mnc = in.readInt();
433 } catch (FileNotFoundException e) {
434 // Ignore
435 } catch (IOException e) {
436 // Ignore
437 } finally {
438 if (in != null) {
439 try {
440 in.close();
441 } catch (IOException e) {
442 // Ignore
443 }
444 }
445 }
446 }
447
448 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
449 DataOutputStream out = null;
450 try {
451 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
452 out.writeUTF(configuration.locale);
453 out.writeInt(configuration.mcc);
454 out.writeInt(configuration.mnc);
455 out.flush();
456 } catch (FileNotFoundException e) {
457 // Ignore
458 } catch (IOException e) {
459 //noinspection ResultOfMethodCallIgnored
460 context.getFileStreamPath(PREFERENCES).delete();
461 } finally {
462 if (out != null) {
463 try {
464 out.close();
465 } catch (IOException e) {
466 // Ignore
467 }
468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470 }
471
472 static int getScreen() {
473 synchronized (sLock) {
474 return sScreen;
475 }
476 }
477
478 static void setScreen(int screen) {
479 synchronized (sLock) {
480 sScreen = screen;
481 }
482 }
483
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400484 // Note: This doesn't do all the client-id magic that BrowserProvider does
485 // in Browser. (http://b/2425179)
486 private Uri getDefaultBrowserUri() {
487 String url = getString(R.string.default_browser_url);
488 if (url.indexOf("{CID}") != -1) {
489 url = url.replace("{CID}", "android-google");
490 }
491 return Uri.parse(url);
492 }
493
494 // Load the Intent templates from arrays.xml to populate the hotseats. For
495 // each Intent, if it resolves to a single app, use that as the launch
496 // intent & use that app's label as the contentDescription. Otherwise,
497 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400498 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400499 if (mHotseatConfig == null) {
500 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
501 if (mHotseatConfig.length > 0) {
502 mHotseats = new Intent[mHotseatConfig.length];
503 mHotseatLabels = new CharSequence[mHotseatConfig.length];
504 mHotseatIcons = new Drawable[mHotseatConfig.length];
505 } else {
506 mHotseats = null;
507 mHotseatIcons = null;
508 mHotseatLabels = null;
509 }
510
511 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
512 for (int i=0; i<mHotseatConfig.length; i++) {
513 // load icon for this slot; currently unrelated to the actual activity
514 try {
515 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
516 } catch (ArrayIndexOutOfBoundsException ex) {
517 Log.w(TAG, "Missing hotseat_icons array item #" + i);
518 mHotseatIcons[i] = null;
519 }
520 }
521 hotseatIconDrawables.recycle();
522 }
523
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400524 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400525 for (int i=0; i<mHotseatConfig.length; i++) {
526 Intent intent = null;
527 if (mHotseatConfig[i].equals("*BROWSER*")) {
528 // magic value meaning "launch user's default web browser"
529 // replace it with a generic web request so we can see if there is indeed a default
530 String defaultUri = getString(R.string.default_browser_url);
531 intent = new Intent(
532 Intent.ACTION_VIEW,
533 ((defaultUri != null)
534 ? Uri.parse(defaultUri)
535 : getDefaultBrowserUri())
536 ).addCategory(Intent.CATEGORY_BROWSABLE);
537 // note: if the user launches this without a default set, she
538 // will always be taken to the default URL above; this is
539 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700540 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400541 // URL is unavoidably sent to the chosen app.
542 } else {
543 try {
544 intent = Intent.parseUri(mHotseatConfig[i], 0);
545 } catch (java.net.URISyntaxException ex) {
546 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
547 // bogus; leave intent=null
548 }
549 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700550
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400551 if (intent == null) {
552 mHotseats[i] = null;
553 mHotseatLabels[i] = getText(R.string.activity_not_found);
554 continue;
555 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400556
557 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700558 Log.d(TAG, "loadHotseats: hotseat " + i
559 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400560 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400561 + "]");
562 }
563
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400564 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
565 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700566 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400567 Log.d(TAG, "Best match for intent: " + bestMatch);
568 Log.d(TAG, "All matches: ");
569 for (ResolveInfo ri : allMatches) {
570 Log.d(TAG, " --> " + ri);
571 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400572 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400573 // did this resolve to a single app, or the resolver?
574 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700575 // can't find any activity to handle this. let's leave the
576 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400577 // to launch.
578 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400579
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400580 // set accessibility text to "Not installed"
581 mHotseatLabels[i] = getText(R.string.activity_not_found);
582 } else {
583 boolean found = false;
584 for (ResolveInfo ri : allMatches) {
585 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
586 && bestMatch.activityInfo.applicationInfo.packageName
587 .equals(ri.activityInfo.applicationInfo.packageName)) {
588 found = true;
589 break;
590 }
591 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700592
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400593 if (!found) {
594 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
595 // the bestMatch is probably the ResolveActivity, meaning the
596 // user has not yet selected a default
597 // so: we'll keep the original intent for now
598 mHotseats[i] = intent;
599
600 // set the accessibility text to "Select shortcut"
601 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
602 } else {
603 // we have an app!
604 // now reconstruct the intent to launch it through the front
605 // door
606 ComponentName com = new ComponentName(
607 bestMatch.activityInfo.applicationInfo.packageName,
608 bestMatch.activityInfo.name);
609 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
610
611 // load the app label for accessibility
612 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
613 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400614 }
615
616 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700617 Log.d(TAG, "loadHotseats: hotseat " + i
618 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400619 + ((mHotseats[i] == null)
620 ? "null"
621 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400622 + "] label=[" + mHotseatLabels[i]
623 + "]"
624 );
625 }
626 }
627 }
628
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800629 private void completeAdd(PendingAddArguments args) {
630 switch (args.requestCode) {
631 case REQUEST_PICK_APPLICATION:
632 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
633 break;
634 case REQUEST_PICK_SHORTCUT:
635 processShortcut(args.intent);
636 break;
637 case REQUEST_CREATE_SHORTCUT:
638 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
639 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800640 case REQUEST_PICK_APPWIDGET:
641 addAppWidgetFromPick(args.intent);
642 break;
643 case REQUEST_CREATE_APPWIDGET:
644 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
645 completeAddAppWidget(appWidgetId, args.screen);
646 break;
647 case REQUEST_PICK_WALLPAPER:
648 // We just wanted the activity result here so we can clear mWaitingForResult
649 break;
650 }
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800654 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700655 mWaitingForResult = false;
656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 // The pattern used here is that a user PICKs a specific application,
658 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
661 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700662
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 final PendingAddArguments args = new PendingAddArguments();
665 args.requestCode = requestCode;
666 args.intent = data;
667 args.screen = mAddScreen;
668 args.cellX = mAddIntersectCellX;
669 args.cellY = mAddIntersectCellY;
670
671 // If the loader is still running, defer the add until it is done.
672 if (isWorkspaceLocked()) {
673 sPendingAddList.add(args);
674 } else {
675 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
Romain Guy18042c82009-11-06 11:44:55 -0800677 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
678 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
679 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700680 // Clean up the appWidgetId if we canceled
681 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
682 if (appWidgetId != -1) {
683 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
687
688 @Override
689 protected void onResume() {
690 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800691 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700692 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400693 mWorkspaceLoading = true;
694 mModel.startLoader(this, true);
695 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700696 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800698 if (mWaitingForResume != null) {
699 mWaitingForResume.setStayPressed(false);
700 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700701 // When we resume Launcher, a different Activity might be responsible for the app
702 // market intent, so refresh the icon
703 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 }
705
706 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 protected void onPause() {
708 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700709 // Some launcher layouts don't have a previous and next view
710 if (mPreviousView != null) {
711 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700712 }
713 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700714 dismissPreview(mNextView);
715 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700716 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800717 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 }
Romain Guycbb89e42009-06-08 15:52:54 -0700719
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700720 @Override
721 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 // Flag the loader to stop early before switching
723 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700724 if (mAllAppsGrid != null) {
725 mAllAppsGrid.surrender();
726 }
727 if (mAppsCustomizeContent != null) {
728 mAppsCustomizeContent.surrender();
729 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800730 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700731 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800733 // We can't hide the IME if it was forced open. So don't bother
734 /*
735 @Override
736 public void onWindowFocusChanged(boolean hasFocus) {
737 super.onWindowFocusChanged(hasFocus);
738
739 if (hasFocus) {
740 final InputMethodManager inputManager = (InputMethodManager)
741 getSystemService(Context.INPUT_METHOD_SERVICE);
742 WindowManager.LayoutParams lp = getWindow().getAttributes();
743 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
744 android.os.Handler()) {
745 protected void onReceiveResult(int resultCode, Bundle resultData) {
746 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
747 }
748 });
749 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
750 }
751 }
752 */
753
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 private boolean acceptFilter() {
755 final InputMethodManager inputManager = (InputMethodManager)
756 getSystemService(Context.INPUT_METHOD_SERVICE);
757 return !inputManager.isFullscreenMode();
758 }
759
760 @Override
761 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700762 final int uniChar = event.getUnicodeChar();
763 final boolean handled = super.onKeyDown(keyCode, event);
764 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
765 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
767 keyCode, event);
768 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700769 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700770 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700771 // showSearchDialog()
772 // If there are multiple keystrokes before the search dialog takes focus,
773 // onSearchRequested() will be called for every keystroke,
774 // but it is idempotent, so it's fine.
775 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 }
777 }
778
Joe Onorato8a9625e2010-01-28 15:55:35 -0800779 // Eat the long press event so the keyboard doesn't come up.
780 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
781 return true;
782 }
783
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 return handled;
785 }
786
Karl Rosaen138a0412009-04-23 19:00:21 -0700787 private String getTypedText() {
788 return mDefaultKeySsb.toString();
789 }
790
791 private void clearTypedText() {
792 mDefaultKeySsb.clear();
793 mDefaultKeySsb.clearSpans();
794 Selection.setSelection(mDefaultKeySsb, 0);
795 }
796
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700798 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
799 * State
800 */
801 private static State intToState(int stateOrdinal) {
802 State state = State.WORKSPACE;
803 final State[] stateValues = State.values();
804 for (int i = 0; i < stateValues.length; i++) {
805 if (stateValues[i].ordinal() == stateOrdinal) {
806 state = stateValues[i];
807 break;
808 }
809 }
810 return state;
811 }
812
813 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 * Restores the previous state, if it exists.
815 *
816 * @param savedState The previous state.
817 */
818 private void restoreState(Bundle savedState) {
819 if (savedState == null) {
820 return;
821 }
822
Michael Jurka883f55b2010-10-21 15:47:14 -0700823 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
824
Winson Chung785d2eb2011-04-14 16:08:02 -0700825 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800826 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700827 } else if (state == State.CUSTOMIZE) {
828 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800829 }
830
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
832 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700833 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
835
836 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700839 mAddScreen = addScreen;
840 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
841 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 mRestoring = true;
843 }
844
845 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
846 if (renameFolder) {
847 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700848 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 mRestoring = true;
850 }
Winson Chunga12a2502010-12-20 14:41:35 -0800851
852 // Restore the current AllApps drawer tab
853 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
854 String curTab = savedState.getString("allapps_currentTab");
855 if (curTab != null) {
856 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
857 tabhost.setCurrentTabByTag(curTab);
858 }
859 int curPage = savedState.getInt("allapps_currentPage", -1);
860 if (curPage > -1) {
861 mAllAppsPagedView.setRestorePage(curPage);
862 }
863 }
864
865 // Restore the current customization drawer tab
866 if (mHomeCustomizationDrawer != null) {
867 String curTab = savedState.getString("customize_currentTab");
868 if (curTab != null) {
869 // We set this directly so that there is no delay before the tab is set
Michael Jurka7ef959b2011-02-23 11:48:32 -0800870 mCustomizePagedView.setCustomizationFilter(
871 mHomeCustomizationDrawer.getCustomizeFilterForTabTag(curTab));
Winson Chunga12a2502010-12-20 14:41:35 -0800872 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
873 }
874
875 // Note: currently we do not restore the page for the customization tray because unlike
876 // AllApps, the page content can change drastically
877 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700878
879 // Restore the AppsCustomize tab
880 if (mAppsCustomizeTabHost != null) {
881 String curTab = savedState.getString("apps_customize_currentTab");
882 if (curTab != null) {
883 // We set this directly so that there is no delay before the tab is set
884 mAppsCustomizeContent.setContentType(
885 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
886 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
887 }
888
889 // Note: currently we do not restore the page for the AppsCustomize pane because the
890 // change in layout can drastically affect the saved page index
891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893
894 /**
895 * Finds all the views we need and configure them properly.
896 */
897 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700898 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400899
Winson Chung4c98d922011-05-31 16:50:48 -0700900 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
901 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902
Winson Chung4c98d922011-05-31 16:50:48 -0700903 // Setup the drag layer
904 mDragLayer.setup(this, dragController);
905
906 // Setup the workspace
907 mWorkspace.setHapticFeedbackEnabled(false);
908 mWorkspace.setOnLongClickListener(this);
909 mWorkspace.setup(this, dragController);
910 mWorkspace.setWallpaperDimension();
911
912 // Setup the different configurations
913 DeleteZone allAppsDeleteZone = null;
914 ApplicationInfoDropTarget allAppsInfoTarget = null;
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700915 if (LauncherApplication.isScreenLarge()) {
Winson Chung4c98d922011-05-31 16:50:48 -0700916 // Setup AllApps
917 mAllAppsGrid = (AllAppsView) mDragLayer.findViewById(R.id.all_apps_view);
Winson Chung785d2eb2011-04-14 16:08:02 -0700918 mAllAppsGrid.setup(this, dragController);
919 // We don't want a hole punched in our window.
920 ((View) mAllAppsGrid).setWillNotDraw(false);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700921
Winson Chung4c98d922011-05-31 16:50:48 -0700922 // Setup Customize
Winson Chung80baf5a2010-08-09 16:03:15 -0700923 mCustomizePagedView.setLauncher(this);
924 mCustomizePagedView.setDragController(dragController);
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700925 mCustomizePagedView.setAllAppsPagedView(mAllAppsPagedView);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400926
Winson Chung4c98d922011-05-31 16:50:48 -0700927 // Setup DeleteZone
928 mDeleteZone = (DeleteZone) mDragLayer.findViewById(R.id.delete_zone);
929 mDeleteZone.setLauncher(this);
930 mDeleteZone.setDragController(dragController);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800931
Winson Chung4c98d922011-05-31 16:50:48 -0700932 // Setup the top-right Apps/Customize buttons
933 mAllAppsButton = findViewById(R.id.all_apps_button);
934 mDivider = findViewById(R.id.all_apps_divider);
935 mConfigureButton = findViewById(R.id.configure_button);
936 mDeleteZone.setOverlappingViews(new View[] { mAllAppsButton, mDivider,
937 mConfigureButton });
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938
Winson Chung4c98d922011-05-31 16:50:48 -0700939 // Setup the AllApps Delete toolbar button
940 allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
Adam Cohencdc30d52010-12-01 15:09:47 -0800941 allAppsDeleteZone.setLauncher(this);
942 allAppsDeleteZone.setDragController(dragController);
943 allAppsDeleteZone.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800944
Winson Chung4c98d922011-05-31 16:50:48 -0700945 // Setup the AllApps Info toolbar button
946 allAppsInfoTarget = (ApplicationInfoDropTarget) findViewById(R.id.all_apps_info_target);
Adam Cohencdc30d52010-12-01 15:09:47 -0800947 allAppsInfoTarget.setLauncher(this);
Adam Cohencdc30d52010-12-01 15:09:47 -0800948 allAppsInfoTarget.setDragAndDropEnabled(false);
Winson Chung4c98d922011-05-31 16:50:48 -0700949
950 // Setup the AllApps Market toolbar button
951 View marketButton = findViewById(R.id.market_button);
952 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chung4c98d922011-05-31 16:50:48 -0700953 } else {
954 // Get the search/delete bar
955 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
956
957 // Setup AppsCustomize
958 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
959 findViewById(R.id.apps_customize_pane);
960 mAppsCustomizeContent = (AppsCustomizePagedView)
961 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
962 mAppsCustomizeContent.setup(this, dragController);
963
964 // Setup AppsCustomize button
965 mHandleView = (HandleView) mDragLayer.findViewById(R.id.all_apps_button);
966 mHandleView.setLauncher(this);
967 mHandleView.setOnLongClickListener(this);
968 mHandleView.setOnClickListener(new OnClickListener() {
969 public void onClick(View v) {
970 onClickAllAppsButton(v);
971 }
972 });
973
974 // Setup Hotseat
975 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
976 hotseatLeft.setContentDescription(mHotseatLabels[0]);
977 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
978 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
979 hotseatRight.setContentDescription(mHotseatLabels[1]);
980 hotseatRight.setImageDrawable(mHotseatIcons[1]);
981
982 View.OnKeyListener listener = new IndicatorKeyEventListener();
983 mPreviousView = (ImageView) mDragLayer.findViewById(R.id.previous_screen);
984 mPreviousView.setOnKeyListener(listener);
985 mNextView = (ImageView) mDragLayer.findViewById(R.id.next_screen);
986 mNextView.setOnKeyListener(listener);
987
988 Drawable previous = mPreviousView.getDrawable();
989 Drawable next = mNextView.getDrawable();
990 mWorkspace.setIndicators(previous, next);
991
992 mPreviousView.setHapticFeedbackEnabled(false);
993 mPreviousView.setOnLongClickListener(this);
994 mNextView.setHapticFeedbackEnabled(false);
995 mNextView.setOnLongClickListener(this);
Adam Cohencdc30d52010-12-01 15:09:47 -0800996 }
997
Winson Chung4c98d922011-05-31 16:50:48 -0700998 // Setup keylistener for button cluster
Winson Chung97d85d22011-04-13 11:27:36 -0700999 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
Winson Chung4e6a9762011-05-09 11:56:34 -07001000 View.OnKeyListener listener = null;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001001 if (LauncherApplication.isScreenLarge()) {
Winson Chung4e6a9762011-05-09 11:56:34 -07001002 // For tablets, AllApps lives in the button bar at the top
1003 listener = new ButtonBarKeyEventListener();
1004 } else {
1005 // For phones, AppsCustomize lives in the "dock" at the bottom
1006 listener = new DockKeyEventListener();
1007 }
Winson Chung97d85d22011-04-13 11:27:36 -07001008 int buttonCount = mButtonCluster.getChildCount();
1009 for (int i = 0; i < buttonCount; ++i) {
1010 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
1011 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001012
Winson Chung4c98d922011-05-31 16:50:48 -07001013 // Setup the drag controller (the drop targets have to be added in reverse order)
1014 dragController.setDragScoller(mWorkspace);
1015 dragController.setScrollView(mDragLayer);
1016 dragController.setMoveTarget(mWorkspace);
1017 dragController.addDropTarget(mWorkspace);
1018 if (mDeleteZone != null) {
1019 dragController.addDragListener(mDeleteZone);
1020 dragController.addDropTarget(mDeleteZone);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001021 }
Winson Chung4c98d922011-05-31 16:50:48 -07001022 if (mSearchDeleteBar != null) {
1023 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001024 }
Winson Chung4c98d922011-05-31 16:50:48 -07001025 if (allAppsDeleteZone != null) {
1026 dragController.addDragListener(allAppsDeleteZone);
1027 dragController.addDropTarget(allAppsDeleteZone);
1028 }
1029 if (allAppsInfoTarget != null) {
1030 dragController.addDragListener(allAppsInfoTarget);
1031 dragController.addDropTarget(allAppsInfoTarget);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 }
1034
Romain Guy8a73c512009-11-09 19:19:59 -08001035 @SuppressWarnings({"UnusedDeclaration"})
1036 public void previousScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001037 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001038 mWorkspace.scrollLeft();
1039 }
Romain Guy8a73c512009-11-09 19:19:59 -08001040 }
1041
1042 @SuppressWarnings({"UnusedDeclaration"})
1043 public void nextScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001044 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001045 mWorkspace.scrollRight();
1046 }
Romain Guy8a73c512009-11-09 19:19:59 -08001047 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001048
1049 @SuppressWarnings({"UnusedDeclaration"})
1050 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001051 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001052
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001053 int index = -1;
1054 if (v.getId() == R.id.hotseat_left) {
1055 index = 0;
1056 } else if (v.getId() == R.id.hotseat_right) {
1057 index = 1;
1058 }
1059
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001060 // reload these every tap; you never know when they might change
1061 loadHotseats();
1062 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001063 startActivitySafely(
1064 mHotseats[index],
1065 "hotseat"
1066 );
1067 }
1068 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 /**
1071 * Creates a view representing a shortcut.
1072 *
1073 * @param info The data structure describing the shortcut.
1074 *
1075 * @return A View inflated from R.layout.application.
1076 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001077 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001079 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 }
1081
1082 /**
1083 * Creates a view representing a shortcut inflated from the specified resource.
1084 *
1085 * @param layoutResId The id of the XML layout used to create the shortcut.
1086 * @param parent The group the shortcut belongs to.
1087 * @param info The data structure describing the shortcut.
1088 *
1089 * @return A View inflated from layoutResId.
1090 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001091 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001092 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1093 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 return favorite;
1096 }
1097
1098 /**
1099 * Add an application shortcut to the workspace.
1100 *
1101 * @param data The intent describing the application.
1102 * @param cellInfo The position on screen where to create the shortcut.
1103 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001104 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001105 int intersectCellX, int intersectCellY) {
1106 final int[] cellXY = mTmpAddItemCellCoordinates;
1107 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1108
1109 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1110 showOutOfSpaceMessage();
1111 return;
1112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001114 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001115
Romain Guy73b979d2009-06-09 12:57:21 -07001116 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001117 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001119 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001120 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1121 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001122 } else {
1123 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
1125 }
Romain Guycbb89e42009-06-08 15:52:54 -07001126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 /**
1128 * Add a shortcut to the workspace.
1129 *
1130 * @param data The intent describing the shortcut.
1131 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001133 private void completeAddShortcut(Intent data, int screen,
1134 int intersectCellX, int intersectCellY) {
1135 final int[] cellXY = mTmpAddItemCellCoordinates;
1136 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001137
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001138 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001139 boolean foundCellSpan = false;
1140 if (touchXY != null) {
1141 // when dragging and dropping, just find the closest free spot
1142 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1143 int[] result = screenLayout.findNearestVacantArea(
1144 touchXY[0], touchXY[1], 1, 1, cellXY);
1145 foundCellSpan = (result != null);
1146 } else {
1147 foundCellSpan = layout.findCellForSpanThatIntersects(
1148 cellXY, 1, 1, intersectCellX, intersectCellY);
1149 }
1150
1151 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001152 showOutOfSpaceMessage();
1153 return;
1154 }
1155
1156 final ShortcutInfo info = mModel.addShortcut(
1157 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158
1159 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001161 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 }
1163 }
1164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001166 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001168 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001169 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 */
Winson Chungf7640c82011-02-28 13:47:29 -08001171 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001172 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001173
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001174 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1176 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001179 // We have saved the position to which the widget was dragged-- this really only matters
1180 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001182
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001183 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 boolean foundCellSpan = false;
1185 if (touchXY != null) {
1186 // when dragging and dropping, just find the closest free spot
1187 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1188 int[] result = screenLayout.findNearestVacantArea(
1189 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001190 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001191 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001192 // if we long pressed on an empty cell to bring up a menu,
1193 // make sure we intersect the empty cell
1194 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1195 // findCellForSpanThatIntersects will just ignore them
1196 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1197 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001198 }
1199
Michael Jurka0280c3b2010-09-17 15:00:07 -07001200 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001201 if (appWidgetId != -1) {
1202 // Deleting an app widget ID is a void call but writes to disk before returning
1203 // to the caller...
1204 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1205 new Thread("deleteAppWidgetId") {
1206 public void run() {
1207 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1208 }
1209 }.start();
1210 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001212 return;
1213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001215 // Build Launcher-specific widget info and save to database
1216 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001217 launcherInfo.spanX = spanXY[0];
1218 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 LauncherModel.addItemToDatabase(this, launcherInfo,
1221 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223
1224 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001225 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001228 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001229
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001230 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001232
Michael Jurka0280c3b2010-09-17 15:00:07 -07001233 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001234 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001235
1236 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 }
1238 }
Romain Guycbb89e42009-06-08 15:52:54 -07001239
Adam Cohended9f8d2010-11-03 13:25:16 -07001240 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1241 @Override
1242 public void onReceive(Context context, Intent intent) {
1243 final String action = intent.getAction();
1244 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1245 mUserPresent = false;
1246 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001247
1248 // Reset AllApps to it's initial state
1249 if (mAllAppsGrid != null) {
1250 mAllAppsGrid.reset();
1251 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001252 if (mAppsCustomizeContent != null) {
1253 mAppsCustomizeContent.reset();
1254 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001255 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1256 mUserPresent = true;
1257 updateRunning();
1258 }
1259 }
1260 };
1261
1262 @Override
1263 public void onAttachedToWindow() {
1264 super.onAttachedToWindow();
1265
1266 // Listen for broadcasts related to user-presence
1267 final IntentFilter filter = new IntentFilter();
1268 filter.addAction(Intent.ACTION_SCREEN_OFF);
1269 filter.addAction(Intent.ACTION_USER_PRESENT);
1270 registerReceiver(mReceiver, filter);
1271
Adam Cohend113e0c2010-11-11 10:48:05 -08001272 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001273 mVisible = true;
1274 }
1275
1276 @Override
1277 public void onDetachedFromWindow() {
1278 super.onDetachedFromWindow();
1279 mVisible = false;
1280
Adam Cohend113e0c2010-11-11 10:48:05 -08001281 if (mAttached) {
1282 unregisterReceiver(mReceiver);
1283 mAttached = false;
1284 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001285 updateRunning();
1286 }
1287
1288 public void onWindowVisibilityChanged(int visibility) {
1289 mVisible = visibility == View.VISIBLE;
1290 updateRunning();
1291 }
1292
1293 private void sendAdvanceMessage(long delay) {
1294 mHandler.removeMessages(ADVANCE_MSG);
1295 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1296 mHandler.sendMessageDelayed(msg, delay);
1297 mAutoAdvanceSentTime = System.currentTimeMillis();
1298 }
1299
1300 private void updateRunning() {
1301 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1302 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1303 mAutoAdvanceRunning = autoAdvanceRunning;
1304 if (autoAdvanceRunning) {
1305 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1306 sendAdvanceMessage(delay);
1307 } else {
1308 if (!mWidgetsToAdvance.isEmpty()) {
1309 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1310 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1311 }
1312 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001313 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001314 }
1315 }
1316 }
1317
1318 private final Handler mHandler = new Handler() {
1319 @Override
1320 public void handleMessage(Message msg) {
1321 if (msg.what == ADVANCE_MSG) {
1322 int i = 0;
1323 for (View key: mWidgetsToAdvance.keySet()) {
1324 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1325 final int delay = mAdvanceStagger * i;
1326 if (v instanceof Advanceable) {
1327 postDelayed(new Runnable() {
1328 public void run() {
1329 ((Advanceable) v).advance();
1330 }
1331 }, delay);
1332 }
1333 i++;
1334 }
1335 sendAdvanceMessage(mAdvanceInterval);
1336 }
1337 }
1338 };
1339
1340 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1341 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1342 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1343 if (v instanceof Advanceable) {
1344 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001345 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001346 updateRunning();
1347 }
1348 }
1349
1350 void removeWidgetToAutoAdvance(View hostView) {
1351 if (mWidgetsToAdvance.containsKey(hostView)) {
1352 mWidgetsToAdvance.remove(hostView);
1353 updateRunning();
1354 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001355 }
1356
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1358 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001359 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 launcherInfo.hostView = null;
1361 }
1362
Adam Cohended9f8d2010-11-03 13:25:16 -07001363 void showOutOfSpaceMessage() {
1364 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1365 }
1366
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001367 public LauncherAppWidgetHost getAppWidgetHost() {
1368 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 }
Romain Guycbb89e42009-06-08 15:52:54 -07001370
Winson Chunga9abd0e2010-10-27 17:18:37 -07001371 public LauncherModel getModel() {
1372 return mModel;
1373 }
1374
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001375 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001376 getWindow().closeAllPanels();
1377
1378 try {
1379 dismissDialog(DIALOG_CREATE_SHORTCUT);
1380 // Unlock the workspace if the dialog was showing
1381 } catch (Exception e) {
1382 // An exception is thrown if the dialog is not visible, which is fine
1383 }
1384
1385 try {
1386 dismissDialog(DIALOG_RENAME_FOLDER);
1387 // Unlock the workspace if the dialog was showing
1388 } catch (Exception e) {
1389 // An exception is thrown if the dialog is not visible, which is fine
1390 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001391
1392 // Whatever we were doing is hereby canceled.
1393 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001394 }
1395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 @Override
1397 protected void onNewIntent(Intent intent) {
1398 super.onNewIntent(intent);
1399
1400 // Close the menu
1401 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001402 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001403 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001404
Joe Onorato14f122b2009-11-19 14:06:36 -08001405 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1406 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001407
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001408 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001409 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001410 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001411 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001412 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001413 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001414 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001415
Joe Onorato3a8820b2009-11-10 15:06:42 -08001416 final View v = getWindow().peekDecorView();
1417 if (v != null && v.getWindowToken() != null) {
1418 InputMethodManager imm = (InputMethodManager)getSystemService(
1419 INPUT_METHOD_SERVICE);
1420 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001422
1423 // Reset AllApps to it's initial state
1424 if (mAllAppsGrid != null) {
1425 mAllAppsGrid.reset();
1426 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001427 if (mAppsCustomizeContent != null) {
1428 mAppsCustomizeContent.reset();
1429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 }
1431 }
1432
1433 @Override
1434 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1435 // Do not call super here
1436 mSavedInstanceState = savedInstanceState;
1437 }
1438
1439 @Override
1440 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001441 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442
1443 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1444 if (folders.size() > 0) {
1445 final int count = folders.size();
1446 long[] ids = new long[count];
1447 for (int i = 0; i < count; i++) {
1448 final FolderInfo info = folders.get(i).getInfo();
1449 ids[i] = info.id;
1450 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001451 outState.putLongArray(RUNTIME_STATE_FOLDERS, ids);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 } else {
1453 super.onSaveInstanceState(outState);
1454 }
1455
Michael Jurka883f55b2010-10-21 15:47:14 -07001456 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457
Michael Jurka0280c3b2010-09-17 15:00:07 -07001458 if (mAddScreen > -1 && mWaitingForResult) {
1459 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1460 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1461 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463
1464 if (mFolderInfo != null && mWaitingForResult) {
1465 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1466 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1467 }
Michael Jurka946ad472010-07-09 18:05:18 -07001468
Winson Chung43b119d2010-12-06 16:45:05 -08001469 // Save the current AllApps drawer tab
1470 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1471 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1472 String currentTabTag = tabhost.getCurrentTabTag();
1473 if (currentTabTag != null) {
1474 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001475 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001476 }
1477 }
Winson Chung43b119d2010-12-06 16:45:05 -08001478 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001479 if (mHomeCustomizationDrawer != null) {
1480 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1481 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001482 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001483 }
1484 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001485 // Save the current AppsCustomize tab
1486 if (mAppsCustomizeTabHost != null) {
1487 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1488 if (currentTabTag != null) {
1489 outState.putString("apps_customize_currentTab", currentTabTag);
1490 }
1491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
1493
1494 @Override
1495 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001499 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001501 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001503 mAppWidgetHost = null;
1504
1505 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506
1507 TextKeyListener.getInstance().release();
1508
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510
Joe Onorato9c1289c2009-08-17 11:03:03 -04001511 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001512
1513 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001514
Patrick Dubroyab962b72010-07-26 12:10:10 -07001515 // Some launcher layouts don't have a previous and next view
1516 if (mPreviousView != null) {
1517 dismissPreview(mPreviousView);
1518 }
1519 if (mNextView != null) {
1520 dismissPreview(mNextView);
1521 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001522
1523 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001524
1525 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1526 mWorkspace.removeAllViews();
1527 mWorkspace = null;
1528 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001529
1530 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
1532
Adam Cohena9cf38f2011-05-02 15:36:58 -07001533 public DragController getDragController() {
1534 return mDragController;
1535 }
1536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 @Override
1538 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001539 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 super.startActivityForResult(intent, requestCode);
1541 }
1542
1543 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001544 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001546
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001547 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001548
Karl Rosaen138a0412009-04-23 19:00:21 -07001549 if (initialQuery == null) {
1550 // Use any text typed in the launcher as the initial query
1551 initialQuery = getTypedText();
1552 clearTypedText();
1553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 if (appSearchData == null) {
1555 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001556 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 }
Romain Guycbb89e42009-06-08 15:52:54 -07001558
Karl Rosaen138a0412009-04-23 19:00:21 -07001559 final SearchManager searchManager =
1560 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001561 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001562 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
1564
1565 @Override
1566 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001567 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001568 return false;
1569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570
1571 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1574 .setIcon(android.R.drawable.ic_menu_add)
1575 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001576 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1577 .setIcon(android.R.drawable.ic_menu_manage)
1578 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001579 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 .setIcon(android.R.drawable.ic_menu_gallery)
1581 .setAlphabeticShortcut('W');
1582 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1583 .setIcon(android.R.drawable.ic_search_category_default)
1584 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1585 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1586 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1587 .setAlphabeticShortcut('N');
1588
1589 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001590 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1591 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592
1593 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1594 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1595 .setIntent(settings);
1596
1597 return true;
1598 }
1599
1600 @Override
1601 public boolean onPrepareOptionsMenu(Menu menu) {
1602 super.onPrepareOptionsMenu(menu);
1603
Winson Chung785d2eb2011-04-14 16:08:02 -07001604 if (mAllAppsGrid != null) {
1605 // If all apps is animating, don't show the menu, because we don't know
1606 // which one to show.
1607 if (mAllAppsGrid.isAnimating()) {
1608 return false;
1609 }
1610
1611 // Only show the add and wallpaper options when we're not in all apps.
1612 boolean visible = !mAllAppsGrid.isVisible();
1613 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1614 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1615
1616 // Disable add if the workspace is full.
1617 if (visible) {
1618 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1619 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
1620 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001621 }
1622
Winson Chung785d2eb2011-04-14 16:08:02 -07001623 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1624 // related code?
1625 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626
1627 return true;
1628 }
1629
1630 @Override
1631 public boolean onOptionsItemSelected(MenuItem item) {
1632 switch (item.getItemId()) {
1633 case MENU_ADD:
1634 addItems();
1635 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001636 case MENU_MANAGE_APPS:
1637 manageApps();
1638 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 case MENU_WALLPAPER_SETTINGS:
1640 startWallpaper();
1641 return true;
1642 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001643 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 return true;
1645 case MENU_NOTIFICATIONS:
1646 showNotifications();
1647 return true;
1648 }
1649
1650 return super.onOptionsItemSelected(item);
1651 }
Romain Guycbb89e42009-06-08 15:52:54 -07001652
Karl Rosaen138a0412009-04-23 19:00:21 -07001653 /**
1654 * Indicates that we want global search for this activity by setting the globalSearch
1655 * argument for {@link #startSearch} to true.
1656 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001658 @Override
1659 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001660 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001661 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001662 }
1663
Joe Onorato9c1289c2009-08-17 11:03:03 -04001664 public boolean isWorkspaceLocked() {
1665 return mWorkspaceLoading || mWaitingForResult;
1666 }
1667
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001668 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1669 private boolean isPreviewVisible() {
1670 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1671 (mNextView != null && mNextView.getTag() != null);
1672 }
1673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 private void addItems() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001675 if (LauncherApplication.isScreenLarge()) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001676 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001677 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001678 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001679 }
1680 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001681 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001682 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 }
1685
Michael Jurka0280c3b2010-09-17 15:00:07 -07001686 private void resetAddInfo() {
1687 mAddScreen = -1;
1688 mAddIntersectCellX = -1;
1689 mAddIntersectCellY = -1;
1690 mAddDropPosition = null;
1691 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001692
Winson Chung55cef262010-10-28 14:14:18 -07001693 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001694 resetAddInfo();
1695 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001696 mAddDropPosition = position;
1697
Michael Jurkaaf442092010-06-10 17:01:57 -07001698 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001699 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1700 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001701 }
1702
Winson Chung7ad01412010-09-27 11:33:03 -07001703 private void manageApps() {
1704 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1705 }
1706
Michael Jurkaaf442092010-06-10 17:01:57 -07001707 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001708 // TODO: catch bad widget exception when sent
1709 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001710 // TODO: Is this log message meaningful?
1711 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001712 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001713 }
1714
Winson Chung55cef262010-10-28 14:14:18 -07001715 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001716 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717
Bjorn Bringert7984c942009-12-09 15:38:25 +00001718 if (appWidget.configure != null) {
1719 // Launch over to configure widget, if needed
1720 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1721 intent.setComponent(appWidget.configure);
1722 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001723 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001724 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1725 intent.putExtra(
1726 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1727 info.mimeType);
1728
1729 final String mimeType = info.mimeType;
1730 final ClipData clipData = (ClipData) info.configurationData;
1731 final ClipDescription clipDesc = clipData.getDescription();
1732 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1733 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001734 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001735 final CharSequence stringData = item.getText();
1736 final Uri uriData = item.getUri();
1737 final Intent intentData = item.getIntent();
1738 final String key =
1739 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1740 if (uriData != null) {
1741 intent.putExtra(key, uriData);
1742 } else if (intentData != null) {
1743 intent.putExtra(key, intentData);
1744 } else if (stringData != null) {
1745 intent.putExtra(key, stringData);
1746 }
1747 break;
1748 }
1749 }
1750 }
Winson Chung55cef262010-10-28 14:14:18 -07001751 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001752
Romain Guy8e633c52010-03-04 12:51:36 -08001753 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001755 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001756 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 }
1758 }
Romain Guycbb89e42009-06-08 15:52:54 -07001759
Michael Jurka0280c3b2010-09-17 15:00:07 -07001760 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1761 resetAddInfo();
1762 mAddScreen = screen;
1763 mAddDropPosition = position;
1764
1765 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1766 createShortcutIntent.setComponent(componentName);
1767 processShortcut(createShortcutIntent);
1768 }
1769
Joe Onoratodeb98af2010-02-19 14:59:39 -08001770 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001771 // Handle case where user selected "Applications"
1772 String applicationName = getResources().getString(R.string.group_applications);
1773 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001774
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001775 if (applicationName != null && applicationName.equals(shortcutName)) {
1776 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1777 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001778
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001779 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1780 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001781 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001782 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001783 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001784 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001785 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
1787
Winson Chung24ab2f12010-09-16 14:10:47 -07001788 void processWallpaper(Intent intent) {
1789 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1790 }
1791
Adam Cohendf035382011-04-11 17:22:04 -07001792 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001793 FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 folderInfo.title = getText(R.string.folder_name);
1795
Michael Jurka0280c3b2010-09-17 15:00:07 -07001796 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1797 final int[] cellXY = mTmpAddItemCellCoordinates;
1798 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1799 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001800 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802
1803 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804 LauncherModel.addItemToDatabase(this, folderInfo,
1805 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001806 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001807 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808
1809 // Create the view
1810 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001811 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1812 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001813 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001814 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
Romain Guycbb89e42009-06-08 15:52:54 -07001816
Joe Onorato9c1289c2009-08-17 11:03:03 -04001817 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001818 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 private void showNotifications() {
1822 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1823 if (statusBar != null) {
1824 statusBar.expand();
1825 }
1826 }
1827
1828 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001829 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001831 Intent chooser = Intent.createChooser(pickWallpaper,
1832 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001833 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1834 // Removed in Eclair MR1
1835// WallpaperManager wm = (WallpaperManager)
1836// getSystemService(Context.WALLPAPER_SERVICE);
1837// WallpaperInfo wi = wm.getWallpaperInfo();
1838// if (wi != null && wi.getSettingsActivity() != null) {
1839// LabeledIntent li = new LabeledIntent(getPackageName(),
1840// R.string.configure_wallpaper, 0);
1841// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1842// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1843// }
Mike Clerona0618e42009-10-22 13:55:21 -07001844 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
1846
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001847 /**
1848 * Registers various content observers. The current implementation registers
1849 * only a favorites observer to keep track of the favorites applications.
1850 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001851 private void registerContentObservers() {
1852 ContentResolver resolver = getContentResolver();
1853 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1854 true, mWidgetObserver);
1855 }
1856
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 @Override
1858 public boolean dispatchKeyEvent(KeyEvent event) {
1859 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1860 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001862 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001863 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001864 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001865 dumpState();
1866 return true;
1867 }
1868 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001869 }
1870 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1871 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001872 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 return true;
1874 }
1875 }
1876
1877 return super.dispatchKeyEvent(event);
1878 }
1879
Joe Onorato88ec0992009-11-19 13:16:06 -08001880 @Override
1881 public void onBackPressed() {
Winson Chung785d2eb2011-04-14 16:08:02 -07001882 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001883 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001884 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001885 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001886 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001887 dismissPreview(mPreviousView);
1888 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001889 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001890 mWorkspace.exitWidgetResizeMode();
1891
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001892 // Back button is a no-op here, but give at least some feedback for the button press
1893 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001894 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001895 }
1896
Adam Cohen2801caf2011-05-13 20:57:39 -07001897 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 Folder folder = mWorkspace.getOpenFolder();
1899 if (folder != null) {
1900 closeFolder(folder);
Adam Cohen2801caf2011-05-13 20:57:39 -07001901 mDragLayer.setCurrentFolder(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
1903 }
1904
1905 void closeFolder(Folder folder) {
1906 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001907
Michael Jurka8c920dd2011-01-20 14:16:56 -08001908 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001910 CellLayout cl = (CellLayout) parent;
Adam Cohen2801caf2011-05-13 20:57:39 -07001911 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1912 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001913 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001915
1916 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 folder.onClose();
1918 }
1919
1920 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001921 * Re-listen when widgets are reset.
1922 */
1923 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001924 if (mAppWidgetHost != null) {
1925 mAppWidgetHost.startListening();
1926 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001927 }
1928
1929 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001930 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1931 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001933 private void unbindDesktopItems() {
1934 for (ItemInfo item: mDesktopItems) {
1935 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001937 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001939
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 /**
1941 * Launches the intent referred by the clicked shortcut.
1942 *
1943 * @param v The view representing the clicked shortcut.
1944 */
1945 public void onClick(View v) {
1946 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001947 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001949 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001950 int[] pos = new int[2];
1951 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001952 intent.setSourceBounds(new Rect(pos[0], pos[1],
1953 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001954 boolean success = startActivitySafely(intent, tag);
1955
1956 if (success && v instanceof BubbleTextView) {
1957 mWaitingForResume = (BubbleTextView) v;
1958 mWaitingForResume.setStayPressed(true);
1959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001961 if (v instanceof FolderIcon) {
1962 FolderIcon fi = (FolderIcon) v;
1963 handleFolderClick(fi);
1964 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001965 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001966 if (mState == State.ALL_APPS) {
1967 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001968 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001969 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
1972 }
1973
Michael Jurka0e260592010-06-30 17:07:39 -07001974 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001975 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001976 // clicking anywhere on the workspace causes the customization drawer to slide down
1977 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001978 return false;
1979 }
1980
Michael Jurkaaf442092010-06-10 17:01:57 -07001981 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001982 * Event handler for the search button
1983 *
1984 * @param v The view that was clicked.
1985 */
1986 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001987 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001988 // Use a custom animation for launching search
1989 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001990 }
1991
1992 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001993 * Event handler for the voice button
1994 *
1995 * @param v The view that was clicked.
1996 */
1997 public void onClickVoiceButton(View v) {
1998 startVoiceSearch();
1999 }
2000
2001 private void startVoiceSearch() {
2002 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2003 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2004 startActivity(intent);
2005 }
2006
2007 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002008 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002009 * enters home screen customization mode.
2010 *
2011 * @param v The view that was clicked.
2012 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002013 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002014 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002015 }
2016
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002017 /**
2018 * Event handler for the "grid" button that appears on the home screen, which
2019 * enters all apps mode.
2020 *
2021 * @param v The view that was clicked.
2022 */
2023 public void onClickAllAppsButton(View v) {
2024 showAllApps(true);
2025 }
2026
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002027 public void onClickAppMarketButton(View v) {
2028 if (mAppMarketIntent != null) {
2029 startActivitySafely(mAppMarketIntent, "app market");
2030 }
2031 }
2032
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002033 void startApplicationDetailsActivity(ComponentName componentName) {
2034 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002035 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2036 Uri.fromParts("package", packageName, null));
2037 startActivity(intent);
2038 }
2039
Patrick Dubroy5539af72010-09-07 15:22:01 -07002040 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2041 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2042 // System applications cannot be installed. For now, show a toast explaining that.
2043 // We may give them the option of disabling apps this way.
2044 int messageId = R.string.uninstall_system_app_text;
2045 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2046 } else {
2047 String packageName = appInfo.componentName.getPackageName();
2048 String className = appInfo.componentName.getClassName();
2049 Intent intent = new Intent(
2050 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2051 startActivity(intent);
2052 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002053 }
2054
Michael Jurkaddd62e92011-02-16 17:49:14 -08002055 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2057 try {
2058 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002059 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 } catch (ActivityNotFoundException e) {
2061 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002062 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 } catch (SecurityException e) {
2064 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002065 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002067 "or use the exported attribute for this activity. "
2068 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002070 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002072
Romain Guy8e633c52010-03-04 12:51:36 -08002073 void startActivityForResultSafely(Intent intent, int requestCode) {
2074 try {
2075 startActivityForResult(intent, requestCode);
2076 } catch (ActivityNotFoundException e) {
2077 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2078 } catch (SecurityException e) {
2079 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2080 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2081 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2082 "or use the exported attribute for this activity.", e);
2083 }
2084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085
Adam Cohena9cf38f2011-05-02 15:36:58 -07002086 private void handleFolderClick(FolderIcon folderIcon) {
2087 final FolderInfo info = folderIcon.mInfo;
2088 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 // Close any open folder
2090 closeFolder();
2091 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002092 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 } else {
2094 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07002095 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 int folderScreen;
2097 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002098 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 // .. and close it
2100 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002101 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 // Close any folder open on the current screen
2103 closeFolder();
2104 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002105 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 }
2107 }
2108 }
2109 }
2110
Adam Cohen2801caf2011-05-13 20:57:39 -07002111 private void growAndFadeOutFolderIcon(FolderIcon fi) {
2112 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2113 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2114 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2115
2116 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2117 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2118 oa.start();
2119 }
2120
2121 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
2122 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2123 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2124 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2125
2126 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2127 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2128 oa.start();
2129 }
2130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002132 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 * is animated relative to the specified View. If the View is null, no animation
2134 * is played.
2135 *
2136 * @param folderInfo The FolderInfo describing the folder to open.
2137 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002138 public void openFolder(FolderIcon folderIcon) {
2139 Folder folder = folderIcon.mFolder;
2140 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141
Adam Cohen2801caf2011-05-13 20:57:39 -07002142 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002143 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144
Adam Cohena9cf38f2011-05-02 15:36:58 -07002145 mWorkspace.addInFullScreen(folder, info.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07002146 mDragLayer.setCurrentFolder(folder);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002147 folder.animateOpen();
2148 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 }
2150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002152 if (mState != State.WORKSPACE) {
2153 return false;
2154 }
2155
Romain Guy1fbc1c82009-11-09 20:43:08 -08002156 switch (v.getId()) {
2157 case R.id.previous_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002158 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002159 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2160 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002161 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002162 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002163 return true;
2164 case R.id.next_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002165 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002166 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2167 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002168 showPreviews(v);
2169 }
2170 return true;
2171 case R.id.all_apps_button:
Winson Chung785d2eb2011-04-14 16:08:02 -07002172 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002173 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2174 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2175 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002176 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002177 return true;
2178 }
2179
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 return false;
2182 }
2183
2184 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002185 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 }
2187
Michael Jurka0280c3b2010-09-17 15:00:07 -07002188 resetAddInfo();
2189 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002191 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 return true;
2193 }
2194
Michael Jurka0280c3b2010-09-17 15:00:07 -07002195 final View itemUnderLongClick = longClickCellInfo.cell;
2196
Winson Chung304dcde2011-01-07 11:17:23 -08002197 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002198 if (itemUnderLongClick == null) {
2199 // User long pressed on empty space
2200 mWorkspace.setAllowLongPress(false);
2201 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2202 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002203 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002205 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002207 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2208 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002209 mAddIntersectCellX = longClickCellInfo.cellX;
2210 mAddIntersectCellY = longClickCellInfo.cellY;
2211 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 }
2213 }
2214 }
2215 return true;
2216 }
2217
Romain Guye6b8e2f2009-11-10 11:56:55 -08002218 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002219 private void dismissPreview(final View v) {
2220 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002221 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002222 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2223 public void onDismiss() {
2224 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2225 int count = group.getChildCount();
2226 for (int i = 0; i < count; i++) {
2227 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2228 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002229 ArrayList<Bitmap> bitmaps =
2230 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002231 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2232
2233 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002234 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002235 window.setOnDismissListener(null);
2236 }
2237 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002238 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002239 }
2240 v.setTag(null);
2241 }
2242
Romain Guyf8e6a802009-12-07 17:48:02 -08002243 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002244 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002245 }
2246
Romain Guya6abce82009-11-10 02:54:41 -08002247 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002248 final Resources resources = getResources();
2249 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002250
Romain Guya6abce82009-11-10 02:54:41 -08002251 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002252
Romain Guy9d31e9f2009-11-11 18:54:28 -08002253 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002254
Romain Guyf8e6a802009-12-07 17:48:02 -08002255 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002256 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002257 int extraW = (int) ((r.left + r.right) * max);
2258 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002259
2260 int aW = cell.getWidth() - extraW;
2261 float w = aW / max;
2262
2263 int width = cell.getWidth();
2264 int height = cell.getHeight();
2265 int x = cell.getLeftPadding();
2266 int y = cell.getTopPadding();
2267 width -= (x + cell.getRightPadding());
2268 height -= (y + cell.getBottomPadding());
2269
2270 float scale = w / width;
2271
2272 int count = end - start;
2273
2274 final float sWidth = width * scale;
2275 float sHeight = height * scale;
2276
Romain Guye6b8e2f2009-11-10 11:56:55 -08002277 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002278
Romain Guye6b8e2f2009-11-10 11:56:55 -08002279 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2280 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002281
2282 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002283 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002284 cell = (CellLayout) workspace.getChildAt(i);
2285
Romain Guyf8e6a802009-12-07 17:48:02 -08002286 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002287 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002288
2289 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002290 c.scale(scale, scale);
2291 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002292 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002293
Romain Guy9d31e9f2009-11-11 18:54:28 -08002294 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002295 image.setImageBitmap(bitmap);
2296 image.setTag(i);
2297 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002298 image.setOnFocusChangeListener(handler);
2299 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002300 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002301
2302 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002303 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2304
Winson Chungaafa03c2010-06-11 17:34:16 -07002305 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002306 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002307
2308 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002309 p.setContentView(preview);
2310 p.setWidth((int) (sWidth * count + extraW));
2311 p.setHeight((int) (sHeight + extraH));
2312 p.setAnimationStyle(R.style.AnimationPreview);
2313 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002314 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002315 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002316 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002317
Romain Guye6b8e2f2009-11-10 11:56:55 -08002318 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2319 public void onDismiss() {
2320 dismissPreview(anchor);
2321 }
2322 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002323
2324 anchor.setTag(p);
2325 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002326 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002327 }
2328
Romain Guy9d31e9f2009-11-11 18:54:28 -08002329 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002330 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002331
Romain Guye6b8e2f2009-11-10 11:56:55 -08002332 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002333 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002334 }
2335
2336 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002337 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002338 v.post(this);
2339 }
2340
2341 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002342 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002343 }
2344
2345 public void onFocusChange(View v, boolean hasFocus) {
2346 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002347 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002348 }
Romain Guya6abce82009-11-10 02:54:41 -08002349 }
2350 }
2351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 Workspace getWorkspace() {
2353 return mWorkspace;
2354 }
2355
Michael Jurka3c38d562011-02-18 19:22:12 -08002356 TabHost getCustomizationDrawer() {
2357 return mHomeCustomizationDrawer;
2358 }
2359
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002360 @Override
2361 protected Dialog onCreateDialog(int id) {
2362 switch (id) {
2363 case DIALOG_CREATE_SHORTCUT:
2364 return new CreateShortcut().createDialog();
2365 case DIALOG_RENAME_FOLDER:
2366 return new RenameFolder().createDialog();
2367 }
2368
2369 return super.onCreateDialog(id);
2370 }
2371
2372 @Override
2373 protected void onPrepareDialog(int id, Dialog dialog) {
2374 switch (id) {
2375 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 break;
2377 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002378 if (mFolderInfo != null) {
2379 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2380 final CharSequence text = mFolderInfo.title;
2381 input.setText(text);
2382 input.setSelection(0, text.length());
2383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 break;
2385 }
2386 }
2387
2388 void showRenameDialog(FolderInfo info) {
2389 mFolderInfo = info;
2390 mWaitingForResult = true;
2391 showDialog(DIALOG_RENAME_FOLDER);
2392 }
2393
Michael Jurka0280c3b2010-09-17 15:00:07 -07002394 private void showAddDialog(int intersectX, int intersectY) {
2395 resetAddInfo();
2396 mAddIntersectCellX = intersectX;
2397 mAddIntersectCellY = intersectY;
2398 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 mWaitingForResult = true;
2400 showDialog(DIALOG_CREATE_SHORTCUT);
2401 }
2402
Joe Onoratodeb98af2010-02-19 14:59:39 -08002403 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002404 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002405 Bundle bundle = new Bundle();
2406
2407 ArrayList<String> shortcutNames = new ArrayList<String>();
2408 shortcutNames.add(getString(R.string.group_applications));
2409 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2410
2411 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2412 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2413 R.drawable.ic_launcher_application));
2414 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2415
2416 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2417 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002418 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002419 pickIntent.putExtras(bundle);
2420
Joe Onoratodeb98af2010-02-19 14:59:39 -08002421 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002422 }
2423
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 private class RenameFolder {
2425 private EditText mInput;
2426
2427 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2429 mInput = (EditText) layout.findViewById(R.id.folder_name);
2430
2431 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2432 builder.setIcon(0);
2433 builder.setTitle(getString(R.string.rename_folder_title));
2434 builder.setCancelable(true);
2435 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2436 public void onCancel(DialogInterface dialog) {
2437 cleanup();
2438 }
2439 });
2440 builder.setNegativeButton(getString(R.string.cancel_action),
2441 new Dialog.OnClickListener() {
2442 public void onClick(DialogInterface dialog, int which) {
2443 cleanup();
2444 }
2445 }
2446 );
2447 builder.setPositiveButton(getString(R.string.rename_action),
2448 new Dialog.OnClickListener() {
2449 public void onClick(DialogInterface dialog, int which) {
2450 changeFolderName();
2451 }
2452 }
2453 );
2454 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002455
2456 final AlertDialog dialog = builder.create();
2457 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2458 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002459 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002460 mInput.requestFocus();
2461 InputMethodManager inputManager = (InputMethodManager)
2462 getSystemService(Context.INPUT_METHOD_SERVICE);
2463 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002464 }
2465 });
2466
2467 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 }
2469
2470 private void changeFolderName() {
2471 final String name = mInput.getText().toString();
2472 if (!TextUtils.isEmpty(name)) {
2473 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002474 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 mFolderInfo.title = name;
2476 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2477
Joe Onorato9c1289c2009-08-17 11:03:03 -04002478 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002479 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002480 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002481 } else {
2482 final FolderIcon folderIcon = (FolderIcon)
2483 mWorkspace.getViewForTag(mFolderInfo);
2484 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002485 // TODO: At some point we'll probably want some version of setting
2486 // the text for a folder icon.
2487 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 getWorkspace().requestLayout();
2489 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002490 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002491 mWorkspaceLoading = true;
2492 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 }
2494 }
2495 }
2496 cleanup();
2497 }
2498
2499 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 dismissDialog(DIALOG_RENAME_FOLDER);
2501 mWaitingForResult = false;
2502 mFolderInfo = null;
2503 }
2504 }
2505
Daniel Sandler843e8602010-06-07 14:59:01 -04002506 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2507 public boolean isAllAppsVisible() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002508 return (mState == State.ALL_APPS) || (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002509 }
2510
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002511 // AllAppsView.Watcher
2512 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002513 // In XLarge view, we zoom down the workspace below all apps so it's still visible
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002514 if (zoom == 1.0f && !LauncherApplication.isScreenLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002515 mWorkspace.setVisibility(View.GONE);
2516 }
2517 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002518
Michael Jurkaafca9532011-02-16 14:50:35 -08002519 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002520 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002521 }
2522
2523 private void hideToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002524 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2525 button.setVisibility(View.INVISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002526 button.setAlpha(0.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002527 }
2528
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002529 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002530 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002531 *
2532 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2533 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002534 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002535 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002536 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002537 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002538 final boolean showing = show;
2539 final boolean hiding = !show;
2540
2541 final int duration = show ?
2542 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2543 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2544
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002545 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002546 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002547 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002548 anim.addUpdateListener(new AnimatorUpdateListener() {
2549 public void onAnimationUpdate(ValueAnimator animation) {
2550 view.setAlpha((Float) animation.getAnimatedValue());
2551 }
2552 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002553 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002554 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002555 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002556 if (showing) showAndEnableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002557 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002558 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002559 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002560 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562 });
2563 seq.play(anim);
2564 } else {
2565 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002566 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002567 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002568 } else {
2569 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002570 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002571 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002572 }
2573
2574 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 * Show/hide the appropriate toolbar buttons for newState.
2576 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002577 *
2578 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002579 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2580 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002581 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002582 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002583 switch (newState) {
2584 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002585 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
Winson Chung4c98d922011-05-31 16:50:48 -07002586 if (mDeleteZone != null) {
2587 mDeleteZone.setDragAndDropEnabled(true);
2588 if (LauncherApplication.isScreenLarge()) {
2589 mDeleteZone.setText(
2590 getResources().getString(R.string.delete_zone_label_workspace));
2591 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002592 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002593 break;
2594 case ALL_APPS:
Winson Chung785d2eb2011-04-14 16:08:02 -07002595 case APPS_CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002596 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Winson Chung4c98d922011-05-31 16:50:48 -07002597 if (mDeleteZone != null) {
2598 mDeleteZone.setDragAndDropEnabled(false);
2599 if (LauncherApplication.isScreenLarge()) {
2600 mDeleteZone.setText(
2601 getResources().getString(R.string.delete_zone_label_all_apps));
2602 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002603 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002604 break;
2605 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002606 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Winson Chung4c98d922011-05-31 16:50:48 -07002607 if (mDeleteZone != null) {
2608 mDeleteZone.setDragAndDropEnabled(false);
2609 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002610 break;
2611 }
2612 }
2613
2614 /**
2615 * Helper method for the cameraZoomIn/cameraZoomOut animations
2616 * @param view The view being animated
2617 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2618 * @param scaleFactor The scale factor used for the zoom
2619 */
2620 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2621 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002622
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002623 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002624 // Set pivotY so that at the starting zoom factor, the view is partially
2625 // visible. Modifying initialHeightFactor changes how much of the view is
2626 // initially showing, and hence the perceived angle from which the view enters.
Winson Chung785d2eb2011-04-14 16:08:02 -07002627 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002628 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002629 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002630 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002631 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002632 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633 }
2634 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002635
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002636 /**
2637 * Zoom the camera out from the workspace to reveal 'toView'.
2638 * Assumes that the view to show is anchored at either the very top or very bottom
2639 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002640 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002641 */
Winson Chung20f71112011-04-06 14:22:04 -07002642 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002643 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002644 final boolean toAllApps = (toState == State.ALL_APPS)
2645 || (toState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002646
Winson Chung785d2eb2011-04-14 16:08:02 -07002647 final int duration = (toAllApps ?
2648 res.getInteger(R.integer.config_appsCustomizeZoomInTime) :
2649 res.getInteger(R.integer.config_customizeZoomInTime));
2650 final int fadeDuration = (toAllApps ?
2651 res.getInteger(R.integer.config_appsCustomizeFadeInTime) :
2652 res.getInteger(R.integer.config_customizeFadeInTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002653
2654 final float scale = toAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002655 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002656 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2657
Winson Chung785d2eb2011-04-14 16:08:02 -07002658 View tmpView;
2659 if (toAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002660 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002661 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2662 } else {
2663 tmpView = mHomeCustomizationDrawer;
2664 }
2665 final View toView = tmpView;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002666
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002667 setPivotsForZoom(toView, toState, scale);
2668
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002669 if (toAllApps) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002670 if (springLoaded) {
2671 if (toState == State.APPS_CUSTOMIZE) {
2672 // Shrink workspaces away if going back to AppsCustomize from spring loaded mode
2673 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2674 } else {
2675 // Shrink workspaces to bottom if going back to AllApps from spring loaded mode
2676 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2677 }
2678 } else {
2679 // Shrink workspaces away if going to AllApps/AppsCustomize from workspace
Winson Chung20f71112011-04-06 14:22:04 -07002680 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2681
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002682 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002683 // Everytime we launch into AllApps, we reset the successful drop flag which
2684 // controls when it should hide/show the mini workspaces
2685 mAllAppsPagedView.resetSuccessfulDropFlag();
2686 }
Winson Chung20f71112011-04-06 14:22:04 -07002687 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002688 } else {
Winson Chungb26f3d62011-06-02 10:49:29 -07002689 // In Customize mode, shrink the workspaces to the top
Michael Jurkaaf91de02010-11-23 16:23:58 -08002690 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002691 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002692
2693 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002694 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002695 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002696 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2697 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002698 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002699 toView.setFastScaleX(a * scale + b * 1f);
2700 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002701 }
2702 });
Adam Cohen61033d32010-11-15 18:29:44 -08002703
Adam Cohenf16e5712011-01-13 13:31:45 -08002704 if (toAllApps) {
Winson Chung4e6a9762011-05-09 11:56:34 -07002705 toView.setVisibility(View.VISIBLE);
Michael Jurka2763be32011-02-24 11:19:57 -08002706 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002707 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002708 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002709 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2710 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002711 // don't need to invalidate because we do so above
2712 toView.setFastAlpha(a * 0f + b * 1f);
2713 }
2714 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002715 alphaAnim.start();
2716 }
2717
Michael Jurkaabded662011-03-04 12:06:57 -08002718 if (toView instanceof LauncherTransitionable) {
2719 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002720 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002721 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002722 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002723 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002724 // Prepare the position
2725 toView.setTranslationX(0.0f);
2726 toView.setTranslationY(0.0f);
2727 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002728 toView.bringToFront();
Adam Cohenf16e5712011-01-13 13:31:45 -08002729 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002730 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002731 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002732 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002733 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002734 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002735 // If we don't set the final scale values here, if this animation is cancelled
2736 // it will have the wrong scale value and subsequent cameraPan animations will
2737 // not fix that
2738 toView.setScaleX(1.0f);
2739 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002740 if (toView instanceof LauncherTransitionable) {
2741 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002742 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002743 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002744 });
2745
Chet Haaseb1254a62010-09-07 13:35:00 -07002746 AnimatorSet toolbarHideAnim = new AnimatorSet();
2747 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002748 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2749
2750 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002751 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002752
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002753 if (mStateAnimation != null) mStateAnimation.cancel();
2754 mStateAnimation = new AnimatorSet();
2755 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2756 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002757
2758 // Show the new toolbar buttons just as the main animation is ending
2759 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002760 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2761 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002762 } else {
2763 toView.setTranslationX(0.0f);
2764 toView.setTranslationY(0.0f);
2765 toView.setScaleX(1.0f);
2766 toView.setScaleY(1.0f);
2767 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002768 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002769 if (toView instanceof LauncherTransitionable) {
2770 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2771 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2772 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002773 hideAndShowToolbarButtons(toState, null, null);
2774 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002775 }
2776
2777 /**
2778 * Zoom the camera back into the workspace, hiding 'fromView'.
2779 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002780 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2781 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002782 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002783 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002784 Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002785 final boolean fromAllApps = (fromState == State.ALL_APPS)
2786 || (fromState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002787
2788 int duration = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002789 res.getInteger(R.integer.config_appsCustomizeZoomOutTime) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002790 res.getInteger(R.integer.config_customizeZoomOutTime);
2791
Michael Jurka742574b2011-02-02 23:51:01 -08002792 final float scaleFactor = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002793 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002794 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2795
Winson Chung785d2eb2011-04-14 16:08:02 -07002796 View tmpView;
2797 if (fromAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002798 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002799 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2800 } else {
2801 tmpView = mHomeCustomizationDrawer;
2802 }
2803 final View fromView = tmpView;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002804
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002805 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002806 mCustomizePagedView.endChoiceMode();
2807 mAllAppsPagedView.endChoiceMode();
2808 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002809
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002810 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002811
Michael Jurkad3ef3062010-11-23 16:23:58 -08002812 if (!springLoaded) {
2813 mWorkspace.unshrink(animated);
2814 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002815 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002816 if (mStateAnimation != null) mStateAnimation.cancel();
2817 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002818
Michael Jurka742574b2011-02-02 23:51:01 -08002819 final float oldScaleX = fromView.getScaleX();
2820 final float oldScaleY = fromView.getScaleY();
2821
2822 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2823 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002824 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2825 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002826 ((View)fromView.getParent()).fastInvalidate();
2827 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2828 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2829 }
2830 });
Michael Jurkaabded662011-03-04 12:06:57 -08002831 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002832 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002833 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002834 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2835 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002836 // don't need to invalidate because we do so above
2837 fromView.setFastAlpha(a * 1f + b * 0f);
2838 }
2839 });
Michael Jurkaabded662011-03-04 12:06:57 -08002840 if (fromView instanceof LauncherTransitionable) {
2841 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002842 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002843 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002844 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002845 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002846 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002847 if (fromView instanceof LauncherTransitionable) {
2848 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002849 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002850 }
2851 });
2852
Chet Haaseb1254a62010-09-07 13:35:00 -07002853 AnimatorSet toolbarHideAnim = new AnimatorSet();
2854 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002855 if (!springLoaded) {
2856 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2857 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002858
Adam Cohen61033d32010-11-15 18:29:44 -08002859 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002860
2861 // Show the new toolbar buttons at the very end of the whole animation
2862 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2863 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002864 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2865 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002866 } else {
2867 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002868 if (fromView instanceof LauncherTransitionable) {
2869 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2870 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2871 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002872 if (!springLoaded) {
2873 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2874 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002875 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002876 }
2877
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002878 void showWorkspace(boolean animated) {
2879 showWorkspace(animated, null);
2880 }
2881
2882 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002883 if (layout != null) {
2884 // always animated, but that's ok since we never specify a layout and
2885 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002886 mWorkspace.unshrink(layout);
2887 } else {
2888 mWorkspace.unshrink(animated);
2889 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002890 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002891 closeAllApps(animated);
2892 } else if (mState == State.CUSTOMIZE) {
2893 hideCustomizationDrawer(animated);
2894 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002895
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002896 // Change the state *after* we've called all the transition code
2897 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002898
Winson Chung5fb63472011-02-02 17:03:37 -08002899 // Resume the auto-advance of widgets
2900 mUserPresent = true;
2901 updateRunning();
2902
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002903 // send an accessibility event to announce the context change
2904 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002905 }
2906
Michael Jurkad3ef3062010-11-23 16:23:58 -08002907 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002908 // Enter spring loaded mode on a new layout
Michael Jurkad3ef3062010-11-23 16:23:58 -08002909 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002910
2911 if (mState == State.APPS_CUSTOMIZE) {
2912 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
2913 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
2914 } else {
2915 // Do nothing
2916 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002917 }
2918
2919 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002920 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2921 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_HIDDEN);
2922 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2923 mState = State.APPS_CUSTOMIZE;
2924 } else {
2925 // Do nothing
2926 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002927 }
2928
Winson Chung785d2eb2011-04-14 16:08:02 -07002929 void showAllApps(boolean animated) {
2930 if (mState != State.WORKSPACE) return;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002931 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002932 cameraZoomOut(State.ALL_APPS, animated, false);
2933 ((View) mAllAppsGrid).requestFocus();
2934
Winson Chung4c98d922011-05-31 16:50:48 -07002935 if (mDeleteZone != null) {
2936 mDeleteZone.setVisibility(View.GONE);
2937 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002938
2939 // Change the state *after* we've called all the transition code
2940 mState = State.ALL_APPS;
2941 } else {
Winson Chung785d2eb2011-04-14 16:08:02 -07002942 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
Winson Chung55b65502011-05-26 12:03:43 -07002943 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002944
2945 // Change the state *after* we've called all the transition code
2946 mState = State.APPS_CUSTOMIZE;
2947 }
2948
2949 // Pause the auto-advance of widgets until we are out of AllApps
2950 mUserPresent = false;
2951 updateRunning();
2952
2953 // Send an accessibility event to announce the context change
2954 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2955 }
2956
Joe Onoratob2061212009-11-24 16:13:54 -05002957 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002958 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002959 * - Home from workspace
2960 * - from center screen
2961 * - from other screens
2962 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002963 * - from center screen
2964 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002965 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002966 * - from center screen
2967 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002968 * - Launch app from workspace and quit
2969 * - with back
2970 * - with home
2971 * - Launch app from all apps and quit
2972 * - with back
2973 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002974 * - Go to a screen that's not the default, then all
2975 * apps, and launch and app, and go back
2976 * - with back
2977 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002978 * - On workspace, long press power and go back
2979 * - with back
2980 * - with home
2981 * - On all apps, long press power and go back
2982 * - with back
2983 * - with home
2984 * - On workspace, power off
2985 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002986 * - Launch an app and turn off the screen while in that app
2987 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002988 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002989 * - From all apps
2990 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002991 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2992 * - From all apps
2993 * - From the center workspace
2994 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002995 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002996 void closeAllApps(boolean animated) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002997 if (LauncherApplication.isScreenLarge()) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002998 if (mState == State.ALL_APPS) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002999 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07003000 cameraZoomIn(State.ALL_APPS, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003001
3002 // Set focus to the AllApps button
3003 findViewById(R.id.all_apps_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003004 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003005 } else {
Winson Chungb26f3d62011-06-02 10:49:29 -07003006 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003007 mWorkspace.setVisibility(View.VISIBLE);
3008 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
3009
3010 // Set focus to the AllApps button
3011 findViewById(R.id.all_apps_button).requestFocus();
3012 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003013 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003014 }
3015
Joe Onorato7c312c12009-08-13 21:36:53 -07003016 void lockAllApps() {
3017 // TODO
3018 }
3019
3020 void unlockAllApps() {
3021 // TODO
3022 }
3023
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003024 // Show the customization drawer (only exists in x-large configuration)
3025 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003026 if (mState != State.WORKSPACE) {
3027 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003028 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003029
Winson Chung20f71112011-04-06 14:22:04 -07003030 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003031
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003032 // Change the state *after* we've called all the transition code
3033 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003034
3035 // Pause the auto-advance of widgets until we are out of Customization drawer
3036 mUserPresent = false;
3037 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003038 }
3039
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003040 // Hide the customization drawer (only exists in x-large configuration)
3041 void hideCustomizationDrawer(boolean animated) {
Winson Chungb26f3d62011-06-02 10:49:29 -07003042 if (mState == State.CUSTOMIZE) {
Winson Chung20f71112011-04-06 14:22:04 -07003043 cameraZoomIn(State.CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003044
3045 // Set focus to the customize button
3046 findViewById(R.id.configure_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003047 }
3048 }
3049
Patrick Dubroy5f445422011-02-18 14:35:21 -08003050 /**
3051 * Add an item from all apps or customize onto the given workspace screen.
3052 * If layout is null, add to the current screen.
3053 */
3054 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003055 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3056 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003057 } else {
3058 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003059 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003060 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003061
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003062 void onWorkspaceClick(CellLayout layout) {
3063 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003064 }
3065
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003066 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003067 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003068 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003069 // Look for the toolbar icon specified in the activity meta-data
3070 Bundle metaData = packageManager.getActivityInfo(
3071 activityName, PackageManager.GET_META_DATA).metaData;
3072 if (metaData != null) {
3073 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3074 if (iconResId != 0) {
3075 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003076 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003077 }
3078 }
3079 } catch (NameNotFoundException e) {
3080 // Do nothing
3081 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003082 return null;
3083 }
3084
3085 // if successful in getting icon, return it; otherwise, set button to use default drawable
3086 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3087 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3088 TextView button = (TextView) findViewById(buttonId);
3089 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3090
3091 // If we were unable to find the icon via the meta-data, use a generic one
3092 if (toolbarIcon == null) {
3093 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3094 return null;
3095 } else {
3096 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3097 return toolbarIcon.getConstantState();
3098 }
3099 }
3100
3101 // if successful in getting icon, return it; otherwise, set button to use default drawable
3102 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3103 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3104 ImageView button = (ImageView) findViewById(buttonId);
3105 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3106
Michael Jurka0423dcf2010-10-05 14:56:18 -07003107 // If we were unable to find the icon via the meta-data, use a generic one
3108 if (toolbarIcon == null) {
3109 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003110 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003111 } else {
3112 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003113 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003114 }
3115 }
3116
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003117 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3118 TextView button = (TextView) findViewById(buttonId);
3119 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3120 }
3121
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003122 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003123 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003124 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003125 }
3126
Michael Jurka0423dcf2010-10-05 14:56:18 -07003127 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07003128 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
3129 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07003130
Winson Chung1cad91e2011-05-25 17:41:01 -07003131 final SearchManager searchManager =
3132 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3133 ComponentName activityName = searchManager.getGlobalSearchActivity();
3134 if (activityName != null) {
3135 // In landscape mode on the Phone UI, we only have enough space to show the magnifying
3136 // glass icon
3137 boolean iconLoaded = false;
3138 if (!LauncherApplication.isScreenLarge()) {
3139 // TODO-APPS_CUSTOMIZE: Remove when the QSB fixes itself?
3140 int orientation = getResources().getConfiguration().orientation;
3141 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
3142 searchButton.setImageResource(R.drawable.ic_generic_search);
3143 iconLoaded = true;
3144 sGlobalSearchIcon = null;
3145 }
3146 }
3147 if (!iconLoaded) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003148 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003149 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003150 }
Winson Chung1cad91e2011-05-25 17:41:01 -07003151 searchButton.setVisibility(View.VISIBLE);
3152 searchDivider.setVisibility(View.VISIBLE);
3153 } else {
3154 searchButton.setVisibility(View.GONE);
3155 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003156 }
3157 }
3158
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003159 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003160 updateButtonWithDrawable(R.id.search_button, d);
3161 }
3162
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003163 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07003164 final View searchDivider = findViewById(R.id.search_divider);
3165 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003166
Winson Chung1cad91e2011-05-25 17:41:01 -07003167 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3168 ComponentName activityName = intent.resolveActivity(getPackageManager());
3169 if (activityName != null) {
3170 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
3171 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3172 searchDivider.setVisibility(View.VISIBLE);
3173 voiceButton.setVisibility(View.VISIBLE);
3174 } else {
3175 searchDivider.setVisibility(View.GONE);
3176 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003177 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003178 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003179
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003180 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003181 updateButtonWithDrawable(R.id.voice_button, d);
3182 }
3183
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003184 /**
3185 * Sets the app market icon (shown when all apps is visible on x-large screens)
3186 */
3187 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003188 final View marketButton = findViewById(R.id.market_button);
3189 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3190 // Find the app market activity by resolving an intent.
3191 // (If multiple app markets are installed, it will return the ResolverActivity.)
3192 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003193 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003194 mAppMarketIntent = intent;
3195 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
3196 R.id.market_button, activityName, R.drawable.app_market_generic);
3197 marketButton.setVisibility(View.VISIBLE);
3198 } else {
3199 // We should hide and disable the view so that we don't try and restore the visibility
3200 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3201 marketButton.setVisibility(View.GONE);
3202 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003203 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003204 }
3205
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003206 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003207 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003208 }
3209
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003210 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211 * Displays the shortcut creation dialog and launches, if necessary, the
3212 * appropriate activity.
3213 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003214 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003215 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3216 DialogInterface.OnShowListener {
3217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003222
Winson Chung55b65502011-05-26 12:03:43 -07003223 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
3224 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003226 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003228 AlertDialog dialog = builder.create();
3229 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003230 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003231 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233 return dialog;
3234 }
3235
3236 public void onCancel(DialogInterface dialog) {
3237 mWaitingForResult = false;
3238 cleanup();
3239 }
3240
Romain Guycbb89e42009-06-08 15:52:54 -07003241 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07003242 mWaitingForResult = false;
3243 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003244 }
3245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003246 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003247 try {
3248 dismissDialog(DIALOG_CREATE_SHORTCUT);
3249 } catch (Exception e) {
3250 // An exception is thrown if the dialog is not visible, which is fine
3251 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 }
3253
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003254 /**
3255 * Handle the action clicked in the "Add to home" dialog.
3256 */
3257 public void onClick(DialogInterface dialog, int which) {
3258 Resources res = getResources();
3259 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003260
Winson Chung55b65502011-05-26 12:03:43 -07003261 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
3262 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003263 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003264 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003265 break;
3266 }
Winson Chung55b65502011-05-26 12:03:43 -07003267 case AddAdapter.ITEM_APPLICATION: {
3268 if (mAppsCustomizeTabHost != null) {
3269 mAppsCustomizeTabHost.selectAppsTab();
3270 }
3271 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003272 break;
3273 }
Winson Chung55b65502011-05-26 12:03:43 -07003274 case AddAdapter.ITEM_APPWIDGET: {
3275 if (mAppsCustomizeTabHost != null) {
3276 mAppsCustomizeTabHost.selectWidgetsTab();
3277 }
3278 showAllApps(true);
3279 break;
3280 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003281 case AddAdapter.ITEM_WALLPAPER: {
3282 startWallpaper();
3283 break;
3284 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003285 }
3286 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003287
3288 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003289 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003291 }
3292
3293 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003294 * Receives notifications when applications are added/removed.
3295 */
3296 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3297 @Override
3298 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003299 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003300 String reason = intent.getStringExtra("reason");
3301 if (!"homekey".equals(reason)) {
3302 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003303 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003304 animate = false;
3305 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003306 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003307 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003308 }
3309 }
3310
3311 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003312 * Receives notifications whenever the appwidgets are reset.
3313 */
3314 private class AppWidgetResetObserver extends ContentObserver {
3315 public AppWidgetResetObserver() {
3316 super(new Handler());
3317 }
3318
3319 @Override
3320 public void onChange(boolean selfChange) {
3321 onAppWidgetReset();
3322 }
3323 }
3324
3325 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003326 * If the activity is currently paused, signal that we need to re-run the loader
3327 * in onResume.
3328 *
3329 * This needs to be called from incoming places where resources might have been loaded
3330 * while we are paused. That is becaues the Configuration might be wrong
3331 * when we're not running, and if it comes back to what it was when we
3332 * were paused, we are not restarted.
3333 *
3334 * Implementation of the method from LauncherModel.Callbacks.
3335 *
3336 * @return true if we are currently paused. The caller might be able to
3337 * skip some work in that case since we will come back again.
3338 */
3339 public boolean setLoadOnResume() {
3340 if (mPaused) {
3341 Log.i(TAG, "setLoadOnResume");
3342 mOnResumeNeedsLoad = true;
3343 return true;
3344 } else {
3345 return false;
3346 }
3347 }
3348
3349 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003350 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003351 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003352 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003353 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003354 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003355 } else {
3356 return SCREEN_COUNT / 2;
3357 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003358 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003359
Winson Chunga12a2502010-12-20 14:41:35 -08003360 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003361 mAllAppsPagedView = view;
3362 }
3363
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364 /**
3365 * Refreshes the shortcuts shown on the workspace.
3366 *
3367 * Implementation of the method from LauncherModel.Callbacks.
3368 */
3369 public void startBinding() {
3370 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003371
3372 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003373 int count = workspace.getChildCount();
3374 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003375 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003376 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3377 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003378 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003379
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 if (DEBUG_USER_INTERFACE) {
3381 android.widget.Button finishButton = new android.widget.Button(this);
3382 finishButton.setText("Finish");
3383 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3384
3385 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3386 public void onClick(View v) {
3387 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003388 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003390 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003391
3392 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3393 // mDesktopItems -> AppWidgetInfo -> hostView).
3394 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003395 }
3396
3397 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003398 * Bind the items start-end from the list.
3399 *
3400 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003401 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003402 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3403
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003404 setLoadOnResume();
3405
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 final Workspace workspace = mWorkspace;
3407
3408 for (int i=start; i<end; i++) {
3409 final ItemInfo item = shortcuts.get(i);
3410 mDesktopItems.add(item);
3411 switch (item.itemType) {
3412 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3413 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003414 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003415 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3416 false);
3417 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003418 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003420 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003421 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3423 false);
3424 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003426 }
3427
Joe Onorato9c1289c2009-08-17 11:03:03 -04003428 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003429 }
3430
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 /**
3432 * Implementation of the method from LauncherModel.Callbacks.
3433 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003434 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003435 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003436 sFolders.clear();
3437 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003438 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003439
3440 /**
3441 * Add the views for a widget to the workspace.
3442 *
3443 * Implementation of the method from LauncherModel.Callbacks.
3444 */
3445 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003446 setLoadOnResume();
3447
Daniel Sandler843e8602010-06-07 14:59:01 -04003448 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3449 if (DEBUG_WIDGETS) {
3450 Log.d(TAG, "bindAppWidget: " + item);
3451 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003452 final Workspace workspace = mWorkspace;
3453
3454 final int appWidgetId = item.appWidgetId;
3455 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003456 if (DEBUG_WIDGETS) {
3457 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3458 }
3459
Joe Onorato9c1289c2009-08-17 11:03:03 -04003460 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3461
Joe Onorato9c1289c2009-08-17 11:03:03 -04003462 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3463 item.hostView.setTag(item);
3464
3465 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3466 item.cellY, item.spanX, item.spanY, false);
3467
Adam Cohended9f8d2010-11-03 13:25:16 -07003468 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3469
Joe Onorato9c1289c2009-08-17 11:03:03 -04003470 workspace.requestLayout();
3471
3472 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003473
3474 if (DEBUG_WIDGETS) {
3475 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3476 + (SystemClock.uptimeMillis()-start) + "ms");
3477 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003478 }
3479
3480 /**
3481 * Callback saying that there aren't any more items to bind.
3482 *
3483 * Implementation of the method from LauncherModel.Callbacks.
3484 */
3485 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003486 setLoadOnResume();
3487
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 if (mSavedState != null) {
3489 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003490 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 }
3492
Adam Cohendf2cc412011-04-27 16:56:57 -07003493 final long[] folders = mSavedState.getLongArray(RUNTIME_STATE_FOLDERS);
3494 if (folders != null) {
3495 for (long folderId : folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003496 final FolderInfo info = sFolders.get(folderId);
Adam Cohena9cf38f2011-05-02 15:36:58 -07003497 final FolderIcon folderIcon = (FolderIcon)
3498 mWorkspace.getViewForTag(info);
3499 if (folderIcon != null) {
3500 openFolder(folderIcon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 }
3502 }
3503 final Folder openFolder = mWorkspace.getOpenFolder();
3504 if (openFolder != null) {
3505 openFolder.requestFocus();
3506 }
3507 }
3508
Joe Onorato9c1289c2009-08-17 11:03:03 -04003509 mSavedState = null;
3510 }
3511
3512 if (mSavedInstanceState != null) {
3513 super.onRestoreInstanceState(mSavedInstanceState);
3514 mSavedInstanceState = null;
3515 }
3516
Winson Chung32bb5e52011-01-31 14:51:56 -08003517 // Workaround a bug that occurs when rotating the device while the customization mode is
3518 // open, we trigger a new layout on all the CellLayout children.
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003519 if (LauncherApplication.isScreenLarge() && (mState == State.CUSTOMIZE)) {
Winson Chung32bb5e52011-01-31 14:51:56 -08003520 final int childCount = mWorkspace.getChildCount();
3521 for (int i = 0; i < childCount; ++i) {
3522 mWorkspace.getChildAt(i).requestLayout();
3523 }
3524 }
3525
Joe Onorato9c1289c2009-08-17 11:03:03 -04003526 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003527
3528 // If we received the result of any pending adds while the loader was running (e.g. the
3529 // widget configuration forced an orientation change), process them now.
3530 for (int i = 0; i < sPendingAddList.size(); i++) {
3531 completeAdd(sPendingAddList.get(i));
3532 }
3533 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534 }
3535
3536 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003537 * Updates the icons on the launcher that are affected by changes to the package list
3538 * on the device.
3539 */
3540 private void updateIconsAffectedByPackageManagerChanges() {
3541 updateAppMarketIcon();
3542 updateGlobalSearchIcon();
3543 updateVoiceSearchIcon();
3544 }
3545
3546 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 * Add the icons for all apps.
3548 *
3549 * Implementation of the method from LauncherModel.Callbacks.
3550 */
3551 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003552 if (mAllAppsGrid != null) {
3553 mAllAppsGrid.setApps(apps);
3554 }
3555 if (mAppsCustomizeContent != null) {
3556 mAppsCustomizeContent.setApps(apps);
3557 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003558 if (mCustomizePagedView != null) {
3559 mCustomizePagedView.setApps(apps);
3560 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003561 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 }
3563
3564 /**
3565 * A package was installed.
3566 *
3567 * Implementation of the method from LauncherModel.Callbacks.
3568 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003569 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003570 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chung785d2eb2011-04-14 16:08:02 -07003572 if (mAllAppsGrid != null) {
3573 mAllAppsGrid.addApps(apps);
3574 }
3575 if (mAppsCustomizeContent != null) {
3576 mAppsCustomizeContent.addApps(apps);
3577 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003578 if (mCustomizePagedView != null) {
3579 mCustomizePagedView.addApps(apps);
3580 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003581 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 }
3583
3584 /**
3585 * A package was updated.
3586 *
3587 * Implementation of the method from LauncherModel.Callbacks.
3588 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003589 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003590 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003592 if (mWorkspace != null) {
3593 mWorkspace.updateShortcuts(apps);
3594 }
3595 if (mAllAppsGrid != null) {
3596 mAllAppsGrid.updateApps(apps);
3597 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003598 if (mAppsCustomizeContent != null) {
3599 mAppsCustomizeContent.updateApps(apps);
3600 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003601 if (mCustomizePagedView != null) {
3602 mCustomizePagedView.updateApps(apps);
3603 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003604 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 }
3606
3607 /**
3608 * A package was uninstalled.
3609 *
3610 * Implementation of the method from LauncherModel.Callbacks.
3611 */
Joe Onorato36115782010-06-17 13:28:48 -04003612 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003614 if (permanent) {
3615 mWorkspace.removeItems(apps);
3616 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003617 if (mAllAppsGrid != null) {
3618 mAllAppsGrid.removeApps(apps);
3619 }
3620 if (mAppsCustomizeContent != null) {
3621 mAppsCustomizeContent.removeApps(apps);
3622 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003623 if (mCustomizePagedView != null) {
3624 mCustomizePagedView.removeApps(apps);
3625 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003626 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003627 }
Joe Onoratobe386092009-11-17 17:32:16 -08003628
3629 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003630 * A number of packages were updated.
3631 */
3632 public void bindPackagesUpdated() {
3633 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003634 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003635 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003636 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003637 if (mAppsCustomizeContent != null) {
3638 mAppsCustomizeContent.onPackagesUpdated();
3639 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003640 }
3641
Winson Chung400438b2011-01-16 17:53:48 -08003642 private int mapConfigurationOriActivityInfoOri(int configOri) {
3643 final Display d = getWindowManager().getDefaultDisplay();
3644 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3645 switch (d.getRotation()) {
3646 case Surface.ROTATION_0:
3647 case Surface.ROTATION_180:
3648 // We are currently in the same basic orientation as the natural orientation
3649 naturalOri = configOri;
3650 break;
3651 case Surface.ROTATION_90:
3652 case Surface.ROTATION_270:
3653 // We are currently in the other basic orientation to the natural orientation
3654 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3655 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3656 break;
3657 }
3658
3659 int[] oriMap = {
3660 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3661 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3662 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3663 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3664 };
3665 // Since the map starts at portrait, we need to offset if this device's natural orientation
3666 // is landscape.
3667 int indexOffset = 0;
3668 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3669 indexOffset = 1;
3670 }
3671 return oriMap[(d.getRotation() + indexOffset) % 4];
3672 }
3673 public void lockScreenOrientation() {
3674 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3675 .getConfiguration().orientation));
3676 }
3677 public void unlockScreenOrientation() {
3678 mHandler.postDelayed(new Runnable() {
3679 public void run() {
3680 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3681 }
3682 }, mRestoreScreenOrientationDelay);
3683 }
3684
Winson Chung80baf5a2010-08-09 16:03:15 -07003685 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003686 * Prints out out state for debugging.
3687 */
3688 public void dumpState() {
3689 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003690 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003691 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3692 Log.d(TAG, "mRestoring=" + mRestoring);
3693 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3694 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3695 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003696 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003697 mModel.dumpState();
Winson Chung785d2eb2011-04-14 16:08:02 -07003698 if (mAllAppsGrid != null) {
3699 mAllAppsGrid.dumpState();
3700 }
3701 if (mAppsCustomizeContent != null) {
3702 mAppsCustomizeContent.dumpState();
3703 }
Joe Onoratobe386092009-11-17 17:32:16 -08003704 Log.d(TAG, "END launcher2 dump state");
3705 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003706}
Michael Jurka2763be32011-02-24 11:19:57 -08003707
Michael Jurkaabded662011-03-04 12:06:57 -08003708interface LauncherTransitionable {
3709 void onLauncherTransitionStart(Animator animation);
3710 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003711}