blob: 2fe1c7d5b9ac91799722840f0f344e661dca8193 [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;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070031import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080032import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070033import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.app.Dialog;
36import android.app.SearchManager;
37import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070038import android.appwidget.AppWidgetManager;
39import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080041import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070042import android.content.ClipData;
43import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040044import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080045import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.Context;
47import android.content.DialogInterface;
48import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080049import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080050import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080053import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080054import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070056import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040057import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080060import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070061import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080062import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040064import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070065import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020067import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080068import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070069import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040070import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080071import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070072import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080073import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.text.Selection;
75import android.text.SpannableStringBuilder;
76import android.text.TextUtils;
77import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070078import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080080import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.KeyEvent;
82import android.view.LayoutInflater;
83import android.view.Menu;
84import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070085import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080086import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.View;
88import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070089import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080090import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080091import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070095import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070097import android.widget.ImageView;
98import android.widget.LinearLayout;
99import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700100import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -0700101import android.widget.TextView;
102import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700103
Adam Cohendf2cc412011-04-27 16:56:57 -0700104import com.android.common.Search;
105import com.android.launcher.R;
106import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -0800107
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108/**
109 * Default launcher application.
110 */
Michael Jurka946ad472010-07-09 18:05:18 -0700111public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700112 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
113 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800114 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700115 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400118 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800122 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700125 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
126 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
128 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700129 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700132 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int REQUEST_PICK_APPLICATION = 6;
134 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700136 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
138 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
139
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800140 static final int SCREEN_COUNT = 5;
141 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Joe Onorato7c312c12009-08-13 21:36:53 -0700143 static final int DIALOG_CREATE_SHORTCUT = 1;
144 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Romain Guy98d01652009-06-30 16:21:04 -0700146 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
148 // Type: int
149 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700150 // Type: int
151 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 // Type: long
Adam Cohendf2cc412011-04-27 16:56:57 -0700153 private static final String RUNTIME_STATE_FOLDERS = "launcher.folder";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: int
155 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 // Type: boolean
161 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
162 // Type: long
163 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
164
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700165 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
166
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700167 /** The different states that Launcher can be in. */
Winson Chung785d2eb2011-04-14 16:08:02 -0700168 private enum State { WORKSPACE, APPS_CUSTOMIZE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800169 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700170 private State mState = State.WORKSPACE;
171 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700172
Joe Onorato9c1289c2009-08-17 11:03:03 -0400173 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800176 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800178 private final BroadcastReceiver mCloseSystemDialogsReceiver
179 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800180 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private LayoutInflater mInflater;
183
Joe Onorato00acb122009-08-04 16:04:30 -0400184 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700186
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700187 private AppWidgetManager mAppWidgetManager;
188 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700189
Michael Jurka0280c3b2010-09-17 15:00:07 -0700190 private int mAddScreen = -1;
191 private int mAddIntersectCellX = -1;
192 private int mAddIntersectCellY = -1;
193 private int[] mAddDropPosition;
194 private int[] mTmpAddItemCellCoordinates = new int[2];
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private FolderInfo mFolderInfo;
197
Joe Onorato7c312c12009-08-13 21:36:53 -0700198 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700199 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700200 private AllAppsView mAllAppsGrid;
Winson Chung785d2eb2011-04-14 16:08:02 -0700201 private AppsCustomizeTabHost mAppsCustomizeTabHost;
202 private AppsCustomizePagedView mAppsCustomizeContent;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800203 private CustomizeTrayTabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700204 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700205
Winson Chung97d85d22011-04-13 11:27:36 -0700206 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800207 private View mAllAppsButton;
208 private View mDivider;
209 private View mConfigureButton;
210
Winson Chunga12a2502010-12-20 14:41:35 -0800211 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700212 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private Bundle mSavedState;
215
216 private SpannableStringBuilder mDefaultKeySsb = null;
217
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218 private boolean mWorkspaceLoading = true;
219
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800220 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private boolean mRestoring;
222 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700223 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224
225 private Bundle mSavedInstanceState;
226
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800228 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800229 private boolean mUserPresent = true;
230 private boolean mVisible = false;
231 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700233 private static LocaleConfiguration sLocaleConfiguration = null;
234
Romain Guy84f296c2009-11-04 15:00:44 -0800235 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700236
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700237 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800239 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800240 private ImageView mPreviousView;
241 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500242
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400243 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400244 private String[] mHotseatConfig = null;
245 private Intent[] mHotseats = null;
246 private Drawable[] mHotseatIcons = null;
247 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400248
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700249 private Intent mAppMarketIntent = null;
250
Adam Cohended9f8d2010-11-03 13:25:16 -0700251 // Related to the auto-advancing of widgets
252 private final int ADVANCE_MSG = 1;
253 private final int mAdvanceInterval = 20000;
254 private final int mAdvanceStagger = 250;
255 private long mAutoAdvanceSentTime;
256 private long mAutoAdvanceTimeLeft = -1;
257 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
258 new HashMap<View, AppWidgetProviderInfo>();
259
Winson Chung400438b2011-01-16 17:53:48 -0800260 // Determines how long to wait after a rotation before restoring the screen orientation to
261 // match the sensor state.
262 private final int mRestoreScreenOrientationDelay = 500;
263
Michael Jurka4ef207b2010-11-29 17:05:45 -0800264 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800265 private static Drawable.ConstantState sGlobalSearchIcon;
266 private static Drawable.ConstantState sVoiceSearchIcon;
267 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800268
Michael Jurkaddd62e92011-02-16 17:49:14 -0800269 private BubbleTextView mWaitingForResume;
270
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800271 private static ArrayList<PendingAddArguments> sPendingAddList
272 = new ArrayList<PendingAddArguments>();
273
274 private static class PendingAddArguments {
275 int requestCode;
276 Intent intent;
277 int screen;
278 int cellX;
279 int cellY;
280 }
281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 @Override
283 protected void onCreate(Bundle savedInstanceState) {
284 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700285
Winson Chungaafa03c2010-06-11 17:34:16 -0700286 if (LauncherApplication.isInPlaceRotationEnabled()) {
287 // hide the status bar (temporary until we get the status bar design figured out)
288 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
289 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
290 }
291
Joe Onorato0589f0f2010-02-08 13:44:00 -0800292 LauncherApplication app = ((LauncherApplication)getApplication());
293 mModel = app.setLauncher(this);
294 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400295 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700297
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700298 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700299 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
300 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200303 android.os.Debug.startMethodTracing(
304 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 }
306
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400307 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 setContentView(R.layout.launcher);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800310 mHomeCustomizationDrawer = (CustomizeTrayTabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700311 if (mHomeCustomizationDrawer != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700312 // share the same customization workspace across all the tabs
Michael Jurka7ef959b2011-02-23 11:48:32 -0800313 mCustomizePagedView = (CustomizePagedView) findViewById(
314 R.id.customization_drawer_tab_contents);
Michael Jurka946ad472010-07-09 18:05:18 -0700315 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 setupViews();
317
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800318 registerContentObservers();
319
Joe Onorato7c312c12009-08-13 21:36:53 -0700320 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700321
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 mSavedState = savedInstanceState;
323 restoreState(mSavedState);
324
Winson Chunga12a2502010-12-20 14:41:35 -0800325 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800326 if (mCustomizePagedView != null) {
327 mCustomizePagedView.update();
328 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700329 if (mAppsCustomizeContent != null) {
330 mAppsCustomizeContent.onPackagesUpdated();
331 }
Winson Chunga12a2502010-12-20 14:41:35 -0800332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 if (PROFILE_STARTUP) {
334 android.os.Debug.stopMethodTracing();
335 }
336
337 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400338 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 }
340
341 // For handling default keys
342 mDefaultKeySsb = new SpannableStringBuilder();
343 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800344
345 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
346 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800347
348 // If we have a saved version of these external icons, we load them up immediately
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700349 if (LauncherApplication.isScreenLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800350 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
351 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800352 }
Michael Jurka1c3f8ec2011-03-07 16:41:21 -0800353 if (sGlobalSearchIcon != null) {
354 updateGlobalSearchIcon(sGlobalSearchIcon);
355 }
356 if (sVoiceSearchIcon != null) {
357 updateVoiceSearchIcon(sVoiceSearchIcon);
358 }
359 if (sAppMarketIcon != null) {
360 updateAppMarketIcon(sAppMarketIcon);
361 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 }
Romain Guycbb89e42009-06-08 15:52:54 -0700364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700366 if (sLocaleConfiguration == null) {
367 new AsyncTask<Void, Void, LocaleConfiguration>() {
368 @Override
369 protected LocaleConfiguration doInBackground(Void... unused) {
370 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
371 readConfiguration(Launcher.this, localeConfiguration);
372 return localeConfiguration;
373 }
374
375 @Override
376 protected void onPostExecute(LocaleConfiguration result) {
377 sLocaleConfiguration = result;
378 checkForLocaleChange(); // recursive, but now with a locale configuration
379 }
380 }.execute();
381 return;
382 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 final Configuration configuration = getResources().getConfiguration();
385
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700386 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 final String locale = configuration.locale.toString();
388
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700389 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 final int mcc = configuration.mcc;
391
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700392 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 final int mnc = configuration.mnc;
394
Romain Guy84f296c2009-11-04 15:00:44 -0800395 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396
Romain Guy84f296c2009-11-04 15:00:44 -0800397 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700398 sLocaleConfiguration.locale = locale;
399 sLocaleConfiguration.mcc = mcc;
400 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700401
Joe Onorato0589f0f2010-02-08 13:44:00 -0800402 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400403 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700404
405 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
406 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700407 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700408 public void run() {
409 writeConfiguration(Launcher.this, localeConfiguration);
410 }
411 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700412 }
413 }
414
415 private static class LocaleConfiguration {
416 public String locale;
417 public int mcc = -1;
418 public int mnc = -1;
419 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700420
Romain Guy98d01652009-06-30 16:21:04 -0700421 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
422 DataInputStream in = null;
423 try {
424 in = new DataInputStream(context.openFileInput(PREFERENCES));
425 configuration.locale = in.readUTF();
426 configuration.mcc = in.readInt();
427 configuration.mnc = in.readInt();
428 } catch (FileNotFoundException e) {
429 // Ignore
430 } catch (IOException e) {
431 // Ignore
432 } finally {
433 if (in != null) {
434 try {
435 in.close();
436 } catch (IOException e) {
437 // Ignore
438 }
439 }
440 }
441 }
442
443 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
444 DataOutputStream out = null;
445 try {
446 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
447 out.writeUTF(configuration.locale);
448 out.writeInt(configuration.mcc);
449 out.writeInt(configuration.mnc);
450 out.flush();
451 } catch (FileNotFoundException e) {
452 // Ignore
453 } catch (IOException e) {
454 //noinspection ResultOfMethodCallIgnored
455 context.getFileStreamPath(PREFERENCES).delete();
456 } finally {
457 if (out != null) {
458 try {
459 out.close();
460 } catch (IOException e) {
461 // Ignore
462 }
463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 }
465 }
466
467 static int getScreen() {
468 synchronized (sLock) {
469 return sScreen;
470 }
471 }
472
473 static void setScreen(int screen) {
474 synchronized (sLock) {
475 sScreen = screen;
476 }
477 }
478
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400479 // Note: This doesn't do all the client-id magic that BrowserProvider does
480 // in Browser. (http://b/2425179)
481 private Uri getDefaultBrowserUri() {
482 String url = getString(R.string.default_browser_url);
483 if (url.indexOf("{CID}") != -1) {
484 url = url.replace("{CID}", "android-google");
485 }
486 return Uri.parse(url);
487 }
488
489 // Load the Intent templates from arrays.xml to populate the hotseats. For
490 // each Intent, if it resolves to a single app, use that as the launch
491 // intent & use that app's label as the contentDescription. Otherwise,
492 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400493 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400494 if (mHotseatConfig == null) {
495 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
496 if (mHotseatConfig.length > 0) {
497 mHotseats = new Intent[mHotseatConfig.length];
498 mHotseatLabels = new CharSequence[mHotseatConfig.length];
499 mHotseatIcons = new Drawable[mHotseatConfig.length];
500 } else {
501 mHotseats = null;
502 mHotseatIcons = null;
503 mHotseatLabels = null;
504 }
505
506 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
507 for (int i=0; i<mHotseatConfig.length; i++) {
508 // load icon for this slot; currently unrelated to the actual activity
509 try {
510 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
511 } catch (ArrayIndexOutOfBoundsException ex) {
512 Log.w(TAG, "Missing hotseat_icons array item #" + i);
513 mHotseatIcons[i] = null;
514 }
515 }
516 hotseatIconDrawables.recycle();
517 }
518
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400519 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400520 for (int i=0; i<mHotseatConfig.length; i++) {
521 Intent intent = null;
522 if (mHotseatConfig[i].equals("*BROWSER*")) {
523 // magic value meaning "launch user's default web browser"
524 // replace it with a generic web request so we can see if there is indeed a default
525 String defaultUri = getString(R.string.default_browser_url);
526 intent = new Intent(
527 Intent.ACTION_VIEW,
528 ((defaultUri != null)
529 ? Uri.parse(defaultUri)
530 : getDefaultBrowserUri())
531 ).addCategory(Intent.CATEGORY_BROWSABLE);
532 // note: if the user launches this without a default set, she
533 // will always be taken to the default URL above; this is
534 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700535 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400536 // URL is unavoidably sent to the chosen app.
537 } else {
538 try {
539 intent = Intent.parseUri(mHotseatConfig[i], 0);
540 } catch (java.net.URISyntaxException ex) {
541 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
542 // bogus; leave intent=null
543 }
544 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700545
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400546 if (intent == null) {
547 mHotseats[i] = null;
548 mHotseatLabels[i] = getText(R.string.activity_not_found);
549 continue;
550 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400551
552 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700553 Log.d(TAG, "loadHotseats: hotseat " + i
554 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400555 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400556 + "]");
557 }
558
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
560 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700561 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 Log.d(TAG, "Best match for intent: " + bestMatch);
563 Log.d(TAG, "All matches: ");
564 for (ResolveInfo ri : allMatches) {
565 Log.d(TAG, " --> " + ri);
566 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400567 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400568 // did this resolve to a single app, or the resolver?
569 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700570 // can't find any activity to handle this. let's leave the
571 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400572 // to launch.
573 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400574
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 // set accessibility text to "Not installed"
576 mHotseatLabels[i] = getText(R.string.activity_not_found);
577 } else {
578 boolean found = false;
579 for (ResolveInfo ri : allMatches) {
580 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
581 && bestMatch.activityInfo.applicationInfo.packageName
582 .equals(ri.activityInfo.applicationInfo.packageName)) {
583 found = true;
584 break;
585 }
586 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700587
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400588 if (!found) {
589 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
590 // the bestMatch is probably the ResolveActivity, meaning the
591 // user has not yet selected a default
592 // so: we'll keep the original intent for now
593 mHotseats[i] = intent;
594
595 // set the accessibility text to "Select shortcut"
596 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
597 } else {
598 // we have an app!
599 // now reconstruct the intent to launch it through the front
600 // door
601 ComponentName com = new ComponentName(
602 bestMatch.activityInfo.applicationInfo.packageName,
603 bestMatch.activityInfo.name);
604 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
605
606 // load the app label for accessibility
607 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
608 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400609 }
610
611 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700612 Log.d(TAG, "loadHotseats: hotseat " + i
613 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400614 + ((mHotseats[i] == null)
615 ? "null"
616 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400617 + "] label=[" + mHotseatLabels[i]
618 + "]"
619 );
620 }
621 }
622 }
623
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800624 private void completeAdd(PendingAddArguments args) {
625 switch (args.requestCode) {
626 case REQUEST_PICK_APPLICATION:
627 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
628 break;
629 case REQUEST_PICK_SHORTCUT:
630 processShortcut(args.intent);
631 break;
632 case REQUEST_CREATE_SHORTCUT:
633 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
634 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800635 case REQUEST_PICK_APPWIDGET:
636 addAppWidgetFromPick(args.intent);
637 break;
638 case REQUEST_CREATE_APPWIDGET:
639 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
640 completeAddAppWidget(appWidgetId, args.screen);
641 break;
642 case REQUEST_PICK_WALLPAPER:
643 // We just wanted the activity result here so we can clear mWaitingForResult
644 break;
645 }
646 }
647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800649 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700650 mWaitingForResult = false;
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 // The pattern used here is that a user PICKs a specific application,
653 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
656 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700657
Michael Jurka0280c3b2010-09-17 15:00:07 -0700658 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 final PendingAddArguments args = new PendingAddArguments();
660 args.requestCode = requestCode;
661 args.intent = data;
662 args.screen = mAddScreen;
663 args.cellX = mAddIntersectCellX;
664 args.cellY = mAddIntersectCellY;
665
666 // If the loader is still running, defer the add until it is done.
667 if (isWorkspaceLocked()) {
668 sPendingAddList.add(args);
669 } else {
670 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
Romain Guy18042c82009-11-06 11:44:55 -0800672 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
673 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
674 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700675 // Clean up the appWidgetId if we canceled
676 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
677 if (appWidgetId != -1) {
678 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
680 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
682
683 @Override
684 protected void onResume() {
685 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800686 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700687 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400688 mWorkspaceLoading = true;
689 mModel.startLoader(this, true);
690 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700691 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800693 if (mWaitingForResume != null) {
694 mWaitingForResume.setStayPressed(false);
695 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700696 // When we resume Launcher, a different Activity might be responsible for the app
697 // market intent, so refresh the icon
698 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 }
700
701 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700702 protected void onPause() {
703 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700704 // Some launcher layouts don't have a previous and next view
705 if (mPreviousView != null) {
706 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700707 }
708 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700709 dismissPreview(mNextView);
710 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700711 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800712 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700713 }
Romain Guycbb89e42009-06-08 15:52:54 -0700714
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700715 @Override
716 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 // Flag the loader to stop early before switching
718 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700719 if (mAllAppsGrid != null) {
720 mAllAppsGrid.surrender();
721 }
722 if (mAppsCustomizeContent != null) {
723 mAppsCustomizeContent.surrender();
724 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800725 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700726 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800728 // We can't hide the IME if it was forced open. So don't bother
729 /*
730 @Override
731 public void onWindowFocusChanged(boolean hasFocus) {
732 super.onWindowFocusChanged(hasFocus);
733
734 if (hasFocus) {
735 final InputMethodManager inputManager = (InputMethodManager)
736 getSystemService(Context.INPUT_METHOD_SERVICE);
737 WindowManager.LayoutParams lp = getWindow().getAttributes();
738 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
739 android.os.Handler()) {
740 protected void onReceiveResult(int resultCode, Bundle resultData) {
741 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
742 }
743 });
744 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
745 }
746 }
747 */
748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 private boolean acceptFilter() {
750 final InputMethodManager inputManager = (InputMethodManager)
751 getSystemService(Context.INPUT_METHOD_SERVICE);
752 return !inputManager.isFullscreenMode();
753 }
754
755 @Override
756 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700757 final int uniChar = event.getUnicodeChar();
758 final boolean handled = super.onKeyDown(keyCode, event);
759 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
760 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
762 keyCode, event);
763 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700764 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700765 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700766 // showSearchDialog()
767 // If there are multiple keystrokes before the search dialog takes focus,
768 // onSearchRequested() will be called for every keystroke,
769 // but it is idempotent, so it's fine.
770 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 }
772 }
773
Joe Onorato8a9625e2010-01-28 15:55:35 -0800774 // Eat the long press event so the keyboard doesn't come up.
775 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
776 return true;
777 }
778
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 return handled;
780 }
781
Karl Rosaen138a0412009-04-23 19:00:21 -0700782 private String getTypedText() {
783 return mDefaultKeySsb.toString();
784 }
785
786 private void clearTypedText() {
787 mDefaultKeySsb.clear();
788 mDefaultKeySsb.clearSpans();
789 Selection.setSelection(mDefaultKeySsb, 0);
790 }
791
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700793 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
794 * State
795 */
796 private static State intToState(int stateOrdinal) {
797 State state = State.WORKSPACE;
798 final State[] stateValues = State.values();
799 for (int i = 0; i < stateValues.length; i++) {
800 if (stateValues[i].ordinal() == stateOrdinal) {
801 state = stateValues[i];
802 break;
803 }
804 }
805 return state;
806 }
807
808 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 * Restores the previous state, if it exists.
810 *
811 * @param savedState The previous state.
812 */
813 private void restoreState(Bundle savedState) {
814 if (savedState == null) {
815 return;
816 }
817
Michael Jurka883f55b2010-10-21 15:47:14 -0700818 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
819
Winson Chung785d2eb2011-04-14 16:08:02 -0700820 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800821 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700822 } else if (state == State.CUSTOMIZE) {
823 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800824 }
825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
827 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700828 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 }
830
831 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700832
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700834 mAddScreen = addScreen;
835 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
836 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 mRestoring = true;
838 }
839
840 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
841 if (renameFolder) {
842 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700843 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 mRestoring = true;
845 }
Winson Chunga12a2502010-12-20 14:41:35 -0800846
847 // Restore the current AllApps drawer tab
848 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
849 String curTab = savedState.getString("allapps_currentTab");
850 if (curTab != null) {
851 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
852 tabhost.setCurrentTabByTag(curTab);
853 }
854 int curPage = savedState.getInt("allapps_currentPage", -1);
855 if (curPage > -1) {
856 mAllAppsPagedView.setRestorePage(curPage);
857 }
858 }
859
860 // Restore the current customization drawer tab
861 if (mHomeCustomizationDrawer != null) {
862 String curTab = savedState.getString("customize_currentTab");
863 if (curTab != null) {
864 // We set this directly so that there is no delay before the tab is set
Michael Jurka7ef959b2011-02-23 11:48:32 -0800865 mCustomizePagedView.setCustomizationFilter(
866 mHomeCustomizationDrawer.getCustomizeFilterForTabTag(curTab));
Winson Chunga12a2502010-12-20 14:41:35 -0800867 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
868 }
869
870 // Note: currently we do not restore the page for the customization tray because unlike
871 // AllApps, the page content can change drastically
872 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700873
874 // Restore the AppsCustomize tab
875 if (mAppsCustomizeTabHost != null) {
876 String curTab = savedState.getString("apps_customize_currentTab");
877 if (curTab != null) {
878 // We set this directly so that there is no delay before the tab is set
879 mAppsCustomizeContent.setContentType(
880 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
881 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
882 }
883
884 // Note: currently we do not restore the page for the AppsCustomize pane because the
885 // change in layout can drastically affect the saved page index
886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 }
888
889 /**
890 * Finds all the views we need and configure them properly.
891 */
892 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700893 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400894
Romain Guyb1b69f52009-08-10 15:10:15 -0700895 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400896 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700898 if (LauncherApplication.isScreenLarge()) {
Winson Chung63257c12011-05-05 17:06:13 -0700899 mAllAppsGrid = (AllAppsView) dragLayer.findViewById(R.id.all_apps_view);
Winson Chung785d2eb2011-04-14 16:08:02 -0700900 mAllAppsGrid.setup(this, dragController);
901 // We don't want a hole punched in our window.
902 ((View) mAllAppsGrid).setWillNotDraw(false);
903 } else {
904 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
905 findViewById(R.id.apps_customize_pane);
906 mAppsCustomizeContent = (AppsCustomizePagedView)
907 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
908 mAppsCustomizeContent.setup(this, dragController);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700909 }
910
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700912
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500914 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700915
Joe Onorato7c312c12009-08-13 21:36:53 -0700916 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
917 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700919 View handleView = findViewById(R.id.all_apps_button);
920 if (handleView != null && handleView instanceof HandleView) {
921 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700922 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700923 mHandleView.setLauncher(this);
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700924 mHandleView.setOnLongClickListener(this);
925 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800926
Winson Chung80baf5a2010-08-09 16:03:15 -0700927 if (mCustomizePagedView != null) {
928 mCustomizePagedView.setLauncher(this);
929 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700930 } else {
931 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
932 hotseatLeft.setContentDescription(mHotseatLabels[0]);
933 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
934 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
935 hotseatRight.setContentDescription(mHotseatLabels[1]);
936 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400937
Winson Chung4e6a9762011-05-09 11:56:34 -0700938 View.OnKeyListener listener = new IndicatorKeyEventListener();
Michael Jurkaaf442092010-06-10 17:01:57 -0700939 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
Winson Chung4e6a9762011-05-09 11:56:34 -0700940 mPreviousView.setOnKeyListener(listener);
Michael Jurkaaf442092010-06-10 17:01:57 -0700941 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Winson Chung4e6a9762011-05-09 11:56:34 -0700942 mNextView.setOnKeyListener(listener);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800943
Michael Jurkaaf442092010-06-10 17:01:57 -0700944 Drawable previous = mPreviousView.getDrawable();
945 Drawable next = mNextView.getDrawable();
946 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947
Michael Jurkaaf442092010-06-10 17:01:57 -0700948 mPreviousView.setHapticFeedbackEnabled(false);
949 mPreviousView.setOnLongClickListener(this);
950 mNextView.setHapticFeedbackEnabled(false);
951 mNextView.setOnLongClickListener(this);
952 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800953
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400955 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800957 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958
959 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400960 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -0800961
962 final View allAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -0700963 final View divider = findViewById(R.id.all_apps_divider);
Michael Jurkab8e14472010-12-20 16:06:10 -0800964 final View configureButton = findViewById(R.id.configure_button);
965
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700966 if (LauncherApplication.isScreenLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800967 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -0700968 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800969 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -0700970 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700971 dragController.addDragListener(deleteZone);
972
Adam Cohencdc30d52010-12-01 15:09:47 -0800973 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
974 if (allAppsDeleteZone != null) {
975 allAppsDeleteZone.setLauncher(this);
976 allAppsDeleteZone.setDragController(dragController);
977 allAppsDeleteZone.setDragAndDropEnabled(false);
978 dragController.addDragListener(allAppsDeleteZone);
979 dragController.addDropTarget(allAppsDeleteZone);
980 }
981
982 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
983 findViewById(R.id.all_apps_info_target);
984 if (allAppsInfoTarget != null) {
985 allAppsInfoTarget.setLauncher(this);
986 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -0800987 allAppsInfoTarget.setDragAndDropEnabled(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700988 }
989 View marketButton = findViewById(R.id.market_button);
990 if (marketButton != null) {
991 if (allAppsInfoTarget != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800992 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -0800993 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700994 marketButton.setOnClickListener(new OnClickListener() {
995 public void onClick(View v) {
996 onClickAppMarketButton(v);
997 }
998 });
Adam Cohencdc30d52010-12-01 15:09:47 -0800999 }
1000
Joe Onorato00acb122009-08-04 16:04:30 -04001001 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001002 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001003 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001004
Joe Onorato00acb122009-08-04 16:04:30 -04001005 // The order here is bottom to top.
1006 dragController.addDropTarget(workspace);
1007 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001008 if (allAppsInfoTarget != null) {
1009 dragController.addDropTarget(allAppsInfoTarget);
1010 }
1011 if (allAppsDeleteZone != null) {
1012 dragController.addDropTarget(allAppsDeleteZone);
1013 }
Winson Chung97d85d22011-04-13 11:27:36 -07001014 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
Winson Chung4e6a9762011-05-09 11:56:34 -07001015 View.OnKeyListener listener = null;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001016 if (LauncherApplication.isScreenLarge()) {
Winson Chung4e6a9762011-05-09 11:56:34 -07001017 // For tablets, AllApps lives in the button bar at the top
1018 listener = new ButtonBarKeyEventListener();
1019 } else {
1020 // For phones, AppsCustomize lives in the "dock" at the bottom
1021 listener = new DockKeyEventListener();
1022 }
Winson Chung97d85d22011-04-13 11:27:36 -07001023 int buttonCount = mButtonCluster.getChildCount();
1024 for (int i = 0; i < buttonCount; ++i) {
1025 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
1026 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001027
1028 mAllAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -07001029 mDivider = findViewById(R.id.all_apps_divider);
Michael Jurka838a4ca2011-02-07 13:33:06 -08001030 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001031
1032 // We had previously set these click handlers in XML, but the first time we launched
1033 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1034 // found the right handler. Setting the handlers directly here eliminates that cost.
1035 if (mConfigureButton != null) {
1036 mConfigureButton.setOnClickListener(new OnClickListener() {
1037 public void onClick(View v) {
1038 onClickConfigureButton(v);
1039 }
1040 });
1041 }
1042 if (mDivider != null) {
1043 mDivider.setOnClickListener(new OnClickListener() {
1044 public void onClick(View v) {
1045 onClickAllAppsButton(v);
1046 }
1047 });
1048 }
1049 if (mAllAppsButton != null) {
1050 mAllAppsButton.setOnClickListener(new OnClickListener() {
1051 public void onClick(View v) {
1052 onClickAllAppsButton(v);
1053 }
1054 });
1055 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 }
1057
Romain Guy8a73c512009-11-09 19:19:59 -08001058 @SuppressWarnings({"UnusedDeclaration"})
1059 public void previousScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001060 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001061 mWorkspace.scrollLeft();
1062 }
Romain Guy8a73c512009-11-09 19:19:59 -08001063 }
1064
1065 @SuppressWarnings({"UnusedDeclaration"})
1066 public void nextScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001067 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001068 mWorkspace.scrollRight();
1069 }
Romain Guy8a73c512009-11-09 19:19:59 -08001070 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001071
1072 @SuppressWarnings({"UnusedDeclaration"})
1073 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001074 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001075
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001076 int index = -1;
1077 if (v.getId() == R.id.hotseat_left) {
1078 index = 0;
1079 } else if (v.getId() == R.id.hotseat_right) {
1080 index = 1;
1081 }
1082
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001083 // reload these every tap; you never know when they might change
1084 loadHotseats();
1085 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001086 startActivitySafely(
1087 mHotseats[index],
1088 "hotseat"
1089 );
1090 }
1091 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 /**
1094 * Creates a view representing a shortcut.
1095 *
1096 * @param info The data structure describing the shortcut.
1097 *
1098 * @return A View inflated from R.layout.application.
1099 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001100 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001102 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 }
1104
1105 /**
1106 * Creates a view representing a shortcut inflated from the specified resource.
1107 *
1108 * @param layoutResId The id of the XML layout used to create the shortcut.
1109 * @param parent The group the shortcut belongs to.
1110 * @param info The data structure describing the shortcut.
1111 *
1112 * @return A View inflated from layoutResId.
1113 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001114 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001115 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1116 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 return favorite;
1119 }
1120
1121 /**
1122 * Add an application shortcut to the workspace.
1123 *
1124 * @param data The intent describing the application.
1125 * @param cellInfo The position on screen where to create the shortcut.
1126 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001127 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001128 int intersectCellX, int intersectCellY) {
1129 final int[] cellXY = mTmpAddItemCellCoordinates;
1130 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1131
1132 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1133 showOutOfSpaceMessage();
1134 return;
1135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001137 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001138
Romain Guy73b979d2009-06-09 12:57:21 -07001139 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001140 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001142 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001143 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1144 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001145 } else {
1146 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 }
1148 }
Romain Guycbb89e42009-06-08 15:52:54 -07001149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 /**
1151 * Add a shortcut to the workspace.
1152 *
1153 * @param data The intent describing the shortcut.
1154 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 private void completeAddShortcut(Intent data, int screen,
1157 int intersectCellX, int intersectCellY) {
1158 final int[] cellXY = mTmpAddItemCellCoordinates;
1159 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001160
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001161 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001162 boolean foundCellSpan = false;
1163 if (touchXY != null) {
1164 // when dragging and dropping, just find the closest free spot
1165 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1166 int[] result = screenLayout.findNearestVacantArea(
1167 touchXY[0], touchXY[1], 1, 1, cellXY);
1168 foundCellSpan = (result != null);
1169 } else {
1170 foundCellSpan = layout.findCellForSpanThatIntersects(
1171 cellXY, 1, 1, intersectCellX, intersectCellY);
1172 }
1173
1174 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 showOutOfSpaceMessage();
1176 return;
1177 }
1178
1179 final ShortcutInfo info = mModel.addShortcut(
1180 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181
1182 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 }
1186 }
1187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001189 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001191 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001192 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 */
Winson Chungf7640c82011-02-28 13:47:29 -08001194 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001195 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001196
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001197 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001198 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1199 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001202 // We have saved the position to which the widget was dragged-- this really only matters
1203 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001204 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001205
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001206 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 boolean foundCellSpan = false;
1208 if (touchXY != null) {
1209 // when dragging and dropping, just find the closest free spot
1210 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1211 int[] result = screenLayout.findNearestVacantArea(
1212 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001213 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001215 // if we long pressed on an empty cell to bring up a menu,
1216 // make sure we intersect the empty cell
1217 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1218 // findCellForSpanThatIntersects will just ignore them
1219 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1220 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001221 }
1222
Michael Jurka0280c3b2010-09-17 15:00:07 -07001223 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001224 if (appWidgetId != -1) {
1225 // Deleting an app widget ID is a void call but writes to disk before returning
1226 // to the caller...
1227 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1228 new Thread("deleteAppWidgetId") {
1229 public void run() {
1230 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1231 }
1232 }.start();
1233 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001234 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001235 return;
1236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001238 // Build Launcher-specific widget info and save to database
1239 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 launcherInfo.spanX = spanXY[0];
1241 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 LauncherModel.addItemToDatabase(this, launcherInfo,
1244 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001245 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246
1247 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001248 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001251 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001252
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001253 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001255
Michael Jurka0280c3b2010-09-17 15:00:07 -07001256 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001258
1259 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261 }
Romain Guycbb89e42009-06-08 15:52:54 -07001262
Adam Cohended9f8d2010-11-03 13:25:16 -07001263 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1264 @Override
1265 public void onReceive(Context context, Intent intent) {
1266 final String action = intent.getAction();
1267 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1268 mUserPresent = false;
1269 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001270
1271 // Reset AllApps to it's initial state
1272 if (mAllAppsGrid != null) {
1273 mAllAppsGrid.reset();
1274 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001275 if (mAppsCustomizeContent != null) {
1276 mAppsCustomizeContent.reset();
1277 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001278 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1279 mUserPresent = true;
1280 updateRunning();
1281 }
1282 }
1283 };
1284
1285 @Override
1286 public void onAttachedToWindow() {
1287 super.onAttachedToWindow();
1288
1289 // Listen for broadcasts related to user-presence
1290 final IntentFilter filter = new IntentFilter();
1291 filter.addAction(Intent.ACTION_SCREEN_OFF);
1292 filter.addAction(Intent.ACTION_USER_PRESENT);
1293 registerReceiver(mReceiver, filter);
1294
Adam Cohend113e0c2010-11-11 10:48:05 -08001295 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001296 mVisible = true;
1297 }
1298
1299 @Override
1300 public void onDetachedFromWindow() {
1301 super.onDetachedFromWindow();
1302 mVisible = false;
1303
Adam Cohend113e0c2010-11-11 10:48:05 -08001304 if (mAttached) {
1305 unregisterReceiver(mReceiver);
1306 mAttached = false;
1307 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001308 updateRunning();
1309 }
1310
1311 public void onWindowVisibilityChanged(int visibility) {
1312 mVisible = visibility == View.VISIBLE;
1313 updateRunning();
1314 }
1315
1316 private void sendAdvanceMessage(long delay) {
1317 mHandler.removeMessages(ADVANCE_MSG);
1318 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1319 mHandler.sendMessageDelayed(msg, delay);
1320 mAutoAdvanceSentTime = System.currentTimeMillis();
1321 }
1322
1323 private void updateRunning() {
1324 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1325 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1326 mAutoAdvanceRunning = autoAdvanceRunning;
1327 if (autoAdvanceRunning) {
1328 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1329 sendAdvanceMessage(delay);
1330 } else {
1331 if (!mWidgetsToAdvance.isEmpty()) {
1332 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1333 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1334 }
1335 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001336 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001337 }
1338 }
1339 }
1340
1341 private final Handler mHandler = new Handler() {
1342 @Override
1343 public void handleMessage(Message msg) {
1344 if (msg.what == ADVANCE_MSG) {
1345 int i = 0;
1346 for (View key: mWidgetsToAdvance.keySet()) {
1347 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1348 final int delay = mAdvanceStagger * i;
1349 if (v instanceof Advanceable) {
1350 postDelayed(new Runnable() {
1351 public void run() {
1352 ((Advanceable) v).advance();
1353 }
1354 }, delay);
1355 }
1356 i++;
1357 }
1358 sendAdvanceMessage(mAdvanceInterval);
1359 }
1360 }
1361 };
1362
1363 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1364 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1365 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1366 if (v instanceof Advanceable) {
1367 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001368 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001369 updateRunning();
1370 }
1371 }
1372
1373 void removeWidgetToAutoAdvance(View hostView) {
1374 if (mWidgetsToAdvance.containsKey(hostView)) {
1375 mWidgetsToAdvance.remove(hostView);
1376 updateRunning();
1377 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001378 }
1379
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1381 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001382 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383 launcherInfo.hostView = null;
1384 }
1385
Adam Cohended9f8d2010-11-03 13:25:16 -07001386 void showOutOfSpaceMessage() {
1387 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1388 }
1389
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001390 public LauncherAppWidgetHost getAppWidgetHost() {
1391 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
Romain Guycbb89e42009-06-08 15:52:54 -07001393
Winson Chunga9abd0e2010-10-27 17:18:37 -07001394 public LauncherModel getModel() {
1395 return mModel;
1396 }
1397
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001398 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001399 getWindow().closeAllPanels();
1400
1401 try {
1402 dismissDialog(DIALOG_CREATE_SHORTCUT);
1403 // Unlock the workspace if the dialog was showing
1404 } catch (Exception e) {
1405 // An exception is thrown if the dialog is not visible, which is fine
1406 }
1407
1408 try {
1409 dismissDialog(DIALOG_RENAME_FOLDER);
1410 // Unlock the workspace if the dialog was showing
1411 } catch (Exception e) {
1412 // An exception is thrown if the dialog is not visible, which is fine
1413 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001414
1415 // Whatever we were doing is hereby canceled.
1416 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001417 }
1418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 @Override
1420 protected void onNewIntent(Intent intent) {
1421 super.onNewIntent(intent);
1422
1423 // Close the menu
1424 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001425 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001426 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001427
Joe Onorato14f122b2009-11-19 14:06:36 -08001428 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1429 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001430
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001431 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001432 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001433 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001434 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001435 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001436 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001437 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001438
Joe Onorato3a8820b2009-11-10 15:06:42 -08001439 final View v = getWindow().peekDecorView();
1440 if (v != null && v.getWindowToken() != null) {
1441 InputMethodManager imm = (InputMethodManager)getSystemService(
1442 INPUT_METHOD_SERVICE);
1443 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001445
1446 // Reset AllApps to it's initial state
1447 if (mAllAppsGrid != null) {
1448 mAllAppsGrid.reset();
1449 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001450 if (mAppsCustomizeContent != null) {
1451 mAppsCustomizeContent.reset();
1452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
1454 }
1455
1456 @Override
1457 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1458 // Do not call super here
1459 mSavedInstanceState = savedInstanceState;
1460 }
1461
1462 @Override
1463 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001464 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465
1466 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1467 if (folders.size() > 0) {
1468 final int count = folders.size();
1469 long[] ids = new long[count];
1470 for (int i = 0; i < count; i++) {
1471 final FolderInfo info = folders.get(i).getInfo();
1472 ids[i] = info.id;
1473 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001474 outState.putLongArray(RUNTIME_STATE_FOLDERS, ids);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 } else {
1476 super.onSaveInstanceState(outState);
1477 }
1478
Michael Jurka883f55b2010-10-21 15:47:14 -07001479 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480
Michael Jurka0280c3b2010-09-17 15:00:07 -07001481 if (mAddScreen > -1 && mWaitingForResult) {
1482 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1483 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1484 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
1486
1487 if (mFolderInfo != null && mWaitingForResult) {
1488 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1489 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1490 }
Michael Jurka946ad472010-07-09 18:05:18 -07001491
Winson Chung43b119d2010-12-06 16:45:05 -08001492 // Save the current AllApps drawer tab
1493 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1494 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1495 String currentTabTag = tabhost.getCurrentTabTag();
1496 if (currentTabTag != null) {
1497 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001498 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001499 }
1500 }
Winson Chung43b119d2010-12-06 16:45:05 -08001501 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001502 if (mHomeCustomizationDrawer != null) {
1503 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1504 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001505 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001506 }
1507 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001508 // Save the current AppsCustomize tab
1509 if (mAppsCustomizeTabHost != null) {
1510 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1511 if (currentTabTag != null) {
1512 outState.putString("apps_customize_currentTab", currentTabTag);
1513 }
1514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
1516
1517 @Override
1518 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001522 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001524 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001526 mAppWidgetHost = null;
1527
1528 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529
1530 TextKeyListener.getInstance().release();
1531
Joe Onorato9c1289c2009-08-17 11:03:03 -04001532 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533
Joe Onorato9c1289c2009-08-17 11:03:03 -04001534 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001535
1536 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001537
Patrick Dubroyab962b72010-07-26 12:10:10 -07001538 // Some launcher layouts don't have a previous and next view
1539 if (mPreviousView != null) {
1540 dismissPreview(mPreviousView);
1541 }
1542 if (mNextView != null) {
1543 dismissPreview(mNextView);
1544 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001545
1546 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001547
1548 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1549 mWorkspace.removeAllViews();
1550 mWorkspace = null;
1551 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001552
1553 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555
Adam Cohena9cf38f2011-05-02 15:36:58 -07001556 public DragController getDragController() {
1557 return mDragController;
1558 }
1559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 @Override
1561 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001562 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 super.startActivityForResult(intent, requestCode);
1564 }
1565
1566 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001567 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001569
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001570 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001571
Karl Rosaen138a0412009-04-23 19:00:21 -07001572 if (initialQuery == null) {
1573 // Use any text typed in the launcher as the initial query
1574 initialQuery = getTypedText();
1575 clearTypedText();
1576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 if (appSearchData == null) {
1578 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001579 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
Romain Guycbb89e42009-06-08 15:52:54 -07001581
Karl Rosaen138a0412009-04-23 19:00:21 -07001582 final SearchManager searchManager =
1583 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001584 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001585 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
1587
1588 @Override
1589 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001590 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001591 return false;
1592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593
1594 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001595
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1597 .setIcon(android.R.drawable.ic_menu_add)
1598 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001599 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1600 .setIcon(android.R.drawable.ic_menu_manage)
1601 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001602 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 .setIcon(android.R.drawable.ic_menu_gallery)
1604 .setAlphabeticShortcut('W');
1605 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1606 .setIcon(android.R.drawable.ic_search_category_default)
1607 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1608 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1609 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1610 .setAlphabeticShortcut('N');
1611
1612 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001613 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1614 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615
1616 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1617 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1618 .setIntent(settings);
1619
1620 return true;
1621 }
1622
1623 @Override
1624 public boolean onPrepareOptionsMenu(Menu menu) {
1625 super.onPrepareOptionsMenu(menu);
1626
Winson Chung785d2eb2011-04-14 16:08:02 -07001627 if (mAllAppsGrid != null) {
1628 // If all apps is animating, don't show the menu, because we don't know
1629 // which one to show.
1630 if (mAllAppsGrid.isAnimating()) {
1631 return false;
1632 }
1633
1634 // Only show the add and wallpaper options when we're not in all apps.
1635 boolean visible = !mAllAppsGrid.isVisible();
1636 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1637 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1638
1639 // Disable add if the workspace is full.
1640 if (visible) {
1641 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1642 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
1643 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001644 }
1645
Winson Chung785d2eb2011-04-14 16:08:02 -07001646 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1647 // related code?
1648 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649
1650 return true;
1651 }
1652
1653 @Override
1654 public boolean onOptionsItemSelected(MenuItem item) {
1655 switch (item.getItemId()) {
1656 case MENU_ADD:
1657 addItems();
1658 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001659 case MENU_MANAGE_APPS:
1660 manageApps();
1661 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 case MENU_WALLPAPER_SETTINGS:
1663 startWallpaper();
1664 return true;
1665 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001666 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 return true;
1668 case MENU_NOTIFICATIONS:
1669 showNotifications();
1670 return true;
1671 }
1672
1673 return super.onOptionsItemSelected(item);
1674 }
Romain Guycbb89e42009-06-08 15:52:54 -07001675
Karl Rosaen138a0412009-04-23 19:00:21 -07001676 /**
1677 * Indicates that we want global search for this activity by setting the globalSearch
1678 * argument for {@link #startSearch} to true.
1679 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001681 @Override
1682 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001683 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001684 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001685 }
1686
Joe Onorato9c1289c2009-08-17 11:03:03 -04001687 public boolean isWorkspaceLocked() {
1688 return mWorkspaceLoading || mWaitingForResult;
1689 }
1690
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001691 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1692 private boolean isPreviewVisible() {
1693 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1694 (mNextView != null && mNextView.getTag() != null);
1695 }
1696
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 private void addItems() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001698 if (LauncherApplication.isScreenLarge()) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001699 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001700 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001701 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001702 }
1703 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001704 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001705 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 }
1708
Michael Jurka0280c3b2010-09-17 15:00:07 -07001709 private void resetAddInfo() {
1710 mAddScreen = -1;
1711 mAddIntersectCellX = -1;
1712 mAddIntersectCellY = -1;
1713 mAddDropPosition = null;
1714 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001715
Winson Chung55cef262010-10-28 14:14:18 -07001716 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001717 resetAddInfo();
1718 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001719 mAddDropPosition = position;
1720
Michael Jurkaaf442092010-06-10 17:01:57 -07001721 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001722 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1723 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001724 }
1725
Winson Chung7ad01412010-09-27 11:33:03 -07001726 private void manageApps() {
1727 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1728 }
1729
Michael Jurkaaf442092010-06-10 17:01:57 -07001730 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001731 // TODO: catch bad widget exception when sent
1732 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001733 // TODO: Is this log message meaningful?
1734 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001735 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001736 }
1737
Winson Chung55cef262010-10-28 14:14:18 -07001738 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001739 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740
Bjorn Bringert7984c942009-12-09 15:38:25 +00001741 if (appWidget.configure != null) {
1742 // Launch over to configure widget, if needed
1743 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1744 intent.setComponent(appWidget.configure);
1745 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001746 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001747 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1748 intent.putExtra(
1749 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1750 info.mimeType);
1751
1752 final String mimeType = info.mimeType;
1753 final ClipData clipData = (ClipData) info.configurationData;
1754 final ClipDescription clipDesc = clipData.getDescription();
1755 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1756 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001757 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001758 final CharSequence stringData = item.getText();
1759 final Uri uriData = item.getUri();
1760 final Intent intentData = item.getIntent();
1761 final String key =
1762 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1763 if (uriData != null) {
1764 intent.putExtra(key, uriData);
1765 } else if (intentData != null) {
1766 intent.putExtra(key, intentData);
1767 } else if (stringData != null) {
1768 intent.putExtra(key, stringData);
1769 }
1770 break;
1771 }
1772 }
1773 }
Winson Chung55cef262010-10-28 14:14:18 -07001774 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001775
Romain Guy8e633c52010-03-04 12:51:36 -08001776 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001778 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001779 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
1781 }
Romain Guycbb89e42009-06-08 15:52:54 -07001782
Michael Jurka0280c3b2010-09-17 15:00:07 -07001783 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1784 resetAddInfo();
1785 mAddScreen = screen;
1786 mAddDropPosition = position;
1787
1788 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1789 createShortcutIntent.setComponent(componentName);
1790 processShortcut(createShortcutIntent);
1791 }
1792
Joe Onoratodeb98af2010-02-19 14:59:39 -08001793 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001794 // Handle case where user selected "Applications"
1795 String applicationName = getResources().getString(R.string.group_applications);
1796 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001797
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001798 if (applicationName != null && applicationName.equals(shortcutName)) {
1799 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1800 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001801
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001802 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1803 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001804 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001805 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001806 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001807 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
1810
Winson Chung24ab2f12010-09-16 14:10:47 -07001811 void processWallpaper(Intent intent) {
1812 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1813 }
1814
Adam Cohendf035382011-04-11 17:22:04 -07001815 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001816 FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 folderInfo.title = getText(R.string.folder_name);
1818
Michael Jurka0280c3b2010-09-17 15:00:07 -07001819 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1820 final int[] cellXY = mTmpAddItemCellCoordinates;
1821 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1822 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001823 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001824 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825
1826 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001827 LauncherModel.addItemToDatabase(this, folderInfo,
1828 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001829 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001830 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831
1832 // Create the view
1833 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001834 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1835 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001836 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001837 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 }
Romain Guycbb89e42009-06-08 15:52:54 -07001839
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001841 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 }
1843
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 private void showNotifications() {
1845 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1846 if (statusBar != null) {
1847 statusBar.expand();
1848 }
1849 }
1850
1851 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001852 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001854 Intent chooser = Intent.createChooser(pickWallpaper,
1855 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001856 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1857 // Removed in Eclair MR1
1858// WallpaperManager wm = (WallpaperManager)
1859// getSystemService(Context.WALLPAPER_SERVICE);
1860// WallpaperInfo wi = wm.getWallpaperInfo();
1861// if (wi != null && wi.getSettingsActivity() != null) {
1862// LabeledIntent li = new LabeledIntent(getPackageName(),
1863// R.string.configure_wallpaper, 0);
1864// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1865// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1866// }
Mike Clerona0618e42009-10-22 13:55:21 -07001867 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 }
1869
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001870 /**
1871 * Registers various content observers. The current implementation registers
1872 * only a favorites observer to keep track of the favorites applications.
1873 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001874 private void registerContentObservers() {
1875 ContentResolver resolver = getContentResolver();
1876 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1877 true, mWidgetObserver);
1878 }
1879
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 @Override
1881 public boolean dispatchKeyEvent(KeyEvent event) {
1882 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1883 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001885 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001886 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001887 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001888 dumpState();
1889 return true;
1890 }
1891 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001892 }
1893 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1894 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001895 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 return true;
1897 }
1898 }
1899
1900 return super.dispatchKeyEvent(event);
1901 }
1902
Joe Onorato88ec0992009-11-19 13:16:06 -08001903 @Override
1904 public void onBackPressed() {
Winson Chung785d2eb2011-04-14 16:08:02 -07001905 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001906 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001907 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001908 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001909 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001910 dismissPreview(mPreviousView);
1911 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001912 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001913 mWorkspace.exitWidgetResizeMode();
1914
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001915 // Back button is a no-op here, but give at least some feedback for the button press
1916 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001917 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001918 }
1919
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 private void closeFolder() {
1921 Folder folder = mWorkspace.getOpenFolder();
1922 if (folder != null) {
1923 closeFolder(folder);
1924 }
1925 }
1926
1927 void closeFolder(Folder folder) {
1928 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001929
Michael Jurka8c920dd2011-01-20 14:16:56 -08001930 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001932 CellLayout cl = (CellLayout) parent;
Adam Cohendf2cc412011-04-27 16:56:57 -07001933 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001935
1936 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 folder.onClose();
1938 }
1939
1940 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001941 * Re-listen when widgets are reset.
1942 */
1943 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001944 if (mAppWidgetHost != null) {
1945 mAppWidgetHost.startListening();
1946 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001947 }
1948
1949 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001950 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1951 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 private void unbindDesktopItems() {
1954 for (ItemInfo item: mDesktopItems) {
1955 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001957 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 /**
1961 * Launches the intent referred by the clicked shortcut.
1962 *
1963 * @param v The view representing the clicked shortcut.
1964 */
1965 public void onClick(View v) {
1966 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001967 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001969 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001970 int[] pos = new int[2];
1971 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001972 intent.setSourceBounds(new Rect(pos[0], pos[1],
1973 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001974 boolean success = startActivitySafely(intent, tag);
1975
1976 if (success && v instanceof BubbleTextView) {
1977 mWaitingForResume = (BubbleTextView) v;
1978 mWaitingForResume.setStayPressed(true);
1979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001981 if (v instanceof FolderIcon) {
1982 FolderIcon fi = (FolderIcon) v;
1983 handleFolderClick(fi);
1984 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001985 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001986 if (mState == State.ALL_APPS) {
1987 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001988 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001989 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
1992 }
1993
Michael Jurka0e260592010-06-30 17:07:39 -07001994 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001995 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001996 // clicking anywhere on the workspace causes the customization drawer to slide down
1997 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001998 return false;
1999 }
2000
Michael Jurkaaf442092010-06-10 17:01:57 -07002001 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002002 * Event handler for the search button
2003 *
2004 * @param v The view that was clicked.
2005 */
2006 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002007 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002008 // Use a custom animation for launching search
2009 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002010 }
2011
2012 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002013 * Event handler for the voice button
2014 *
2015 * @param v The view that was clicked.
2016 */
2017 public void onClickVoiceButton(View v) {
2018 startVoiceSearch();
2019 }
2020
2021 private void startVoiceSearch() {
2022 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2023 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2024 startActivity(intent);
2025 }
2026
2027 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002028 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002029 * enters home screen customization mode.
2030 *
2031 * @param v The view that was clicked.
2032 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002033 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002034 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002035 }
2036
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002037 /**
2038 * Event handler for the "grid" button that appears on the home screen, which
2039 * enters all apps mode.
2040 *
2041 * @param v The view that was clicked.
2042 */
2043 public void onClickAllAppsButton(View v) {
2044 showAllApps(true);
2045 }
2046
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002047 public void onClickAppMarketButton(View v) {
2048 if (mAppMarketIntent != null) {
2049 startActivitySafely(mAppMarketIntent, "app market");
2050 }
2051 }
2052
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002053 void startApplicationDetailsActivity(ComponentName componentName) {
2054 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002055 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2056 Uri.fromParts("package", packageName, null));
2057 startActivity(intent);
2058 }
2059
Patrick Dubroy5539af72010-09-07 15:22:01 -07002060 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2061 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2062 // System applications cannot be installed. For now, show a toast explaining that.
2063 // We may give them the option of disabling apps this way.
2064 int messageId = R.string.uninstall_system_app_text;
2065 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2066 } else {
2067 String packageName = appInfo.componentName.getPackageName();
2068 String className = appInfo.componentName.getClassName();
2069 Intent intent = new Intent(
2070 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2071 startActivity(intent);
2072 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002073 }
2074
Michael Jurkaddd62e92011-02-16 17:49:14 -08002075 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2077 try {
2078 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002079 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 } catch (ActivityNotFoundException e) {
2081 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002082 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 } catch (SecurityException e) {
2084 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002085 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002087 "or use the exported attribute for this activity. "
2088 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002090 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002092
Romain Guy8e633c52010-03-04 12:51:36 -08002093 void startActivityForResultSafely(Intent intent, int requestCode) {
2094 try {
2095 startActivityForResult(intent, requestCode);
2096 } catch (ActivityNotFoundException e) {
2097 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2098 } catch (SecurityException e) {
2099 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2100 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2101 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2102 "or use the exported attribute for this activity.", e);
2103 }
2104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105
Adam Cohena9cf38f2011-05-02 15:36:58 -07002106 private void handleFolderClick(FolderIcon folderIcon) {
2107 final FolderInfo info = folderIcon.mInfo;
2108 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 // Close any open folder
2110 closeFolder();
2111 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002112 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 } else {
2114 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07002115 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 int folderScreen;
2117 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002118 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 // .. and close it
2120 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002121 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 // Close any folder open on the current screen
2123 closeFolder();
2124 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002125 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127 }
2128 }
2129 }
2130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002132 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 * is animated relative to the specified View. If the View is null, no animation
2134 * is played.
2135 *
2136 * @param folderInfo The FolderInfo describing the folder to open.
2137 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002138 public void openFolder(FolderIcon folderIcon) {
2139 Folder folder = folderIcon.mFolder;
2140 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141
Adam Cohena9cf38f2011-05-02 15:36:58 -07002142 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143
Adam Cohena9cf38f2011-05-02 15:36:58 -07002144 mWorkspace.addInFullScreen(folder, info.screen);
2145 folder.animateOpen();
2146 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 }
2148
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002150 if (mState != State.WORKSPACE) {
2151 return false;
2152 }
2153
Romain Guy1fbc1c82009-11-09 20:43:08 -08002154 switch (v.getId()) {
2155 case R.id.previous_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002156 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002157 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2158 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002159 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002160 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002161 return true;
2162 case R.id.next_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);
2167 }
2168 return true;
2169 case R.id.all_apps_button:
Winson Chung785d2eb2011-04-14 16:08:02 -07002170 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002171 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2172 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2173 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002174 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002175 return true;
2176 }
2177
Joe Onorato9c1289c2009-08-17 11:03:03 -04002178 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 return false;
2180 }
2181
2182 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002183 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 }
2185
Michael Jurka0280c3b2010-09-17 15:00:07 -07002186 resetAddInfo();
2187 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002189 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 return true;
2191 }
2192
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 final View itemUnderLongClick = longClickCellInfo.cell;
2194
Winson Chung304dcde2011-01-07 11:17:23 -08002195 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002196 if (itemUnderLongClick == null) {
2197 // User long pressed on empty space
2198 mWorkspace.setAllowLongPress(false);
2199 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2200 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002201 if (LauncherApplication.isScreenLarge()) {
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002202 addItems();
2203 } else {
Winson Chung4dbea792011-05-05 14:21:32 -07002204 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 }
2206 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002207 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002209 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2210 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002211 mAddIntersectCellX = longClickCellInfo.cellX;
2212 mAddIntersectCellY = longClickCellInfo.cellY;
2213 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
2215 }
2216 }
2217 return true;
2218 }
2219
Romain Guye6b8e2f2009-11-10 11:56:55 -08002220 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002221 private void dismissPreview(final View v) {
2222 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002223 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002224 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2225 public void onDismiss() {
2226 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2227 int count = group.getChildCount();
2228 for (int i = 0; i < count; i++) {
2229 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2230 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002231 ArrayList<Bitmap> bitmaps =
2232 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002233 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2234
2235 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002236 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002237 window.setOnDismissListener(null);
2238 }
2239 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002240 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002241 }
2242 v.setTag(null);
2243 }
2244
Romain Guyf8e6a802009-12-07 17:48:02 -08002245 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002246 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002247 }
2248
Romain Guya6abce82009-11-10 02:54:41 -08002249 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002250 final Resources resources = getResources();
2251 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002252
Romain Guya6abce82009-11-10 02:54:41 -08002253 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002254
Romain Guy9d31e9f2009-11-11 18:54:28 -08002255 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002256
Romain Guyf8e6a802009-12-07 17:48:02 -08002257 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002258 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002259 int extraW = (int) ((r.left + r.right) * max);
2260 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002261
2262 int aW = cell.getWidth() - extraW;
2263 float w = aW / max;
2264
2265 int width = cell.getWidth();
2266 int height = cell.getHeight();
2267 int x = cell.getLeftPadding();
2268 int y = cell.getTopPadding();
2269 width -= (x + cell.getRightPadding());
2270 height -= (y + cell.getBottomPadding());
2271
2272 float scale = w / width;
2273
2274 int count = end - start;
2275
2276 final float sWidth = width * scale;
2277 float sHeight = height * scale;
2278
Romain Guye6b8e2f2009-11-10 11:56:55 -08002279 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002280
Romain Guye6b8e2f2009-11-10 11:56:55 -08002281 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2282 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002283
2284 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002285 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002286 cell = (CellLayout) workspace.getChildAt(i);
2287
Romain Guyf8e6a802009-12-07 17:48:02 -08002288 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002289 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002290
2291 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002292 c.scale(scale, scale);
2293 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002294 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002295
Romain Guy9d31e9f2009-11-11 18:54:28 -08002296 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002297 image.setImageBitmap(bitmap);
2298 image.setTag(i);
2299 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002300 image.setOnFocusChangeListener(handler);
2301 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002302 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002303
2304 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002305 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2306
Winson Chungaafa03c2010-06-11 17:34:16 -07002307 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002308 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002309
2310 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002311 p.setContentView(preview);
2312 p.setWidth((int) (sWidth * count + extraW));
2313 p.setHeight((int) (sHeight + extraH));
2314 p.setAnimationStyle(R.style.AnimationPreview);
2315 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002316 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002317 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002318 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002319
Romain Guye6b8e2f2009-11-10 11:56:55 -08002320 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2321 public void onDismiss() {
2322 dismissPreview(anchor);
2323 }
2324 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002325
2326 anchor.setTag(p);
2327 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002328 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002329 }
2330
Romain Guy9d31e9f2009-11-11 18:54:28 -08002331 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002332 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002333
Romain Guye6b8e2f2009-11-10 11:56:55 -08002334 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002335 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002336 }
2337
2338 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002339 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002340 v.post(this);
2341 }
2342
2343 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002344 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002345 }
2346
2347 public void onFocusChange(View v, boolean hasFocus) {
2348 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002349 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002350 }
Romain Guya6abce82009-11-10 02:54:41 -08002351 }
2352 }
2353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 Workspace getWorkspace() {
2355 return mWorkspace;
2356 }
2357
Michael Jurka3c38d562011-02-18 19:22:12 -08002358 TabHost getCustomizationDrawer() {
2359 return mHomeCustomizationDrawer;
2360 }
2361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 @Override
2363 protected Dialog onCreateDialog(int id) {
2364 switch (id) {
2365 case DIALOG_CREATE_SHORTCUT:
2366 return new CreateShortcut().createDialog();
2367 case DIALOG_RENAME_FOLDER:
2368 return new RenameFolder().createDialog();
2369 }
2370
2371 return super.onCreateDialog(id);
2372 }
2373
2374 @Override
2375 protected void onPrepareDialog(int id, Dialog dialog) {
2376 switch (id) {
2377 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 break;
2379 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002380 if (mFolderInfo != null) {
2381 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2382 final CharSequence text = mFolderInfo.title;
2383 input.setText(text);
2384 input.setSelection(0, text.length());
2385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 break;
2387 }
2388 }
2389
2390 void showRenameDialog(FolderInfo info) {
2391 mFolderInfo = info;
2392 mWaitingForResult = true;
2393 showDialog(DIALOG_RENAME_FOLDER);
2394 }
2395
Michael Jurka0280c3b2010-09-17 15:00:07 -07002396 private void showAddDialog(int intersectX, int intersectY) {
2397 resetAddInfo();
2398 mAddIntersectCellX = intersectX;
2399 mAddIntersectCellY = intersectY;
2400 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 mWaitingForResult = true;
2402 showDialog(DIALOG_CREATE_SHORTCUT);
2403 }
2404
Joe Onoratodeb98af2010-02-19 14:59:39 -08002405 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002406 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002407 Bundle bundle = new Bundle();
2408
2409 ArrayList<String> shortcutNames = new ArrayList<String>();
2410 shortcutNames.add(getString(R.string.group_applications));
2411 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2412
2413 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2414 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2415 R.drawable.ic_launcher_application));
2416 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2417
2418 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2419 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002420 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002421 pickIntent.putExtras(bundle);
2422
Joe Onoratodeb98af2010-02-19 14:59:39 -08002423 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002424 }
2425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 private class RenameFolder {
2427 private EditText mInput;
2428
2429 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2431 mInput = (EditText) layout.findViewById(R.id.folder_name);
2432
2433 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2434 builder.setIcon(0);
2435 builder.setTitle(getString(R.string.rename_folder_title));
2436 builder.setCancelable(true);
2437 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2438 public void onCancel(DialogInterface dialog) {
2439 cleanup();
2440 }
2441 });
2442 builder.setNegativeButton(getString(R.string.cancel_action),
2443 new Dialog.OnClickListener() {
2444 public void onClick(DialogInterface dialog, int which) {
2445 cleanup();
2446 }
2447 }
2448 );
2449 builder.setPositiveButton(getString(R.string.rename_action),
2450 new Dialog.OnClickListener() {
2451 public void onClick(DialogInterface dialog, int which) {
2452 changeFolderName();
2453 }
2454 }
2455 );
2456 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002457
2458 final AlertDialog dialog = builder.create();
2459 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2460 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002461 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002462 mInput.requestFocus();
2463 InputMethodManager inputManager = (InputMethodManager)
2464 getSystemService(Context.INPUT_METHOD_SERVICE);
2465 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002466 }
2467 });
2468
2469 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 }
2471
2472 private void changeFolderName() {
2473 final String name = mInput.getText().toString();
2474 if (!TextUtils.isEmpty(name)) {
2475 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002476 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 mFolderInfo.title = name;
2478 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2479
Joe Onorato9c1289c2009-08-17 11:03:03 -04002480 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002481 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002482 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 } else {
2484 final FolderIcon folderIcon = (FolderIcon)
2485 mWorkspace.getViewForTag(mFolderInfo);
2486 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002487 // TODO: At some point we'll probably want some version of setting
2488 // the text for a folder icon.
2489 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 getWorkspace().requestLayout();
2491 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002492 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002493 mWorkspaceLoading = true;
2494 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 }
2496 }
2497 }
2498 cleanup();
2499 }
2500
2501 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 dismissDialog(DIALOG_RENAME_FOLDER);
2503 mWaitingForResult = false;
2504 mFolderInfo = null;
2505 }
2506 }
2507
Daniel Sandler843e8602010-06-07 14:59:01 -04002508 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2509 public boolean isAllAppsVisible() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002510 return (mState == State.ALL_APPS) || (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002511 }
2512
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002513 // AllAppsView.Watcher
2514 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002515 // In XLarge view, we zoom down the workspace below all apps so it's still visible
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002516 if (zoom == 1.0f && !LauncherApplication.isScreenLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002517 mWorkspace.setVisibility(View.GONE);
2518 }
2519 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002520
Michael Jurkaafca9532011-02-16 14:50:35 -08002521 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002522 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002523 }
2524
2525 private void hideToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002526 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2527 button.setVisibility(View.INVISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002528 button.setAlpha(0.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002529 }
2530
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002531 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002532 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002533 *
2534 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2535 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002536 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002537 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002538 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002539 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002540 final boolean showing = show;
2541 final boolean hiding = !show;
2542
2543 final int duration = show ?
2544 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2545 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2546
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002547 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002548 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002549 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002550 anim.addUpdateListener(new AnimatorUpdateListener() {
2551 public void onAnimationUpdate(ValueAnimator animation) {
2552 view.setAlpha((Float) animation.getAnimatedValue());
2553 }
2554 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002555 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002556 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002557 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002558 if (showing) showAndEnableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002559 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002560 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002561 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002564 });
2565 seq.play(anim);
2566 } else {
2567 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002568 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002569 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002570 } else {
2571 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002572 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002573 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002574 }
2575
2576 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002577 * Show/hide the appropriate toolbar buttons for newState.
2578 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002579 *
2580 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002581 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2582 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002583 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002584 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002585 switch (newState) {
2586 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002587 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002588 mDeleteZone.setDragAndDropEnabled(true);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002589 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002590 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
2591 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002592 break;
2593 case ALL_APPS:
Winson Chung785d2eb2011-04-14 16:08:02 -07002594 case APPS_CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002595 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002596 mDeleteZone.setDragAndDropEnabled(false);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002597 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002598 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
2599 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002600 break;
2601 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002602 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002603 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002604 break;
2605 }
2606 }
2607
2608 /**
2609 * Helper method for the cameraZoomIn/cameraZoomOut animations
2610 * @param view The view being animated
2611 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2612 * @param scaleFactor The scale factor used for the zoom
2613 */
2614 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2615 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002616
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002618 // Set pivotY so that at the starting zoom factor, the view is partially
2619 // visible. Modifying initialHeightFactor changes how much of the view is
2620 // initially showing, and hence the perceived angle from which the view enters.
Winson Chung785d2eb2011-04-14 16:08:02 -07002621 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002622 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002623 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002624 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002625 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002626 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 }
2628 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002629
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002630 /**
2631 * Zoom the camera out from the workspace to reveal 'toView'.
2632 * Assumes that the view to show is anchored at either the very top or very bottom
2633 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002634 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002635 */
Winson Chung20f71112011-04-06 14:22:04 -07002636 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002637 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002638 final boolean toAllApps = (toState == State.ALL_APPS)
2639 || (toState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002640
Winson Chung785d2eb2011-04-14 16:08:02 -07002641 final int duration = (toAllApps ?
2642 res.getInteger(R.integer.config_appsCustomizeZoomInTime) :
2643 res.getInteger(R.integer.config_customizeZoomInTime));
2644 final int fadeDuration = (toAllApps ?
2645 res.getInteger(R.integer.config_appsCustomizeFadeInTime) :
2646 res.getInteger(R.integer.config_customizeFadeInTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002647
2648 final float scale = toAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002649 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002650 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2651
Winson Chung785d2eb2011-04-14 16:08:02 -07002652 View tmpView;
2653 if (toAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002654 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002655 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2656 } else {
2657 tmpView = mHomeCustomizationDrawer;
2658 }
2659 final View toView = tmpView;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002660
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002661 setPivotsForZoom(toView, toState, scale);
2662
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002663 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002664 if (!springLoaded) {
2665 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2666
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002667 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002668 // Everytime we launch into AllApps, we reset the successful drop flag which
2669 // controls when it should hide/show the mini workspaces
2670 mAllAppsPagedView.resetSuccessfulDropFlag();
2671 }
Winson Chung20f71112011-04-06 14:22:04 -07002672 } else {
2673 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2674 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002675 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002676 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002677 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002678
2679 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002680 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002681 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002682 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2683 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002684 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002685 toView.setFastScaleX(a * scale + b * 1f);
2686 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002687 }
2688 });
Adam Cohen61033d32010-11-15 18:29:44 -08002689
Adam Cohenf16e5712011-01-13 13:31:45 -08002690 if (toAllApps) {
Winson Chung4e6a9762011-05-09 11:56:34 -07002691 toView.setVisibility(View.VISIBLE);
Michael Jurka2763be32011-02-24 11:19:57 -08002692 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002693 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002694 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002695 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2696 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002697 // don't need to invalidate because we do so above
2698 toView.setFastAlpha(a * 0f + b * 1f);
2699 }
2700 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002701 alphaAnim.start();
2702 }
2703
Michael Jurkaabded662011-03-04 12:06:57 -08002704 if (toView instanceof LauncherTransitionable) {
2705 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002706 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002707 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002708 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002709 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002710 // Prepare the position
2711 toView.setTranslationX(0.0f);
2712 toView.setTranslationY(0.0f);
2713 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002714 toView.bringToFront();
Adam Cohenf16e5712011-01-13 13:31:45 -08002715 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002716 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002717 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002718 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002719 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002720 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002721 // If we don't set the final scale values here, if this animation is cancelled
2722 // it will have the wrong scale value and subsequent cameraPan animations will
2723 // not fix that
2724 toView.setScaleX(1.0f);
2725 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002726 if (toView instanceof LauncherTransitionable) {
2727 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002728 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002729 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002730 });
2731
Chet Haaseb1254a62010-09-07 13:35:00 -07002732 AnimatorSet toolbarHideAnim = new AnimatorSet();
2733 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002734 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2735
2736 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002737 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002738
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002739 if (mStateAnimation != null) mStateAnimation.cancel();
2740 mStateAnimation = new AnimatorSet();
2741 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2742 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002743
2744 // Show the new toolbar buttons just as the main animation is ending
2745 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002746 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2747 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002748 } else {
2749 toView.setTranslationX(0.0f);
2750 toView.setTranslationY(0.0f);
2751 toView.setScaleX(1.0f);
2752 toView.setScaleY(1.0f);
2753 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002754 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002755 if (toView instanceof LauncherTransitionable) {
2756 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2757 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2758 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002759 hideAndShowToolbarButtons(toState, null, null);
2760 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002761 }
2762
2763 /**
2764 * Zoom the camera back into the workspace, hiding 'fromView'.
2765 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002766 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2767 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002768 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002769 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002770 Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002771 final boolean fromAllApps = (fromState == State.ALL_APPS)
2772 || (fromState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002773
2774 int duration = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002775 res.getInteger(R.integer.config_appsCustomizeZoomOutTime) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002776 res.getInteger(R.integer.config_customizeZoomOutTime);
2777
Michael Jurka742574b2011-02-02 23:51:01 -08002778 final float scaleFactor = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002779 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002780 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2781
Winson Chung785d2eb2011-04-14 16:08:02 -07002782 View tmpView;
2783 if (fromAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002784 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002785 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2786 } else {
2787 tmpView = mHomeCustomizationDrawer;
2788 }
2789 final View fromView = tmpView;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002790
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002791 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002792 mCustomizePagedView.endChoiceMode();
2793 mAllAppsPagedView.endChoiceMode();
2794 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002795
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002796 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002797
Michael Jurkad3ef3062010-11-23 16:23:58 -08002798 if (!springLoaded) {
2799 mWorkspace.unshrink(animated);
2800 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002801 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002802 if (mStateAnimation != null) mStateAnimation.cancel();
2803 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002804
Michael Jurka742574b2011-02-02 23:51:01 -08002805 final float oldScaleX = fromView.getScaleX();
2806 final float oldScaleY = fromView.getScaleY();
2807
2808 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2809 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002810 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2811 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002812 ((View)fromView.getParent()).fastInvalidate();
2813 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2814 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2815 }
2816 });
Michael Jurkaabded662011-03-04 12:06:57 -08002817 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002818 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002819 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002820 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2821 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002822 // don't need to invalidate because we do so above
2823 fromView.setFastAlpha(a * 1f + b * 0f);
2824 }
2825 });
Michael Jurkaabded662011-03-04 12:06:57 -08002826 if (fromView instanceof LauncherTransitionable) {
2827 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002828 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002829 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002830 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002831 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002832 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002833 if (fromView instanceof LauncherTransitionable) {
2834 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002835 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002836 }
2837 });
2838
Chet Haaseb1254a62010-09-07 13:35:00 -07002839 AnimatorSet toolbarHideAnim = new AnimatorSet();
2840 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002841 if (!springLoaded) {
2842 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2843 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002844
Adam Cohen61033d32010-11-15 18:29:44 -08002845 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002846
2847 // Show the new toolbar buttons at the very end of the whole animation
2848 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2849 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002850 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2851 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002852 } else {
2853 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002854 if (fromView instanceof LauncherTransitionable) {
2855 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2856 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2857 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002858 if (!springLoaded) {
2859 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2860 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002861 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002862 }
2863
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002864 void showWorkspace(boolean animated) {
2865 showWorkspace(animated, null);
2866 }
2867
2868 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002869 if (layout != null) {
2870 // always animated, but that's ok since we never specify a layout and
2871 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002872 mWorkspace.unshrink(layout);
2873 } else {
2874 mWorkspace.unshrink(animated);
2875 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002876 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002877 closeAllApps(animated);
2878 } else if (mState == State.CUSTOMIZE) {
2879 hideCustomizationDrawer(animated);
2880 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002881
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002882 // Change the state *after* we've called all the transition code
2883 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002884
Winson Chung5fb63472011-02-02 17:03:37 -08002885 // Resume the auto-advance of widgets
2886 mUserPresent = true;
2887 updateRunning();
2888
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002889 // send an accessibility event to announce the context change
2890 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002891 }
2892
Michael Jurkad3ef3062010-11-23 16:23:58 -08002893 void enterSpringLoadedDragMode(CellLayout layout) {
2894 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07002895 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002896 mState = State.ALL_APPS_SPRING_LOADED;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002897 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002898 cameraZoomIn(State.ALL_APPS, true, true);
2899 } else {
2900 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
2901 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002902 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002903 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002904 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002905 }/* else {
2906 // we're already in spring loaded mode; don't do anything
2907 }*/
2908 }
2909
2910 void exitSpringLoadedDragMode() {
2911 if (mState == State.ALL_APPS_SPRING_LOADED) {
2912 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002913 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002914 cameraZoomOut(State.ALL_APPS, true, true);
2915 mState = State.ALL_APPS;
2916 } else {
2917 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2918 mState = State.APPS_CUSTOMIZE;
2919 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002920 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2921 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07002922 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002923 mState = State.CUSTOMIZE;
2924 }/* else {
2925 // we're not in spring loaded mode; don't do anything
2926 }*/
2927 }
2928
Winson Chung785d2eb2011-04-14 16:08:02 -07002929 void showAllApps(boolean animated) {
2930 if (mState != State.WORKSPACE) return;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002931 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002932 cameraZoomOut(State.ALL_APPS, animated, false);
2933 ((View) mAllAppsGrid).requestFocus();
2934
2935 // TODO: fade these two too
2936 mDeleteZone.setVisibility(View.GONE);
2937
2938 // Change the state *after* we've called all the transition code
2939 mState = State.ALL_APPS;
2940 } else {
2941 View appsCustomizePane = findViewById(R.id.apps_customize_pane);
2942 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2943 appsCustomizePane.requestFocus();
2944
2945 // Change the state *after* we've called all the transition code
2946 mState = State.APPS_CUSTOMIZE;
2947 }
2948
2949 // Pause the auto-advance of widgets until we are out of AllApps
2950 mUserPresent = false;
2951 updateRunning();
2952
2953 // Send an accessibility event to announce the context change
2954 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2955 }
2956
Joe Onoratob2061212009-11-24 16:13:54 -05002957 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002958 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002959 * - Home from workspace
2960 * - from center screen
2961 * - from other screens
2962 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002963 * - from center screen
2964 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002965 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002966 * - from center screen
2967 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002968 * - Launch app from workspace and quit
2969 * - with back
2970 * - with home
2971 * - Launch app from all apps and quit
2972 * - with back
2973 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002974 * - Go to a screen that's not the default, then all
2975 * apps, and launch and app, and go back
2976 * - with back
2977 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002978 * - On workspace, long press power and go back
2979 * - with back
2980 * - with home
2981 * - On all apps, long press power and go back
2982 * - with back
2983 * - with home
2984 * - On workspace, power off
2985 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002986 * - Launch an app and turn off the screen while in that app
2987 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002988 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002989 * - From all apps
2990 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002991 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2992 * - From all apps
2993 * - From the center workspace
2994 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002995 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002996 void closeAllApps(boolean animated) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002997 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002998 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
2999 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07003000 cameraZoomIn(State.ALL_APPS, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003001
3002 // Set focus to the AllApps button
3003 findViewById(R.id.all_apps_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003004 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003005 } else {
3006 if (mState == State.APPS_CUSTOMIZE || mState == State.ALL_APPS_SPRING_LOADED) {
3007 mWorkspace.setVisibility(View.VISIBLE);
3008 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
3009
3010 // Set focus to the AllApps button
3011 findViewById(R.id.all_apps_button).requestFocus();
3012 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003013 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003014 }
3015
Joe Onorato7c312c12009-08-13 21:36:53 -07003016 void lockAllApps() {
3017 // TODO
3018 }
3019
3020 void unlockAllApps() {
3021 // TODO
3022 }
3023
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003024 // Show the customization drawer (only exists in x-large configuration)
3025 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003026 if (mState != State.WORKSPACE) {
3027 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003028 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003029
Winson Chung20f71112011-04-06 14:22:04 -07003030 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003031
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003032 // Change the state *after* we've called all the transition code
3033 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003034
3035 // Pause the auto-advance of widgets until we are out of Customization drawer
3036 mUserPresent = false;
3037 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003038 }
3039
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003040 // Hide the customization drawer (only exists in x-large configuration)
3041 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003042 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003043 cameraZoomIn(State.CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003044
3045 // Set focus to the customize button
3046 findViewById(R.id.configure_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003047 }
3048 }
3049
Patrick Dubroy5f445422011-02-18 14:35:21 -08003050 /**
3051 * Add an item from all apps or customize onto the given workspace screen.
3052 * If layout is null, add to the current screen.
3053 */
3054 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003055 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3056 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003057 } else {
3058 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003059 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003060 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003061
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003062 void onWorkspaceClick(CellLayout layout) {
3063 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003064 }
3065
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003066 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003067 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003068 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003069 // Look for the toolbar icon specified in the activity meta-data
3070 Bundle metaData = packageManager.getActivityInfo(
3071 activityName, PackageManager.GET_META_DATA).metaData;
3072 if (metaData != null) {
3073 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3074 if (iconResId != 0) {
3075 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003076 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003077 }
3078 }
3079 } catch (NameNotFoundException e) {
3080 // Do nothing
3081 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003082 return null;
3083 }
3084
3085 // if successful in getting icon, return it; otherwise, set button to use default drawable
3086 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3087 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3088 TextView button = (TextView) findViewById(buttonId);
3089 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3090
3091 // If we were unable to find the icon via the meta-data, use a generic one
3092 if (toolbarIcon == null) {
3093 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3094 return null;
3095 } else {
3096 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3097 return toolbarIcon.getConstantState();
3098 }
3099 }
3100
3101 // if successful in getting icon, return it; otherwise, set button to use default drawable
3102 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3103 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3104 ImageView button = (ImageView) findViewById(buttonId);
3105 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3106
Michael Jurka0423dcf2010-10-05 14:56:18 -07003107 // If we were unable to find the icon via the meta-data, use a generic one
3108 if (toolbarIcon == null) {
3109 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003110 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003111 } else {
3112 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003113 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003114 }
3115 }
3116
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003117 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3118 TextView button = (TextView) findViewById(buttonId);
3119 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3120 }
3121
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003122 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003123 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003124 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003125 }
3126
Michael Jurka0423dcf2010-10-05 14:56:18 -07003127 private void updateGlobalSearchIcon() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003128 if (LauncherApplication.isScreenLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003129 final View searchButton = findViewById(R.id.search_button);
3130 final View searchDivider = findViewById(R.id.search_divider);
3131
Michael Jurka0423dcf2010-10-05 14:56:18 -07003132 final SearchManager searchManager =
3133 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3134 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003135 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003136 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003137 R.id.search_button, activityName, R.drawable.ic_generic_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003138 searchButton.setVisibility(View.VISIBLE);
3139 searchDivider.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003140 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003141 searchButton.setVisibility(View.GONE);
3142 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003143 }
3144 }
3145 }
3146
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003147 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003148 updateButtonWithDrawable(R.id.search_button, d);
3149 }
3150
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003151 private void updateVoiceSearchIcon() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003152 if (LauncherApplication.isScreenLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003153 final View searchDivider = findViewById(R.id.search_divider);
3154 final View voiceButton = findViewById(R.id.voice_button);
3155
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003156 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3157 ComponentName activityName = intent.resolveActivity(getPackageManager());
3158 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003159 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003160 R.id.voice_button, activityName, R.drawable.ic_voice_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003161 searchDivider.setVisibility(View.VISIBLE);
3162 voiceButton.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003163 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003164 searchDivider.setVisibility(View.GONE);
3165 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003166 }
3167 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003168 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003169
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003170 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003171 updateButtonWithDrawable(R.id.voice_button, d);
3172 }
3173
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003174 /**
3175 * Sets the app market icon (shown when all apps is visible on x-large screens)
3176 */
3177 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003178 final View marketButton = findViewById(R.id.market_button);
3179 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3180 // Find the app market activity by resolving an intent.
3181 // (If multiple app markets are installed, it will return the ResolverActivity.)
3182 ComponentName activityName = intent.resolveActivity(getPackageManager());
3183 if (activityName != null) {
3184 mAppMarketIntent = intent;
3185 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
3186 R.id.market_button, activityName, R.drawable.app_market_generic);
3187 marketButton.setVisibility(View.VISIBLE);
3188 } else {
3189 // We should hide and disable the view so that we don't try and restore the visibility
3190 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3191 marketButton.setVisibility(View.GONE);
3192 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003193 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003194 }
3195
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003196 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003197 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003198 }
3199
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003200 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003201 * Displays the shortcut creation dialog and launches, if necessary, the
3202 * appropriate activity.
3203 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003204 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003205 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3206 DialogInterface.OnShowListener {
3207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003208 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003209
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003210 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3214 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003215 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003217 builder.setInverseBackgroundForced(true);
3218
3219 AlertDialog dialog = builder.create();
3220 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003221 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003222 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003224 return dialog;
3225 }
3226
3227 public void onCancel(DialogInterface dialog) {
3228 mWaitingForResult = false;
3229 cleanup();
3230 }
3231
Romain Guycbb89e42009-06-08 15:52:54 -07003232 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003233 }
3234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003236 try {
3237 dismissDialog(DIALOG_CREATE_SHORTCUT);
3238 } catch (Exception e) {
3239 // An exception is thrown if the dialog is not visible, which is fine
3240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003241 }
3242
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003243 /**
3244 * Handle the action clicked in the "Add to home" dialog.
3245 */
3246 public void onClick(DialogInterface dialog, int which) {
3247 Resources res = getResources();
3248 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003249
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003250 switch (which) {
3251 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003252 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003253 break;
3254 }
Romain Guycbb89e42009-06-08 15:52:54 -07003255
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003256 case AddAdapter.ITEM_APPWIDGET: {
3257 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003258
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003259 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3260 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003261 // start the pick activity
3262 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3263 break;
3264 }
Romain Guycbb89e42009-06-08 15:52:54 -07003265
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003266 case AddAdapter.ITEM_WALLPAPER: {
3267 startWallpaper();
3268 break;
3269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003270 }
3271 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003272
3273 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003274 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003275 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003276 }
3277
3278 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003279 * Receives notifications when applications are added/removed.
3280 */
3281 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3282 @Override
3283 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003284 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003285 String reason = intent.getStringExtra("reason");
3286 if (!"homekey".equals(reason)) {
3287 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003288 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003289 animate = false;
3290 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003291 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003292 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003293 }
3294 }
3295
3296 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003297 * Receives notifications whenever the appwidgets are reset.
3298 */
3299 private class AppWidgetResetObserver extends ContentObserver {
3300 public AppWidgetResetObserver() {
3301 super(new Handler());
3302 }
3303
3304 @Override
3305 public void onChange(boolean selfChange) {
3306 onAppWidgetReset();
3307 }
3308 }
3309
3310 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003311 * If the activity is currently paused, signal that we need to re-run the loader
3312 * in onResume.
3313 *
3314 * This needs to be called from incoming places where resources might have been loaded
3315 * while we are paused. That is becaues the Configuration might be wrong
3316 * when we're not running, and if it comes back to what it was when we
3317 * were paused, we are not restarted.
3318 *
3319 * Implementation of the method from LauncherModel.Callbacks.
3320 *
3321 * @return true if we are currently paused. The caller might be able to
3322 * skip some work in that case since we will come back again.
3323 */
3324 public boolean setLoadOnResume() {
3325 if (mPaused) {
3326 Log.i(TAG, "setLoadOnResume");
3327 mOnResumeNeedsLoad = true;
3328 return true;
3329 } else {
3330 return false;
3331 }
3332 }
3333
3334 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003336 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003337 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003338 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003339 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003340 } else {
3341 return SCREEN_COUNT / 2;
3342 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003343 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003344
Winson Chunga12a2502010-12-20 14:41:35 -08003345 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003346 mAllAppsPagedView = view;
3347 }
3348
Joe Onorato9c1289c2009-08-17 11:03:03 -04003349 /**
3350 * Refreshes the shortcuts shown on the workspace.
3351 *
3352 * Implementation of the method from LauncherModel.Callbacks.
3353 */
3354 public void startBinding() {
3355 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003356
3357 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003358 int count = workspace.getChildCount();
3359 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003360 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003361 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3362 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003364
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 if (DEBUG_USER_INTERFACE) {
3366 android.widget.Button finishButton = new android.widget.Button(this);
3367 finishButton.setText("Finish");
3368 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3369
3370 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3371 public void onClick(View v) {
3372 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003375 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003376
3377 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3378 // mDesktopItems -> AppWidgetInfo -> hostView).
3379 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003380 }
3381
3382 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003383 * Bind the items start-end from the list.
3384 *
3385 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003386 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3388
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003389 setLoadOnResume();
3390
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 final Workspace workspace = mWorkspace;
3392
3393 for (int i=start; i<end; i++) {
3394 final ItemInfo item = shortcuts.get(i);
3395 mDesktopItems.add(item);
3396 switch (item.itemType) {
3397 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3398 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003399 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3401 false);
3402 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003403 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003405 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003406 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3408 false);
3409 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003410 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003411 }
3412
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003414 }
3415
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 /**
3417 * Implementation of the method from LauncherModel.Callbacks.
3418 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003419 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003420 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003421 sFolders.clear();
3422 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003423 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424
3425 /**
3426 * Add the views for a widget to the workspace.
3427 *
3428 * Implementation of the method from LauncherModel.Callbacks.
3429 */
3430 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003431 setLoadOnResume();
3432
Daniel Sandler843e8602010-06-07 14:59:01 -04003433 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3434 if (DEBUG_WIDGETS) {
3435 Log.d(TAG, "bindAppWidget: " + item);
3436 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 final Workspace workspace = mWorkspace;
3438
3439 final int appWidgetId = item.appWidgetId;
3440 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003441 if (DEBUG_WIDGETS) {
3442 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3443 }
3444
Joe Onorato9c1289c2009-08-17 11:03:03 -04003445 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3446
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3448 item.hostView.setTag(item);
3449
3450 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3451 item.cellY, item.spanX, item.spanY, false);
3452
Adam Cohended9f8d2010-11-03 13:25:16 -07003453 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3454
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455 workspace.requestLayout();
3456
3457 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003458
3459 if (DEBUG_WIDGETS) {
3460 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3461 + (SystemClock.uptimeMillis()-start) + "ms");
3462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003463 }
3464
3465 /**
3466 * Callback saying that there aren't any more items to bind.
3467 *
3468 * Implementation of the method from LauncherModel.Callbacks.
3469 */
3470 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003471 setLoadOnResume();
3472
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 if (mSavedState != null) {
3474 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003475 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476 }
3477
Adam Cohendf2cc412011-04-27 16:56:57 -07003478 final long[] folders = mSavedState.getLongArray(RUNTIME_STATE_FOLDERS);
3479 if (folders != null) {
3480 for (long folderId : folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003481 final FolderInfo info = sFolders.get(folderId);
Adam Cohena9cf38f2011-05-02 15:36:58 -07003482 final FolderIcon folderIcon = (FolderIcon)
3483 mWorkspace.getViewForTag(info);
3484 if (folderIcon != null) {
3485 openFolder(folderIcon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486 }
3487 }
3488 final Folder openFolder = mWorkspace.getOpenFolder();
3489 if (openFolder != null) {
3490 openFolder.requestFocus();
3491 }
3492 }
3493
Joe Onorato9c1289c2009-08-17 11:03:03 -04003494 mSavedState = null;
3495 }
3496
3497 if (mSavedInstanceState != null) {
3498 super.onRestoreInstanceState(mSavedInstanceState);
3499 mSavedInstanceState = null;
3500 }
3501
Winson Chung32bb5e52011-01-31 14:51:56 -08003502 // Workaround a bug that occurs when rotating the device while the customization mode is
3503 // open, we trigger a new layout on all the CellLayout children.
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003504 if (LauncherApplication.isScreenLarge() && (mState == State.CUSTOMIZE)) {
Winson Chung32bb5e52011-01-31 14:51:56 -08003505 final int childCount = mWorkspace.getChildCount();
3506 for (int i = 0; i < childCount; ++i) {
3507 mWorkspace.getChildAt(i).requestLayout();
3508 }
3509 }
3510
Joe Onorato9c1289c2009-08-17 11:03:03 -04003511 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003512
3513 // If we received the result of any pending adds while the loader was running (e.g. the
3514 // widget configuration forced an orientation change), process them now.
3515 for (int i = 0; i < sPendingAddList.size(); i++) {
3516 completeAdd(sPendingAddList.get(i));
3517 }
3518 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003519 }
3520
3521 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003522 * Updates the icons on the launcher that are affected by changes to the package list
3523 * on the device.
3524 */
3525 private void updateIconsAffectedByPackageManagerChanges() {
3526 updateAppMarketIcon();
3527 updateGlobalSearchIcon();
3528 updateVoiceSearchIcon();
3529 }
3530
3531 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003532 * Add the icons for all apps.
3533 *
3534 * Implementation of the method from LauncherModel.Callbacks.
3535 */
3536 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003537 if (mAllAppsGrid != null) {
3538 mAllAppsGrid.setApps(apps);
3539 }
3540 if (mAppsCustomizeContent != null) {
3541 mAppsCustomizeContent.setApps(apps);
3542 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003543 if (mCustomizePagedView != null) {
3544 mCustomizePagedView.setApps(apps);
3545 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003546 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 }
3548
3549 /**
3550 * A package was installed.
3551 *
3552 * Implementation of the method from LauncherModel.Callbacks.
3553 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003554 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003555 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chung785d2eb2011-04-14 16:08:02 -07003557 if (mAllAppsGrid != null) {
3558 mAllAppsGrid.addApps(apps);
3559 }
3560 if (mAppsCustomizeContent != null) {
3561 mAppsCustomizeContent.addApps(apps);
3562 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003563 if (mCustomizePagedView != null) {
3564 mCustomizePagedView.addApps(apps);
3565 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003566 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 }
3568
3569 /**
3570 * A package was updated.
3571 *
3572 * Implementation of the method from LauncherModel.Callbacks.
3573 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003574 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003575 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003577 if (mWorkspace != null) {
3578 mWorkspace.updateShortcuts(apps);
3579 }
3580 if (mAllAppsGrid != null) {
3581 mAllAppsGrid.updateApps(apps);
3582 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003583 if (mAppsCustomizeContent != null) {
3584 mAppsCustomizeContent.updateApps(apps);
3585 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003586 if (mCustomizePagedView != null) {
3587 mCustomizePagedView.updateApps(apps);
3588 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003589 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 }
3591
3592 /**
3593 * A package was uninstalled.
3594 *
3595 * Implementation of the method from LauncherModel.Callbacks.
3596 */
Joe Onorato36115782010-06-17 13:28:48 -04003597 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003599 if (permanent) {
3600 mWorkspace.removeItems(apps);
3601 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003602 if (mAllAppsGrid != null) {
3603 mAllAppsGrid.removeApps(apps);
3604 }
3605 if (mAppsCustomizeContent != null) {
3606 mAppsCustomizeContent.removeApps(apps);
3607 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003608 if (mCustomizePagedView != null) {
3609 mCustomizePagedView.removeApps(apps);
3610 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003611 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003612 }
Joe Onoratobe386092009-11-17 17:32:16 -08003613
3614 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003615 * A number of packages were updated.
3616 */
3617 public void bindPackagesUpdated() {
3618 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003619 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003620 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003621 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003622 if (mAppsCustomizeContent != null) {
3623 mAppsCustomizeContent.onPackagesUpdated();
3624 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003625 }
3626
Winson Chung400438b2011-01-16 17:53:48 -08003627 private int mapConfigurationOriActivityInfoOri(int configOri) {
3628 final Display d = getWindowManager().getDefaultDisplay();
3629 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3630 switch (d.getRotation()) {
3631 case Surface.ROTATION_0:
3632 case Surface.ROTATION_180:
3633 // We are currently in the same basic orientation as the natural orientation
3634 naturalOri = configOri;
3635 break;
3636 case Surface.ROTATION_90:
3637 case Surface.ROTATION_270:
3638 // We are currently in the other basic orientation to the natural orientation
3639 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3640 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3641 break;
3642 }
3643
3644 int[] oriMap = {
3645 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3646 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3647 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3648 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3649 };
3650 // Since the map starts at portrait, we need to offset if this device's natural orientation
3651 // is landscape.
3652 int indexOffset = 0;
3653 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3654 indexOffset = 1;
3655 }
3656 return oriMap[(d.getRotation() + indexOffset) % 4];
3657 }
3658 public void lockScreenOrientation() {
3659 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3660 .getConfiguration().orientation));
3661 }
3662 public void unlockScreenOrientation() {
3663 mHandler.postDelayed(new Runnable() {
3664 public void run() {
3665 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3666 }
3667 }, mRestoreScreenOrientationDelay);
3668 }
3669
Winson Chung80baf5a2010-08-09 16:03:15 -07003670 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003671 * Prints out out state for debugging.
3672 */
3673 public void dumpState() {
3674 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003675 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003676 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3677 Log.d(TAG, "mRestoring=" + mRestoring);
3678 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3679 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3680 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003681 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003682 mModel.dumpState();
Winson Chung785d2eb2011-04-14 16:08:02 -07003683 if (mAllAppsGrid != null) {
3684 mAllAppsGrid.dumpState();
3685 }
3686 if (mAppsCustomizeContent != null) {
3687 mAppsCustomizeContent.dumpState();
3688 }
Joe Onoratobe386092009-11-17 17:32:16 -08003689 Log.d(TAG, "END launcher2 dump state");
3690 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003691}
Michael Jurka2763be32011-02-24 11:19:57 -08003692
Michael Jurkaabded662011-03-04 12:06:57 -08003693interface LauncherTransitionable {
3694 void onLauncherTransitionStart(Animator animation);
3695 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003696}