blob: 701dc66645118ae40bdb075cd428b7feac9cc307 [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
349 if (LauncherApplication.isScreenXLarge()) {
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
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700898 if (LauncherApplication.isScreenXLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700899 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
900 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
Michael Jurkaaf442092010-06-10 17:01:57 -0700938 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
939 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800940
Michael Jurkaaf442092010-06-10 17:01:57 -0700941 Drawable previous = mPreviousView.getDrawable();
942 Drawable next = mNextView.getDrawable();
943 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944
Michael Jurkaaf442092010-06-10 17:01:57 -0700945 mPreviousView.setHapticFeedbackEnabled(false);
946 mPreviousView.setOnLongClickListener(this);
947 mNextView.setHapticFeedbackEnabled(false);
948 mNextView.setOnLongClickListener(this);
949 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400952 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800954 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955
956 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400957 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -0800958
959 final View allAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -0700960 final View divider = findViewById(R.id.all_apps_divider);
Michael Jurkab8e14472010-12-20 16:06:10 -0800961 final View configureButton = findViewById(R.id.configure_button);
962
Michael Jurka54f7ac32010-08-02 13:56:46 -0700963 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800964 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -0700965 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800966 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -0700967 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700968 dragController.addDragListener(deleteZone);
969
Adam Cohencdc30d52010-12-01 15:09:47 -0800970 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
971 if (allAppsDeleteZone != null) {
972 allAppsDeleteZone.setLauncher(this);
973 allAppsDeleteZone.setDragController(dragController);
974 allAppsDeleteZone.setDragAndDropEnabled(false);
975 dragController.addDragListener(allAppsDeleteZone);
976 dragController.addDropTarget(allAppsDeleteZone);
977 }
978
979 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
980 findViewById(R.id.all_apps_info_target);
981 if (allAppsInfoTarget != null) {
982 allAppsInfoTarget.setLauncher(this);
983 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -0800984 allAppsInfoTarget.setDragAndDropEnabled(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700985 }
986 View marketButton = findViewById(R.id.market_button);
987 if (marketButton != null) {
988 if (allAppsInfoTarget != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800989 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -0800990 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700991 marketButton.setOnClickListener(new OnClickListener() {
992 public void onClick(View v) {
993 onClickAppMarketButton(v);
994 }
995 });
Adam Cohencdc30d52010-12-01 15:09:47 -0800996 }
997
Joe Onorato00acb122009-08-04 16:04:30 -0400998 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400999 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001000 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001001
Joe Onorato00acb122009-08-04 16:04:30 -04001002 // The order here is bottom to top.
1003 dragController.addDropTarget(workspace);
1004 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001005 if (allAppsInfoTarget != null) {
1006 dragController.addDropTarget(allAppsInfoTarget);
1007 }
1008 if (allAppsDeleteZone != null) {
1009 dragController.addDropTarget(allAppsDeleteZone);
1010 }
Winson Chung97d85d22011-04-13 11:27:36 -07001011 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
1012 View.OnKeyListener listener = new ButtonBarKeyEventListener();
1013 int buttonCount = mButtonCluster.getChildCount();
1014 for (int i = 0; i < buttonCount; ++i) {
1015 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
1016 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001017
1018 mAllAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -07001019 mDivider = findViewById(R.id.all_apps_divider);
Michael Jurka838a4ca2011-02-07 13:33:06 -08001020 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001021
1022 // We had previously set these click handlers in XML, but the first time we launched
1023 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1024 // found the right handler. Setting the handlers directly here eliminates that cost.
1025 if (mConfigureButton != null) {
1026 mConfigureButton.setOnClickListener(new OnClickListener() {
1027 public void onClick(View v) {
1028 onClickConfigureButton(v);
1029 }
1030 });
1031 }
1032 if (mDivider != null) {
1033 mDivider.setOnClickListener(new OnClickListener() {
1034 public void onClick(View v) {
1035 onClickAllAppsButton(v);
1036 }
1037 });
1038 }
1039 if (mAllAppsButton != null) {
1040 mAllAppsButton.setOnClickListener(new OnClickListener() {
1041 public void onClick(View v) {
1042 onClickAllAppsButton(v);
1043 }
1044 });
1045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 }
1047
Romain Guy8a73c512009-11-09 19:19:59 -08001048 @SuppressWarnings({"UnusedDeclaration"})
1049 public void previousScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001050 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001051 mWorkspace.scrollLeft();
1052 }
Romain Guy8a73c512009-11-09 19:19:59 -08001053 }
1054
1055 @SuppressWarnings({"UnusedDeclaration"})
1056 public void nextScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001057 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001058 mWorkspace.scrollRight();
1059 }
Romain Guy8a73c512009-11-09 19:19:59 -08001060 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001061
1062 @SuppressWarnings({"UnusedDeclaration"})
1063 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001064 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001065
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001066 int index = -1;
1067 if (v.getId() == R.id.hotseat_left) {
1068 index = 0;
1069 } else if (v.getId() == R.id.hotseat_right) {
1070 index = 1;
1071 }
1072
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001073 // reload these every tap; you never know when they might change
1074 loadHotseats();
1075 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001076 startActivitySafely(
1077 mHotseats[index],
1078 "hotseat"
1079 );
1080 }
1081 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001082
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 /**
1084 * Creates a view representing a shortcut.
1085 *
1086 * @param info The data structure describing the shortcut.
1087 *
1088 * @return A View inflated from R.layout.application.
1089 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001090 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001092 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
1094
1095 /**
1096 * Creates a view representing a shortcut inflated from the specified resource.
1097 *
1098 * @param layoutResId The id of the XML layout used to create the shortcut.
1099 * @param parent The group the shortcut belongs to.
1100 * @param info The data structure describing the shortcut.
1101 *
1102 * @return A View inflated from layoutResId.
1103 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001104 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001105 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1106 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 return favorite;
1109 }
1110
1111 /**
1112 * Add an application shortcut to the workspace.
1113 *
1114 * @param data The intent describing the application.
1115 * @param cellInfo The position on screen where to create the shortcut.
1116 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001117 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001118 int intersectCellX, int intersectCellY) {
1119 final int[] cellXY = mTmpAddItemCellCoordinates;
1120 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1121
1122 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1123 showOutOfSpaceMessage();
1124 return;
1125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001127 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001128
Romain Guy73b979d2009-06-09 12:57:21 -07001129 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001130 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001132 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001133 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1134 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001135 } else {
1136 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
1138 }
Romain Guycbb89e42009-06-08 15:52:54 -07001139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 /**
1141 * Add a shortcut to the workspace.
1142 *
1143 * @param data The intent describing the shortcut.
1144 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001146 private void completeAddShortcut(Intent data, int screen,
1147 int intersectCellX, int intersectCellY) {
1148 final int[] cellXY = mTmpAddItemCellCoordinates;
1149 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001150
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001151 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001152 boolean foundCellSpan = false;
1153 if (touchXY != null) {
1154 // when dragging and dropping, just find the closest free spot
1155 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1156 int[] result = screenLayout.findNearestVacantArea(
1157 touchXY[0], touchXY[1], 1, 1, cellXY);
1158 foundCellSpan = (result != null);
1159 } else {
1160 foundCellSpan = layout.findCellForSpanThatIntersects(
1161 cellXY, 1, 1, intersectCellX, intersectCellY);
1162 }
1163
1164 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001165 showOutOfSpaceMessage();
1166 return;
1167 }
1168
1169 final ShortcutInfo info = mModel.addShortcut(
1170 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171
1172 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001174 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
1176 }
1177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001179 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001181 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001182 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 */
Winson Chungf7640c82011-02-28 13:47:29 -08001184 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001185 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001186
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001187 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1189 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001192 // We have saved the position to which the widget was dragged-- this really only matters
1193 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001194 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001195
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001196 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001197 boolean foundCellSpan = false;
1198 if (touchXY != null) {
1199 // when dragging and dropping, just find the closest free spot
1200 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1201 int[] result = screenLayout.findNearestVacantArea(
1202 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001203 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001204 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001205 // if we long pressed on an empty cell to bring up a menu,
1206 // make sure we intersect the empty cell
1207 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1208 // findCellForSpanThatIntersects will just ignore them
1209 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1210 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001211 }
1212
Michael Jurka0280c3b2010-09-17 15:00:07 -07001213 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001214 if (appWidgetId != -1) {
1215 // Deleting an app widget ID is a void call but writes to disk before returning
1216 // to the caller...
1217 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1218 new Thread("deleteAppWidgetId") {
1219 public void run() {
1220 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1221 }
1222 }.start();
1223 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001224 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001225 return;
1226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001228 // Build Launcher-specific widget info and save to database
1229 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001230 launcherInfo.spanX = spanXY[0];
1231 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 LauncherModel.addItemToDatabase(this, launcherInfo,
1234 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001235 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236
1237 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001241 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001242
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001243 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001245
Michael Jurka0280c3b2010-09-17 15:00:07 -07001246 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001248
1249 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
1251 }
Romain Guycbb89e42009-06-08 15:52:54 -07001252
Adam Cohended9f8d2010-11-03 13:25:16 -07001253 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1254 @Override
1255 public void onReceive(Context context, Intent intent) {
1256 final String action = intent.getAction();
1257 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1258 mUserPresent = false;
1259 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001260
1261 // Reset AllApps to it's initial state
1262 if (mAllAppsGrid != null) {
1263 mAllAppsGrid.reset();
1264 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001265 if (mAppsCustomizeContent != null) {
1266 mAppsCustomizeContent.reset();
1267 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001268 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1269 mUserPresent = true;
1270 updateRunning();
1271 }
1272 }
1273 };
1274
1275 @Override
1276 public void onAttachedToWindow() {
1277 super.onAttachedToWindow();
1278
1279 // Listen for broadcasts related to user-presence
1280 final IntentFilter filter = new IntentFilter();
1281 filter.addAction(Intent.ACTION_SCREEN_OFF);
1282 filter.addAction(Intent.ACTION_USER_PRESENT);
1283 registerReceiver(mReceiver, filter);
1284
Adam Cohend113e0c2010-11-11 10:48:05 -08001285 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001286 mVisible = true;
1287 }
1288
1289 @Override
1290 public void onDetachedFromWindow() {
1291 super.onDetachedFromWindow();
1292 mVisible = false;
1293
Adam Cohend113e0c2010-11-11 10:48:05 -08001294 if (mAttached) {
1295 unregisterReceiver(mReceiver);
1296 mAttached = false;
1297 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001298 updateRunning();
1299 }
1300
1301 public void onWindowVisibilityChanged(int visibility) {
1302 mVisible = visibility == View.VISIBLE;
1303 updateRunning();
1304 }
1305
1306 private void sendAdvanceMessage(long delay) {
1307 mHandler.removeMessages(ADVANCE_MSG);
1308 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1309 mHandler.sendMessageDelayed(msg, delay);
1310 mAutoAdvanceSentTime = System.currentTimeMillis();
1311 }
1312
1313 private void updateRunning() {
1314 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1315 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1316 mAutoAdvanceRunning = autoAdvanceRunning;
1317 if (autoAdvanceRunning) {
1318 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1319 sendAdvanceMessage(delay);
1320 } else {
1321 if (!mWidgetsToAdvance.isEmpty()) {
1322 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1323 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1324 }
1325 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001326 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001327 }
1328 }
1329 }
1330
1331 private final Handler mHandler = new Handler() {
1332 @Override
1333 public void handleMessage(Message msg) {
1334 if (msg.what == ADVANCE_MSG) {
1335 int i = 0;
1336 for (View key: mWidgetsToAdvance.keySet()) {
1337 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1338 final int delay = mAdvanceStagger * i;
1339 if (v instanceof Advanceable) {
1340 postDelayed(new Runnable() {
1341 public void run() {
1342 ((Advanceable) v).advance();
1343 }
1344 }, delay);
1345 }
1346 i++;
1347 }
1348 sendAdvanceMessage(mAdvanceInterval);
1349 }
1350 }
1351 };
1352
1353 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1354 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1355 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1356 if (v instanceof Advanceable) {
1357 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001358 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001359 updateRunning();
1360 }
1361 }
1362
1363 void removeWidgetToAutoAdvance(View hostView) {
1364 if (mWidgetsToAdvance.containsKey(hostView)) {
1365 mWidgetsToAdvance.remove(hostView);
1366 updateRunning();
1367 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368 }
1369
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1371 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001372 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001373 launcherInfo.hostView = null;
1374 }
1375
Adam Cohended9f8d2010-11-03 13:25:16 -07001376 void showOutOfSpaceMessage() {
1377 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1378 }
1379
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001380 public LauncherAppWidgetHost getAppWidgetHost() {
1381 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
Romain Guycbb89e42009-06-08 15:52:54 -07001383
Winson Chunga9abd0e2010-10-27 17:18:37 -07001384 public LauncherModel getModel() {
1385 return mModel;
1386 }
1387
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001388 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001389 getWindow().closeAllPanels();
1390
1391 try {
1392 dismissDialog(DIALOG_CREATE_SHORTCUT);
1393 // Unlock the workspace if the dialog was showing
1394 } catch (Exception e) {
1395 // An exception is thrown if the dialog is not visible, which is fine
1396 }
1397
1398 try {
1399 dismissDialog(DIALOG_RENAME_FOLDER);
1400 // Unlock the workspace if the dialog was showing
1401 } catch (Exception e) {
1402 // An exception is thrown if the dialog is not visible, which is fine
1403 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001404
1405 // Whatever we were doing is hereby canceled.
1406 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001407 }
1408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 @Override
1410 protected void onNewIntent(Intent intent) {
1411 super.onNewIntent(intent);
1412
1413 // Close the menu
1414 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001415 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001416 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001417
Joe Onorato14f122b2009-11-19 14:06:36 -08001418 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1419 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001420
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001421 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001422 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001423 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001424 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001425 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001426 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001427 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001428
Joe Onorato3a8820b2009-11-10 15:06:42 -08001429 final View v = getWindow().peekDecorView();
1430 if (v != null && v.getWindowToken() != null) {
1431 InputMethodManager imm = (InputMethodManager)getSystemService(
1432 INPUT_METHOD_SERVICE);
1433 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001435
1436 // Reset AllApps to it's initial state
1437 if (mAllAppsGrid != null) {
1438 mAllAppsGrid.reset();
1439 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001440 if (mAppsCustomizeContent != null) {
1441 mAppsCustomizeContent.reset();
1442 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
1444 }
1445
1446 @Override
1447 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1448 // Do not call super here
1449 mSavedInstanceState = savedInstanceState;
1450 }
1451
1452 @Override
1453 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001454 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455
1456 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1457 if (folders.size() > 0) {
1458 final int count = folders.size();
1459 long[] ids = new long[count];
1460 for (int i = 0; i < count; i++) {
1461 final FolderInfo info = folders.get(i).getInfo();
1462 ids[i] = info.id;
1463 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001464 outState.putLongArray(RUNTIME_STATE_FOLDERS, ids);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 } else {
1466 super.onSaveInstanceState(outState);
1467 }
1468
Michael Jurka883f55b2010-10-21 15:47:14 -07001469 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470
Michael Jurka0280c3b2010-09-17 15:00:07 -07001471 if (mAddScreen > -1 && mWaitingForResult) {
1472 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1473 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1474 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
1476
1477 if (mFolderInfo != null && mWaitingForResult) {
1478 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1479 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1480 }
Michael Jurka946ad472010-07-09 18:05:18 -07001481
Winson Chung43b119d2010-12-06 16:45:05 -08001482 // Save the current AllApps drawer tab
1483 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1484 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1485 String currentTabTag = tabhost.getCurrentTabTag();
1486 if (currentTabTag != null) {
1487 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001488 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001489 }
1490 }
Winson Chung43b119d2010-12-06 16:45:05 -08001491 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001492 if (mHomeCustomizationDrawer != null) {
1493 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1494 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001495 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001496 }
1497 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001498 // Save the current AppsCustomize tab
1499 if (mAppsCustomizeTabHost != null) {
1500 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1501 if (currentTabTag != null) {
1502 outState.putString("apps_customize_currentTab", currentTabTag);
1503 }
1504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 }
1506
1507 @Override
1508 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001510
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001512 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001514 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001516 mAppWidgetHost = null;
1517
1518 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519
1520 TextKeyListener.getInstance().release();
1521
Joe Onorato9c1289c2009-08-17 11:03:03 -04001522 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523
Joe Onorato9c1289c2009-08-17 11:03:03 -04001524 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001525
1526 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001527
Patrick Dubroyab962b72010-07-26 12:10:10 -07001528 // Some launcher layouts don't have a previous and next view
1529 if (mPreviousView != null) {
1530 dismissPreview(mPreviousView);
1531 }
1532 if (mNextView != null) {
1533 dismissPreview(mNextView);
1534 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001535
1536 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001537
1538 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1539 mWorkspace.removeAllViews();
1540 mWorkspace = null;
1541 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001542
1543 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
1545
1546 @Override
1547 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001548 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 super.startActivityForResult(intent, requestCode);
1550 }
1551
1552 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001553 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001555
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001556 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001557
Karl Rosaen138a0412009-04-23 19:00:21 -07001558 if (initialQuery == null) {
1559 // Use any text typed in the launcher as the initial query
1560 initialQuery = getTypedText();
1561 clearTypedText();
1562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 if (appSearchData == null) {
1564 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001565 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
Romain Guycbb89e42009-06-08 15:52:54 -07001567
Karl Rosaen138a0412009-04-23 19:00:21 -07001568 final SearchManager searchManager =
1569 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001570 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001571 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
1573
1574 @Override
1575 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001576 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001577 return false;
1578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579
1580 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1583 .setIcon(android.R.drawable.ic_menu_add)
1584 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001585 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1586 .setIcon(android.R.drawable.ic_menu_manage)
1587 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001588 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 .setIcon(android.R.drawable.ic_menu_gallery)
1590 .setAlphabeticShortcut('W');
1591 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1592 .setIcon(android.R.drawable.ic_search_category_default)
1593 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1594 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1595 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1596 .setAlphabeticShortcut('N');
1597
1598 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001599 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1600 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601
1602 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1603 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1604 .setIntent(settings);
1605
1606 return true;
1607 }
1608
1609 @Override
1610 public boolean onPrepareOptionsMenu(Menu menu) {
1611 super.onPrepareOptionsMenu(menu);
1612
Winson Chung785d2eb2011-04-14 16:08:02 -07001613 if (mAllAppsGrid != null) {
1614 // If all apps is animating, don't show the menu, because we don't know
1615 // which one to show.
1616 if (mAllAppsGrid.isAnimating()) {
1617 return false;
1618 }
1619
1620 // Only show the add and wallpaper options when we're not in all apps.
1621 boolean visible = !mAllAppsGrid.isVisible();
1622 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1623 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1624
1625 // Disable add if the workspace is full.
1626 if (visible) {
1627 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1628 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
1629 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001630 }
1631
Winson Chung785d2eb2011-04-14 16:08:02 -07001632 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1633 // related code?
1634 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635
1636 return true;
1637 }
1638
1639 @Override
1640 public boolean onOptionsItemSelected(MenuItem item) {
1641 switch (item.getItemId()) {
1642 case MENU_ADD:
1643 addItems();
1644 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001645 case MENU_MANAGE_APPS:
1646 manageApps();
1647 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 case MENU_WALLPAPER_SETTINGS:
1649 startWallpaper();
1650 return true;
1651 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001652 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 return true;
1654 case MENU_NOTIFICATIONS:
1655 showNotifications();
1656 return true;
1657 }
1658
1659 return super.onOptionsItemSelected(item);
1660 }
Romain Guycbb89e42009-06-08 15:52:54 -07001661
Karl Rosaen138a0412009-04-23 19:00:21 -07001662 /**
1663 * Indicates that we want global search for this activity by setting the globalSearch
1664 * argument for {@link #startSearch} to true.
1665 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001667 @Override
1668 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001669 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001670 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001671 }
1672
Joe Onorato9c1289c2009-08-17 11:03:03 -04001673 public boolean isWorkspaceLocked() {
1674 return mWorkspaceLoading || mWaitingForResult;
1675 }
1676
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001677 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1678 private boolean isPreviewVisible() {
1679 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1680 (mNextView != null && mNextView.getTag() != null);
1681 }
1682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001684 if (LauncherApplication.isScreenXLarge()) {
1685 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001686 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001687 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001688 }
1689 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001690 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001691 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 }
1694
Michael Jurka0280c3b2010-09-17 15:00:07 -07001695 private void resetAddInfo() {
1696 mAddScreen = -1;
1697 mAddIntersectCellX = -1;
1698 mAddIntersectCellY = -1;
1699 mAddDropPosition = null;
1700 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001701
Winson Chung55cef262010-10-28 14:14:18 -07001702 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001703 resetAddInfo();
1704 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001705 mAddDropPosition = position;
1706
Michael Jurkaaf442092010-06-10 17:01:57 -07001707 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001708 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1709 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001710 }
1711
Winson Chung7ad01412010-09-27 11:33:03 -07001712 private void manageApps() {
1713 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1714 }
1715
Michael Jurkaaf442092010-06-10 17:01:57 -07001716 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001717 // TODO: catch bad widget exception when sent
1718 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001719 // TODO: Is this log message meaningful?
1720 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001721 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001722 }
1723
Winson Chung55cef262010-10-28 14:14:18 -07001724 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001725 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726
Bjorn Bringert7984c942009-12-09 15:38:25 +00001727 if (appWidget.configure != null) {
1728 // Launch over to configure widget, if needed
1729 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1730 intent.setComponent(appWidget.configure);
1731 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001732 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001733 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1734 intent.putExtra(
1735 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1736 info.mimeType);
1737
1738 final String mimeType = info.mimeType;
1739 final ClipData clipData = (ClipData) info.configurationData;
1740 final ClipDescription clipDesc = clipData.getDescription();
1741 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1742 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001743 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001744 final CharSequence stringData = item.getText();
1745 final Uri uriData = item.getUri();
1746 final Intent intentData = item.getIntent();
1747 final String key =
1748 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1749 if (uriData != null) {
1750 intent.putExtra(key, uriData);
1751 } else if (intentData != null) {
1752 intent.putExtra(key, intentData);
1753 } else if (stringData != null) {
1754 intent.putExtra(key, stringData);
1755 }
1756 break;
1757 }
1758 }
1759 }
Winson Chung55cef262010-10-28 14:14:18 -07001760 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001761
Romain Guy8e633c52010-03-04 12:51:36 -08001762 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001764 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001765 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 }
1767 }
Romain Guycbb89e42009-06-08 15:52:54 -07001768
Michael Jurka0280c3b2010-09-17 15:00:07 -07001769 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1770 resetAddInfo();
1771 mAddScreen = screen;
1772 mAddDropPosition = position;
1773
1774 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1775 createShortcutIntent.setComponent(componentName);
1776 processShortcut(createShortcutIntent);
1777 }
1778
Joe Onoratodeb98af2010-02-19 14:59:39 -08001779 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001780 // Handle case where user selected "Applications"
1781 String applicationName = getResources().getString(R.string.group_applications);
1782 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001783
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001784 if (applicationName != null && applicationName.equals(shortcutName)) {
1785 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1786 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001787
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001788 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1789 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001790 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001791 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001792 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001793 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001794 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 }
1796
Winson Chung24ab2f12010-09-16 14:10:47 -07001797 void processWallpaper(Intent intent) {
1798 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1799 }
1800
Adam Cohendf035382011-04-11 17:22:04 -07001801 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001802 FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 folderInfo.title = getText(R.string.folder_name);
1804
Michael Jurka0280c3b2010-09-17 15:00:07 -07001805 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1806 final int[] cellXY = mTmpAddItemCellCoordinates;
1807 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1808 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001809 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811
1812 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001813 LauncherModel.addItemToDatabase(this, folderInfo,
1814 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001815 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001816 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817
1818 // Create the view
1819 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001820 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1821 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001822 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001823 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 }
Romain Guycbb89e42009-06-08 15:52:54 -07001825
Joe Onorato9c1289c2009-08-17 11:03:03 -04001826 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001827 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001828 }
1829
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 private void showNotifications() {
1831 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1832 if (statusBar != null) {
1833 statusBar.expand();
1834 }
1835 }
1836
1837 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001838 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001840 Intent chooser = Intent.createChooser(pickWallpaper,
1841 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001842 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1843 // Removed in Eclair MR1
1844// WallpaperManager wm = (WallpaperManager)
1845// getSystemService(Context.WALLPAPER_SERVICE);
1846// WallpaperInfo wi = wm.getWallpaperInfo();
1847// if (wi != null && wi.getSettingsActivity() != null) {
1848// LabeledIntent li = new LabeledIntent(getPackageName(),
1849// R.string.configure_wallpaper, 0);
1850// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1851// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1852// }
Mike Clerona0618e42009-10-22 13:55:21 -07001853 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 }
1855
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001856 /**
1857 * Registers various content observers. The current implementation registers
1858 * only a favorites observer to keep track of the favorites applications.
1859 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001860 private void registerContentObservers() {
1861 ContentResolver resolver = getContentResolver();
1862 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1863 true, mWidgetObserver);
1864 }
1865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 @Override
1867 public boolean dispatchKeyEvent(KeyEvent event) {
1868 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1869 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001871 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001872 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001873 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001874 dumpState();
1875 return true;
1876 }
1877 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001878 }
1879 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1880 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001881 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 return true;
1883 }
1884 }
1885
1886 return super.dispatchKeyEvent(event);
1887 }
1888
Joe Onorato88ec0992009-11-19 13:16:06 -08001889 @Override
1890 public void onBackPressed() {
Winson Chung785d2eb2011-04-14 16:08:02 -07001891 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001892 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001893 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001894 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001895 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001896 dismissPreview(mPreviousView);
1897 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001898 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001899 mWorkspace.exitWidgetResizeMode();
1900
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001901 // Back button is a no-op here, but give at least some feedback for the button press
1902 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001903 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001904 }
1905
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 private void closeFolder() {
1907 Folder folder = mWorkspace.getOpenFolder();
1908 if (folder != null) {
1909 closeFolder(folder);
1910 }
1911 }
1912
1913 void closeFolder(Folder folder) {
1914 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001915
Michael Jurka8c920dd2011-01-20 14:16:56 -08001916 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001918 CellLayout cl = (CellLayout) parent;
Adam Cohendf2cc412011-04-27 16:56:57 -07001919 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001921
1922 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 folder.onClose();
1924 }
1925
1926 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001927 * Re-listen when widgets are reset.
1928 */
1929 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001930 if (mAppWidgetHost != null) {
1931 mAppWidgetHost.startListening();
1932 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001933 }
1934
1935 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001936 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1937 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939 private void unbindDesktopItems() {
1940 for (ItemInfo item: mDesktopItems) {
1941 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001943 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 /**
1947 * Launches the intent referred by the clicked shortcut.
1948 *
1949 * @param v The view representing the clicked shortcut.
1950 */
1951 public void onClick(View v) {
1952 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001953 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001955 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001956 int[] pos = new int[2];
1957 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001958 intent.setSourceBounds(new Rect(pos[0], pos[1],
1959 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001960 boolean success = startActivitySafely(intent, tag);
1961
1962 if (success && v instanceof BubbleTextView) {
1963 mWaitingForResume = (BubbleTextView) v;
1964 mWaitingForResume.setStayPressed(true);
1965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 } else if (tag instanceof FolderInfo) {
1967 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001968 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001969 if (mState == State.ALL_APPS) {
1970 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001971 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001972 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975 }
1976
Michael Jurka0e260592010-06-30 17:07:39 -07001977 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001978 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001979 // clicking anywhere on the workspace causes the customization drawer to slide down
1980 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001981 return false;
1982 }
1983
Michael Jurkaaf442092010-06-10 17:01:57 -07001984 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001985 * Event handler for the search button
1986 *
1987 * @param v The view that was clicked.
1988 */
1989 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001990 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001991 // Use a custom animation for launching search
1992 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001993 }
1994
1995 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001996 * Event handler for the voice button
1997 *
1998 * @param v The view that was clicked.
1999 */
2000 public void onClickVoiceButton(View v) {
2001 startVoiceSearch();
2002 }
2003
2004 private void startVoiceSearch() {
2005 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2006 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2007 startActivity(intent);
2008 }
2009
2010 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002011 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002012 * enters home screen customization mode.
2013 *
2014 * @param v The view that was clicked.
2015 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002016 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002017 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002018 }
2019
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002020 /**
2021 * Event handler for the "grid" button that appears on the home screen, which
2022 * enters all apps mode.
2023 *
2024 * @param v The view that was clicked.
2025 */
2026 public void onClickAllAppsButton(View v) {
2027 showAllApps(true);
2028 }
2029
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002030 public void onClickAppMarketButton(View v) {
2031 if (mAppMarketIntent != null) {
2032 startActivitySafely(mAppMarketIntent, "app market");
2033 }
2034 }
2035
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002036 void startApplicationDetailsActivity(ComponentName componentName) {
2037 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002038 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2039 Uri.fromParts("package", packageName, null));
2040 startActivity(intent);
2041 }
2042
Patrick Dubroy5539af72010-09-07 15:22:01 -07002043 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2044 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2045 // System applications cannot be installed. For now, show a toast explaining that.
2046 // We may give them the option of disabling apps this way.
2047 int messageId = R.string.uninstall_system_app_text;
2048 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2049 } else {
2050 String packageName = appInfo.componentName.getPackageName();
2051 String className = appInfo.componentName.getClassName();
2052 Intent intent = new Intent(
2053 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2054 startActivity(intent);
2055 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002056 }
2057
Michael Jurkaddd62e92011-02-16 17:49:14 -08002058 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2060 try {
2061 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002062 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 } catch (ActivityNotFoundException e) {
2064 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002065 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 } catch (SecurityException e) {
2067 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002068 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002070 "or use the exported attribute for this activity. "
2071 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002073 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002075
Romain Guy8e633c52010-03-04 12:51:36 -08002076 void startActivityForResultSafely(Intent intent, int requestCode) {
2077 try {
2078 startActivityForResult(intent, requestCode);
2079 } catch (ActivityNotFoundException e) {
2080 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2081 } catch (SecurityException e) {
2082 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2083 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2084 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2085 "or use the exported attribute for this activity.", e);
2086 }
2087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088
2089 private void handleFolderClick(FolderInfo folderInfo) {
2090 if (!folderInfo.opened) {
2091 // Close any open folder
2092 closeFolder();
2093 // Open the requested folder
2094 openFolder(folderInfo);
2095 } else {
2096 // Find the open folder...
2097 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2098 int folderScreen;
2099 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002100 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 // .. and close it
2102 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002103 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 // Close any folder open on the current screen
2105 closeFolder();
2106 // Pull the folder onto this screen
2107 openFolder(folderInfo);
2108 }
2109 }
2110 }
2111 }
2112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002114 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 * is animated relative to the specified View. If the View is null, no animation
2116 * is played.
2117 *
2118 * @param folderInfo The FolderInfo describing the folder to open.
2119 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002120 public void openFolder(FolderInfo folderInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -07002121 Folder openFolder = Folder.fromXml(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122
Joe Onorato00acb122009-08-04 16:04:30 -04002123 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 openFolder.setLauncher(this);
2125
2126 openFolder.bind(folderInfo);
2127 folderInfo.opened = true;
2128
Winson Chungaafa03c2010-06-11 17:34:16 -07002129 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
Adam Cohendf2cc412011-04-27 16:56:57 -07002130 openFolder.animateOpen();
Winson Chungaafa03c2010-06-11 17:34:16 -07002131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 openFolder.onOpen();
2133 }
2134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002136 if (mState != State.WORKSPACE) {
2137 return false;
2138 }
2139
Romain Guy1fbc1c82009-11-09 20:43:08 -08002140 switch (v.getId()) {
2141 case R.id.previous_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002142 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002143 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2144 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002145 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002146 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002147 return true;
2148 case R.id.next_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002149 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002150 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2151 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002152 showPreviews(v);
2153 }
2154 return true;
2155 case R.id.all_apps_button:
Winson Chung785d2eb2011-04-14 16:08:02 -07002156 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002157 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2158 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2159 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002160 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002161 return true;
2162 }
2163
Joe Onorato9c1289c2009-08-17 11:03:03 -04002164 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 return false;
2166 }
2167
2168 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002169 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 }
2171
Michael Jurka0280c3b2010-09-17 15:00:07 -07002172 resetAddInfo();
2173 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002175 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 return true;
2177 }
2178
Michael Jurka0280c3b2010-09-17 15:00:07 -07002179 final View itemUnderLongClick = longClickCellInfo.cell;
2180
Winson Chung304dcde2011-01-07 11:17:23 -08002181 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002182 if (itemUnderLongClick == null) {
2183 // User long pressed on empty space
2184 mWorkspace.setAllowLongPress(false);
2185 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2186 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002187 if (LauncherApplication.isScreenXLarge()) {
2188 addItems();
2189 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002190 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
2192 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002195 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2196 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002197 mAddIntersectCellX = longClickCellInfo.cellX;
2198 mAddIntersectCellY = longClickCellInfo.cellY;
2199 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 }
2201 }
2202 }
2203 return true;
2204 }
2205
Romain Guye6b8e2f2009-11-10 11:56:55 -08002206 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002207 private void dismissPreview(final View v) {
2208 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002209 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002210 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2211 public void onDismiss() {
2212 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2213 int count = group.getChildCount();
2214 for (int i = 0; i < count; i++) {
2215 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2216 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002217 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2218 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2219
2220 v.setTag(R.id.workspace, null);
2221 v.setTag(R.id.icon, null);
2222 window.setOnDismissListener(null);
2223 }
2224 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002225 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002226 }
2227 v.setTag(null);
2228 }
2229
Romain Guyf8e6a802009-12-07 17:48:02 -08002230 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002231 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002232 }
2233
Romain Guya6abce82009-11-10 02:54:41 -08002234 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002235 final Resources resources = getResources();
2236 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002237
Romain Guya6abce82009-11-10 02:54:41 -08002238 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002239
Romain Guy9d31e9f2009-11-11 18:54:28 -08002240 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002241
Romain Guyf8e6a802009-12-07 17:48:02 -08002242 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002243 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002244 int extraW = (int) ((r.left + r.right) * max);
2245 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002246
2247 int aW = cell.getWidth() - extraW;
2248 float w = aW / max;
2249
2250 int width = cell.getWidth();
2251 int height = cell.getHeight();
2252 int x = cell.getLeftPadding();
2253 int y = cell.getTopPadding();
2254 width -= (x + cell.getRightPadding());
2255 height -= (y + cell.getBottomPadding());
2256
2257 float scale = w / width;
2258
2259 int count = end - start;
2260
2261 final float sWidth = width * scale;
2262 float sHeight = height * scale;
2263
Romain Guye6b8e2f2009-11-10 11:56:55 -08002264 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002265
Romain Guye6b8e2f2009-11-10 11:56:55 -08002266 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2267 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002268
2269 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002270 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002271 cell = (CellLayout) workspace.getChildAt(i);
2272
Romain Guyf8e6a802009-12-07 17:48:02 -08002273 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002274 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002275
2276 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002277 c.scale(scale, scale);
2278 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002279 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002280
Romain Guy9d31e9f2009-11-11 18:54:28 -08002281 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002282 image.setImageBitmap(bitmap);
2283 image.setTag(i);
2284 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002285 image.setOnFocusChangeListener(handler);
2286 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002287 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002288
2289 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002290 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2291
Winson Chungaafa03c2010-06-11 17:34:16 -07002292 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002293 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002294
2295 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002296 p.setContentView(preview);
2297 p.setWidth((int) (sWidth * count + extraW));
2298 p.setHeight((int) (sHeight + extraH));
2299 p.setAnimationStyle(R.style.AnimationPreview);
2300 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002301 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002302 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002303 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002304
Romain Guye6b8e2f2009-11-10 11:56:55 -08002305 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2306 public void onDismiss() {
2307 dismissPreview(anchor);
2308 }
2309 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002310
2311 anchor.setTag(p);
2312 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002313 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002314 }
2315
Romain Guy9d31e9f2009-11-11 18:54:28 -08002316 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002317 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002318
Romain Guye6b8e2f2009-11-10 11:56:55 -08002319 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002320 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002321 }
2322
2323 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002324 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002325 v.post(this);
2326 }
2327
2328 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002329 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002330 }
2331
2332 public void onFocusChange(View v, boolean hasFocus) {
2333 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002334 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002335 }
Romain Guya6abce82009-11-10 02:54:41 -08002336 }
2337 }
2338
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 Workspace getWorkspace() {
2340 return mWorkspace;
2341 }
2342
Michael Jurka3c38d562011-02-18 19:22:12 -08002343 TabHost getCustomizationDrawer() {
2344 return mHomeCustomizationDrawer;
2345 }
2346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 @Override
2348 protected Dialog onCreateDialog(int id) {
2349 switch (id) {
2350 case DIALOG_CREATE_SHORTCUT:
2351 return new CreateShortcut().createDialog();
2352 case DIALOG_RENAME_FOLDER:
2353 return new RenameFolder().createDialog();
2354 }
2355
2356 return super.onCreateDialog(id);
2357 }
2358
2359 @Override
2360 protected void onPrepareDialog(int id, Dialog dialog) {
2361 switch (id) {
2362 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363 break;
2364 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002365 if (mFolderInfo != null) {
2366 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2367 final CharSequence text = mFolderInfo.title;
2368 input.setText(text);
2369 input.setSelection(0, text.length());
2370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 break;
2372 }
2373 }
2374
2375 void showRenameDialog(FolderInfo info) {
2376 mFolderInfo = info;
2377 mWaitingForResult = true;
2378 showDialog(DIALOG_RENAME_FOLDER);
2379 }
2380
Michael Jurka0280c3b2010-09-17 15:00:07 -07002381 private void showAddDialog(int intersectX, int intersectY) {
2382 resetAddInfo();
2383 mAddIntersectCellX = intersectX;
2384 mAddIntersectCellY = intersectY;
2385 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 mWaitingForResult = true;
2387 showDialog(DIALOG_CREATE_SHORTCUT);
2388 }
2389
Joe Onoratodeb98af2010-02-19 14:59:39 -08002390 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002391 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002392 Bundle bundle = new Bundle();
2393
2394 ArrayList<String> shortcutNames = new ArrayList<String>();
2395 shortcutNames.add(getString(R.string.group_applications));
2396 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2397
2398 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2399 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2400 R.drawable.ic_launcher_application));
2401 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2402
2403 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2404 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002405 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002406 pickIntent.putExtras(bundle);
2407
Joe Onoratodeb98af2010-02-19 14:59:39 -08002408 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002409 }
2410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 private class RenameFolder {
2412 private EditText mInput;
2413
2414 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2416 mInput = (EditText) layout.findViewById(R.id.folder_name);
2417
2418 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2419 builder.setIcon(0);
2420 builder.setTitle(getString(R.string.rename_folder_title));
2421 builder.setCancelable(true);
2422 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2423 public void onCancel(DialogInterface dialog) {
2424 cleanup();
2425 }
2426 });
2427 builder.setNegativeButton(getString(R.string.cancel_action),
2428 new Dialog.OnClickListener() {
2429 public void onClick(DialogInterface dialog, int which) {
2430 cleanup();
2431 }
2432 }
2433 );
2434 builder.setPositiveButton(getString(R.string.rename_action),
2435 new Dialog.OnClickListener() {
2436 public void onClick(DialogInterface dialog, int which) {
2437 changeFolderName();
2438 }
2439 }
2440 );
2441 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002442
2443 final AlertDialog dialog = builder.create();
2444 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2445 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002446 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002447 mInput.requestFocus();
2448 InputMethodManager inputManager = (InputMethodManager)
2449 getSystemService(Context.INPUT_METHOD_SERVICE);
2450 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002451 }
2452 });
2453
2454 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 }
2456
2457 private void changeFolderName() {
2458 final String name = mInput.getText().toString();
2459 if (!TextUtils.isEmpty(name)) {
2460 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002461 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002462 mFolderInfo.title = name;
2463 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2464
Joe Onorato9c1289c2009-08-17 11:03:03 -04002465 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002466 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002467 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 } else {
2469 final FolderIcon folderIcon = (FolderIcon)
2470 mWorkspace.getViewForTag(mFolderInfo);
2471 if (folderIcon != null) {
2472 folderIcon.setText(name);
2473 getWorkspace().requestLayout();
2474 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002475 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002476 mWorkspaceLoading = true;
2477 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 }
2479 }
2480 }
2481 cleanup();
2482 }
2483
2484 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 dismissDialog(DIALOG_RENAME_FOLDER);
2486 mWaitingForResult = false;
2487 mFolderInfo = null;
2488 }
2489 }
2490
Daniel Sandler843e8602010-06-07 14:59:01 -04002491 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2492 public boolean isAllAppsVisible() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002493 return (mState == State.ALL_APPS) || (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002494 }
2495
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002496 // AllAppsView.Watcher
2497 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002498 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2499 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002500 mWorkspace.setVisibility(View.GONE);
2501 }
2502 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002503
Michael Jurkaafca9532011-02-16 14:50:35 -08002504 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002505 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002506 }
2507
2508 private void hideToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002509 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2510 button.setVisibility(View.INVISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002511 button.setAlpha(0.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002512 }
2513
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002514 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002515 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002516 *
2517 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2518 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002519 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002520 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002521 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002522 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002523 final boolean showing = show;
2524 final boolean hiding = !show;
2525
2526 final int duration = show ?
2527 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2528 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2529
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002530 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002531 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002532 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002533 anim.addUpdateListener(new AnimatorUpdateListener() {
2534 public void onAnimationUpdate(ValueAnimator animation) {
2535 view.setAlpha((Float) animation.getAnimatedValue());
2536 }
2537 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002538 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002539 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002540 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002541 if (showing) showAndEnableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002542 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002543 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002544 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002545 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002546 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002547 });
2548 seq.play(anim);
2549 } else {
2550 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002551 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002552 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002553 } else {
2554 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002555 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002556 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002557 }
2558
2559 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002560 * Show/hide the appropriate toolbar buttons for newState.
2561 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002562 *
2563 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002564 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2565 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002566 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002567 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002568 switch (newState) {
2569 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002570 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002571 mDeleteZone.setDragAndDropEnabled(true);
Winson Chung785d2eb2011-04-14 16:08:02 -07002572 if (LauncherApplication.isScreenXLarge()) {
2573 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
2574 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002575 break;
2576 case ALL_APPS:
Winson Chung785d2eb2011-04-14 16:08:02 -07002577 case APPS_CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002578 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002579 mDeleteZone.setDragAndDropEnabled(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002580 if (LauncherApplication.isScreenXLarge()) {
2581 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
2582 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002583 break;
2584 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002585 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002586 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002587 break;
2588 }
2589 }
2590
2591 /**
2592 * Helper method for the cameraZoomIn/cameraZoomOut animations
2593 * @param view The view being animated
2594 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2595 * @param scaleFactor The scale factor used for the zoom
2596 */
2597 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2598 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002599
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002600 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002601 // Set pivotY so that at the starting zoom factor, the view is partially
2602 // visible. Modifying initialHeightFactor changes how much of the view is
2603 // initially showing, and hence the perceived angle from which the view enters.
Winson Chung785d2eb2011-04-14 16:08:02 -07002604 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002605 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002606 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002608 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002609 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002610 }
2611 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002612
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002613 /**
2614 * Zoom the camera out from the workspace to reveal 'toView'.
2615 * Assumes that the view to show is anchored at either the very top or very bottom
2616 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002618 */
Winson Chung20f71112011-04-06 14:22:04 -07002619 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002620 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002621 final boolean toAllApps = (toState == State.ALL_APPS)
2622 || (toState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002623
Winson Chung785d2eb2011-04-14 16:08:02 -07002624 final int duration = (toAllApps ?
2625 res.getInteger(R.integer.config_appsCustomizeZoomInTime) :
2626 res.getInteger(R.integer.config_customizeZoomInTime));
2627 final int fadeDuration = (toAllApps ?
2628 res.getInteger(R.integer.config_appsCustomizeFadeInTime) :
2629 res.getInteger(R.integer.config_customizeFadeInTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002630
2631 final float scale = toAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002632 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002633 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2634
Winson Chung785d2eb2011-04-14 16:08:02 -07002635 View tmpView;
2636 if (toAllApps) {
2637 tmpView = (LauncherApplication.isScreenXLarge())
2638 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2639 } else {
2640 tmpView = mHomeCustomizationDrawer;
2641 }
2642 final View toView = tmpView;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002643
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002644 setPivotsForZoom(toView, toState, scale);
2645
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002646 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002647 if (!springLoaded) {
2648 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2649
Winson Chung785d2eb2011-04-14 16:08:02 -07002650 if (LauncherApplication.isScreenXLarge()) {
2651 // Everytime we launch into AllApps, we reset the successful drop flag which
2652 // controls when it should hide/show the mini workspaces
2653 mAllAppsPagedView.resetSuccessfulDropFlag();
2654 }
Winson Chung20f71112011-04-06 14:22:04 -07002655 } else {
2656 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2657 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002658 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002659 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002661
2662 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002663 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002664 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002665 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2666 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002667 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002668 toView.setFastScaleX(a * scale + b * 1f);
2669 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002670 }
2671 });
Adam Cohen61033d32010-11-15 18:29:44 -08002672
Adam Cohenf16e5712011-01-13 13:31:45 -08002673 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002674 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002675 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002676 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002677 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2678 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002679 // don't need to invalidate because we do so above
2680 toView.setFastAlpha(a * 0f + b * 1f);
2681 }
2682 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002683 alphaAnim.start();
2684 }
2685
Michael Jurkaabded662011-03-04 12:06:57 -08002686 if (toView instanceof LauncherTransitionable) {
2687 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002688 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002689 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002690 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002691 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002692 // Prepare the position
2693 toView.setTranslationX(0.0f);
2694 toView.setTranslationY(0.0f);
2695 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002696 toView.bringToFront();
Adam Cohenf16e5712011-01-13 13:31:45 -08002697 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002698 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002699 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002700 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002701 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002702 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002703 // If we don't set the final scale values here, if this animation is cancelled
2704 // it will have the wrong scale value and subsequent cameraPan animations will
2705 // not fix that
2706 toView.setScaleX(1.0f);
2707 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002708 if (toView instanceof LauncherTransitionable) {
2709 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002710 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002711 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002712 });
2713
Chet Haaseb1254a62010-09-07 13:35:00 -07002714 AnimatorSet toolbarHideAnim = new AnimatorSet();
2715 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002716 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2717
2718 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002719 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002720
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002721 if (mStateAnimation != null) mStateAnimation.cancel();
2722 mStateAnimation = new AnimatorSet();
2723 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2724 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002725
2726 // Show the new toolbar buttons just as the main animation is ending
2727 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002728 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2729 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002730 } else {
2731 toView.setTranslationX(0.0f);
2732 toView.setTranslationY(0.0f);
2733 toView.setScaleX(1.0f);
2734 toView.setScaleY(1.0f);
2735 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002736 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002737 if (toView instanceof LauncherTransitionable) {
2738 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2739 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2740 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002741 hideAndShowToolbarButtons(toState, null, null);
2742 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002743 }
2744
2745 /**
2746 * Zoom the camera back into the workspace, hiding 'fromView'.
2747 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002748 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2749 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002750 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002751 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002752 Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002753 final boolean fromAllApps = (fromState == State.ALL_APPS)
2754 || (fromState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002755
2756 int duration = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002757 res.getInteger(R.integer.config_appsCustomizeZoomOutTime) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002758 res.getInteger(R.integer.config_customizeZoomOutTime);
2759
Michael Jurka742574b2011-02-02 23:51:01 -08002760 final float scaleFactor = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002761 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002762 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2763
Winson Chung785d2eb2011-04-14 16:08:02 -07002764 View tmpView;
2765 if (fromAllApps) {
2766 tmpView = (LauncherApplication.isScreenXLarge())
2767 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2768 } else {
2769 tmpView = mHomeCustomizationDrawer;
2770 }
2771 final View fromView = tmpView;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002772
Winson Chung785d2eb2011-04-14 16:08:02 -07002773 if (LauncherApplication.isScreenXLarge()) {
2774 mCustomizePagedView.endChoiceMode();
2775 mAllAppsPagedView.endChoiceMode();
2776 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002777
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002778 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002779
Michael Jurkad3ef3062010-11-23 16:23:58 -08002780 if (!springLoaded) {
2781 mWorkspace.unshrink(animated);
2782 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002783 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002784 if (mStateAnimation != null) mStateAnimation.cancel();
2785 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002786
Michael Jurka742574b2011-02-02 23:51:01 -08002787 final float oldScaleX = fromView.getScaleX();
2788 final float oldScaleY = fromView.getScaleY();
2789
2790 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2791 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002792 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2793 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002794 ((View)fromView.getParent()).fastInvalidate();
2795 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2796 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2797 }
2798 });
Michael Jurkaabded662011-03-04 12:06:57 -08002799 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002800 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002801 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002802 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2803 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002804 // don't need to invalidate because we do so above
2805 fromView.setFastAlpha(a * 1f + b * 0f);
2806 }
2807 });
Michael Jurkaabded662011-03-04 12:06:57 -08002808 if (fromView instanceof LauncherTransitionable) {
2809 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002810 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002811 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002812 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002813 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002814 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002815 if (fromView instanceof LauncherTransitionable) {
2816 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002817 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002818 }
2819 });
2820
Chet Haaseb1254a62010-09-07 13:35:00 -07002821 AnimatorSet toolbarHideAnim = new AnimatorSet();
2822 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002823 if (!springLoaded) {
2824 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2825 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002826
Adam Cohen61033d32010-11-15 18:29:44 -08002827 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002828
2829 // Show the new toolbar buttons at the very end of the whole animation
2830 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2831 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002832 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2833 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002834 } else {
2835 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002836 if (fromView instanceof LauncherTransitionable) {
2837 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2838 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2839 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002840 if (!springLoaded) {
2841 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2842 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002843 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002844 }
2845
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002846 void showWorkspace(boolean animated) {
2847 showWorkspace(animated, null);
2848 }
2849
2850 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002851 if (layout != null) {
2852 // always animated, but that's ok since we never specify a layout and
2853 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002854 mWorkspace.unshrink(layout);
2855 } else {
2856 mWorkspace.unshrink(animated);
2857 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002858 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002859 closeAllApps(animated);
2860 } else if (mState == State.CUSTOMIZE) {
2861 hideCustomizationDrawer(animated);
2862 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002863
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002864 // Change the state *after* we've called all the transition code
2865 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002866
Winson Chung5fb63472011-02-02 17:03:37 -08002867 // Resume the auto-advance of widgets
2868 mUserPresent = true;
2869 updateRunning();
2870
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002871 // send an accessibility event to announce the context change
2872 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002873 }
2874
Michael Jurkad3ef3062010-11-23 16:23:58 -08002875 void enterSpringLoadedDragMode(CellLayout layout) {
2876 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07002877 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002878 mState = State.ALL_APPS_SPRING_LOADED;
Winson Chung785d2eb2011-04-14 16:08:02 -07002879 if (LauncherApplication.isScreenXLarge()) {
2880 cameraZoomIn(State.ALL_APPS, true, true);
2881 } else {
2882 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
2883 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002884 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002885 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002886 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002887 }/* else {
2888 // we're already in spring loaded mode; don't do anything
2889 }*/
2890 }
2891
2892 void exitSpringLoadedDragMode() {
2893 if (mState == State.ALL_APPS_SPRING_LOADED) {
2894 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002895 if (LauncherApplication.isScreenXLarge()) {
2896 cameraZoomOut(State.ALL_APPS, true, true);
2897 mState = State.ALL_APPS;
2898 } else {
2899 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2900 mState = State.APPS_CUSTOMIZE;
2901 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002902 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2903 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07002904 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002905 mState = State.CUSTOMIZE;
2906 }/* else {
2907 // we're not in spring loaded mode; don't do anything
2908 }*/
2909 }
2910
Winson Chung785d2eb2011-04-14 16:08:02 -07002911 void showAllApps(boolean animated) {
2912 if (mState != State.WORKSPACE) return;
2913 if (LauncherApplication.isScreenXLarge()) {
2914 cameraZoomOut(State.ALL_APPS, animated, false);
2915 ((View) mAllAppsGrid).requestFocus();
2916
2917 // TODO: fade these two too
2918 mDeleteZone.setVisibility(View.GONE);
2919
2920 // Change the state *after* we've called all the transition code
2921 mState = State.ALL_APPS;
2922 } else {
2923 View appsCustomizePane = findViewById(R.id.apps_customize_pane);
2924 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2925 appsCustomizePane.requestFocus();
2926
2927 // Change the state *after* we've called all the transition code
2928 mState = State.APPS_CUSTOMIZE;
2929 }
2930
2931 // Pause the auto-advance of widgets until we are out of AllApps
2932 mUserPresent = false;
2933 updateRunning();
2934
2935 // Send an accessibility event to announce the context change
2936 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2937 }
2938
Joe Onoratob2061212009-11-24 16:13:54 -05002939 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002940 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002941 * - Home from workspace
2942 * - from center screen
2943 * - from other screens
2944 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002945 * - from center screen
2946 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002947 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002948 * - from center screen
2949 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002950 * - Launch app from workspace and quit
2951 * - with back
2952 * - with home
2953 * - Launch app from all apps and quit
2954 * - with back
2955 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002956 * - Go to a screen that's not the default, then all
2957 * apps, and launch and app, and go back
2958 * - with back
2959 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002960 * - On workspace, long press power and go back
2961 * - with back
2962 * - with home
2963 * - On all apps, long press power and go back
2964 * - with back
2965 * - with home
2966 * - On workspace, power off
2967 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002968 * - Launch an app and turn off the screen while in that app
2969 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002970 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002971 * - From all apps
2972 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002973 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2974 * - From all apps
2975 * - From the center workspace
2976 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002977 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002978 void closeAllApps(boolean animated) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002979 if (LauncherApplication.isScreenXLarge()) {
2980 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
2981 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07002982 cameraZoomIn(State.ALL_APPS, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002983
2984 // Set focus to the AllApps button
2985 findViewById(R.id.all_apps_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002986 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002987 } else {
2988 if (mState == State.APPS_CUSTOMIZE || mState == State.ALL_APPS_SPRING_LOADED) {
2989 mWorkspace.setVisibility(View.VISIBLE);
2990 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
2991
2992 // Set focus to the AllApps button
2993 findViewById(R.id.all_apps_button).requestFocus();
2994 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002995 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002996 }
2997
Joe Onorato7c312c12009-08-13 21:36:53 -07002998 void lockAllApps() {
2999 // TODO
3000 }
3001
3002 void unlockAllApps() {
3003 // TODO
3004 }
3005
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003006 // Show the customization drawer (only exists in x-large configuration)
3007 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003008 if (mState != State.WORKSPACE) {
3009 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003010 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003011
Winson Chung20f71112011-04-06 14:22:04 -07003012 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003013
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003014 // Change the state *after* we've called all the transition code
3015 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003016
3017 // Pause the auto-advance of widgets until we are out of Customization drawer
3018 mUserPresent = false;
3019 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003020 }
3021
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003022 // Hide the customization drawer (only exists in x-large configuration)
3023 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003024 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003025 cameraZoomIn(State.CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003026
3027 // Set focus to the customize button
3028 findViewById(R.id.configure_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003029 }
3030 }
3031
Patrick Dubroy5f445422011-02-18 14:35:21 -08003032 /**
3033 * Add an item from all apps or customize onto the given workspace screen.
3034 * If layout is null, add to the current screen.
3035 */
3036 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003037 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3038 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003039 } else {
3040 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003041 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003042 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003043
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003044 void onWorkspaceClick(CellLayout layout) {
3045 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003046 }
3047
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003048 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003049 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003050 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003051 // Look for the toolbar icon specified in the activity meta-data
3052 Bundle metaData = packageManager.getActivityInfo(
3053 activityName, PackageManager.GET_META_DATA).metaData;
3054 if (metaData != null) {
3055 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3056 if (iconResId != 0) {
3057 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003058 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003059 }
3060 }
3061 } catch (NameNotFoundException e) {
3062 // Do nothing
3063 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003064 return null;
3065 }
3066
3067 // if successful in getting icon, return it; otherwise, set button to use default drawable
3068 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3069 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3070 TextView button = (TextView) findViewById(buttonId);
3071 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3072
3073 // If we were unable to find the icon via the meta-data, use a generic one
3074 if (toolbarIcon == null) {
3075 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3076 return null;
3077 } else {
3078 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3079 return toolbarIcon.getConstantState();
3080 }
3081 }
3082
3083 // if successful in getting icon, return it; otherwise, set button to use default drawable
3084 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3085 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3086 ImageView button = (ImageView) findViewById(buttonId);
3087 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3088
Michael Jurka0423dcf2010-10-05 14:56:18 -07003089 // If we were unable to find the icon via the meta-data, use a generic one
3090 if (toolbarIcon == null) {
3091 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003092 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003093 } else {
3094 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003095 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003096 }
3097 }
3098
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003099 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3100 TextView button = (TextView) findViewById(buttonId);
3101 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3102 }
3103
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003104 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003105 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003106 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003107 }
3108
Michael Jurka0423dcf2010-10-05 14:56:18 -07003109 private void updateGlobalSearchIcon() {
3110 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003111 final View searchButton = findViewById(R.id.search_button);
3112 final View searchDivider = findViewById(R.id.search_divider);
3113
Michael Jurka0423dcf2010-10-05 14:56:18 -07003114 final SearchManager searchManager =
3115 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3116 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003117 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003118 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003119 R.id.search_button, activityName, R.drawable.ic_generic_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003120 searchButton.setVisibility(View.VISIBLE);
3121 searchDivider.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003122 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003123 searchButton.setVisibility(View.GONE);
3124 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003125 }
3126 }
3127 }
3128
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003129 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003130 updateButtonWithDrawable(R.id.search_button, d);
3131 }
3132
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003133 private void updateVoiceSearchIcon() {
3134 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003135 final View searchDivider = findViewById(R.id.search_divider);
3136 final View voiceButton = findViewById(R.id.voice_button);
3137
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003138 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3139 ComponentName activityName = intent.resolveActivity(getPackageManager());
3140 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003141 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003142 R.id.voice_button, activityName, R.drawable.ic_voice_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003143 searchDivider.setVisibility(View.VISIBLE);
3144 voiceButton.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003145 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003146 searchDivider.setVisibility(View.GONE);
3147 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003148 }
3149 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003150 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003151
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003152 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003153 updateButtonWithDrawable(R.id.voice_button, d);
3154 }
3155
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003156 /**
3157 * Sets the app market icon (shown when all apps is visible on x-large screens)
3158 */
3159 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003160 final View marketButton = findViewById(R.id.market_button);
3161 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3162 // Find the app market activity by resolving an intent.
3163 // (If multiple app markets are installed, it will return the ResolverActivity.)
3164 ComponentName activityName = intent.resolveActivity(getPackageManager());
3165 if (activityName != null) {
3166 mAppMarketIntent = intent;
3167 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
3168 R.id.market_button, activityName, R.drawable.app_market_generic);
3169 marketButton.setVisibility(View.VISIBLE);
3170 } else {
3171 // We should hide and disable the view so that we don't try and restore the visibility
3172 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3173 marketButton.setVisibility(View.GONE);
3174 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003175 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003176 }
3177
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003178 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003179 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003180 }
3181
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003182 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003183 * Displays the shortcut creation dialog and launches, if necessary, the
3184 * appropriate activity.
3185 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003186 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003187 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3188 DialogInterface.OnShowListener {
3189
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003193 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3196 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003197 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003199 builder.setInverseBackgroundForced(true);
3200
3201 AlertDialog dialog = builder.create();
3202 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003203 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003204 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 return dialog;
3207 }
3208
3209 public void onCancel(DialogInterface dialog) {
3210 mWaitingForResult = false;
3211 cleanup();
3212 }
3213
Romain Guycbb89e42009-06-08 15:52:54 -07003214 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003215 }
3216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003217 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003218 try {
3219 dismissDialog(DIALOG_CREATE_SHORTCUT);
3220 } catch (Exception e) {
3221 // An exception is thrown if the dialog is not visible, which is fine
3222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223 }
3224
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003225 /**
3226 * Handle the action clicked in the "Add to home" dialog.
3227 */
3228 public void onClick(DialogInterface dialog, int which) {
3229 Resources res = getResources();
3230 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003231
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003232 switch (which) {
3233 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003234 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003235 break;
3236 }
Romain Guycbb89e42009-06-08 15:52:54 -07003237
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003238 case AddAdapter.ITEM_APPWIDGET: {
3239 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003240
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003241 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3242 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003243 // start the pick activity
3244 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3245 break;
3246 }
Romain Guycbb89e42009-06-08 15:52:54 -07003247
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003248 case AddAdapter.ITEM_WALLPAPER: {
3249 startWallpaper();
3250 break;
3251 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 }
3253 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003254
3255 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003256 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003257 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003258 }
3259
3260 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003261 * Receives notifications when applications are added/removed.
3262 */
3263 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3264 @Override
3265 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003266 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003267 String reason = intent.getStringExtra("reason");
3268 if (!"homekey".equals(reason)) {
3269 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003270 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003271 animate = false;
3272 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003273 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003274 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003275 }
3276 }
3277
3278 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003279 * Receives notifications whenever the appwidgets are reset.
3280 */
3281 private class AppWidgetResetObserver extends ContentObserver {
3282 public AppWidgetResetObserver() {
3283 super(new Handler());
3284 }
3285
3286 @Override
3287 public void onChange(boolean selfChange) {
3288 onAppWidgetReset();
3289 }
3290 }
3291
3292 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003293 * If the activity is currently paused, signal that we need to re-run the loader
3294 * in onResume.
3295 *
3296 * This needs to be called from incoming places where resources might have been loaded
3297 * while we are paused. That is becaues the Configuration might be wrong
3298 * when we're not running, and if it comes back to what it was when we
3299 * were paused, we are not restarted.
3300 *
3301 * Implementation of the method from LauncherModel.Callbacks.
3302 *
3303 * @return true if we are currently paused. The caller might be able to
3304 * skip some work in that case since we will come back again.
3305 */
3306 public boolean setLoadOnResume() {
3307 if (mPaused) {
3308 Log.i(TAG, "setLoadOnResume");
3309 mOnResumeNeedsLoad = true;
3310 return true;
3311 } else {
3312 return false;
3313 }
3314 }
3315
3316 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003317 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003318 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003319 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003320 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003321 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003322 } else {
3323 return SCREEN_COUNT / 2;
3324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003326
Winson Chunga12a2502010-12-20 14:41:35 -08003327 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003328 mAllAppsPagedView = view;
3329 }
3330
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 /**
3332 * Refreshes the shortcuts shown on the workspace.
3333 *
3334 * Implementation of the method from LauncherModel.Callbacks.
3335 */
3336 public void startBinding() {
3337 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003338
3339 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003340 int count = workspace.getChildCount();
3341 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003342 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003343 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3344 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003345 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003346
Joe Onorato9c1289c2009-08-17 11:03:03 -04003347 if (DEBUG_USER_INTERFACE) {
3348 android.widget.Button finishButton = new android.widget.Button(this);
3349 finishButton.setText("Finish");
3350 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3351
3352 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3353 public void onClick(View v) {
3354 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003356 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003357 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003358
3359 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3360 // mDesktopItems -> AppWidgetInfo -> hostView).
3361 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003362 }
3363
3364 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 * Bind the items start-end from the list.
3366 *
3367 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003368 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3370
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003371 setLoadOnResume();
3372
Joe Onorato9c1289c2009-08-17 11:03:03 -04003373 final Workspace workspace = mWorkspace;
3374
3375 for (int i=start; i<end; i++) {
3376 final ItemInfo item = shortcuts.get(i);
3377 mDesktopItems.add(item);
3378 switch (item.itemType) {
3379 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3380 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003381 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003382 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3383 false);
3384 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003385 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003386 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003387 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003388 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3390 false);
3391 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003393 }
3394
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003396 }
3397
Joe Onorato9c1289c2009-08-17 11:03:03 -04003398 /**
3399 * Implementation of the method from LauncherModel.Callbacks.
3400 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003401 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003402 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003403 sFolders.clear();
3404 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003405 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406
3407 /**
3408 * Add the views for a widget to the workspace.
3409 *
3410 * Implementation of the method from LauncherModel.Callbacks.
3411 */
3412 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003413 setLoadOnResume();
3414
Daniel Sandler843e8602010-06-07 14:59:01 -04003415 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3416 if (DEBUG_WIDGETS) {
3417 Log.d(TAG, "bindAppWidget: " + item);
3418 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 final Workspace workspace = mWorkspace;
3420
3421 final int appWidgetId = item.appWidgetId;
3422 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003423 if (DEBUG_WIDGETS) {
3424 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3425 }
3426
Joe Onorato9c1289c2009-08-17 11:03:03 -04003427 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3428
Joe Onorato9c1289c2009-08-17 11:03:03 -04003429 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3430 item.hostView.setTag(item);
3431
3432 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3433 item.cellY, item.spanX, item.spanY, false);
3434
Adam Cohended9f8d2010-11-03 13:25:16 -07003435 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3436
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 workspace.requestLayout();
3438
3439 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003440
3441 if (DEBUG_WIDGETS) {
3442 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3443 + (SystemClock.uptimeMillis()-start) + "ms");
3444 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003445 }
3446
3447 /**
3448 * Callback saying that there aren't any more items to bind.
3449 *
3450 * Implementation of the method from LauncherModel.Callbacks.
3451 */
3452 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003453 setLoadOnResume();
3454
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455 if (mSavedState != null) {
3456 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003457 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 }
3459
Adam Cohendf2cc412011-04-27 16:56:57 -07003460 final long[] folders = mSavedState.getLongArray(RUNTIME_STATE_FOLDERS);
3461 if (folders != null) {
3462 for (long folderId : folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003463 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003464 if (info != null) {
3465 openFolder(info);
3466 }
3467 }
3468 final Folder openFolder = mWorkspace.getOpenFolder();
3469 if (openFolder != null) {
3470 openFolder.requestFocus();
3471 }
3472 }
3473
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 mSavedState = null;
3475 }
3476
3477 if (mSavedInstanceState != null) {
3478 super.onRestoreInstanceState(mSavedInstanceState);
3479 mSavedInstanceState = null;
3480 }
3481
Winson Chung32bb5e52011-01-31 14:51:56 -08003482 // Workaround a bug that occurs when rotating the device while the customization mode is
3483 // open, we trigger a new layout on all the CellLayout children.
3484 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3485 final int childCount = mWorkspace.getChildCount();
3486 for (int i = 0; i < childCount; ++i) {
3487 mWorkspace.getChildAt(i).requestLayout();
3488 }
3489 }
3490
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003492
3493 // If we received the result of any pending adds while the loader was running (e.g. the
3494 // widget configuration forced an orientation change), process them now.
3495 for (int i = 0; i < sPendingAddList.size(); i++) {
3496 completeAdd(sPendingAddList.get(i));
3497 }
3498 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003499 }
3500
3501 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003502 * Updates the icons on the launcher that are affected by changes to the package list
3503 * on the device.
3504 */
3505 private void updateIconsAffectedByPackageManagerChanges() {
3506 updateAppMarketIcon();
3507 updateGlobalSearchIcon();
3508 updateVoiceSearchIcon();
3509 }
3510
3511 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 * Add the icons for all apps.
3513 *
3514 * Implementation of the method from LauncherModel.Callbacks.
3515 */
3516 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003517 if (mAllAppsGrid != null) {
3518 mAllAppsGrid.setApps(apps);
3519 }
3520 if (mAppsCustomizeContent != null) {
3521 mAppsCustomizeContent.setApps(apps);
3522 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003523 if (mCustomizePagedView != null) {
3524 mCustomizePagedView.setApps(apps);
3525 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003526 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 }
3528
3529 /**
3530 * A package was installed.
3531 *
3532 * Implementation of the method from LauncherModel.Callbacks.
3533 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003534 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003535 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chung785d2eb2011-04-14 16:08:02 -07003537 if (mAllAppsGrid != null) {
3538 mAllAppsGrid.addApps(apps);
3539 }
3540 if (mAppsCustomizeContent != null) {
3541 mAppsCustomizeContent.addApps(apps);
3542 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003543 if (mCustomizePagedView != null) {
3544 mCustomizePagedView.addApps(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 updated.
3551 *
3552 * Implementation of the method from LauncherModel.Callbacks.
3553 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003554 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003555 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003557 if (mWorkspace != null) {
3558 mWorkspace.updateShortcuts(apps);
3559 }
3560 if (mAllAppsGrid != null) {
3561 mAllAppsGrid.updateApps(apps);
3562 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003563 if (mAppsCustomizeContent != null) {
3564 mAppsCustomizeContent.updateApps(apps);
3565 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003566 if (mCustomizePagedView != null) {
3567 mCustomizePagedView.updateApps(apps);
3568 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003569 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 }
3571
3572 /**
3573 * A package was uninstalled.
3574 *
3575 * Implementation of the method from LauncherModel.Callbacks.
3576 */
Joe Onorato36115782010-06-17 13:28:48 -04003577 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003578 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003579 if (permanent) {
3580 mWorkspace.removeItems(apps);
3581 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003582 if (mAllAppsGrid != null) {
3583 mAllAppsGrid.removeApps(apps);
3584 }
3585 if (mAppsCustomizeContent != null) {
3586 mAppsCustomizeContent.removeApps(apps);
3587 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003588 if (mCustomizePagedView != null) {
3589 mCustomizePagedView.removeApps(apps);
3590 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003591 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 }
Joe Onoratobe386092009-11-17 17:32:16 -08003593
3594 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003595 * A number of packages were updated.
3596 */
3597 public void bindPackagesUpdated() {
3598 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003599 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003600 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003601 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003602 if (mAppsCustomizeContent != null) {
3603 mAppsCustomizeContent.onPackagesUpdated();
3604 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003605 }
3606
Winson Chung400438b2011-01-16 17:53:48 -08003607 private int mapConfigurationOriActivityInfoOri(int configOri) {
3608 final Display d = getWindowManager().getDefaultDisplay();
3609 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3610 switch (d.getRotation()) {
3611 case Surface.ROTATION_0:
3612 case Surface.ROTATION_180:
3613 // We are currently in the same basic orientation as the natural orientation
3614 naturalOri = configOri;
3615 break;
3616 case Surface.ROTATION_90:
3617 case Surface.ROTATION_270:
3618 // We are currently in the other basic orientation to the natural orientation
3619 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3620 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3621 break;
3622 }
3623
3624 int[] oriMap = {
3625 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3626 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3627 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3628 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3629 };
3630 // Since the map starts at portrait, we need to offset if this device's natural orientation
3631 // is landscape.
3632 int indexOffset = 0;
3633 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3634 indexOffset = 1;
3635 }
3636 return oriMap[(d.getRotation() + indexOffset) % 4];
3637 }
3638 public void lockScreenOrientation() {
3639 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3640 .getConfiguration().orientation));
3641 }
3642 public void unlockScreenOrientation() {
3643 mHandler.postDelayed(new Runnable() {
3644 public void run() {
3645 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3646 }
3647 }, mRestoreScreenOrientationDelay);
3648 }
3649
Winson Chung80baf5a2010-08-09 16:03:15 -07003650 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003651 * Prints out out state for debugging.
3652 */
3653 public void dumpState() {
3654 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003655 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003656 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3657 Log.d(TAG, "mRestoring=" + mRestoring);
3658 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3659 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3660 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003661 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003662 mModel.dumpState();
Winson Chung785d2eb2011-04-14 16:08:02 -07003663 if (mAllAppsGrid != null) {
3664 mAllAppsGrid.dumpState();
3665 }
3666 if (mAppsCustomizeContent != null) {
3667 mAppsCustomizeContent.dumpState();
3668 }
Joe Onoratobe386092009-11-17 17:32:16 -08003669 Log.d(TAG, "END launcher2 dump state");
3670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003671}
Michael Jurka2763be32011-02-24 11:19:57 -08003672
Michael Jurkaabded662011-03-04 12:06:57 -08003673interface LauncherTransitionable {
3674 void onLauncherTransitionStart(Animator animation);
3675 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003676}