blob: c3d0eeac2b10bbb6d59b63776edb4bed9e9b176b [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
Adam Cohendf2cc412011-04-27 16:56:57 -070020import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080029import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070031import android.animation.ObjectAnimator;
32import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080034import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070035import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.app.Dialog;
38import android.app.SearchManager;
39import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070040import android.appwidget.AppWidgetManager;
41import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080043import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070044import android.content.ClipData;
45import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040046import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080047import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
49import android.content.DialogInterface;
50import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080051import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080052import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070053import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080055import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080056import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070058import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040059import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080062import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080064import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070065import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040066import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070067import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020069import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080070import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070071import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040072import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080073import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070074import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080075import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.Selection;
77import android.text.SpannableStringBuilder;
78import android.text.TextUtils;
79import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070080import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080081import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080082import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.KeyEvent;
84import android.view.LayoutInflater;
85import android.view.Menu;
86import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070087import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080088import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.View;
90import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070091import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080092import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080093import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080094import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080095import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070097import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070099import android.widget.ImageView;
100import android.widget.LinearLayout;
101import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700102import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -0700103import android.widget.TextView;
104import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700105
Adam Cohendf2cc412011-04-27 16:56:57 -0700106import com.android.common.Search;
107import com.android.launcher.R;
108import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -0800109
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,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800171 CUSTOMIZE_SPRING_LOADED, ALL_APPS_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 Chung785d2eb2011-04-14 16:08:02 -0700953 marketButton.setOnClickListener(new OnClickListener() {
954 public void onClick(View v) {
955 onClickAppMarketButton(v);
956 }
957 });
Winson Chung4c98d922011-05-31 16:50:48 -0700958 } else {
959 // Get the search/delete bar
960 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
961
962 // Setup AppsCustomize
963 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
964 findViewById(R.id.apps_customize_pane);
965 mAppsCustomizeContent = (AppsCustomizePagedView)
966 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
967 mAppsCustomizeContent.setup(this, dragController);
968
969 // Setup AppsCustomize button
970 mHandleView = (HandleView) mDragLayer.findViewById(R.id.all_apps_button);
971 mHandleView.setLauncher(this);
972 mHandleView.setOnLongClickListener(this);
973 mHandleView.setOnClickListener(new OnClickListener() {
974 public void onClick(View v) {
975 onClickAllAppsButton(v);
976 }
977 });
978
979 // Setup Hotseat
980 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
981 hotseatLeft.setContentDescription(mHotseatLabels[0]);
982 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
983 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
984 hotseatRight.setContentDescription(mHotseatLabels[1]);
985 hotseatRight.setImageDrawable(mHotseatIcons[1]);
986
987 View.OnKeyListener listener = new IndicatorKeyEventListener();
988 mPreviousView = (ImageView) mDragLayer.findViewById(R.id.previous_screen);
989 mPreviousView.setOnKeyListener(listener);
990 mNextView = (ImageView) mDragLayer.findViewById(R.id.next_screen);
991 mNextView.setOnKeyListener(listener);
992
993 Drawable previous = mPreviousView.getDrawable();
994 Drawable next = mNextView.getDrawable();
995 mWorkspace.setIndicators(previous, next);
996
997 mPreviousView.setHapticFeedbackEnabled(false);
998 mPreviousView.setOnLongClickListener(this);
999 mNextView.setHapticFeedbackEnabled(false);
1000 mNextView.setOnLongClickListener(this);
Adam Cohencdc30d52010-12-01 15:09:47 -08001001 }
1002
Winson Chung4c98d922011-05-31 16:50:48 -07001003 // Setup keylistener for button cluster
Winson Chung97d85d22011-04-13 11:27:36 -07001004 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
Winson Chung4e6a9762011-05-09 11:56:34 -07001005 View.OnKeyListener listener = null;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001006 if (LauncherApplication.isScreenLarge()) {
Winson Chung4e6a9762011-05-09 11:56:34 -07001007 // For tablets, AllApps lives in the button bar at the top
1008 listener = new ButtonBarKeyEventListener();
1009 } else {
1010 // For phones, AppsCustomize lives in the "dock" at the bottom
1011 listener = new DockKeyEventListener();
1012 }
Winson Chung97d85d22011-04-13 11:27:36 -07001013 int buttonCount = mButtonCluster.getChildCount();
1014 for (int i = 0; i < buttonCount; ++i) {
1015 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
1016 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001017
Winson Chung4c98d922011-05-31 16:50:48 -07001018 // Setup the drag controller (the drop targets have to be added in reverse order)
1019 dragController.setDragScoller(mWorkspace);
1020 dragController.setScrollView(mDragLayer);
1021 dragController.setMoveTarget(mWorkspace);
1022 dragController.addDropTarget(mWorkspace);
1023 if (mDeleteZone != null) {
1024 dragController.addDragListener(mDeleteZone);
1025 dragController.addDropTarget(mDeleteZone);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001026 }
Winson Chung4c98d922011-05-31 16:50:48 -07001027 if (mSearchDeleteBar != null) {
1028 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001029 }
Winson Chung4c98d922011-05-31 16:50:48 -07001030 if (allAppsDeleteZone != null) {
1031 dragController.addDragListener(allAppsDeleteZone);
1032 dragController.addDropTarget(allAppsDeleteZone);
1033 }
1034 if (allAppsInfoTarget != null) {
1035 dragController.addDragListener(allAppsInfoTarget);
1036 dragController.addDropTarget(allAppsInfoTarget);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
1039
Romain Guy8a73c512009-11-09 19:19:59 -08001040 @SuppressWarnings({"UnusedDeclaration"})
1041 public void previousScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001042 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001043 mWorkspace.scrollLeft();
1044 }
Romain Guy8a73c512009-11-09 19:19:59 -08001045 }
1046
1047 @SuppressWarnings({"UnusedDeclaration"})
1048 public void nextScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001049 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001050 mWorkspace.scrollRight();
1051 }
Romain Guy8a73c512009-11-09 19:19:59 -08001052 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001053
1054 @SuppressWarnings({"UnusedDeclaration"})
1055 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001056 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001057
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001058 int index = -1;
1059 if (v.getId() == R.id.hotseat_left) {
1060 index = 0;
1061 } else if (v.getId() == R.id.hotseat_right) {
1062 index = 1;
1063 }
1064
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001065 // reload these every tap; you never know when they might change
1066 loadHotseats();
1067 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001068 startActivitySafely(
1069 mHotseats[index],
1070 "hotseat"
1071 );
1072 }
1073 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 /**
1076 * Creates a view representing a shortcut.
1077 *
1078 * @param info The data structure describing the shortcut.
1079 *
1080 * @return A View inflated from R.layout.application.
1081 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001082 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001084 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 }
1086
1087 /**
1088 * Creates a view representing a shortcut inflated from the specified resource.
1089 *
1090 * @param layoutResId The id of the XML layout used to create the shortcut.
1091 * @param parent The group the shortcut belongs to.
1092 * @param info The data structure describing the shortcut.
1093 *
1094 * @return A View inflated from layoutResId.
1095 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001096 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001097 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1098 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 return favorite;
1101 }
1102
1103 /**
1104 * Add an application shortcut to the workspace.
1105 *
1106 * @param data The intent describing the application.
1107 * @param cellInfo The position on screen where to create the shortcut.
1108 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001109 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001110 int intersectCellX, int intersectCellY) {
1111 final int[] cellXY = mTmpAddItemCellCoordinates;
1112 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1113
1114 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1115 showOutOfSpaceMessage();
1116 return;
1117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001119 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001120
Romain Guy73b979d2009-06-09 12:57:21 -07001121 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001122 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001124 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001125 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1126 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001127 } else {
1128 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 }
1130 }
Romain Guycbb89e42009-06-08 15:52:54 -07001131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 /**
1133 * Add a shortcut to the workspace.
1134 *
1135 * @param data The intent describing the shortcut.
1136 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001138 private void completeAddShortcut(Intent data, int screen,
1139 int intersectCellX, int intersectCellY) {
1140 final int[] cellXY = mTmpAddItemCellCoordinates;
1141 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001142
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001143 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001144 boolean foundCellSpan = false;
1145 if (touchXY != null) {
1146 // when dragging and dropping, just find the closest free spot
1147 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1148 int[] result = screenLayout.findNearestVacantArea(
1149 touchXY[0], touchXY[1], 1, 1, cellXY);
1150 foundCellSpan = (result != null);
1151 } else {
1152 foundCellSpan = layout.findCellForSpanThatIntersects(
1153 cellXY, 1, 1, intersectCellX, intersectCellY);
1154 }
1155
1156 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001157 showOutOfSpaceMessage();
1158 return;
1159 }
1160
1161 final ShortcutInfo info = mModel.addShortcut(
1162 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163
1164 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001166 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 }
1168 }
1169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001171 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001173 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001174 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 */
Winson Chungf7640c82011-02-28 13:47:29 -08001176 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001177 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001178
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001179 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001180 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1181 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001184 // We have saved the position to which the widget was dragged-- this really only matters
1185 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001186 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001187
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001188 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 boolean foundCellSpan = false;
1190 if (touchXY != null) {
1191 // when dragging and dropping, just find the closest free spot
1192 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1193 int[] result = screenLayout.findNearestVacantArea(
1194 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001195 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001196 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001197 // if we long pressed on an empty cell to bring up a menu,
1198 // make sure we intersect the empty cell
1199 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1200 // findCellForSpanThatIntersects will just ignore them
1201 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1202 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001203 }
1204
Michael Jurka0280c3b2010-09-17 15:00:07 -07001205 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001206 if (appWidgetId != -1) {
1207 // Deleting an app widget ID is a void call but writes to disk before returning
1208 // to the caller...
1209 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1210 new Thread("deleteAppWidgetId") {
1211 public void run() {
1212 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1213 }
1214 }.start();
1215 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001217 return;
1218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001220 // Build Launcher-specific widget info and save to database
1221 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 launcherInfo.spanX = spanXY[0];
1223 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 LauncherModel.addItemToDatabase(this, launcherInfo,
1226 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001227 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228
1229 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001230 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001233 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001234
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001235 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001237
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001240
1241 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243 }
Romain Guycbb89e42009-06-08 15:52:54 -07001244
Adam Cohended9f8d2010-11-03 13:25:16 -07001245 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1246 @Override
1247 public void onReceive(Context context, Intent intent) {
1248 final String action = intent.getAction();
1249 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1250 mUserPresent = false;
1251 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001252
1253 // Reset AllApps to it's initial state
1254 if (mAllAppsGrid != null) {
1255 mAllAppsGrid.reset();
1256 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001257 if (mAppsCustomizeContent != null) {
1258 mAppsCustomizeContent.reset();
1259 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001260 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1261 mUserPresent = true;
1262 updateRunning();
1263 }
1264 }
1265 };
1266
1267 @Override
1268 public void onAttachedToWindow() {
1269 super.onAttachedToWindow();
1270
1271 // Listen for broadcasts related to user-presence
1272 final IntentFilter filter = new IntentFilter();
1273 filter.addAction(Intent.ACTION_SCREEN_OFF);
1274 filter.addAction(Intent.ACTION_USER_PRESENT);
1275 registerReceiver(mReceiver, filter);
1276
Adam Cohend113e0c2010-11-11 10:48:05 -08001277 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001278 mVisible = true;
1279 }
1280
1281 @Override
1282 public void onDetachedFromWindow() {
1283 super.onDetachedFromWindow();
1284 mVisible = false;
1285
Adam Cohend113e0c2010-11-11 10:48:05 -08001286 if (mAttached) {
1287 unregisterReceiver(mReceiver);
1288 mAttached = false;
1289 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001290 updateRunning();
1291 }
1292
1293 public void onWindowVisibilityChanged(int visibility) {
1294 mVisible = visibility == View.VISIBLE;
1295 updateRunning();
1296 }
1297
1298 private void sendAdvanceMessage(long delay) {
1299 mHandler.removeMessages(ADVANCE_MSG);
1300 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1301 mHandler.sendMessageDelayed(msg, delay);
1302 mAutoAdvanceSentTime = System.currentTimeMillis();
1303 }
1304
1305 private void updateRunning() {
1306 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1307 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1308 mAutoAdvanceRunning = autoAdvanceRunning;
1309 if (autoAdvanceRunning) {
1310 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1311 sendAdvanceMessage(delay);
1312 } else {
1313 if (!mWidgetsToAdvance.isEmpty()) {
1314 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1315 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1316 }
1317 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001318 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001319 }
1320 }
1321 }
1322
1323 private final Handler mHandler = new Handler() {
1324 @Override
1325 public void handleMessage(Message msg) {
1326 if (msg.what == ADVANCE_MSG) {
1327 int i = 0;
1328 for (View key: mWidgetsToAdvance.keySet()) {
1329 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1330 final int delay = mAdvanceStagger * i;
1331 if (v instanceof Advanceable) {
1332 postDelayed(new Runnable() {
1333 public void run() {
1334 ((Advanceable) v).advance();
1335 }
1336 }, delay);
1337 }
1338 i++;
1339 }
1340 sendAdvanceMessage(mAdvanceInterval);
1341 }
1342 }
1343 };
1344
1345 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1346 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1347 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1348 if (v instanceof Advanceable) {
1349 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001350 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001351 updateRunning();
1352 }
1353 }
1354
1355 void removeWidgetToAutoAdvance(View hostView) {
1356 if (mWidgetsToAdvance.containsKey(hostView)) {
1357 mWidgetsToAdvance.remove(hostView);
1358 updateRunning();
1359 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001360 }
1361
Joe Onorato9c1289c2009-08-17 11:03:03 -04001362 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1363 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001364 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365 launcherInfo.hostView = null;
1366 }
1367
Adam Cohended9f8d2010-11-03 13:25:16 -07001368 void showOutOfSpaceMessage() {
1369 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1370 }
1371
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001372 public LauncherAppWidgetHost getAppWidgetHost() {
1373 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 }
Romain Guycbb89e42009-06-08 15:52:54 -07001375
Winson Chunga9abd0e2010-10-27 17:18:37 -07001376 public LauncherModel getModel() {
1377 return mModel;
1378 }
1379
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001380 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001381 getWindow().closeAllPanels();
1382
1383 try {
1384 dismissDialog(DIALOG_CREATE_SHORTCUT);
1385 // Unlock the workspace if the dialog was showing
1386 } catch (Exception e) {
1387 // An exception is thrown if the dialog is not visible, which is fine
1388 }
1389
1390 try {
1391 dismissDialog(DIALOG_RENAME_FOLDER);
1392 // Unlock the workspace if the dialog was showing
1393 } catch (Exception e) {
1394 // An exception is thrown if the dialog is not visible, which is fine
1395 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001396
1397 // Whatever we were doing is hereby canceled.
1398 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001399 }
1400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 @Override
1402 protected void onNewIntent(Intent intent) {
1403 super.onNewIntent(intent);
1404
1405 // Close the menu
1406 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001407 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001408 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001409
Joe Onorato14f122b2009-11-19 14:06:36 -08001410 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1411 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001412
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001413 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001414 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001415 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001416 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001417 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001418 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001419 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001420
Joe Onorato3a8820b2009-11-10 15:06:42 -08001421 final View v = getWindow().peekDecorView();
1422 if (v != null && v.getWindowToken() != null) {
1423 InputMethodManager imm = (InputMethodManager)getSystemService(
1424 INPUT_METHOD_SERVICE);
1425 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001427
1428 // Reset AllApps to it's initial state
1429 if (mAllAppsGrid != null) {
1430 mAllAppsGrid.reset();
1431 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001432 if (mAppsCustomizeContent != null) {
1433 mAppsCustomizeContent.reset();
1434 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436 }
1437
1438 @Override
1439 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1440 // Do not call super here
1441 mSavedInstanceState = savedInstanceState;
1442 }
1443
1444 @Override
1445 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001446 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447
1448 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1449 if (folders.size() > 0) {
1450 final int count = folders.size();
1451 long[] ids = new long[count];
1452 for (int i = 0; i < count; i++) {
1453 final FolderInfo info = folders.get(i).getInfo();
1454 ids[i] = info.id;
1455 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001456 outState.putLongArray(RUNTIME_STATE_FOLDERS, ids);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 } else {
1458 super.onSaveInstanceState(outState);
1459 }
1460
Michael Jurka883f55b2010-10-21 15:47:14 -07001461 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462
Michael Jurka0280c3b2010-09-17 15:00:07 -07001463 if (mAddScreen > -1 && mWaitingForResult) {
1464 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1465 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1466 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468
1469 if (mFolderInfo != null && mWaitingForResult) {
1470 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1471 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1472 }
Michael Jurka946ad472010-07-09 18:05:18 -07001473
Winson Chung43b119d2010-12-06 16:45:05 -08001474 // Save the current AllApps drawer tab
1475 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1476 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1477 String currentTabTag = tabhost.getCurrentTabTag();
1478 if (currentTabTag != null) {
1479 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001480 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001481 }
1482 }
Winson Chung43b119d2010-12-06 16:45:05 -08001483 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001484 if (mHomeCustomizationDrawer != null) {
1485 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1486 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001487 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001488 }
1489 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001490 // Save the current AppsCustomize tab
1491 if (mAppsCustomizeTabHost != null) {
1492 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1493 if (currentTabTag != null) {
1494 outState.putString("apps_customize_currentTab", currentTabTag);
1495 }
1496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
1498
1499 @Override
1500 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001504 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001506 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001508 mAppWidgetHost = null;
1509
1510 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
1512 TextKeyListener.getInstance().release();
1513
Joe Onorato9c1289c2009-08-17 11:03:03 -04001514 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515
Joe Onorato9c1289c2009-08-17 11:03:03 -04001516 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001517
1518 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001519
Patrick Dubroyab962b72010-07-26 12:10:10 -07001520 // Some launcher layouts don't have a previous and next view
1521 if (mPreviousView != null) {
1522 dismissPreview(mPreviousView);
1523 }
1524 if (mNextView != null) {
1525 dismissPreview(mNextView);
1526 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001527
1528 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001529
1530 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1531 mWorkspace.removeAllViews();
1532 mWorkspace = null;
1533 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001534
1535 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
1537
Adam Cohena9cf38f2011-05-02 15:36:58 -07001538 public DragController getDragController() {
1539 return mDragController;
1540 }
1541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 @Override
1543 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001544 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 super.startActivityForResult(intent, requestCode);
1546 }
1547
1548 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001549 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001551
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001552 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001553
Karl Rosaen138a0412009-04-23 19:00:21 -07001554 if (initialQuery == null) {
1555 // Use any text typed in the launcher as the initial query
1556 initialQuery = getTypedText();
1557 clearTypedText();
1558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 if (appSearchData == null) {
1560 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001561 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Karl Rosaen138a0412009-04-23 19:00:21 -07001564 final SearchManager searchManager =
1565 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001566 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001567 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
1569
1570 @Override
1571 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001572 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001573 return false;
1574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575
1576 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1579 .setIcon(android.R.drawable.ic_menu_add)
1580 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001581 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1582 .setIcon(android.R.drawable.ic_menu_manage)
1583 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001584 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 .setIcon(android.R.drawable.ic_menu_gallery)
1586 .setAlphabeticShortcut('W');
1587 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1588 .setIcon(android.R.drawable.ic_search_category_default)
1589 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1590 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1591 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1592 .setAlphabeticShortcut('N');
1593
1594 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001595 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1596 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597
1598 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1599 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1600 .setIntent(settings);
1601
1602 return true;
1603 }
1604
1605 @Override
1606 public boolean onPrepareOptionsMenu(Menu menu) {
1607 super.onPrepareOptionsMenu(menu);
1608
Winson Chung785d2eb2011-04-14 16:08:02 -07001609 if (mAllAppsGrid != null) {
1610 // If all apps is animating, don't show the menu, because we don't know
1611 // which one to show.
1612 if (mAllAppsGrid.isAnimating()) {
1613 return false;
1614 }
1615
1616 // Only show the add and wallpaper options when we're not in all apps.
1617 boolean visible = !mAllAppsGrid.isVisible();
1618 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1619 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1620
1621 // Disable add if the workspace is full.
1622 if (visible) {
1623 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1624 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
1625 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001626 }
1627
Winson Chung785d2eb2011-04-14 16:08:02 -07001628 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1629 // related code?
1630 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631
1632 return true;
1633 }
1634
1635 @Override
1636 public boolean onOptionsItemSelected(MenuItem item) {
1637 switch (item.getItemId()) {
1638 case MENU_ADD:
1639 addItems();
1640 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001641 case MENU_MANAGE_APPS:
1642 manageApps();
1643 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 case MENU_WALLPAPER_SETTINGS:
1645 startWallpaper();
1646 return true;
1647 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001648 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 return true;
1650 case MENU_NOTIFICATIONS:
1651 showNotifications();
1652 return true;
1653 }
1654
1655 return super.onOptionsItemSelected(item);
1656 }
Romain Guycbb89e42009-06-08 15:52:54 -07001657
Karl Rosaen138a0412009-04-23 19:00:21 -07001658 /**
1659 * Indicates that we want global search for this activity by setting the globalSearch
1660 * argument for {@link #startSearch} to true.
1661 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001663 @Override
1664 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001665 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001666 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001667 }
1668
Joe Onorato9c1289c2009-08-17 11:03:03 -04001669 public boolean isWorkspaceLocked() {
1670 return mWorkspaceLoading || mWaitingForResult;
1671 }
1672
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001673 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1674 private boolean isPreviewVisible() {
1675 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1676 (mNextView != null && mNextView.getTag() != null);
1677 }
1678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 private void addItems() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001680 if (LauncherApplication.isScreenLarge()) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001681 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001682 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001683 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001684 }
1685 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001686 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001687 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 }
1690
Michael Jurka0280c3b2010-09-17 15:00:07 -07001691 private void resetAddInfo() {
1692 mAddScreen = -1;
1693 mAddIntersectCellX = -1;
1694 mAddIntersectCellY = -1;
1695 mAddDropPosition = null;
1696 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001697
Winson Chung55cef262010-10-28 14:14:18 -07001698 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001699 resetAddInfo();
1700 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001701 mAddDropPosition = position;
1702
Michael Jurkaaf442092010-06-10 17:01:57 -07001703 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001704 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1705 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001706 }
1707
Winson Chung7ad01412010-09-27 11:33:03 -07001708 private void manageApps() {
1709 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1710 }
1711
Michael Jurkaaf442092010-06-10 17:01:57 -07001712 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001713 // TODO: catch bad widget exception when sent
1714 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001715 // TODO: Is this log message meaningful?
1716 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001717 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001718 }
1719
Winson Chung55cef262010-10-28 14:14:18 -07001720 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001721 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722
Bjorn Bringert7984c942009-12-09 15:38:25 +00001723 if (appWidget.configure != null) {
1724 // Launch over to configure widget, if needed
1725 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1726 intent.setComponent(appWidget.configure);
1727 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001728 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001729 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1730 intent.putExtra(
1731 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1732 info.mimeType);
1733
1734 final String mimeType = info.mimeType;
1735 final ClipData clipData = (ClipData) info.configurationData;
1736 final ClipDescription clipDesc = clipData.getDescription();
1737 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1738 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001739 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001740 final CharSequence stringData = item.getText();
1741 final Uri uriData = item.getUri();
1742 final Intent intentData = item.getIntent();
1743 final String key =
1744 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1745 if (uriData != null) {
1746 intent.putExtra(key, uriData);
1747 } else if (intentData != null) {
1748 intent.putExtra(key, intentData);
1749 } else if (stringData != null) {
1750 intent.putExtra(key, stringData);
1751 }
1752 break;
1753 }
1754 }
1755 }
Winson Chung55cef262010-10-28 14:14:18 -07001756 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001757
Romain Guy8e633c52010-03-04 12:51:36 -08001758 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001760 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001761 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 }
1763 }
Romain Guycbb89e42009-06-08 15:52:54 -07001764
Michael Jurka0280c3b2010-09-17 15:00:07 -07001765 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1766 resetAddInfo();
1767 mAddScreen = screen;
1768 mAddDropPosition = position;
1769
1770 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1771 createShortcutIntent.setComponent(componentName);
1772 processShortcut(createShortcutIntent);
1773 }
1774
Joe Onoratodeb98af2010-02-19 14:59:39 -08001775 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001776 // Handle case where user selected "Applications"
1777 String applicationName = getResources().getString(R.string.group_applications);
1778 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001779
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001780 if (applicationName != null && applicationName.equals(shortcutName)) {
1781 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1782 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001783
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001784 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1785 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001786 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001787 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001788 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001789 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001790 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
1792
Winson Chung24ab2f12010-09-16 14:10:47 -07001793 void processWallpaper(Intent intent) {
1794 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1795 }
1796
Adam Cohendf035382011-04-11 17:22:04 -07001797 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001798 FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 folderInfo.title = getText(R.string.folder_name);
1800
Michael Jurka0280c3b2010-09-17 15:00:07 -07001801 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1802 final int[] cellXY = mTmpAddItemCellCoordinates;
1803 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1804 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001805 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807
1808 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001809 LauncherModel.addItemToDatabase(this, folderInfo,
1810 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001811 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001812 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813
1814 // Create the view
1815 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001816 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1817 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001818 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001819 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 }
Romain Guycbb89e42009-06-08 15:52:54 -07001821
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001823 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001824 }
1825
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 private void showNotifications() {
1827 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1828 if (statusBar != null) {
1829 statusBar.expand();
1830 }
1831 }
1832
1833 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001834 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001836 Intent chooser = Intent.createChooser(pickWallpaper,
1837 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001838 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1839 // Removed in Eclair MR1
1840// WallpaperManager wm = (WallpaperManager)
1841// getSystemService(Context.WALLPAPER_SERVICE);
1842// WallpaperInfo wi = wm.getWallpaperInfo();
1843// if (wi != null && wi.getSettingsActivity() != null) {
1844// LabeledIntent li = new LabeledIntent(getPackageName(),
1845// R.string.configure_wallpaper, 0);
1846// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1847// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1848// }
Mike Clerona0618e42009-10-22 13:55:21 -07001849 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 }
1851
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001852 /**
1853 * Registers various content observers. The current implementation registers
1854 * only a favorites observer to keep track of the favorites applications.
1855 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001856 private void registerContentObservers() {
1857 ContentResolver resolver = getContentResolver();
1858 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1859 true, mWidgetObserver);
1860 }
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 @Override
1863 public boolean dispatchKeyEvent(KeyEvent event) {
1864 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1865 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001867 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001868 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001869 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001870 dumpState();
1871 return true;
1872 }
1873 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001874 }
1875 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1876 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001877 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 return true;
1879 }
1880 }
1881
1882 return super.dispatchKeyEvent(event);
1883 }
1884
Joe Onorato88ec0992009-11-19 13:16:06 -08001885 @Override
1886 public void onBackPressed() {
Winson Chung785d2eb2011-04-14 16:08:02 -07001887 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001888 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001889 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001890 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001891 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001892 dismissPreview(mPreviousView);
1893 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001894 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001895 mWorkspace.exitWidgetResizeMode();
1896
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001897 // Back button is a no-op here, but give at least some feedback for the button press
1898 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001899 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001900 }
1901
Adam Cohen2801caf2011-05-13 20:57:39 -07001902 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 Folder folder = mWorkspace.getOpenFolder();
1904 if (folder != null) {
1905 closeFolder(folder);
Adam Cohen2801caf2011-05-13 20:57:39 -07001906 mDragLayer.setCurrentFolder(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
1908 }
1909
1910 void closeFolder(Folder folder) {
1911 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001912
Michael Jurka8c920dd2011-01-20 14:16:56 -08001913 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001915 CellLayout cl = (CellLayout) parent;
Adam Cohen2801caf2011-05-13 20:57:39 -07001916 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1917 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001918 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001920
1921 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 folder.onClose();
1923 }
1924
1925 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001926 * Re-listen when widgets are reset.
1927 */
1928 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001929 if (mAppWidgetHost != null) {
1930 mAppWidgetHost.startListening();
1931 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001932 }
1933
1934 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001935 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1936 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001938 private void unbindDesktopItems() {
1939 for (ItemInfo item: mDesktopItems) {
1940 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001942 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001944
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 /**
1946 * Launches the intent referred by the clicked shortcut.
1947 *
1948 * @param v The view representing the clicked shortcut.
1949 */
1950 public void onClick(View v) {
1951 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001952 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001954 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001955 int[] pos = new int[2];
1956 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001957 intent.setSourceBounds(new Rect(pos[0], pos[1],
1958 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001959 boolean success = startActivitySafely(intent, tag);
1960
1961 if (success && v instanceof BubbleTextView) {
1962 mWaitingForResume = (BubbleTextView) v;
1963 mWaitingForResume.setStayPressed(true);
1964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001966 if (v instanceof FolderIcon) {
1967 FolderIcon fi = (FolderIcon) v;
1968 handleFolderClick(fi);
1969 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001970 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001971 if (mState == State.ALL_APPS) {
1972 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001973 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001974 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
1977 }
1978
Michael Jurka0e260592010-06-30 17:07:39 -07001979 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001980 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001981 // clicking anywhere on the workspace causes the customization drawer to slide down
1982 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001983 return false;
1984 }
1985
Michael Jurkaaf442092010-06-10 17:01:57 -07001986 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001987 * Event handler for the search button
1988 *
1989 * @param v The view that was clicked.
1990 */
1991 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001992 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001993 // Use a custom animation for launching search
1994 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001995 }
1996
1997 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001998 * Event handler for the voice button
1999 *
2000 * @param v The view that was clicked.
2001 */
2002 public void onClickVoiceButton(View v) {
2003 startVoiceSearch();
2004 }
2005
2006 private void startVoiceSearch() {
2007 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2008 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2009 startActivity(intent);
2010 }
2011
2012 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002013 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002014 * enters home screen customization mode.
2015 *
2016 * @param v The view that was clicked.
2017 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002018 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002019 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002020 }
2021
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002022 /**
2023 * Event handler for the "grid" button that appears on the home screen, which
2024 * enters all apps mode.
2025 *
2026 * @param v The view that was clicked.
2027 */
2028 public void onClickAllAppsButton(View v) {
2029 showAllApps(true);
2030 }
2031
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002032 public void onClickAppMarketButton(View v) {
2033 if (mAppMarketIntent != null) {
2034 startActivitySafely(mAppMarketIntent, "app market");
2035 }
2036 }
2037
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002038 void startApplicationDetailsActivity(ComponentName componentName) {
2039 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002040 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2041 Uri.fromParts("package", packageName, null));
2042 startActivity(intent);
2043 }
2044
Patrick Dubroy5539af72010-09-07 15:22:01 -07002045 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2046 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2047 // System applications cannot be installed. For now, show a toast explaining that.
2048 // We may give them the option of disabling apps this way.
2049 int messageId = R.string.uninstall_system_app_text;
2050 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2051 } else {
2052 String packageName = appInfo.componentName.getPackageName();
2053 String className = appInfo.componentName.getClassName();
2054 Intent intent = new Intent(
2055 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2056 startActivity(intent);
2057 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002058 }
2059
Michael Jurkaddd62e92011-02-16 17:49:14 -08002060 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2062 try {
2063 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002064 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 } catch (ActivityNotFoundException e) {
2066 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002067 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 } catch (SecurityException e) {
2069 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002070 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002072 "or use the exported attribute for this activity. "
2073 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002075 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002077
Romain Guy8e633c52010-03-04 12:51:36 -08002078 void startActivityForResultSafely(Intent intent, int requestCode) {
2079 try {
2080 startActivityForResult(intent, requestCode);
2081 } catch (ActivityNotFoundException e) {
2082 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2083 } catch (SecurityException e) {
2084 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2085 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2086 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2087 "or use the exported attribute for this activity.", e);
2088 }
2089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090
Adam Cohena9cf38f2011-05-02 15:36:58 -07002091 private void handleFolderClick(FolderIcon folderIcon) {
2092 final FolderInfo info = folderIcon.mInfo;
2093 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 // Close any open folder
2095 closeFolder();
2096 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002097 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 } else {
2099 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07002100 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 int folderScreen;
2102 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002103 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 // .. and close it
2105 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002106 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 // Close any folder open on the current screen
2108 closeFolder();
2109 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002110 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 }
2112 }
2113 }
2114 }
2115
Adam Cohen2801caf2011-05-13 20:57:39 -07002116 private void growAndFadeOutFolderIcon(FolderIcon fi) {
2117 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2118 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2119 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2120
2121 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2122 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2123 oa.start();
2124 }
2125
2126 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
2127 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2128 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2129 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2130
2131 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2132 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2133 oa.start();
2134 }
2135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002137 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 * is animated relative to the specified View. If the View is null, no animation
2139 * is played.
2140 *
2141 * @param folderInfo The FolderInfo describing the folder to open.
2142 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002143 public void openFolder(FolderIcon folderIcon) {
2144 Folder folder = folderIcon.mFolder;
2145 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146
Adam Cohen2801caf2011-05-13 20:57:39 -07002147 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002148 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149
Adam Cohena9cf38f2011-05-02 15:36:58 -07002150 mWorkspace.addInFullScreen(folder, info.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07002151 mDragLayer.setCurrentFolder(folder);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002152 folder.animateOpen();
2153 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 }
2155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002157 if (mState != State.WORKSPACE) {
2158 return false;
2159 }
2160
Romain Guy1fbc1c82009-11-09 20:43:08 -08002161 switch (v.getId()) {
2162 case R.id.previous_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002163 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002164 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2165 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002166 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002167 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002168 return true;
2169 case R.id.next_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002170 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002171 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2172 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002173 showPreviews(v);
2174 }
2175 return true;
2176 case R.id.all_apps_button:
Winson Chung785d2eb2011-04-14 16:08:02 -07002177 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002178 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2179 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2180 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002181 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002182 return true;
2183 }
2184
Joe Onorato9c1289c2009-08-17 11:03:03 -04002185 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 return false;
2187 }
2188
2189 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002190 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
2192
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 resetAddInfo();
2194 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002196 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 return true;
2198 }
2199
Michael Jurka0280c3b2010-09-17 15:00:07 -07002200 final View itemUnderLongClick = longClickCellInfo.cell;
2201
Winson Chung304dcde2011-01-07 11:17:23 -08002202 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002203 if (itemUnderLongClick == null) {
2204 // User long pressed on empty space
2205 mWorkspace.setAllowLongPress(false);
2206 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2207 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002208 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002210 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002212 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2213 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002214 mAddIntersectCellX = longClickCellInfo.cellX;
2215 mAddIntersectCellY = longClickCellInfo.cellY;
2216 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 }
2218 }
2219 }
2220 return true;
2221 }
2222
Romain Guye6b8e2f2009-11-10 11:56:55 -08002223 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002224 private void dismissPreview(final View v) {
2225 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002226 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002227 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2228 public void onDismiss() {
2229 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2230 int count = group.getChildCount();
2231 for (int i = 0; i < count; i++) {
2232 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2233 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002234 ArrayList<Bitmap> bitmaps =
2235 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002236 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2237
2238 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002239 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002240 window.setOnDismissListener(null);
2241 }
2242 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002243 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002244 }
2245 v.setTag(null);
2246 }
2247
Romain Guyf8e6a802009-12-07 17:48:02 -08002248 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002249 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002250 }
2251
Romain Guya6abce82009-11-10 02:54:41 -08002252 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002253 final Resources resources = getResources();
2254 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002255
Romain Guya6abce82009-11-10 02:54:41 -08002256 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002257
Romain Guy9d31e9f2009-11-11 18:54:28 -08002258 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002259
Romain Guyf8e6a802009-12-07 17:48:02 -08002260 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002261 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002262 int extraW = (int) ((r.left + r.right) * max);
2263 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002264
2265 int aW = cell.getWidth() - extraW;
2266 float w = aW / max;
2267
2268 int width = cell.getWidth();
2269 int height = cell.getHeight();
2270 int x = cell.getLeftPadding();
2271 int y = cell.getTopPadding();
2272 width -= (x + cell.getRightPadding());
2273 height -= (y + cell.getBottomPadding());
2274
2275 float scale = w / width;
2276
2277 int count = end - start;
2278
2279 final float sWidth = width * scale;
2280 float sHeight = height * scale;
2281
Romain Guye6b8e2f2009-11-10 11:56:55 -08002282 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002283
Romain Guye6b8e2f2009-11-10 11:56:55 -08002284 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2285 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002286
2287 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002288 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002289 cell = (CellLayout) workspace.getChildAt(i);
2290
Romain Guyf8e6a802009-12-07 17:48:02 -08002291 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002292 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002293
2294 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002295 c.scale(scale, scale);
2296 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002297 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002298
Romain Guy9d31e9f2009-11-11 18:54:28 -08002299 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002300 image.setImageBitmap(bitmap);
2301 image.setTag(i);
2302 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002303 image.setOnFocusChangeListener(handler);
2304 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002305 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002306
2307 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002308 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2309
Winson Chungaafa03c2010-06-11 17:34:16 -07002310 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002311 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002312
2313 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002314 p.setContentView(preview);
2315 p.setWidth((int) (sWidth * count + extraW));
2316 p.setHeight((int) (sHeight + extraH));
2317 p.setAnimationStyle(R.style.AnimationPreview);
2318 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002319 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002320 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002321 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002322
Romain Guye6b8e2f2009-11-10 11:56:55 -08002323 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2324 public void onDismiss() {
2325 dismissPreview(anchor);
2326 }
2327 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002328
2329 anchor.setTag(p);
2330 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002331 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002332 }
2333
Romain Guy9d31e9f2009-11-11 18:54:28 -08002334 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002335 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002336
Romain Guye6b8e2f2009-11-10 11:56:55 -08002337 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002338 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002339 }
2340
2341 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002342 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002343 v.post(this);
2344 }
2345
2346 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002347 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002348 }
2349
2350 public void onFocusChange(View v, boolean hasFocus) {
2351 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002352 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002353 }
Romain Guya6abce82009-11-10 02:54:41 -08002354 }
2355 }
2356
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002357 Workspace getWorkspace() {
2358 return mWorkspace;
2359 }
2360
Michael Jurka3c38d562011-02-18 19:22:12 -08002361 TabHost getCustomizationDrawer() {
2362 return mHomeCustomizationDrawer;
2363 }
2364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 @Override
2366 protected Dialog onCreateDialog(int id) {
2367 switch (id) {
2368 case DIALOG_CREATE_SHORTCUT:
2369 return new CreateShortcut().createDialog();
2370 case DIALOG_RENAME_FOLDER:
2371 return new RenameFolder().createDialog();
2372 }
2373
2374 return super.onCreateDialog(id);
2375 }
2376
2377 @Override
2378 protected void onPrepareDialog(int id, Dialog dialog) {
2379 switch (id) {
2380 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 break;
2382 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002383 if (mFolderInfo != null) {
2384 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2385 final CharSequence text = mFolderInfo.title;
2386 input.setText(text);
2387 input.setSelection(0, text.length());
2388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 break;
2390 }
2391 }
2392
2393 void showRenameDialog(FolderInfo info) {
2394 mFolderInfo = info;
2395 mWaitingForResult = true;
2396 showDialog(DIALOG_RENAME_FOLDER);
2397 }
2398
Michael Jurka0280c3b2010-09-17 15:00:07 -07002399 private void showAddDialog(int intersectX, int intersectY) {
2400 resetAddInfo();
2401 mAddIntersectCellX = intersectX;
2402 mAddIntersectCellY = intersectY;
2403 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 mWaitingForResult = true;
2405 showDialog(DIALOG_CREATE_SHORTCUT);
2406 }
2407
Joe Onoratodeb98af2010-02-19 14:59:39 -08002408 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002409 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002410 Bundle bundle = new Bundle();
2411
2412 ArrayList<String> shortcutNames = new ArrayList<String>();
2413 shortcutNames.add(getString(R.string.group_applications));
2414 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2415
2416 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2417 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2418 R.drawable.ic_launcher_application));
2419 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2420
2421 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2422 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002423 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002424 pickIntent.putExtras(bundle);
2425
Joe Onoratodeb98af2010-02-19 14:59:39 -08002426 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002427 }
2428
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 private class RenameFolder {
2430 private EditText mInput;
2431
2432 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2434 mInput = (EditText) layout.findViewById(R.id.folder_name);
2435
2436 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2437 builder.setIcon(0);
2438 builder.setTitle(getString(R.string.rename_folder_title));
2439 builder.setCancelable(true);
2440 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2441 public void onCancel(DialogInterface dialog) {
2442 cleanup();
2443 }
2444 });
2445 builder.setNegativeButton(getString(R.string.cancel_action),
2446 new Dialog.OnClickListener() {
2447 public void onClick(DialogInterface dialog, int which) {
2448 cleanup();
2449 }
2450 }
2451 );
2452 builder.setPositiveButton(getString(R.string.rename_action),
2453 new Dialog.OnClickListener() {
2454 public void onClick(DialogInterface dialog, int which) {
2455 changeFolderName();
2456 }
2457 }
2458 );
2459 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002460
2461 final AlertDialog dialog = builder.create();
2462 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2463 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002464 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002465 mInput.requestFocus();
2466 InputMethodManager inputManager = (InputMethodManager)
2467 getSystemService(Context.INPUT_METHOD_SERVICE);
2468 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002469 }
2470 });
2471
2472 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 }
2474
2475 private void changeFolderName() {
2476 final String name = mInput.getText().toString();
2477 if (!TextUtils.isEmpty(name)) {
2478 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002479 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 mFolderInfo.title = name;
2481 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2482
Joe Onorato9c1289c2009-08-17 11:03:03 -04002483 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002484 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002485 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486 } else {
2487 final FolderIcon folderIcon = (FolderIcon)
2488 mWorkspace.getViewForTag(mFolderInfo);
2489 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002490 // TODO: At some point we'll probably want some version of setting
2491 // the text for a folder icon.
2492 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 getWorkspace().requestLayout();
2494 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002495 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002496 mWorkspaceLoading = true;
2497 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 }
2499 }
2500 }
2501 cleanup();
2502 }
2503
2504 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 dismissDialog(DIALOG_RENAME_FOLDER);
2506 mWaitingForResult = false;
2507 mFolderInfo = null;
2508 }
2509 }
2510
Daniel Sandler843e8602010-06-07 14:59:01 -04002511 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2512 public boolean isAllAppsVisible() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002513 return (mState == State.ALL_APPS) || (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002514 }
2515
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002516 // AllAppsView.Watcher
2517 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002518 // In XLarge view, we zoom down the workspace below all apps so it's still visible
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002519 if (zoom == 1.0f && !LauncherApplication.isScreenLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002520 mWorkspace.setVisibility(View.GONE);
2521 }
2522 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002523
Michael Jurkaafca9532011-02-16 14:50:35 -08002524 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002525 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002526 }
2527
2528 private void hideToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002529 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2530 button.setVisibility(View.INVISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002531 button.setAlpha(0.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002532 }
2533
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002534 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002535 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002536 *
2537 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2538 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002539 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002540 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002541 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002542 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002543 final boolean showing = show;
2544 final boolean hiding = !show;
2545
2546 final int duration = show ?
2547 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2548 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2549
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002551 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002552 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002553 anim.addUpdateListener(new AnimatorUpdateListener() {
2554 public void onAnimationUpdate(ValueAnimator animation) {
2555 view.setAlpha((Float) animation.getAnimatedValue());
2556 }
2557 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002558 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002559 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002560 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002561 if (showing) showAndEnableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002562 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002563 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002564 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002565 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002566 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002567 });
2568 seq.play(anim);
2569 } else {
2570 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002571 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002572 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002573 } else {
2574 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002575 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002576 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002577 }
2578
2579 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 * Show/hide the appropriate toolbar buttons for newState.
2581 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002582 *
2583 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002584 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2585 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002586 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002587 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002588 switch (newState) {
2589 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002590 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
Winson Chung4c98d922011-05-31 16:50:48 -07002591 if (mDeleteZone != null) {
2592 mDeleteZone.setDragAndDropEnabled(true);
2593 if (LauncherApplication.isScreenLarge()) {
2594 mDeleteZone.setText(
2595 getResources().getString(R.string.delete_zone_label_workspace));
2596 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002597 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 break;
2599 case ALL_APPS:
Winson Chung785d2eb2011-04-14 16:08:02 -07002600 case APPS_CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002601 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Winson Chung4c98d922011-05-31 16:50:48 -07002602 if (mDeleteZone != null) {
2603 mDeleteZone.setDragAndDropEnabled(false);
2604 if (LauncherApplication.isScreenLarge()) {
2605 mDeleteZone.setText(
2606 getResources().getString(R.string.delete_zone_label_all_apps));
2607 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002608 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609 break;
2610 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002611 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Winson Chung4c98d922011-05-31 16:50:48 -07002612 if (mDeleteZone != null) {
2613 mDeleteZone.setDragAndDropEnabled(false);
2614 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002615 break;
2616 }
2617 }
2618
2619 /**
2620 * Helper method for the cameraZoomIn/cameraZoomOut animations
2621 * @param view The view being animated
2622 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2623 * @param scaleFactor The scale factor used for the zoom
2624 */
2625 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2626 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002627
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002628 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002629 // Set pivotY so that at the starting zoom factor, the view is partially
2630 // visible. Modifying initialHeightFactor changes how much of the view is
2631 // initially showing, and hence the perceived angle from which the view enters.
Winson Chung785d2eb2011-04-14 16:08:02 -07002632 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002633 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002634 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002635 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002636 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002637 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002638 }
2639 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002640
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002641 /**
2642 * Zoom the camera out from the workspace to reveal 'toView'.
2643 * Assumes that the view to show is anchored at either the very top or very bottom
2644 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002645 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002646 */
Winson Chung20f71112011-04-06 14:22:04 -07002647 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002648 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002649 final boolean toAllApps = (toState == State.ALL_APPS)
2650 || (toState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002651
Winson Chung785d2eb2011-04-14 16:08:02 -07002652 final int duration = (toAllApps ?
2653 res.getInteger(R.integer.config_appsCustomizeZoomInTime) :
2654 res.getInteger(R.integer.config_customizeZoomInTime));
2655 final int fadeDuration = (toAllApps ?
2656 res.getInteger(R.integer.config_appsCustomizeFadeInTime) :
2657 res.getInteger(R.integer.config_customizeFadeInTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002658
2659 final float scale = toAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002660 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002661 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2662
Winson Chung785d2eb2011-04-14 16:08:02 -07002663 View tmpView;
2664 if (toAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002665 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002666 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2667 } else {
2668 tmpView = mHomeCustomizationDrawer;
2669 }
2670 final View toView = tmpView;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002671
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 setPivotsForZoom(toView, toState, scale);
2673
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002674 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002675 if (!springLoaded) {
2676 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2677
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002678 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002679 // Everytime we launch into AllApps, we reset the successful drop flag which
2680 // controls when it should hide/show the mini workspaces
2681 mAllAppsPagedView.resetSuccessfulDropFlag();
2682 }
Winson Chung20f71112011-04-06 14:22:04 -07002683 } else {
2684 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2685 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002686 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002687 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002688 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002689
2690 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002691 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002692 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002693 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2694 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002695 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002696 toView.setFastScaleX(a * scale + b * 1f);
2697 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002698 }
2699 });
Adam Cohen61033d32010-11-15 18:29:44 -08002700
Adam Cohenf16e5712011-01-13 13:31:45 -08002701 if (toAllApps) {
Winson Chung4e6a9762011-05-09 11:56:34 -07002702 toView.setVisibility(View.VISIBLE);
Michael Jurka2763be32011-02-24 11:19:57 -08002703 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002704 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002705 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002706 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2707 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002708 // don't need to invalidate because we do so above
2709 toView.setFastAlpha(a * 0f + b * 1f);
2710 }
2711 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002712 alphaAnim.start();
2713 }
2714
Michael Jurkaabded662011-03-04 12:06:57 -08002715 if (toView instanceof LauncherTransitionable) {
2716 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002717 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002718 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002719 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002720 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721 // Prepare the position
2722 toView.setTranslationX(0.0f);
2723 toView.setTranslationY(0.0f);
2724 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002725 toView.bringToFront();
Adam Cohenf16e5712011-01-13 13:31:45 -08002726 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002727 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002728 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002729 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002730 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002731 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002732 // If we don't set the final scale values here, if this animation is cancelled
2733 // it will have the wrong scale value and subsequent cameraPan animations will
2734 // not fix that
2735 toView.setScaleX(1.0f);
2736 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002737 if (toView instanceof LauncherTransitionable) {
2738 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002739 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002740 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002741 });
2742
Chet Haaseb1254a62010-09-07 13:35:00 -07002743 AnimatorSet toolbarHideAnim = new AnimatorSet();
2744 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002745 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2746
2747 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002748 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002749
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002750 if (mStateAnimation != null) mStateAnimation.cancel();
2751 mStateAnimation = new AnimatorSet();
2752 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2753 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002754
2755 // Show the new toolbar buttons just as the main animation is ending
2756 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002757 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2758 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002759 } else {
2760 toView.setTranslationX(0.0f);
2761 toView.setTranslationY(0.0f);
2762 toView.setScaleX(1.0f);
2763 toView.setScaleY(1.0f);
2764 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002765 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002766 if (toView instanceof LauncherTransitionable) {
2767 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2768 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2769 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002770 hideAndShowToolbarButtons(toState, null, null);
2771 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002772 }
2773
2774 /**
2775 * Zoom the camera back into the workspace, hiding 'fromView'.
2776 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002777 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2778 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002779 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002780 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002781 Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002782 final boolean fromAllApps = (fromState == State.ALL_APPS)
2783 || (fromState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002784
2785 int duration = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002786 res.getInteger(R.integer.config_appsCustomizeZoomOutTime) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002787 res.getInteger(R.integer.config_customizeZoomOutTime);
2788
Michael Jurka742574b2011-02-02 23:51:01 -08002789 final float scaleFactor = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002790 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002791 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2792
Winson Chung785d2eb2011-04-14 16:08:02 -07002793 View tmpView;
2794 if (fromAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002795 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002796 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2797 } else {
2798 tmpView = mHomeCustomizationDrawer;
2799 }
2800 final View fromView = tmpView;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002801
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002802 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002803 mCustomizePagedView.endChoiceMode();
2804 mAllAppsPagedView.endChoiceMode();
2805 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002806
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002807 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002808
Michael Jurkad3ef3062010-11-23 16:23:58 -08002809 if (!springLoaded) {
2810 mWorkspace.unshrink(animated);
2811 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002812 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002813 if (mStateAnimation != null) mStateAnimation.cancel();
2814 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002815
Michael Jurka742574b2011-02-02 23:51:01 -08002816 final float oldScaleX = fromView.getScaleX();
2817 final float oldScaleY = fromView.getScaleY();
2818
2819 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2820 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002821 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2822 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002823 ((View)fromView.getParent()).fastInvalidate();
2824 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2825 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2826 }
2827 });
Michael Jurkaabded662011-03-04 12:06:57 -08002828 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002829 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002830 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002831 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2832 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002833 // don't need to invalidate because we do so above
2834 fromView.setFastAlpha(a * 1f + b * 0f);
2835 }
2836 });
Michael Jurkaabded662011-03-04 12:06:57 -08002837 if (fromView instanceof LauncherTransitionable) {
2838 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002839 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002840 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002841 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002842 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002843 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002844 if (fromView instanceof LauncherTransitionable) {
2845 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002846 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002847 }
2848 });
2849
Chet Haaseb1254a62010-09-07 13:35:00 -07002850 AnimatorSet toolbarHideAnim = new AnimatorSet();
2851 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002852 if (!springLoaded) {
2853 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2854 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002855
Adam Cohen61033d32010-11-15 18:29:44 -08002856 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002857
2858 // Show the new toolbar buttons at the very end of the whole animation
2859 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2860 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002861 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2862 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002863 } else {
2864 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002865 if (fromView instanceof LauncherTransitionable) {
2866 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2867 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2868 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002869 if (!springLoaded) {
2870 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2871 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002872 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002873 }
2874
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002875 void showWorkspace(boolean animated) {
2876 showWorkspace(animated, null);
2877 }
2878
2879 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002880 if (layout != null) {
2881 // always animated, but that's ok since we never specify a layout and
2882 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002883 mWorkspace.unshrink(layout);
2884 } else {
2885 mWorkspace.unshrink(animated);
2886 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002887 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002888 closeAllApps(animated);
2889 } else if (mState == State.CUSTOMIZE) {
2890 hideCustomizationDrawer(animated);
2891 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002892
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002893 // Change the state *after* we've called all the transition code
2894 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002895
Winson Chung5fb63472011-02-02 17:03:37 -08002896 // Resume the auto-advance of widgets
2897 mUserPresent = true;
2898 updateRunning();
2899
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002900 // send an accessibility event to announce the context change
2901 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002902 }
2903
Michael Jurkad3ef3062010-11-23 16:23:58 -08002904 void enterSpringLoadedDragMode(CellLayout layout) {
2905 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07002906 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002907 mState = State.ALL_APPS_SPRING_LOADED;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002908 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002909 cameraZoomIn(State.ALL_APPS, true, true);
2910 } else {
2911 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
2912 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002913 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002914 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002915 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002916 }/* else {
2917 // we're already in spring loaded mode; don't do anything
2918 }*/
2919 }
2920
2921 void exitSpringLoadedDragMode() {
2922 if (mState == State.ALL_APPS_SPRING_LOADED) {
2923 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002924 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002925 cameraZoomOut(State.ALL_APPS, true, true);
2926 mState = State.ALL_APPS;
2927 } else {
2928 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2929 mState = State.APPS_CUSTOMIZE;
2930 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002931 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2932 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07002933 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002934 mState = State.CUSTOMIZE;
2935 }/* else {
2936 // we're not in spring loaded mode; don't do anything
2937 }*/
2938 }
2939
Winson Chung785d2eb2011-04-14 16:08:02 -07002940 void showAllApps(boolean animated) {
2941 if (mState != State.WORKSPACE) return;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002942 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002943 cameraZoomOut(State.ALL_APPS, animated, false);
2944 ((View) mAllAppsGrid).requestFocus();
2945
Winson Chung4c98d922011-05-31 16:50:48 -07002946 if (mDeleteZone != null) {
2947 mDeleteZone.setVisibility(View.GONE);
2948 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002949
2950 // Change the state *after* we've called all the transition code
2951 mState = State.ALL_APPS;
2952 } else {
Winson Chung785d2eb2011-04-14 16:08:02 -07002953 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
Winson Chung55b65502011-05-26 12:03:43 -07002954 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002955
2956 // Change the state *after* we've called all the transition code
2957 mState = State.APPS_CUSTOMIZE;
2958 }
2959
2960 // Pause the auto-advance of widgets until we are out of AllApps
2961 mUserPresent = false;
2962 updateRunning();
2963
2964 // Send an accessibility event to announce the context change
2965 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2966 }
2967
Joe Onoratob2061212009-11-24 16:13:54 -05002968 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002969 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002970 * - Home from workspace
2971 * - from center screen
2972 * - from other screens
2973 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002974 * - from center screen
2975 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002976 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002977 * - from center screen
2978 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002979 * - Launch app from workspace and quit
2980 * - with back
2981 * - with home
2982 * - Launch app from all apps and quit
2983 * - with back
2984 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002985 * - Go to a screen that's not the default, then all
2986 * apps, and launch and app, and go back
2987 * - with back
2988 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002989 * - On workspace, long press power and go back
2990 * - with back
2991 * - with home
2992 * - On all apps, long press power and go back
2993 * - with back
2994 * - with home
2995 * - On workspace, power off
2996 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002997 * - Launch an app and turn off the screen while in that app
2998 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002999 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003000 * - From all apps
3001 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003002 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3003 * - From all apps
3004 * - From the center workspace
3005 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003006 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003007 void closeAllApps(boolean animated) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003008 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003009 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
3010 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07003011 cameraZoomIn(State.ALL_APPS, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003012
3013 // Set focus to the AllApps button
3014 findViewById(R.id.all_apps_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003015 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003016 } else {
3017 if (mState == State.APPS_CUSTOMIZE || mState == State.ALL_APPS_SPRING_LOADED) {
3018 mWorkspace.setVisibility(View.VISIBLE);
3019 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
3020
3021 // Set focus to the AllApps button
3022 findViewById(R.id.all_apps_button).requestFocus();
3023 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003024 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003025 }
3026
Joe Onorato7c312c12009-08-13 21:36:53 -07003027 void lockAllApps() {
3028 // TODO
3029 }
3030
3031 void unlockAllApps() {
3032 // TODO
3033 }
3034
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003035 // Show the customization drawer (only exists in x-large configuration)
3036 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003037 if (mState != State.WORKSPACE) {
3038 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003039 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003040
Winson Chung20f71112011-04-06 14:22:04 -07003041 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003042
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003043 // Change the state *after* we've called all the transition code
3044 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003045
3046 // Pause the auto-advance of widgets until we are out of Customization drawer
3047 mUserPresent = false;
3048 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003049 }
3050
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003051 // Hide the customization drawer (only exists in x-large configuration)
3052 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003053 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003054 cameraZoomIn(State.CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003055
3056 // Set focus to the customize button
3057 findViewById(R.id.configure_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003058 }
3059 }
3060
Patrick Dubroy5f445422011-02-18 14:35:21 -08003061 /**
3062 * Add an item from all apps or customize onto the given workspace screen.
3063 * If layout is null, add to the current screen.
3064 */
3065 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003066 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3067 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003068 } else {
3069 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003070 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003071 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003072
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003073 void onWorkspaceClick(CellLayout layout) {
3074 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003075 }
3076
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003077 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003078 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003079 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003080 // Look for the toolbar icon specified in the activity meta-data
3081 Bundle metaData = packageManager.getActivityInfo(
3082 activityName, PackageManager.GET_META_DATA).metaData;
3083 if (metaData != null) {
3084 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3085 if (iconResId != 0) {
3086 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003087 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003088 }
3089 }
3090 } catch (NameNotFoundException e) {
3091 // Do nothing
3092 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003093 return null;
3094 }
3095
3096 // if successful in getting icon, return it; otherwise, set button to use default drawable
3097 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3098 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3099 TextView button = (TextView) findViewById(buttonId);
3100 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3101
3102 // If we were unable to find the icon via the meta-data, use a generic one
3103 if (toolbarIcon == null) {
3104 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3105 return null;
3106 } else {
3107 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3108 return toolbarIcon.getConstantState();
3109 }
3110 }
3111
3112 // if successful in getting icon, return it; otherwise, set button to use default drawable
3113 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3114 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3115 ImageView button = (ImageView) findViewById(buttonId);
3116 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3117
Michael Jurka0423dcf2010-10-05 14:56:18 -07003118 // If we were unable to find the icon via the meta-data, use a generic one
3119 if (toolbarIcon == null) {
3120 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003121 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003122 } else {
3123 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003124 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003125 }
3126 }
3127
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003128 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3129 TextView button = (TextView) findViewById(buttonId);
3130 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3131 }
3132
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003133 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003134 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003135 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003136 }
3137
Michael Jurka0423dcf2010-10-05 14:56:18 -07003138 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07003139 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
3140 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07003141
Winson Chung1cad91e2011-05-25 17:41:01 -07003142 final SearchManager searchManager =
3143 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3144 ComponentName activityName = searchManager.getGlobalSearchActivity();
3145 if (activityName != null) {
3146 // In landscape mode on the Phone UI, we only have enough space to show the magnifying
3147 // glass icon
3148 boolean iconLoaded = false;
3149 if (!LauncherApplication.isScreenLarge()) {
3150 // TODO-APPS_CUSTOMIZE: Remove when the QSB fixes itself?
3151 int orientation = getResources().getConfiguration().orientation;
3152 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
3153 searchButton.setImageResource(R.drawable.ic_generic_search);
3154 iconLoaded = true;
3155 sGlobalSearchIcon = null;
3156 }
3157 }
3158 if (!iconLoaded) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003159 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003160 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003161 }
Winson Chung1cad91e2011-05-25 17:41:01 -07003162 searchButton.setVisibility(View.VISIBLE);
3163 searchDivider.setVisibility(View.VISIBLE);
3164 } else {
3165 searchButton.setVisibility(View.GONE);
3166 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003167 }
3168 }
3169
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003170 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003171 updateButtonWithDrawable(R.id.search_button, d);
3172 }
3173
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003174 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07003175 final View searchDivider = findViewById(R.id.search_divider);
3176 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003177
Winson Chung1cad91e2011-05-25 17:41:01 -07003178 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3179 ComponentName activityName = intent.resolveActivity(getPackageManager());
3180 if (activityName != null) {
3181 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
3182 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3183 searchDivider.setVisibility(View.VISIBLE);
3184 voiceButton.setVisibility(View.VISIBLE);
3185 } else {
3186 searchDivider.setVisibility(View.GONE);
3187 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003188 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003189 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003190
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003191 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003192 updateButtonWithDrawable(R.id.voice_button, d);
3193 }
3194
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003195 /**
3196 * Sets the app market icon (shown when all apps is visible on x-large screens)
3197 */
3198 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003199 final View marketButton = findViewById(R.id.market_button);
3200 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3201 // Find the app market activity by resolving an intent.
3202 // (If multiple app markets are installed, it will return the ResolverActivity.)
3203 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung4c5ac592011-05-23 14:38:58 -07003204 if (activityName != null && LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003205 mAppMarketIntent = intent;
3206 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
3207 R.id.market_button, activityName, R.drawable.app_market_generic);
3208 marketButton.setVisibility(View.VISIBLE);
3209 } else {
3210 // We should hide and disable the view so that we don't try and restore the visibility
3211 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3212 marketButton.setVisibility(View.GONE);
3213 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003214 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003215 }
3216
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003217 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003218 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003219 }
3220
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003221 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222 * Displays the shortcut creation dialog and launches, if necessary, the
3223 * appropriate activity.
3224 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003225 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003226 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3227 DialogInterface.OnShowListener {
3228
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003229 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003231 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003233
Winson Chung55b65502011-05-26 12:03:43 -07003234 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
3235 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003236 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003237 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003239 AlertDialog dialog = builder.create();
3240 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003241 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003242 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 return dialog;
3245 }
3246
3247 public void onCancel(DialogInterface dialog) {
3248 mWaitingForResult = false;
3249 cleanup();
3250 }
3251
Romain Guycbb89e42009-06-08 15:52:54 -07003252 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07003253 mWaitingForResult = false;
3254 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003255 }
3256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003257 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003258 try {
3259 dismissDialog(DIALOG_CREATE_SHORTCUT);
3260 } catch (Exception e) {
3261 // An exception is thrown if the dialog is not visible, which is fine
3262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003263 }
3264
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003265 /**
3266 * Handle the action clicked in the "Add to home" dialog.
3267 */
3268 public void onClick(DialogInterface dialog, int which) {
3269 Resources res = getResources();
3270 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003271
Winson Chung55b65502011-05-26 12:03:43 -07003272 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
3273 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003274 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003275 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003276 break;
3277 }
Winson Chung55b65502011-05-26 12:03:43 -07003278 case AddAdapter.ITEM_APPLICATION: {
3279 if (mAppsCustomizeTabHost != null) {
3280 mAppsCustomizeTabHost.selectAppsTab();
3281 }
3282 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003283 break;
3284 }
Winson Chung55b65502011-05-26 12:03:43 -07003285 case AddAdapter.ITEM_APPWIDGET: {
3286 if (mAppsCustomizeTabHost != null) {
3287 mAppsCustomizeTabHost.selectWidgetsTab();
3288 }
3289 showAllApps(true);
3290 break;
3291 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003292 case AddAdapter.ITEM_WALLPAPER: {
3293 startWallpaper();
3294 break;
3295 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003296 }
3297 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003298
3299 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003300 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003302 }
3303
3304 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003305 * Receives notifications when applications are added/removed.
3306 */
3307 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3308 @Override
3309 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003310 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003311 String reason = intent.getStringExtra("reason");
3312 if (!"homekey".equals(reason)) {
3313 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003314 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003315 animate = false;
3316 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003317 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003318 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003319 }
3320 }
3321
3322 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003323 * Receives notifications whenever the appwidgets are reset.
3324 */
3325 private class AppWidgetResetObserver extends ContentObserver {
3326 public AppWidgetResetObserver() {
3327 super(new Handler());
3328 }
3329
3330 @Override
3331 public void onChange(boolean selfChange) {
3332 onAppWidgetReset();
3333 }
3334 }
3335
3336 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003337 * If the activity is currently paused, signal that we need to re-run the loader
3338 * in onResume.
3339 *
3340 * This needs to be called from incoming places where resources might have been loaded
3341 * while we are paused. That is becaues the Configuration might be wrong
3342 * when we're not running, and if it comes back to what it was when we
3343 * were paused, we are not restarted.
3344 *
3345 * Implementation of the method from LauncherModel.Callbacks.
3346 *
3347 * @return true if we are currently paused. The caller might be able to
3348 * skip some work in that case since we will come back again.
3349 */
3350 public boolean setLoadOnResume() {
3351 if (mPaused) {
3352 Log.i(TAG, "setLoadOnResume");
3353 mOnResumeNeedsLoad = true;
3354 return true;
3355 } else {
3356 return false;
3357 }
3358 }
3359
3360 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003362 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003364 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003365 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003366 } else {
3367 return SCREEN_COUNT / 2;
3368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003370
Winson Chunga12a2502010-12-20 14:41:35 -08003371 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003372 mAllAppsPagedView = view;
3373 }
3374
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 /**
3376 * Refreshes the shortcuts shown on the workspace.
3377 *
3378 * Implementation of the method from LauncherModel.Callbacks.
3379 */
3380 public void startBinding() {
3381 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003382
3383 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003384 int count = workspace.getChildCount();
3385 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003386 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003387 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3388 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003390
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 if (DEBUG_USER_INTERFACE) {
3392 android.widget.Button finishButton = new android.widget.Button(this);
3393 finishButton.setText("Finish");
3394 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3395
3396 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3397 public void onClick(View v) {
3398 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003399 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003401 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003402
3403 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3404 // mDesktopItems -> AppWidgetInfo -> hostView).
3405 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003406 }
3407
3408 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 * Bind the items start-end from the list.
3410 *
3411 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003412 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3414
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003415 setLoadOnResume();
3416
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 final Workspace workspace = mWorkspace;
3418
3419 for (int i=start; i<end; i++) {
3420 final ItemInfo item = shortcuts.get(i);
3421 mDesktopItems.add(item);
3422 switch (item.itemType) {
3423 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3424 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003425 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3427 false);
3428 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003429 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003430 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003431 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003432 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3434 false);
3435 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003436 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003437 }
3438
Joe Onorato9c1289c2009-08-17 11:03:03 -04003439 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003440 }
3441
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 /**
3443 * Implementation of the method from LauncherModel.Callbacks.
3444 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003445 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003446 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003447 sFolders.clear();
3448 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003449 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003450
3451 /**
3452 * Add the views for a widget to the workspace.
3453 *
3454 * Implementation of the method from LauncherModel.Callbacks.
3455 */
3456 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003457 setLoadOnResume();
3458
Daniel Sandler843e8602010-06-07 14:59:01 -04003459 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3460 if (DEBUG_WIDGETS) {
3461 Log.d(TAG, "bindAppWidget: " + item);
3462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003463 final Workspace workspace = mWorkspace;
3464
3465 final int appWidgetId = item.appWidgetId;
3466 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003467 if (DEBUG_WIDGETS) {
3468 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3469 }
3470
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3472
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3474 item.hostView.setTag(item);
3475
3476 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3477 item.cellY, item.spanX, item.spanY, false);
3478
Adam Cohended9f8d2010-11-03 13:25:16 -07003479 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3480
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 workspace.requestLayout();
3482
3483 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003484
3485 if (DEBUG_WIDGETS) {
3486 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3487 + (SystemClock.uptimeMillis()-start) + "ms");
3488 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 }
3490
3491 /**
3492 * Callback saying that there aren't any more items to bind.
3493 *
3494 * Implementation of the method from LauncherModel.Callbacks.
3495 */
3496 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003497 setLoadOnResume();
3498
Joe Onorato9c1289c2009-08-17 11:03:03 -04003499 if (mSavedState != null) {
3500 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003501 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003502 }
3503
Adam Cohendf2cc412011-04-27 16:56:57 -07003504 final long[] folders = mSavedState.getLongArray(RUNTIME_STATE_FOLDERS);
3505 if (folders != null) {
3506 for (long folderId : folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003507 final FolderInfo info = sFolders.get(folderId);
Adam Cohena9cf38f2011-05-02 15:36:58 -07003508 final FolderIcon folderIcon = (FolderIcon)
3509 mWorkspace.getViewForTag(info);
3510 if (folderIcon != null) {
3511 openFolder(folderIcon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 }
3513 }
3514 final Folder openFolder = mWorkspace.getOpenFolder();
3515 if (openFolder != null) {
3516 openFolder.requestFocus();
3517 }
3518 }
3519
Joe Onorato9c1289c2009-08-17 11:03:03 -04003520 mSavedState = null;
3521 }
3522
3523 if (mSavedInstanceState != null) {
3524 super.onRestoreInstanceState(mSavedInstanceState);
3525 mSavedInstanceState = null;
3526 }
3527
Winson Chung32bb5e52011-01-31 14:51:56 -08003528 // Workaround a bug that occurs when rotating the device while the customization mode is
3529 // open, we trigger a new layout on all the CellLayout children.
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003530 if (LauncherApplication.isScreenLarge() && (mState == State.CUSTOMIZE)) {
Winson Chung32bb5e52011-01-31 14:51:56 -08003531 final int childCount = mWorkspace.getChildCount();
3532 for (int i = 0; i < childCount; ++i) {
3533 mWorkspace.getChildAt(i).requestLayout();
3534 }
3535 }
3536
Joe Onorato9c1289c2009-08-17 11:03:03 -04003537 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003538
3539 // If we received the result of any pending adds while the loader was running (e.g. the
3540 // widget configuration forced an orientation change), process them now.
3541 for (int i = 0; i < sPendingAddList.size(); i++) {
3542 completeAdd(sPendingAddList.get(i));
3543 }
3544 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 }
3546
3547 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003548 * Updates the icons on the launcher that are affected by changes to the package list
3549 * on the device.
3550 */
3551 private void updateIconsAffectedByPackageManagerChanges() {
3552 updateAppMarketIcon();
3553 updateGlobalSearchIcon();
3554 updateVoiceSearchIcon();
3555 }
3556
3557 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003558 * Add the icons for all apps.
3559 *
3560 * Implementation of the method from LauncherModel.Callbacks.
3561 */
3562 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003563 if (mAllAppsGrid != null) {
3564 mAllAppsGrid.setApps(apps);
3565 }
3566 if (mAppsCustomizeContent != null) {
3567 mAppsCustomizeContent.setApps(apps);
3568 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003569 if (mCustomizePagedView != null) {
3570 mCustomizePagedView.setApps(apps);
3571 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003572 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 }
3574
3575 /**
3576 * A package was installed.
3577 *
3578 * Implementation of the method from LauncherModel.Callbacks.
3579 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003580 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003581 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chung785d2eb2011-04-14 16:08:02 -07003583 if (mAllAppsGrid != null) {
3584 mAllAppsGrid.addApps(apps);
3585 }
3586 if (mAppsCustomizeContent != null) {
3587 mAppsCustomizeContent.addApps(apps);
3588 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003589 if (mCustomizePagedView != null) {
3590 mCustomizePagedView.addApps(apps);
3591 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003592 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003593 }
3594
3595 /**
3596 * A package was updated.
3597 *
3598 * Implementation of the method from LauncherModel.Callbacks.
3599 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003600 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003601 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003603 if (mWorkspace != null) {
3604 mWorkspace.updateShortcuts(apps);
3605 }
3606 if (mAllAppsGrid != null) {
3607 mAllAppsGrid.updateApps(apps);
3608 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003609 if (mAppsCustomizeContent != null) {
3610 mAppsCustomizeContent.updateApps(apps);
3611 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003612 if (mCustomizePagedView != null) {
3613 mCustomizePagedView.updateApps(apps);
3614 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003615 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 }
3617
3618 /**
3619 * A package was uninstalled.
3620 *
3621 * Implementation of the method from LauncherModel.Callbacks.
3622 */
Joe Onorato36115782010-06-17 13:28:48 -04003623 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003624 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003625 if (permanent) {
3626 mWorkspace.removeItems(apps);
3627 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003628 if (mAllAppsGrid != null) {
3629 mAllAppsGrid.removeApps(apps);
3630 }
3631 if (mAppsCustomizeContent != null) {
3632 mAppsCustomizeContent.removeApps(apps);
3633 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003634 if (mCustomizePagedView != null) {
3635 mCustomizePagedView.removeApps(apps);
3636 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003637 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 }
Joe Onoratobe386092009-11-17 17:32:16 -08003639
3640 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003641 * A number of packages were updated.
3642 */
3643 public void bindPackagesUpdated() {
3644 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003645 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003646 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003647 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003648 if (mAppsCustomizeContent != null) {
3649 mAppsCustomizeContent.onPackagesUpdated();
3650 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003651 }
3652
Winson Chung400438b2011-01-16 17:53:48 -08003653 private int mapConfigurationOriActivityInfoOri(int configOri) {
3654 final Display d = getWindowManager().getDefaultDisplay();
3655 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3656 switch (d.getRotation()) {
3657 case Surface.ROTATION_0:
3658 case Surface.ROTATION_180:
3659 // We are currently in the same basic orientation as the natural orientation
3660 naturalOri = configOri;
3661 break;
3662 case Surface.ROTATION_90:
3663 case Surface.ROTATION_270:
3664 // We are currently in the other basic orientation to the natural orientation
3665 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3666 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3667 break;
3668 }
3669
3670 int[] oriMap = {
3671 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3672 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3673 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3674 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3675 };
3676 // Since the map starts at portrait, we need to offset if this device's natural orientation
3677 // is landscape.
3678 int indexOffset = 0;
3679 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3680 indexOffset = 1;
3681 }
3682 return oriMap[(d.getRotation() + indexOffset) % 4];
3683 }
3684 public void lockScreenOrientation() {
3685 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3686 .getConfiguration().orientation));
3687 }
3688 public void unlockScreenOrientation() {
3689 mHandler.postDelayed(new Runnable() {
3690 public void run() {
3691 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3692 }
3693 }, mRestoreScreenOrientationDelay);
3694 }
3695
Winson Chung80baf5a2010-08-09 16:03:15 -07003696 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003697 * Prints out out state for debugging.
3698 */
3699 public void dumpState() {
3700 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003701 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003702 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3703 Log.d(TAG, "mRestoring=" + mRestoring);
3704 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3705 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3706 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003707 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003708 mModel.dumpState();
Winson Chung785d2eb2011-04-14 16:08:02 -07003709 if (mAllAppsGrid != null) {
3710 mAllAppsGrid.dumpState();
3711 }
3712 if (mAppsCustomizeContent != null) {
3713 mAppsCustomizeContent.dumpState();
3714 }
Joe Onoratobe386092009-11-17 17:32:16 -08003715 Log.d(TAG, "END launcher2 dump state");
3716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003717}
Michael Jurka2763be32011-02-24 11:19:57 -08003718
Michael Jurkaabded662011-03-04 12:06:57 -08003719interface LauncherTransitionable {
3720 void onLauncherTransitionStart(Animator animation);
3721 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003722}