blob: 93fab97a38f9d8571f6ab7b3b7a44fa821217838 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.Dialog;
29import android.app.SearchManager;
30import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen716b51e2011-06-30 12:09:54 -070043import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Winson Chungf0ea4d32011-06-06 14:27:16 -070046import android.content.pm.ResolveInfo;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040050import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080053import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080055import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080064import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
69import android.text.TextUtils;
70import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080072import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080073import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
77import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070078import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.View;
Adam Cohen716b51e2011-06-30 12:09:54 -070081import android.view.ViewGroup;
Michael Jurkae0f5a612011-02-07 16:45:41 -080082import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080083import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Winson Chung68846fd2010-10-29 11:00:27 -070092import android.widget.TextView;
93import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094
Adam Cohendf2cc412011-04-27 16:56:57 -070095import com.android.common.Search;
96import com.android.launcher.R;
97import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
101import java.io.FileNotFoundException;
102import java.io.IOException;
103import java.util.ArrayList;
104import java.util.HashMap;
105import java.util.List;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107/**
108 * Default launcher application.
109 */
Michael Jurka946ad472010-07-09 18:05:18 -0700110public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700111 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
112 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800113 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700114 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400117 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800121 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700124 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
125 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
127 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700128 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700135 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Joe Onorato7c312c12009-08-13 21:36:53 -0700142 static final int DIALOG_CREATE_SHORTCUT = 1;
143 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Romain Guy98d01652009-06-30 16:21:04 -0700145 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 // Type: int
148 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700149 // Type: int
150 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: boolean
158 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
159 // Type: long
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
161
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700162 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
163
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700164 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700165 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700166 private State mState = State.WORKSPACE;
167 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800172 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800174 private final BroadcastReceiver mCloseSystemDialogsReceiver
175 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800176 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private LayoutInflater mInflater;
179
Joe Onorato00acb122009-08-04 16:04:30 -0400180 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700182
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700183 private AppWidgetManager mAppWidgetManager;
184 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700185
Michael Jurka0280c3b2010-09-17 15:00:07 -0700186 private int mAddScreen = -1;
187 private int mAddIntersectCellX = -1;
188 private int mAddIntersectCellY = -1;
189 private int[] mAddDropPosition;
190 private int[] mTmpAddItemCellCoordinates = new int[2];
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private FolderInfo mFolderInfo;
193
Winson Chung97d85d22011-04-13 11:27:36 -0700194 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800195 private View mAllAppsButton;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700196 private SearchDropTargetBar mSearchDeleteBar;
197 private AppsCustomizeTabHost mAppsCustomizeTabHost;
198 private AppsCustomizePagedView mAppsCustomizeContent;
199 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private Bundle mSavedState;
202
203 private SpannableStringBuilder mDefaultKeySsb = null;
204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 private boolean mWorkspaceLoading = true;
206
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800207 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private boolean mRestoring;
209 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700210 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
212 private Bundle mSavedInstanceState;
213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800215 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800216 private boolean mUserPresent = true;
217 private boolean mVisible = false;
218 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700220 private static LocaleConfiguration sLocaleConfiguration = null;
221
Romain Guy84f296c2009-11-04 15:00:44 -0800222 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700223
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700224 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700225
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400226 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400227 private String[] mHotseatConfig = null;
228 private Intent[] mHotseats = null;
229 private Drawable[] mHotseatIcons = null;
230 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400231
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700232 private Intent mAppMarketIntent = null;
233
Adam Cohended9f8d2010-11-03 13:25:16 -0700234 // Related to the auto-advancing of widgets
235 private final int ADVANCE_MSG = 1;
236 private final int mAdvanceInterval = 20000;
237 private final int mAdvanceStagger = 250;
238 private long mAutoAdvanceSentTime;
239 private long mAutoAdvanceTimeLeft = -1;
240 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
241 new HashMap<View, AppWidgetProviderInfo>();
242
Winson Chung400438b2011-01-16 17:53:48 -0800243 // Determines how long to wait after a rotation before restoring the screen orientation to
244 // match the sensor state.
245 private final int mRestoreScreenOrientationDelay = 500;
246
Michael Jurka4ef207b2010-11-29 17:05:45 -0800247 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800248 private static Drawable.ConstantState sGlobalSearchIcon;
249 private static Drawable.ConstantState sVoiceSearchIcon;
250 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800251
Adam Cohen2801caf2011-05-13 20:57:39 -0700252 private DragLayer mDragLayer;
253
Michael Jurkaddd62e92011-02-16 17:49:14 -0800254 private BubbleTextView mWaitingForResume;
255
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800256 private static ArrayList<PendingAddArguments> sPendingAddList
257 = new ArrayList<PendingAddArguments>();
258
259 private static class PendingAddArguments {
260 int requestCode;
261 Intent intent;
262 int screen;
263 int cellX;
264 int cellY;
265 }
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 @Override
268 protected void onCreate(Bundle savedInstanceState) {
269 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 LauncherApplication app = ((LauncherApplication)getApplication());
271 mModel = app.setLauncher(this);
272 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400273 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700275
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700276 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700277 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
278 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200281 android.os.Debug.startMethodTracing(
282 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 }
284
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400285 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 setContentView(R.layout.launcher);
288 setupViews();
289
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800290 registerContentObservers();
291
Joe Onorato7c312c12009-08-13 21:36:53 -0700292 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 mSavedState = savedInstanceState;
295 restoreState(mSavedState);
296
Winson Chunga12a2502010-12-20 14:41:35 -0800297 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700298 if (mAppsCustomizeContent != null) {
299 mAppsCustomizeContent.onPackagesUpdated();
300 }
Winson Chunga12a2502010-12-20 14:41:35 -0800301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 if (PROFILE_STARTUP) {
303 android.os.Debug.stopMethodTracing();
304 }
305
306 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 }
309
310 // For handling default keys
311 mDefaultKeySsb = new SpannableStringBuilder();
312 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800313
314 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
315 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800316
317 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700318 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
319 updateIconsAffectedByPackageManagerChanges();
320 }
321 if (sGlobalSearchIcon != null) {
322 updateGlobalSearchIcon(sGlobalSearchIcon);
323 }
324 if (sVoiceSearchIcon != null) {
325 updateVoiceSearchIcon(sVoiceSearchIcon);
326 }
327 if (sAppMarketIcon != null) {
328 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
Romain Guycbb89e42009-06-08 15:52:54 -0700331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700333 if (sLocaleConfiguration == null) {
334 new AsyncTask<Void, Void, LocaleConfiguration>() {
335 @Override
336 protected LocaleConfiguration doInBackground(Void... unused) {
337 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
338 readConfiguration(Launcher.this, localeConfiguration);
339 return localeConfiguration;
340 }
341
342 @Override
343 protected void onPostExecute(LocaleConfiguration result) {
344 sLocaleConfiguration = result;
345 checkForLocaleChange(); // recursive, but now with a locale configuration
346 }
347 }.execute();
348 return;
349 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 final Configuration configuration = getResources().getConfiguration();
352
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700353 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 final String locale = configuration.locale.toString();
355
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700356 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 final int mcc = configuration.mcc;
358
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700359 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 final int mnc = configuration.mnc;
361
Romain Guy84f296c2009-11-04 15:00:44 -0800362 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363
Romain Guy84f296c2009-11-04 15:00:44 -0800364 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700365 sLocaleConfiguration.locale = locale;
366 sLocaleConfiguration.mcc = mcc;
367 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700368
Joe Onorato0589f0f2010-02-08 13:44:00 -0800369 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400370 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700371
372 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
373 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700374 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700375 public void run() {
376 writeConfiguration(Launcher.this, localeConfiguration);
377 }
378 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700379 }
380 }
381
382 private static class LocaleConfiguration {
383 public String locale;
384 public int mcc = -1;
385 public int mnc = -1;
386 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700387
Romain Guy98d01652009-06-30 16:21:04 -0700388 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
389 DataInputStream in = null;
390 try {
391 in = new DataInputStream(context.openFileInput(PREFERENCES));
392 configuration.locale = in.readUTF();
393 configuration.mcc = in.readInt();
394 configuration.mnc = in.readInt();
395 } catch (FileNotFoundException e) {
396 // Ignore
397 } catch (IOException e) {
398 // Ignore
399 } finally {
400 if (in != null) {
401 try {
402 in.close();
403 } catch (IOException e) {
404 // Ignore
405 }
406 }
407 }
408 }
409
410 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
411 DataOutputStream out = null;
412 try {
413 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
414 out.writeUTF(configuration.locale);
415 out.writeInt(configuration.mcc);
416 out.writeInt(configuration.mnc);
417 out.flush();
418 } catch (FileNotFoundException e) {
419 // Ignore
420 } catch (IOException e) {
421 //noinspection ResultOfMethodCallIgnored
422 context.getFileStreamPath(PREFERENCES).delete();
423 } finally {
424 if (out != null) {
425 try {
426 out.close();
427 } catch (IOException e) {
428 // Ignore
429 }
430 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
432 }
433
Adam Cohen716b51e2011-06-30 12:09:54 -0700434 public DragLayer getDragLayer() {
435 return mDragLayer;
436 }
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 static int getScreen() {
439 synchronized (sLock) {
440 return sScreen;
441 }
442 }
443
444 static void setScreen(int screen) {
445 synchronized (sLock) {
446 sScreen = screen;
447 }
448 }
449
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400450 // Note: This doesn't do all the client-id magic that BrowserProvider does
451 // in Browser. (http://b/2425179)
452 private Uri getDefaultBrowserUri() {
453 String url = getString(R.string.default_browser_url);
454 if (url.indexOf("{CID}") != -1) {
455 url = url.replace("{CID}", "android-google");
456 }
457 return Uri.parse(url);
458 }
459
460 // Load the Intent templates from arrays.xml to populate the hotseats. For
461 // each Intent, if it resolves to a single app, use that as the launch
462 // intent & use that app's label as the contentDescription. Otherwise,
463 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400464 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400465 if (mHotseatConfig == null) {
466 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
467 if (mHotseatConfig.length > 0) {
468 mHotseats = new Intent[mHotseatConfig.length];
469 mHotseatLabels = new CharSequence[mHotseatConfig.length];
470 mHotseatIcons = new Drawable[mHotseatConfig.length];
471 } else {
472 mHotseats = null;
473 mHotseatIcons = null;
474 mHotseatLabels = null;
475 }
476
477 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
478 for (int i=0; i<mHotseatConfig.length; i++) {
479 // load icon for this slot; currently unrelated to the actual activity
480 try {
481 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
482 } catch (ArrayIndexOutOfBoundsException ex) {
483 Log.w(TAG, "Missing hotseat_icons array item #" + i);
484 mHotseatIcons[i] = null;
485 }
486 }
487 hotseatIconDrawables.recycle();
488 }
489
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400490 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400491 for (int i=0; i<mHotseatConfig.length; i++) {
492 Intent intent = null;
493 if (mHotseatConfig[i].equals("*BROWSER*")) {
494 // magic value meaning "launch user's default web browser"
495 // replace it with a generic web request so we can see if there is indeed a default
496 String defaultUri = getString(R.string.default_browser_url);
497 intent = new Intent(
498 Intent.ACTION_VIEW,
499 ((defaultUri != null)
500 ? Uri.parse(defaultUri)
501 : getDefaultBrowserUri())
502 ).addCategory(Intent.CATEGORY_BROWSABLE);
503 // note: if the user launches this without a default set, she
504 // will always be taken to the default URL above; this is
505 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700506 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400507 // URL is unavoidably sent to the chosen app.
508 } else {
509 try {
510 intent = Intent.parseUri(mHotseatConfig[i], 0);
511 } catch (java.net.URISyntaxException ex) {
512 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
513 // bogus; leave intent=null
514 }
515 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700516
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400517 if (intent == null) {
518 mHotseats[i] = null;
519 mHotseatLabels[i] = getText(R.string.activity_not_found);
520 continue;
521 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400522
523 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700524 Log.d(TAG, "loadHotseats: hotseat " + i
525 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400526 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400527 + "]");
528 }
529
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400530 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
531 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700532 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400533 Log.d(TAG, "Best match for intent: " + bestMatch);
534 Log.d(TAG, "All matches: ");
535 for (ResolveInfo ri : allMatches) {
536 Log.d(TAG, " --> " + ri);
537 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400538 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400539 // did this resolve to a single app, or the resolver?
540 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700541 // can't find any activity to handle this. let's leave the
542 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400543 // to launch.
544 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400545
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400546 // set accessibility text to "Not installed"
547 mHotseatLabels[i] = getText(R.string.activity_not_found);
548 } else {
549 boolean found = false;
550 for (ResolveInfo ri : allMatches) {
551 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
552 && bestMatch.activityInfo.applicationInfo.packageName
553 .equals(ri.activityInfo.applicationInfo.packageName)) {
554 found = true;
555 break;
556 }
557 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700558
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 if (!found) {
560 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
561 // the bestMatch is probably the ResolveActivity, meaning the
562 // user has not yet selected a default
563 // so: we'll keep the original intent for now
564 mHotseats[i] = intent;
565
566 // set the accessibility text to "Select shortcut"
567 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
568 } else {
569 // we have an app!
570 // now reconstruct the intent to launch it through the front
571 // door
572 ComponentName com = new ComponentName(
573 bestMatch.activityInfo.applicationInfo.packageName,
574 bestMatch.activityInfo.name);
575 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
576
577 // load the app label for accessibility
578 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
579 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400580 }
581
582 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700583 Log.d(TAG, "loadHotseats: hotseat " + i
584 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400585 + ((mHotseats[i] == null)
586 ? "null"
587 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400588 + "] label=[" + mHotseatLabels[i]
589 + "]"
590 );
591 }
592 }
593 }
594
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800595 private void completeAdd(PendingAddArguments args) {
596 switch (args.requestCode) {
597 case REQUEST_PICK_APPLICATION:
598 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
599 break;
600 case REQUEST_PICK_SHORTCUT:
601 processShortcut(args.intent);
602 break;
603 case REQUEST_CREATE_SHORTCUT:
604 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
605 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800606 case REQUEST_PICK_APPWIDGET:
607 addAppWidgetFromPick(args.intent);
608 break;
609 case REQUEST_CREATE_APPWIDGET:
610 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
611 completeAddAppWidget(appWidgetId, args.screen);
612 break;
613 case REQUEST_PICK_WALLPAPER:
614 // We just wanted the activity result here so we can clear mWaitingForResult
615 break;
616 }
617 }
618
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800620 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700621 mWaitingForResult = false;
622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 // The pattern used here is that a user PICKs a specific application,
624 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
627 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700628
Michael Jurka0280c3b2010-09-17 15:00:07 -0700629 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800630 final PendingAddArguments args = new PendingAddArguments();
631 args.requestCode = requestCode;
632 args.intent = data;
633 args.screen = mAddScreen;
634 args.cellX = mAddIntersectCellX;
635 args.cellY = mAddIntersectCellY;
636
637 // If the loader is still running, defer the add until it is done.
638 if (isWorkspaceLocked()) {
639 sPendingAddList.add(args);
640 } else {
641 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 }
Romain Guy18042c82009-11-06 11:44:55 -0800643 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
644 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
645 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700646 // Clean up the appWidgetId if we canceled
647 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
648 if (appWidgetId != -1) {
649 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
653
654 @Override
655 protected void onResume() {
656 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800657 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700658 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400659 mWorkspaceLoading = true;
660 mModel.startLoader(this, true);
661 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700662 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800664 if (mWaitingForResume != null) {
665 mWaitingForResume.setStayPressed(false);
666 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700667 // When we resume Launcher, a different Activity might be responsible for the app
668 // market intent, so refresh the icon
669 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671
672 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700673 protected void onPause() {
674 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700675 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800676 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 }
Romain Guycbb89e42009-06-08 15:52:54 -0700678
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700679 @Override
680 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400681 // Flag the loader to stop early before switching
682 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700683 if (mAppsCustomizeContent != null) {
684 mAppsCustomizeContent.surrender();
685 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800686 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700687 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800689 // We can't hide the IME if it was forced open. So don't bother
690 /*
691 @Override
692 public void onWindowFocusChanged(boolean hasFocus) {
693 super.onWindowFocusChanged(hasFocus);
694
695 if (hasFocus) {
696 final InputMethodManager inputManager = (InputMethodManager)
697 getSystemService(Context.INPUT_METHOD_SERVICE);
698 WindowManager.LayoutParams lp = getWindow().getAttributes();
699 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
700 android.os.Handler()) {
701 protected void onReceiveResult(int resultCode, Bundle resultData) {
702 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
703 }
704 });
705 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
706 }
707 }
708 */
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 private boolean acceptFilter() {
711 final InputMethodManager inputManager = (InputMethodManager)
712 getSystemService(Context.INPUT_METHOD_SERVICE);
713 return !inputManager.isFullscreenMode();
714 }
715
716 @Override
717 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700718 final int uniChar = event.getUnicodeChar();
719 final boolean handled = super.onKeyDown(keyCode, event);
720 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
721 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
723 keyCode, event);
724 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700725 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700726 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700727 // showSearchDialog()
728 // If there are multiple keystrokes before the search dialog takes focus,
729 // onSearchRequested() will be called for every keystroke,
730 // but it is idempotent, so it's fine.
731 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
733 }
734
Joe Onorato8a9625e2010-01-28 15:55:35 -0800735 // Eat the long press event so the keyboard doesn't come up.
736 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
737 return true;
738 }
739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 return handled;
741 }
742
Karl Rosaen138a0412009-04-23 19:00:21 -0700743 private String getTypedText() {
744 return mDefaultKeySsb.toString();
745 }
746
747 private void clearTypedText() {
748 mDefaultKeySsb.clear();
749 mDefaultKeySsb.clearSpans();
750 Selection.setSelection(mDefaultKeySsb, 0);
751 }
752
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700754 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
755 * State
756 */
757 private static State intToState(int stateOrdinal) {
758 State state = State.WORKSPACE;
759 final State[] stateValues = State.values();
760 for (int i = 0; i < stateValues.length; i++) {
761 if (stateValues[i].ordinal() == stateOrdinal) {
762 state = stateValues[i];
763 break;
764 }
765 }
766 return state;
767 }
768
769 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 * Restores the previous state, if it exists.
771 *
772 * @param savedState The previous state.
773 */
774 private void restoreState(Bundle savedState) {
775 if (savedState == null) {
776 return;
777 }
778
Michael Jurka883f55b2010-10-21 15:47:14 -0700779 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
780
Winson Chungf0ea4d32011-06-06 14:27:16 -0700781 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800782 showAllApps(false);
783 }
784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
786 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700787 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 }
789
790 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700791
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700793 mAddScreen = addScreen;
794 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
795 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 mRestoring = true;
797 }
798
799 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
800 if (renameFolder) {
801 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700802 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 mRestoring = true;
804 }
Winson Chunga12a2502010-12-20 14:41:35 -0800805
Winson Chung785d2eb2011-04-14 16:08:02 -0700806
807 // Restore the AppsCustomize tab
808 if (mAppsCustomizeTabHost != null) {
809 String curTab = savedState.getString("apps_customize_currentTab");
810 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700811 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700812 mAppsCustomizeContent.setContentType(
813 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
814 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
815 }
816
817 // Note: currently we do not restore the page for the AppsCustomize pane because the
818 // change in layout can drastically affect the saved page index
819 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 }
821
822 /**
823 * Finds all the views we need and configure them properly.
824 */
825 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700826 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400827
Winson Chung4c98d922011-05-31 16:50:48 -0700828 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
829 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830
Winson Chung4c98d922011-05-31 16:50:48 -0700831 // Setup the drag layer
832 mDragLayer.setup(this, dragController);
833
834 // Setup the workspace
835 mWorkspace.setHapticFeedbackEnabled(false);
836 mWorkspace.setOnLongClickListener(this);
837 mWorkspace.setup(this, dragController);
838 mWorkspace.setWallpaperDimension();
839
Winson Chungf0ea4d32011-06-06 14:27:16 -0700840 // Get the search/delete bar
841 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700842
Winson Chungf0ea4d32011-06-06 14:27:16 -0700843 // Setup AppsCustomize
844 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
845 findViewById(R.id.apps_customize_pane);
846 mAppsCustomizeContent = (AppsCustomizePagedView)
847 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
848 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400849
Winson Chungf0ea4d32011-06-06 14:27:16 -0700850 // Setup AppsCustomize button
851 mAllAppsButton = mDragLayer.findViewById(R.id.all_apps_button);
852 mAllAppsButton.setOnClickListener(new OnClickListener() {
853 public void onClick(View v) {
854 onClickAllAppsButton(v);
855 }
856 });
Romain Guy1fbc1c82009-11-09 20:43:08 -0800857
Winson Chungf0ea4d32011-06-06 14:27:16 -0700858 if (!LauncherApplication.isScreenLarge()) {
859 // Setup AppsCustomize button on the phone
860 HandleView handleView = (HandleView) mAllAppsButton;
861 handleView.setLauncher(this);
862 handleView.setOnLongClickListener(this);
Winson Chung4c98d922011-05-31 16:50:48 -0700863
864 // Setup Hotseat
865 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
866 hotseatLeft.setContentDescription(mHotseatLabels[0]);
867 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
868 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
869 hotseatRight.setContentDescription(mHotseatLabels[1]);
870 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Adam Cohencdc30d52010-12-01 15:09:47 -0800871 }
872
Winson Chungf0ea4d32011-06-06 14:27:16 -0700873 if (!LauncherApplication.isScreenLarge()) {
874 // Setup keylistener for button cluster
875 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
876 View.OnKeyListener listener = null;
877 if (LauncherApplication.isScreenLarge()) {
878 // For tablets, AllApps lives in the button bar at the top
879 listener = new ButtonBarKeyEventListener();
880 } else {
881 // For phones, AppsCustomize lives in the "dock" at the bottom
882 listener = new DockKeyEventListener();
883 }
884 int buttonCount = mButtonCluster.getChildCount();
885 for (int i = 0; i < buttonCount; ++i) {
886 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
887 }
Winson Chung97d85d22011-04-13 11:27:36 -0700888 }
Michael Jurka838a4ca2011-02-07 13:33:06 -0800889
Winson Chung4c98d922011-05-31 16:50:48 -0700890 // Setup the drag controller (the drop targets have to be added in reverse order)
891 dragController.setDragScoller(mWorkspace);
892 dragController.setScrollView(mDragLayer);
893 dragController.setMoveTarget(mWorkspace);
894 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700895 if (mSearchDeleteBar != null) {
896 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
899
Romain Guy8a73c512009-11-09 19:19:59 -0800900 @SuppressWarnings({"UnusedDeclaration"})
901 public void previousScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700902 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800903 mWorkspace.scrollLeft();
904 }
Romain Guy8a73c512009-11-09 19:19:59 -0800905 }
906
907 @SuppressWarnings({"UnusedDeclaration"})
908 public void nextScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700909 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800910 mWorkspace.scrollRight();
911 }
Romain Guy8a73c512009-11-09 19:19:59 -0800912 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400913
914 @SuppressWarnings({"UnusedDeclaration"})
915 public void launchHotSeat(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700916 if (mState == State.APPS_CUSTOMIZE) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400917
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400918 int index = -1;
919 if (v.getId() == R.id.hotseat_left) {
920 index = 0;
921 } else if (v.getId() == R.id.hotseat_right) {
922 index = 1;
923 }
924
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400925 // reload these every tap; you never know when they might change
926 loadHotseats();
927 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400928 startActivitySafely(
929 mHotseats[index],
930 "hotseat"
931 );
932 }
933 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700934
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 /**
936 * Creates a view representing a shortcut.
937 *
938 * @param info The data structure describing the shortcut.
939 *
940 * @return A View inflated from R.layout.application.
941 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800942 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700944 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 }
946
947 /**
948 * Creates a view representing a shortcut inflated from the specified resource.
949 *
950 * @param layoutResId The id of the XML layout used to create the shortcut.
951 * @param parent The group the shortcut belongs to.
952 * @param info The data structure describing the shortcut.
953 *
954 * @return A View inflated from layoutResId.
955 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800956 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800957 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
958 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 return favorite;
961 }
962
963 /**
964 * Add an application shortcut to the workspace.
965 *
966 * @param data The intent describing the application.
967 * @param cellInfo The position on screen where to create the shortcut.
968 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800969 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -0700970 int intersectCellX, int intersectCellY) {
971 final int[] cellXY = mTmpAddItemCellCoordinates;
972 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
973
974 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
975 showOutOfSpaceMessage();
976 return;
977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800979 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800980
Romain Guy73b979d2009-06-09 12:57:21 -0700981 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800982 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800984 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700985 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
986 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800987 } else {
988 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 }
990 }
Romain Guycbb89e42009-06-08 15:52:54 -0700991
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 /**
993 * Add a shortcut to the workspace.
994 *
995 * @param data The intent describing the shortcut.
996 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700998 private void completeAddShortcut(Intent data, int screen,
999 int intersectCellX, int intersectCellY) {
1000 final int[] cellXY = mTmpAddItemCellCoordinates;
1001 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001002
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001003 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001004 boolean foundCellSpan = false;
1005 if (touchXY != null) {
1006 // when dragging and dropping, just find the closest free spot
1007 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1008 int[] result = screenLayout.findNearestVacantArea(
1009 touchXY[0], touchXY[1], 1, 1, cellXY);
1010 foundCellSpan = (result != null);
1011 } else {
1012 foundCellSpan = layout.findCellForSpanThatIntersects(
1013 cellXY, 1, 1, intersectCellX, intersectCellY);
1014 }
1015
1016 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001017 showOutOfSpaceMessage();
1018 return;
1019 }
1020
1021 final ShortcutInfo info = mModel.addShortcut(
1022 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023
1024 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001026 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 }
1028 }
1029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001031 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001033 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001034 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 */
Winson Chungf7640c82011-02-28 13:47:29 -08001036 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001037 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001038
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001039 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001040 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1041 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001044 // We have saved the position to which the widget was dragged-- this really only matters
1045 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001046 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001047
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001048 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001049 boolean foundCellSpan = false;
1050 if (touchXY != null) {
1051 // when dragging and dropping, just find the closest free spot
1052 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1053 int[] result = screenLayout.findNearestVacantArea(
1054 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001055 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001056 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001057 // if we long pressed on an empty cell to bring up a menu,
1058 // make sure we intersect the empty cell
1059 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1060 // findCellForSpanThatIntersects will just ignore them
1061 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1062 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001063 }
1064
Michael Jurka0280c3b2010-09-17 15:00:07 -07001065 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001066 if (appWidgetId != -1) {
1067 // Deleting an app widget ID is a void call but writes to disk before returning
1068 // to the caller...
1069 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1070 new Thread("deleteAppWidgetId") {
1071 public void run() {
1072 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1073 }
1074 }.start();
1075 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001077 return;
1078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001080 // Build Launcher-specific widget info and save to database
1081 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001082 launcherInfo.spanX = spanXY[0];
1083 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 LauncherModel.addItemToDatabase(this, launcherInfo,
1086 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001087 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088
1089 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001094
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001097
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001099 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001100
1101 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
1103 }
Romain Guycbb89e42009-06-08 15:52:54 -07001104
Adam Cohended9f8d2010-11-03 13:25:16 -07001105 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1106 @Override
1107 public void onReceive(Context context, Intent intent) {
1108 final String action = intent.getAction();
1109 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1110 mUserPresent = false;
1111 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001112
1113 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001114 if (mAppsCustomizeContent != null) {
1115 mAppsCustomizeContent.reset();
1116 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001117 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1118 mUserPresent = true;
1119 updateRunning();
1120 }
1121 }
1122 };
1123
1124 @Override
1125 public void onAttachedToWindow() {
1126 super.onAttachedToWindow();
1127
1128 // Listen for broadcasts related to user-presence
1129 final IntentFilter filter = new IntentFilter();
1130 filter.addAction(Intent.ACTION_SCREEN_OFF);
1131 filter.addAction(Intent.ACTION_USER_PRESENT);
1132 registerReceiver(mReceiver, filter);
1133
Adam Cohend113e0c2010-11-11 10:48:05 -08001134 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001135 mVisible = true;
1136 }
1137
1138 @Override
1139 public void onDetachedFromWindow() {
1140 super.onDetachedFromWindow();
1141 mVisible = false;
1142
Adam Cohend113e0c2010-11-11 10:48:05 -08001143 if (mAttached) {
1144 unregisterReceiver(mReceiver);
1145 mAttached = false;
1146 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001147 updateRunning();
1148 }
1149
1150 public void onWindowVisibilityChanged(int visibility) {
1151 mVisible = visibility == View.VISIBLE;
1152 updateRunning();
1153 }
1154
1155 private void sendAdvanceMessage(long delay) {
1156 mHandler.removeMessages(ADVANCE_MSG);
1157 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1158 mHandler.sendMessageDelayed(msg, delay);
1159 mAutoAdvanceSentTime = System.currentTimeMillis();
1160 }
1161
1162 private void updateRunning() {
1163 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1164 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1165 mAutoAdvanceRunning = autoAdvanceRunning;
1166 if (autoAdvanceRunning) {
1167 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1168 sendAdvanceMessage(delay);
1169 } else {
1170 if (!mWidgetsToAdvance.isEmpty()) {
1171 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1172 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1173 }
1174 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001175 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001176 }
1177 }
1178 }
1179
1180 private final Handler mHandler = new Handler() {
1181 @Override
1182 public void handleMessage(Message msg) {
1183 if (msg.what == ADVANCE_MSG) {
1184 int i = 0;
1185 for (View key: mWidgetsToAdvance.keySet()) {
1186 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1187 final int delay = mAdvanceStagger * i;
1188 if (v instanceof Advanceable) {
1189 postDelayed(new Runnable() {
1190 public void run() {
1191 ((Advanceable) v).advance();
1192 }
1193 }, delay);
1194 }
1195 i++;
1196 }
1197 sendAdvanceMessage(mAdvanceInterval);
1198 }
1199 }
1200 };
1201
1202 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1203 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1204 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1205 if (v instanceof Advanceable) {
1206 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001207 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001208 updateRunning();
1209 }
1210 }
1211
1212 void removeWidgetToAutoAdvance(View hostView) {
1213 if (mWidgetsToAdvance.containsKey(hostView)) {
1214 mWidgetsToAdvance.remove(hostView);
1215 updateRunning();
1216 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001217 }
1218
Joe Onorato9c1289c2009-08-17 11:03:03 -04001219 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1220 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001221 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001222 launcherInfo.hostView = null;
1223 }
1224
Adam Cohended9f8d2010-11-03 13:25:16 -07001225 void showOutOfSpaceMessage() {
1226 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1227 }
1228
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001229 public LauncherAppWidgetHost getAppWidgetHost() {
1230 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 }
Romain Guycbb89e42009-06-08 15:52:54 -07001232
Winson Chunga9abd0e2010-10-27 17:18:37 -07001233 public LauncherModel getModel() {
1234 return mModel;
1235 }
1236
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001237 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001238 getWindow().closeAllPanels();
1239
1240 try {
1241 dismissDialog(DIALOG_CREATE_SHORTCUT);
1242 // Unlock the workspace if the dialog was showing
1243 } catch (Exception e) {
1244 // An exception is thrown if the dialog is not visible, which is fine
1245 }
1246
1247 try {
1248 dismissDialog(DIALOG_RENAME_FOLDER);
1249 // Unlock the workspace if the dialog was showing
1250 } catch (Exception e) {
1251 // An exception is thrown if the dialog is not visible, which is fine
1252 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001253
1254 // Whatever we were doing is hereby canceled.
1255 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001256 }
1257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 @Override
1259 protected void onNewIntent(Intent intent) {
1260 super.onNewIntent(intent);
1261
1262 // Close the menu
1263 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001264 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001265 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001266
Adam Cohen95bb8002011-07-03 23:40:28 -07001267 closeFolder();
1268
Joe Onorato14f122b2009-11-19 14:06:36 -08001269 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1270 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001271
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001272 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001273 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001274 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001275 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001276 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001277 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001278 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001279
Joe Onorato3a8820b2009-11-10 15:06:42 -08001280 final View v = getWindow().peekDecorView();
1281 if (v != null && v.getWindowToken() != null) {
1282 InputMethodManager imm = (InputMethodManager)getSystemService(
1283 INPUT_METHOD_SERVICE);
1284 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001286
1287 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001288 if (mAppsCustomizeContent != null) {
1289 mAppsCustomizeContent.reset();
1290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
1292 }
1293
1294 @Override
1295 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1296 // Do not call super here
1297 mSavedInstanceState = savedInstanceState;
1298 }
1299
1300 @Override
1301 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001302 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001303 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304
Michael Jurka883f55b2010-10-21 15:47:14 -07001305 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306
Michael Jurka0280c3b2010-09-17 15:00:07 -07001307 if (mAddScreen > -1 && mWaitingForResult) {
1308 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1309 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1310 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312
1313 if (mFolderInfo != null && mWaitingForResult) {
1314 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1315 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1316 }
Michael Jurka946ad472010-07-09 18:05:18 -07001317
Winson Chung785d2eb2011-04-14 16:08:02 -07001318 // Save the current AppsCustomize tab
1319 if (mAppsCustomizeTabHost != null) {
1320 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1321 if (currentTabTag != null) {
1322 outState.putString("apps_customize_currentTab", currentTabTag);
1323 }
1324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326
1327 @Override
1328 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001330
Winson Chungcd2b0142011-06-08 16:02:26 -07001331 // Stop callbacks from LauncherModel
1332 LauncherApplication app = ((LauncherApplication) getApplication());
1333 mModel.stopLoader();
1334 app.setLauncher(null);
1335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001337 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001339 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001341 mAppWidgetHost = null;
1342
1343 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344
1345 TextKeyListener.getInstance().release();
1346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347
Joe Onorato9c1289c2009-08-17 11:03:03 -04001348 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001349
1350 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001351 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001352
1353 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1354 mWorkspace.removeAllViews();
1355 mWorkspace = null;
1356 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001357
1358 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360
Adam Cohena9cf38f2011-05-02 15:36:58 -07001361 public DragController getDragController() {
1362 return mDragController;
1363 }
1364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 @Override
1366 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001367 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 super.startActivityForResult(intent, requestCode);
1369 }
1370
1371 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001372 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001374
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001375 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001376
Karl Rosaen138a0412009-04-23 19:00:21 -07001377 if (initialQuery == null) {
1378 // Use any text typed in the launcher as the initial query
1379 initialQuery = getTypedText();
1380 clearTypedText();
1381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 if (appSearchData == null) {
1383 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001384 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
Romain Guycbb89e42009-06-08 15:52:54 -07001386
Karl Rosaen138a0412009-04-23 19:00:21 -07001387 final SearchManager searchManager =
1388 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001389 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001390 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
1392
1393 @Override
1394 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001395 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001396 return false;
1397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398
1399 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1402 .setIcon(android.R.drawable.ic_menu_add)
1403 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001404 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1405 .setIcon(android.R.drawable.ic_menu_manage)
1406 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001407 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 .setIcon(android.R.drawable.ic_menu_gallery)
1409 .setAlphabeticShortcut('W');
1410 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1411 .setIcon(android.R.drawable.ic_search_category_default)
1412 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1413 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1414 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1415 .setAlphabeticShortcut('N');
1416
1417 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001418 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1419 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420
1421 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1422 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1423 .setIntent(settings);
1424
1425 return true;
1426 }
1427
1428 @Override
1429 public boolean onPrepareOptionsMenu(Menu menu) {
1430 super.onPrepareOptionsMenu(menu);
1431
Winson Chung785d2eb2011-04-14 16:08:02 -07001432 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1433 // related code?
1434 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435
1436 return true;
1437 }
1438
1439 @Override
1440 public boolean onOptionsItemSelected(MenuItem item) {
1441 switch (item.getItemId()) {
1442 case MENU_ADD:
1443 addItems();
1444 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001445 case MENU_MANAGE_APPS:
1446 manageApps();
1447 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 case MENU_WALLPAPER_SETTINGS:
1449 startWallpaper();
1450 return true;
1451 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001452 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 return true;
1454 case MENU_NOTIFICATIONS:
1455 showNotifications();
1456 return true;
1457 }
1458
1459 return super.onOptionsItemSelected(item);
1460 }
Romain Guycbb89e42009-06-08 15:52:54 -07001461
Karl Rosaen138a0412009-04-23 19:00:21 -07001462 /**
1463 * Indicates that we want global search for this activity by setting the globalSearch
1464 * argument for {@link #startSearch} to true.
1465 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001467 @Override
1468 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001469 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001470 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001471 }
1472
Joe Onorato9c1289c2009-08-17 11:03:03 -04001473 public boolean isWorkspaceLocked() {
1474 return mWorkspaceLoading || mWaitingForResult;
1475 }
1476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001478 showWorkspace(true);
1479 showAddDialog(-1, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
Michael Jurka0280c3b2010-09-17 15:00:07 -07001482 private void resetAddInfo() {
1483 mAddScreen = -1;
1484 mAddIntersectCellX = -1;
1485 mAddIntersectCellY = -1;
1486 mAddDropPosition = null;
1487 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001488
Winson Chung55cef262010-10-28 14:14:18 -07001489 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001490 resetAddInfo();
1491 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001492 mAddDropPosition = position;
1493
Michael Jurkaaf442092010-06-10 17:01:57 -07001494 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001495 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1496 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001497 }
1498
Winson Chung7ad01412010-09-27 11:33:03 -07001499 private void manageApps() {
1500 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1501 }
1502
Michael Jurkaaf442092010-06-10 17:01:57 -07001503 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001504 // TODO: catch bad widget exception when sent
1505 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001506 // TODO: Is this log message meaningful?
1507 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001508 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001509 }
1510
Winson Chung55cef262010-10-28 14:14:18 -07001511 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001512 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513
Bjorn Bringert7984c942009-12-09 15:38:25 +00001514 if (appWidget.configure != null) {
1515 // Launch over to configure widget, if needed
1516 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1517 intent.setComponent(appWidget.configure);
1518 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001519 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001520 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1521 intent.putExtra(
1522 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1523 info.mimeType);
1524
1525 final String mimeType = info.mimeType;
1526 final ClipData clipData = (ClipData) info.configurationData;
1527 final ClipDescription clipDesc = clipData.getDescription();
1528 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1529 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001530 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001531 final CharSequence stringData = item.getText();
1532 final Uri uriData = item.getUri();
1533 final Intent intentData = item.getIntent();
1534 final String key =
1535 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1536 if (uriData != null) {
1537 intent.putExtra(key, uriData);
1538 } else if (intentData != null) {
1539 intent.putExtra(key, intentData);
1540 } else if (stringData != null) {
1541 intent.putExtra(key, stringData);
1542 }
1543 break;
1544 }
1545 }
1546 }
Winson Chung55cef262010-10-28 14:14:18 -07001547 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001548
Romain Guy8e633c52010-03-04 12:51:36 -08001549 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001551 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001552 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1557 resetAddInfo();
1558 mAddScreen = screen;
1559 mAddDropPosition = position;
1560
1561 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1562 createShortcutIntent.setComponent(componentName);
1563 processShortcut(createShortcutIntent);
1564 }
1565
Joe Onoratodeb98af2010-02-19 14:59:39 -08001566 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001567 // Handle case where user selected "Applications"
1568 String applicationName = getResources().getString(R.string.group_applications);
1569 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001571 if (applicationName != null && applicationName.equals(shortcutName)) {
1572 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1573 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001574
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001575 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1576 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001577 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001578 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001579 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001580 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001581 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
1583
Winson Chung24ab2f12010-09-16 14:10:47 -07001584 void processWallpaper(Intent intent) {
1585 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1586 }
1587
Adam Cohendf035382011-04-11 17:22:04 -07001588 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001589 FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 folderInfo.title = getText(R.string.folder_name);
1591
Michael Jurka0280c3b2010-09-17 15:00:07 -07001592 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1593 final int[] cellXY = mTmpAddItemCellCoordinates;
1594 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1595 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001596 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001597 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598
1599 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001600 LauncherModel.addItemToDatabase(this, folderInfo,
1601 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001602 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001603 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604
1605 // Create the view
1606 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001607 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1608 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001609 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001610 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 }
Romain Guycbb89e42009-06-08 15:52:54 -07001612
Joe Onorato9c1289c2009-08-17 11:03:03 -04001613 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001614 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001615 }
1616
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 private void showNotifications() {
1618 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1619 if (statusBar != null) {
1620 statusBar.expand();
1621 }
1622 }
1623
1624 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001625 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001627 Intent chooser = Intent.createChooser(pickWallpaper,
1628 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001629 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1630 // Removed in Eclair MR1
1631// WallpaperManager wm = (WallpaperManager)
1632// getSystemService(Context.WALLPAPER_SERVICE);
1633// WallpaperInfo wi = wm.getWallpaperInfo();
1634// if (wi != null && wi.getSettingsActivity() != null) {
1635// LabeledIntent li = new LabeledIntent(getPackageName(),
1636// R.string.configure_wallpaper, 0);
1637// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1638// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1639// }
Mike Clerona0618e42009-10-22 13:55:21 -07001640 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 }
1642
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001643 /**
1644 * Registers various content observers. The current implementation registers
1645 * only a favorites observer to keep track of the favorites applications.
1646 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001647 private void registerContentObservers() {
1648 ContentResolver resolver = getContentResolver();
1649 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1650 true, mWidgetObserver);
1651 }
1652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 @Override
1654 public boolean dispatchKeyEvent(KeyEvent event) {
1655 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1656 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001658 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001659 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001660 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001661 dumpState();
1662 return true;
1663 }
1664 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001665 }
1666 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1667 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001668 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 return true;
1670 }
1671 }
1672
1673 return super.dispatchKeyEvent(event);
1674 }
1675
Joe Onorato88ec0992009-11-19 13:16:06 -08001676 @Override
1677 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001678 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001679 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001680 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001681 Folder openFolder = mWorkspace.getOpenFolder();
1682 if (openFolder.isEditingName()) {
1683 openFolder.dismissEditingName();
1684 } else {
1685 closeFolder();
1686 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001687 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001688 mWorkspace.exitWidgetResizeMode();
1689
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001690 // Back button is a no-op here, but give at least some feedback for the button press
1691 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001692 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001693 }
1694
Adam Cohen2801caf2011-05-13 20:57:39 -07001695 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 Folder folder = mWorkspace.getOpenFolder();
1697 if (folder != null) {
1698 closeFolder(folder);
1699 }
1700 }
1701
1702 void closeFolder(Folder folder) {
1703 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001704
Michael Jurka8c920dd2011-01-20 14:16:56 -08001705 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 if (parent != null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001707 CellLayout cl = (CellLayout) mWorkspace.getChildAt(folder.mInfo.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07001708 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1709 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001710 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001712 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
1714
1715 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001716 * Re-listen when widgets are reset.
1717 */
1718 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001719 if (mAppWidgetHost != null) {
1720 mAppWidgetHost.startListening();
1721 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001722 }
1723
1724 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001725 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1726 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001728 private void unbindDesktopItems() {
1729 for (ItemInfo item: mDesktopItems) {
1730 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001732 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001734
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 /**
1736 * Launches the intent referred by the clicked shortcut.
1737 *
1738 * @param v The view representing the clicked shortcut.
1739 */
1740 public void onClick(View v) {
1741 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001742 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001744 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001745 int[] pos = new int[2];
1746 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001747 intent.setSourceBounds(new Rect(pos[0], pos[1],
1748 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001749 boolean success = startActivitySafely(intent, tag);
1750
1751 if (success && v instanceof BubbleTextView) {
1752 mWaitingForResume = (BubbleTextView) v;
1753 mWaitingForResume.setStayPressed(true);
1754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001756 if (v instanceof FolderIcon) {
1757 FolderIcon fi = (FolderIcon) v;
1758 handleFolderClick(fi);
1759 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001760 } else if (v == mAllAppsButton) {
1761 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001762 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001763 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001764 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 }
1767 }
1768
Michael Jurka0e260592010-06-30 17:07:39 -07001769 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001770 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001771 // clicking anywhere on the workspace causes the customization drawer to slide down
1772 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001773 return false;
1774 }
1775
Michael Jurkaaf442092010-06-10 17:01:57 -07001776 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001777 * Event handler for the search button
1778 *
1779 * @param v The view that was clicked.
1780 */
1781 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001782 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001783 // Use a custom animation for launching search
1784 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001785 }
1786
1787 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001788 * Event handler for the voice button
1789 *
1790 * @param v The view that was clicked.
1791 */
1792 public void onClickVoiceButton(View v) {
1793 startVoiceSearch();
1794 }
1795
1796 private void startVoiceSearch() {
1797 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1798 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1799 startActivity(intent);
1800 }
1801
1802 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001803 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001804 * enters home screen customization mode.
1805 *
1806 * @param v The view that was clicked.
1807 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001808 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001809 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001810 }
1811
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001812 /**
1813 * Event handler for the "grid" button that appears on the home screen, which
1814 * enters all apps mode.
1815 *
1816 * @param v The view that was clicked.
1817 */
1818 public void onClickAllAppsButton(View v) {
1819 showAllApps(true);
1820 }
1821
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001822 public void onClickAppMarketButton(View v) {
1823 if (mAppMarketIntent != null) {
1824 startActivitySafely(mAppMarketIntent, "app market");
1825 }
1826 }
1827
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001828 void startApplicationDetailsActivity(ComponentName componentName) {
1829 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001830 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1831 Uri.fromParts("package", packageName, null));
1832 startActivity(intent);
1833 }
1834
Patrick Dubroy5539af72010-09-07 15:22:01 -07001835 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1836 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1837 // System applications cannot be installed. For now, show a toast explaining that.
1838 // We may give them the option of disabling apps this way.
1839 int messageId = R.string.uninstall_system_app_text;
1840 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1841 } else {
1842 String packageName = appInfo.componentName.getPackageName();
1843 String className = appInfo.componentName.getClassName();
1844 Intent intent = new Intent(
1845 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1846 startActivity(intent);
1847 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001848 }
1849
Michael Jurkaddd62e92011-02-16 17:49:14 -08001850 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1852 try {
1853 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001854 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 } catch (ActivityNotFoundException e) {
1856 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001857 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 } catch (SecurityException e) {
1859 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001860 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001862 "or use the exported attribute for this activity. "
1863 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001865 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001867
Romain Guy8e633c52010-03-04 12:51:36 -08001868 void startActivityForResultSafely(Intent intent, int requestCode) {
1869 try {
1870 startActivityForResult(intent, requestCode);
1871 } catch (ActivityNotFoundException e) {
1872 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1873 } catch (SecurityException e) {
1874 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1875 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1876 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1877 "or use the exported attribute for this activity.", e);
1878 }
1879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880
Adam Cohena9cf38f2011-05-02 15:36:58 -07001881 private void handleFolderClick(FolderIcon folderIcon) {
1882 final FolderInfo info = folderIcon.mInfo;
1883 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 // Close any open folder
1885 closeFolder();
1886 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001887 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 } else {
1889 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001890 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 int folderScreen;
1892 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001893 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 // .. and close it
1895 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001896 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 // Close any folder open on the current screen
1898 closeFolder();
1899 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001900 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
1902 }
1903 }
1904 }
1905
Adam Cohen2801caf2011-05-13 20:57:39 -07001906 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1907 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1908 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1909 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1910
1911 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1912 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1913 oa.start();
1914 }
1915
1916 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1917 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1918 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1919 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1920
1921 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1922 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1923 oa.start();
1924 }
1925
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001927 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 * is animated relative to the specified View. If the View is null, no animation
1929 * is played.
1930 *
1931 * @param folderInfo The FolderInfo describing the folder to open.
1932 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001933 public void openFolder(FolderIcon folderIcon) {
1934 Folder folder = folderIcon.mFolder;
1935 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936
Adam Cohen2801caf2011-05-13 20:57:39 -07001937 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001938 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939
Adam Cohen8e776a62011-06-28 18:10:06 -07001940 mDragLayer.addView(folder);
1941 mDragController.addDropTarget((DropTarget) folder);
1942
Adam Cohena9cf38f2011-05-02 15:36:58 -07001943 folder.animateOpen();
1944 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001948 if (mState != State.WORKSPACE) {
1949 return false;
1950 }
1951
Romain Guy1fbc1c82009-11-09 20:43:08 -08001952 switch (v.getId()) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001953 case R.id.all_apps_button:
Winson Chungf0ea4d32011-06-06 14:27:16 -07001954 if (mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001955 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1956 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1957 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001958 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001959 return true;
1960 }
1961
Joe Onorato9c1289c2009-08-17 11:03:03 -04001962 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 return false;
1964 }
1965
1966 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001967 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 }
1969
Michael Jurka0280c3b2010-09-17 15:00:07 -07001970 resetAddInfo();
1971 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001973 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 return true;
1975 }
1976
Michael Jurka0280c3b2010-09-17 15:00:07 -07001977 final View itemUnderLongClick = longClickCellInfo.cell;
1978
Winson Chung304dcde2011-01-07 11:17:23 -08001979 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001980 if (itemUnderLongClick == null) {
1981 // User long pressed on empty space
1982 mWorkspace.setAllowLongPress(false);
1983 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1984 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07001985 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001987 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001989 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1990 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001991 mAddIntersectCellX = longClickCellInfo.cellX;
1992 mAddIntersectCellY = longClickCellInfo.cellY;
1993 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
1995 }
1996 }
1997 return true;
1998 }
1999
Romain Guye6b8e2f2009-11-10 11:56:55 -08002000 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002001 private void dismissPreview(final View v) {
2002 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002003 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002004 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2005 public void onDismiss() {
2006 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2007 int count = group.getChildCount();
2008 for (int i = 0; i < count; i++) {
2009 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2010 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002011 ArrayList<Bitmap> bitmaps =
2012 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002013 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2014
2015 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002016 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002017 window.setOnDismissListener(null);
2018 }
2019 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002020 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002021 }
2022 v.setTag(null);
2023 }
2024
Romain Guyf8e6a802009-12-07 17:48:02 -08002025 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002026 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002027 }
2028
Romain Guya6abce82009-11-10 02:54:41 -08002029 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002030 final Resources resources = getResources();
2031 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002032
Romain Guya6abce82009-11-10 02:54:41 -08002033 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002034
Romain Guy9d31e9f2009-11-11 18:54:28 -08002035 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002036
Romain Guyf8e6a802009-12-07 17:48:02 -08002037 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002038 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002039 int extraW = (int) ((r.left + r.right) * max);
2040 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002041
2042 int aW = cell.getWidth() - extraW;
2043 float w = aW / max;
2044
2045 int width = cell.getWidth();
2046 int height = cell.getHeight();
Winson Chung4b825dcd2011-06-19 12:41:22 -07002047 int x = cell.getPaddingLeft();
2048 int y = cell.getPaddingTop();
2049 width -= (x + cell.getPaddingRight());
2050 height -= (y + cell.getPaddingBottom());
Romain Guya6abce82009-11-10 02:54:41 -08002051
2052 float scale = w / width;
2053
2054 int count = end - start;
2055
2056 final float sWidth = width * scale;
2057 float sHeight = height * scale;
2058
Romain Guye6b8e2f2009-11-10 11:56:55 -08002059 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002060
Romain Guye6b8e2f2009-11-10 11:56:55 -08002061 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2062 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002063
2064 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002065 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002066 cell = (CellLayout) workspace.getChildAt(i);
2067
Romain Guyf8e6a802009-12-07 17:48:02 -08002068 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002069 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002070
2071 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002072 c.scale(scale, scale);
Winson Chung4b825dcd2011-06-19 12:41:22 -07002073 c.translate(-cell.getPaddingLeft(), -cell.getPaddingTop());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002074 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002075
Romain Guy9d31e9f2009-11-11 18:54:28 -08002076 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002077 image.setImageBitmap(bitmap);
2078 image.setTag(i);
2079 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002080 image.setOnFocusChangeListener(handler);
2081 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002082 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002083
2084 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002085 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2086
Winson Chungaafa03c2010-06-11 17:34:16 -07002087 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002088 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002089
2090 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002091 p.setContentView(preview);
2092 p.setWidth((int) (sWidth * count + extraW));
2093 p.setHeight((int) (sHeight + extraH));
2094 p.setAnimationStyle(R.style.AnimationPreview);
2095 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002096 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002097 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002098 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002099
Romain Guye6b8e2f2009-11-10 11:56:55 -08002100 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2101 public void onDismiss() {
2102 dismissPreview(anchor);
2103 }
2104 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002105
2106 anchor.setTag(p);
2107 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002108 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002109 }
2110
Romain Guy9d31e9f2009-11-11 18:54:28 -08002111 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002112 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002113
Romain Guye6b8e2f2009-11-10 11:56:55 -08002114 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002115 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002116 }
2117
2118 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002119 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002120 v.post(this);
2121 }
2122
2123 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002124 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002125 }
2126
2127 public void onFocusChange(View v, boolean hasFocus) {
2128 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002129 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002130 }
Romain Guya6abce82009-11-10 02:54:41 -08002131 }
2132 }
2133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 Workspace getWorkspace() {
2135 return mWorkspace;
2136 }
2137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 @Override
2139 protected Dialog onCreateDialog(int id) {
2140 switch (id) {
2141 case DIALOG_CREATE_SHORTCUT:
2142 return new CreateShortcut().createDialog();
2143 case DIALOG_RENAME_FOLDER:
2144 return new RenameFolder().createDialog();
2145 }
2146
2147 return super.onCreateDialog(id);
2148 }
2149
2150 @Override
2151 protected void onPrepareDialog(int id, Dialog dialog) {
2152 switch (id) {
2153 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 break;
2155 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002156 if (mFolderInfo != null) {
2157 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2158 final CharSequence text = mFolderInfo.title;
2159 input.setText(text);
2160 input.setSelection(0, text.length());
2161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 break;
2163 }
2164 }
2165
2166 void showRenameDialog(FolderInfo info) {
2167 mFolderInfo = info;
2168 mWaitingForResult = true;
2169 showDialog(DIALOG_RENAME_FOLDER);
2170 }
2171
Michael Jurka0280c3b2010-09-17 15:00:07 -07002172 private void showAddDialog(int intersectX, int intersectY) {
2173 resetAddInfo();
2174 mAddIntersectCellX = intersectX;
2175 mAddIntersectCellY = intersectY;
2176 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 mWaitingForResult = true;
2178 showDialog(DIALOG_CREATE_SHORTCUT);
2179 }
2180
Joe Onoratodeb98af2010-02-19 14:59:39 -08002181 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002182 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002183 Bundle bundle = new Bundle();
2184
2185 ArrayList<String> shortcutNames = new ArrayList<String>();
2186 shortcutNames.add(getString(R.string.group_applications));
2187 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2188
2189 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2190 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2191 R.drawable.ic_launcher_application));
2192 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2193
2194 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2195 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002196 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002197 pickIntent.putExtras(bundle);
2198
Joe Onoratodeb98af2010-02-19 14:59:39 -08002199 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002200 }
2201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 private class RenameFolder {
2203 private EditText mInput;
2204
2205 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2207 mInput = (EditText) layout.findViewById(R.id.folder_name);
2208
2209 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2210 builder.setIcon(0);
2211 builder.setTitle(getString(R.string.rename_folder_title));
2212 builder.setCancelable(true);
2213 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2214 public void onCancel(DialogInterface dialog) {
2215 cleanup();
2216 }
2217 });
2218 builder.setNegativeButton(getString(R.string.cancel_action),
2219 new Dialog.OnClickListener() {
2220 public void onClick(DialogInterface dialog, int which) {
2221 cleanup();
2222 }
2223 }
2224 );
2225 builder.setPositiveButton(getString(R.string.rename_action),
2226 new Dialog.OnClickListener() {
2227 public void onClick(DialogInterface dialog, int which) {
2228 changeFolderName();
2229 }
2230 }
2231 );
2232 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002233
2234 final AlertDialog dialog = builder.create();
2235 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2236 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002237 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002238 mInput.requestFocus();
2239 InputMethodManager inputManager = (InputMethodManager)
2240 getSystemService(Context.INPUT_METHOD_SERVICE);
2241 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002242 }
2243 });
2244
2245 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 }
2247
2248 private void changeFolderName() {
2249 final String name = mInput.getText().toString();
2250 if (!TextUtils.isEmpty(name)) {
2251 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002252 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 mFolderInfo.title = name;
2254 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2255
Joe Onorato9c1289c2009-08-17 11:03:03 -04002256 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002257 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002258 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 } else {
2260 final FolderIcon folderIcon = (FolderIcon)
2261 mWorkspace.getViewForTag(mFolderInfo);
2262 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002263 // TODO: At some point we'll probably want some version of setting
2264 // the text for a folder icon.
2265 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 getWorkspace().requestLayout();
2267 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002268 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002269 mWorkspaceLoading = true;
2270 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 }
2272 }
2273 }
2274 cleanup();
2275 }
2276
2277 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 dismissDialog(DIALOG_RENAME_FOLDER);
2279 mWaitingForResult = false;
2280 mFolderInfo = null;
2281 }
2282 }
2283
Daniel Sandler843e8602010-06-07 14:59:01 -04002284 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2285 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002286 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002287 }
2288
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002289 // AllAppsView.Watcher
2290 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002291 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002292 mWorkspace.setVisibility(View.GONE);
2293 }
2294 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002295
2296 /**
2297 * Helper method for the cameraZoomIn/cameraZoomOut animations
2298 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002299 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002300 * @param scaleFactor The scale factor used for the zoom
2301 */
2302 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2303 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002304
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002305 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002306 // Set pivotY so that at the starting zoom factor, the view is partially
2307 // visible. Modifying initialHeightFactor changes how much of the view is
2308 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002309 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002310 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002311 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002312 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002313 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002314 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002315 }
2316 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002317
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002318 /**
2319 * Zoom the camera out from the workspace to reveal 'toView'.
2320 * Assumes that the view to show is anchored at either the very top or very bottom
2321 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002322 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002323 */
Winson Chung20f71112011-04-06 14:22:04 -07002324 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002325 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002326
Winson Chungf0ea4d32011-06-06 14:27:16 -07002327 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2328 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2329 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2330 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002331
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002332 setPivotsForZoom(toView, toState, scale);
2333
Winson Chung20f71112011-04-06 14:22:04 -07002334
Winson Chungf0ea4d32011-06-06 14:27:16 -07002335 if (springLoaded) {
2336 if (toState == State.APPS_CUSTOMIZE) {
2337 // Shrink workspaces away if going back to AppsCustomize from spring loaded mode
2338 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2339 } else {
2340 // Shrink workspaces to bottom if going back to AllApps from spring loaded mode
2341 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
Winson Chung20f71112011-04-06 14:22:04 -07002342 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002343 } else {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002344 // Shrink workspaces away if going to AllApps/AppsCustomize from workspace
2345 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002346 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002347
2348 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002349 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002350 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002351 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2352 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002353 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002354 toView.setFastScaleX(a * scale + b * 1f);
2355 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002356 }
2357 });
Adam Cohen61033d32010-11-15 18:29:44 -08002358
Winson Chungf0ea4d32011-06-06 14:27:16 -07002359 toView.setVisibility(View.VISIBLE);
2360 toView.setFastAlpha(0f);
2361 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2362 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2363 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2364 public void onAnimationUpdate(float a, float b) {
2365 // don't need to invalidate because we do so above
2366 toView.setFastAlpha(a * 0f + b * 1f);
2367 }
2368 });
2369 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002370
Michael Jurkaabded662011-03-04 12:06:57 -08002371 if (toView instanceof LauncherTransitionable) {
2372 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002373 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002374 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002375 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002376 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002377 // Prepare the position
2378 toView.setTranslationX(0.0f);
2379 toView.setTranslationY(0.0f);
2380 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002381 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002382
2383 // Hide the workspace scrollbar
2384 mWorkspace.hideScrollingIndicator(true);
2385 mWorkspace.hideScrollIndicatorTrack();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002386 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002387 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002388 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002389 // If we don't set the final scale values here, if this animation is cancelled
2390 // it will have the wrong scale value and subsequent cameraPan animations will
2391 // not fix that
2392 toView.setScaleX(1.0f);
2393 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002394 if (toView instanceof LauncherTransitionable) {
2395 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002396 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002397 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002398 });
2399
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002400 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002401 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002402
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002403 if (mStateAnimation != null) mStateAnimation.cancel();
2404 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002405 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002406 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002407 } else {
2408 toView.setTranslationX(0.0f);
2409 toView.setTranslationY(0.0f);
2410 toView.setScaleX(1.0f);
2411 toView.setScaleY(1.0f);
2412 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002413 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002414 if (toView instanceof LauncherTransitionable) {
2415 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2416 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002417
2418 // Hide the workspace scrollbar
2419 mWorkspace.hideScrollingIndicator(true);
2420 mWorkspace.hideScrollIndicatorTrack();
Michael Jurkaabded662011-03-04 12:06:57 -08002421 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002422 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002423 }
2424
2425 /**
2426 * Zoom the camera back into the workspace, hiding 'fromView'.
2427 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002428 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002429 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002430 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002431 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002432 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002433
Winson Chungf0ea4d32011-06-06 14:27:16 -07002434 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2435 final float scaleFactor = (float)
2436 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2437 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002438
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002439 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002440
Michael Jurkad3ef3062010-11-23 16:23:58 -08002441 if (!springLoaded) {
2442 mWorkspace.unshrink(animated);
2443 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002444 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002445 if (mStateAnimation != null) mStateAnimation.cancel();
2446 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002447
Michael Jurka742574b2011-02-02 23:51:01 -08002448 final float oldScaleX = fromView.getScaleX();
2449 final float oldScaleY = fromView.getScaleY();
2450
2451 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2452 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002453 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2454 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002455 ((View)fromView.getParent()).fastInvalidate();
2456 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2457 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2458 }
2459 });
Michael Jurkaabded662011-03-04 12:06:57 -08002460 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002461 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002462 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002463 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2464 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002465 // don't need to invalidate because we do so above
2466 fromView.setFastAlpha(a * 1f + b * 0f);
2467 }
2468 });
Michael Jurkaabded662011-03-04 12:06:57 -08002469 if (fromView instanceof LauncherTransitionable) {
2470 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002471 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002472 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002473 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002474 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002475 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002476 if (fromView instanceof LauncherTransitionable) {
2477 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Winson Chung3ac74c52011-06-30 17:39:37 -07002478
2479 // Show the workspace scrollbar
2480 mWorkspace.showScrollIndicatorTrack();
2481 mWorkspace.flashScrollingIndicator();
Michael Jurka2763be32011-02-24 11:19:57 -08002482 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002483 }
2484 });
2485
Winson Chungf0ea4d32011-06-06 14:27:16 -07002486 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002487 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002488 } else {
2489 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002490 if (fromView instanceof LauncherTransitionable) {
2491 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2492 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002493
2494 // Show the workspace scrollbar
2495 mWorkspace.showScrollIndicatorTrack();
2496 mWorkspace.flashScrollingIndicator();
Michael Jurkaabded662011-03-04 12:06:57 -08002497 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002498 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002499 }
2500
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002501 void showWorkspace(boolean animated) {
2502 showWorkspace(animated, null);
2503 }
2504
2505 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002506 if (layout != null) {
2507 // always animated, but that's ok since we never specify a layout and
2508 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002509 mWorkspace.unshrink(layout);
2510 } else {
2511 mWorkspace.unshrink(animated);
2512 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002513 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002514 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002515 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002516
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002517 // Change the state *after* we've called all the transition code
2518 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002519
Winson Chung5fb63472011-02-02 17:03:37 -08002520 // Resume the auto-advance of widgets
2521 mUserPresent = true;
2522 updateRunning();
2523
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002524 // send an accessibility event to announce the context change
2525 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002526 }
2527
Michael Jurkad3ef3062010-11-23 16:23:58 -08002528 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002529 // Enter spring loaded mode on a new layout
Michael Jurkad3ef3062010-11-23 16:23:58 -08002530 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002531
2532 if (mState == State.APPS_CUSTOMIZE) {
2533 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
2534 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungb26f3d62011-06-02 10:49:29 -07002535 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002536 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002537 }
2538
2539 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002540 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002541 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chungb26f3d62011-06-02 10:49:29 -07002542 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2543 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002544 }
2545 // Otherwise, we are not in spring loaded mode, so don't do anything.
2546 }
2547
2548 /**
2549 * Shows the dock/hotseat area.
2550 */
2551 void showDock(boolean animated) {
2552 if (!LauncherApplication.isScreenLarge()) {
2553 if (animated) {
2554 int duration = mSearchDeleteBar.getTransitionInDuration();
2555 mButtonCluster.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002556 } else {
2557 mButtonCluster.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002558 }
2559 }
2560 }
2561
2562 /**
2563 * Hides the dock/hotseat area.
2564 */
2565 void hideDock(boolean animated) {
2566 if (!LauncherApplication.isScreenLarge()) {
2567 if (animated) {
2568 int duration = mSearchDeleteBar.getTransitionOutDuration();
2569 mButtonCluster.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002570 } else {
2571 mButtonCluster.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002572 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002573 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002574 }
2575
Winson Chung785d2eb2011-04-14 16:08:02 -07002576 void showAllApps(boolean animated) {
2577 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002578
Winson Chungf0ea4d32011-06-06 14:27:16 -07002579 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2580 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002581
Winson Chungf0ea4d32011-06-06 14:27:16 -07002582 // Hide the search bar and dock
2583 mSearchDeleteBar.hideSearchBar(animated);
2584 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002585
Winson Chungf0ea4d32011-06-06 14:27:16 -07002586 // Change the state *after* we've called all the transition code
2587 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002588
2589 // Pause the auto-advance of widgets until we are out of AllApps
2590 mUserPresent = false;
2591 updateRunning();
2592
2593 // Send an accessibility event to announce the context change
2594 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2595 }
2596
Joe Onoratob2061212009-11-24 16:13:54 -05002597 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002598 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002599 * - Home from workspace
2600 * - from center screen
2601 * - from other screens
2602 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002603 * - from center screen
2604 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002605 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002606 * - from center screen
2607 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002608 * - Launch app from workspace and quit
2609 * - with back
2610 * - with home
2611 * - Launch app from all apps and quit
2612 * - with back
2613 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002614 * - Go to a screen that's not the default, then all
2615 * apps, and launch and app, and go back
2616 * - with back
2617 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002618 * - On workspace, long press power and go back
2619 * - with back
2620 * - with home
2621 * - On all apps, long press power and go back
2622 * - with back
2623 * - with home
2624 * - On workspace, power off
2625 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002626 * - Launch an app and turn off the screen while in that app
2627 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002628 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002629 * - From all apps
2630 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002631 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2632 * - From all apps
2633 * - From the center workspace
2634 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002635 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002636 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002637 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2638 mWorkspace.setVisibility(View.VISIBLE);
2639 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002640
Winson Chungf0ea4d32011-06-06 14:27:16 -07002641 // Show the search bar and dock
2642 mSearchDeleteBar.showSearchBar(animated);
2643 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002644
Winson Chungf0ea4d32011-06-06 14:27:16 -07002645 // Set focus to the AppsCustomize button
2646 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002647 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002648 }
2649
Joe Onorato7c312c12009-08-13 21:36:53 -07002650 void lockAllApps() {
2651 // TODO
2652 }
2653
2654 void unlockAllApps() {
2655 // TODO
2656 }
2657
Patrick Dubroy5f445422011-02-18 14:35:21 -08002658 /**
2659 * Add an item from all apps or customize onto the given workspace screen.
2660 * If layout is null, add to the current screen.
2661 */
2662 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002663 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2664 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002665 } else {
2666 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002667 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002668 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002669
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002670 void onWorkspaceClick(CellLayout layout) {
2671 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002672 }
2673
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002674 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002675 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002676 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002677 // Look for the toolbar icon specified in the activity meta-data
2678 Bundle metaData = packageManager.getActivityInfo(
2679 activityName, PackageManager.GET_META_DATA).metaData;
2680 if (metaData != null) {
2681 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2682 if (iconResId != 0) {
2683 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002684 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002685 }
2686 }
2687 } catch (NameNotFoundException e) {
2688 // Do nothing
2689 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002690 return null;
2691 }
2692
2693 // if successful in getting icon, return it; otherwise, set button to use default drawable
2694 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2695 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2696 TextView button = (TextView) findViewById(buttonId);
2697 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2698
2699 // If we were unable to find the icon via the meta-data, use a generic one
2700 if (toolbarIcon == null) {
2701 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2702 return null;
2703 } else {
2704 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2705 return toolbarIcon.getConstantState();
2706 }
2707 }
2708
2709 // if successful in getting icon, return it; otherwise, set button to use default drawable
2710 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2711 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2712 ImageView button = (ImageView) findViewById(buttonId);
2713 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2714
Michael Jurka0423dcf2010-10-05 14:56:18 -07002715 // If we were unable to find the icon via the meta-data, use a generic one
2716 if (toolbarIcon == null) {
2717 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002718 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002719 } else {
2720 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002721 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002722 }
2723 }
2724
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002725 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2726 TextView button = (TextView) findViewById(buttonId);
2727 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2728 }
2729
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002730 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002731 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002732 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002733 }
2734
Michael Jurka0423dcf2010-10-05 14:56:18 -07002735 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002736 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2737 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002738
Winson Chung1cad91e2011-05-25 17:41:01 -07002739 final SearchManager searchManager =
2740 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2741 ComponentName activityName = searchManager.getGlobalSearchActivity();
2742 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002743 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
2744 R.id.search_button, activityName, R.drawable.ic_generic_search);
Winson Chung1cad91e2011-05-25 17:41:01 -07002745 searchButton.setVisibility(View.VISIBLE);
2746 searchDivider.setVisibility(View.VISIBLE);
2747 } else {
2748 searchButton.setVisibility(View.GONE);
2749 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002750 }
2751 }
2752
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002753 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002754 updateButtonWithDrawable(R.id.search_button, d);
2755 }
2756
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002757 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002758 final View searchDivider = findViewById(R.id.search_divider);
2759 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002760
Winson Chung1cad91e2011-05-25 17:41:01 -07002761 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2762 ComponentName activityName = intent.resolveActivity(getPackageManager());
2763 if (activityName != null) {
2764 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
2765 R.id.voice_button, activityName, R.drawable.ic_voice_search);
2766 searchDivider.setVisibility(View.VISIBLE);
2767 voiceButton.setVisibility(View.VISIBLE);
2768 } else {
2769 searchDivider.setVisibility(View.GONE);
2770 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002771 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002772 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002773
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002774 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002775 updateButtonWithDrawable(R.id.voice_button, d);
2776 }
2777
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002778 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002779 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002780 */
2781 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002782 final View marketButton = findViewById(R.id.market_button);
2783 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2784 // Find the app market activity by resolving an intent.
2785 // (If multiple app markets are installed, it will return the ResolverActivity.)
2786 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002787 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002788 mAppMarketIntent = intent;
2789 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002790 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002791 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002792
2793 // Remove the shop icon text in the Phone UI
2794 if (!LauncherApplication.isScreenLarge()) {
2795 ((TextView) marketButton).setText("");
2796 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002797 } else {
2798 // We should hide and disable the view so that we don't try and restore the visibility
2799 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2800 marketButton.setVisibility(View.GONE);
2801 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002802 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002803 }
2804
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002805 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002806 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002807 }
2808
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002809 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 * Displays the shortcut creation dialog and launches, if necessary, the
2811 * appropriate activity.
2812 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002813 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002814 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2815 DialogInterface.OnShowListener {
2816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002818
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002819 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002820 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002821
Winson Chung55b65502011-05-26 12:03:43 -07002822 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2823 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002824 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002825 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002826
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002827 AlertDialog dialog = builder.create();
2828 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002829 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002830 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002831
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 return dialog;
2833 }
2834
2835 public void onCancel(DialogInterface dialog) {
2836 mWaitingForResult = false;
2837 cleanup();
2838 }
2839
Romain Guycbb89e42009-06-08 15:52:54 -07002840 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002841 mWaitingForResult = false;
2842 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002843 }
2844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002846 try {
2847 dismissDialog(DIALOG_CREATE_SHORTCUT);
2848 } catch (Exception e) {
2849 // An exception is thrown if the dialog is not visible, which is fine
2850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 }
2852
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002853 /**
2854 * Handle the action clicked in the "Add to home" dialog.
2855 */
2856 public void onClick(DialogInterface dialog, int which) {
2857 Resources res = getResources();
2858 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002859
Winson Chung55b65502011-05-26 12:03:43 -07002860 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2861 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002862 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002863 if (mAppsCustomizeTabHost != null) {
2864 mAppsCustomizeTabHost.selectWidgetsTab();
2865 }
2866 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002867 break;
2868 }
Winson Chung55b65502011-05-26 12:03:43 -07002869 case AddAdapter.ITEM_APPLICATION: {
2870 if (mAppsCustomizeTabHost != null) {
2871 mAppsCustomizeTabHost.selectAppsTab();
2872 }
2873 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002874 break;
2875 }
Winson Chung55b65502011-05-26 12:03:43 -07002876 case AddAdapter.ITEM_APPWIDGET: {
2877 if (mAppsCustomizeTabHost != null) {
2878 mAppsCustomizeTabHost.selectWidgetsTab();
2879 }
2880 showAllApps(true);
2881 break;
2882 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002883 case AddAdapter.ITEM_WALLPAPER: {
2884 startWallpaper();
2885 break;
2886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887 }
2888 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002889
2890 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002891 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002893 }
2894
2895 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002896 * Receives notifications when applications are added/removed.
2897 */
2898 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2899 @Override
2900 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002901 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002902 String reason = intent.getStringExtra("reason");
2903 if (!"homekey".equals(reason)) {
2904 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002905 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002906 animate = false;
2907 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002908 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002909 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002910 }
2911 }
2912
2913 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002914 * Receives notifications whenever the appwidgets are reset.
2915 */
2916 private class AppWidgetResetObserver extends ContentObserver {
2917 public AppWidgetResetObserver() {
2918 super(new Handler());
2919 }
2920
2921 @Override
2922 public void onChange(boolean selfChange) {
2923 onAppWidgetReset();
2924 }
2925 }
2926
2927 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002928 * If the activity is currently paused, signal that we need to re-run the loader
2929 * in onResume.
2930 *
2931 * This needs to be called from incoming places where resources might have been loaded
2932 * while we are paused. That is becaues the Configuration might be wrong
2933 * when we're not running, and if it comes back to what it was when we
2934 * were paused, we are not restarted.
2935 *
2936 * Implementation of the method from LauncherModel.Callbacks.
2937 *
2938 * @return true if we are currently paused. The caller might be able to
2939 * skip some work in that case since we will come back again.
2940 */
2941 public boolean setLoadOnResume() {
2942 if (mPaused) {
2943 Log.i(TAG, "setLoadOnResume");
2944 mOnResumeNeedsLoad = true;
2945 return true;
2946 } else {
2947 return false;
2948 }
2949 }
2950
2951 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002952 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002954 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002955 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002956 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002957 } else {
2958 return SCREEN_COUNT / 2;
2959 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002960 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002961
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002962
Joe Onorato9c1289c2009-08-17 11:03:03 -04002963 /**
2964 * Refreshes the shortcuts shown on the workspace.
2965 *
2966 * Implementation of the method from LauncherModel.Callbacks.
2967 */
2968 public void startBinding() {
2969 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002970
2971 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002972 int count = workspace.getChildCount();
2973 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002974 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002975 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2976 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002977 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002978
Joe Onorato9c1289c2009-08-17 11:03:03 -04002979 if (DEBUG_USER_INTERFACE) {
2980 android.widget.Button finishButton = new android.widget.Button(this);
2981 finishButton.setText("Finish");
2982 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2983
2984 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2985 public void onClick(View v) {
2986 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002990
2991 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
2992 // mDesktopItems -> AppWidgetInfo -> hostView).
2993 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002994 }
2995
2996 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002997 * Bind the items start-end from the list.
2998 *
2999 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003000 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003001 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3002
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003003 setLoadOnResume();
3004
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 final Workspace workspace = mWorkspace;
3006
3007 for (int i=start; i<end; i++) {
3008 final ItemInfo item = shortcuts.get(i);
3009 mDesktopItems.add(item);
3010 switch (item.itemType) {
3011 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3012 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003013 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003014 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3015 false);
3016 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003017 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003018 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003019 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003020 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003021 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3022 false);
3023 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025 }
3026
Joe Onorato9c1289c2009-08-17 11:03:03 -04003027 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
3029
Joe Onorato9c1289c2009-08-17 11:03:03 -04003030 /**
3031 * Implementation of the method from LauncherModel.Callbacks.
3032 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003033 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003034 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003035 sFolders.clear();
3036 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003037 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003038
3039 /**
3040 * Add the views for a widget to the workspace.
3041 *
3042 * Implementation of the method from LauncherModel.Callbacks.
3043 */
3044 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003045 setLoadOnResume();
3046
Daniel Sandler843e8602010-06-07 14:59:01 -04003047 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3048 if (DEBUG_WIDGETS) {
3049 Log.d(TAG, "bindAppWidget: " + item);
3050 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 final Workspace workspace = mWorkspace;
3052
3053 final int appWidgetId = item.appWidgetId;
3054 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003055 if (DEBUG_WIDGETS) {
3056 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3057 }
3058
Joe Onorato9c1289c2009-08-17 11:03:03 -04003059 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3060
Joe Onorato9c1289c2009-08-17 11:03:03 -04003061 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3062 item.hostView.setTag(item);
3063
3064 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3065 item.cellY, item.spanX, item.spanY, false);
3066
Adam Cohended9f8d2010-11-03 13:25:16 -07003067 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3068
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 workspace.requestLayout();
3070
3071 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003072
3073 if (DEBUG_WIDGETS) {
3074 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3075 + (SystemClock.uptimeMillis()-start) + "ms");
3076 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003077 }
3078
3079 /**
3080 * Callback saying that there aren't any more items to bind.
3081 *
3082 * Implementation of the method from LauncherModel.Callbacks.
3083 */
3084 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003085 setLoadOnResume();
3086
Joe Onorato9c1289c2009-08-17 11:03:03 -04003087 if (mSavedState != null) {
3088 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003089 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003090 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 mSavedState = null;
3092 }
3093
3094 if (mSavedInstanceState != null) {
3095 super.onRestoreInstanceState(mSavedInstanceState);
3096 mSavedInstanceState = null;
3097 }
3098
Joe Onorato9c1289c2009-08-17 11:03:03 -04003099 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003100
3101 // If we received the result of any pending adds while the loader was running (e.g. the
3102 // widget configuration forced an orientation change), process them now.
3103 for (int i = 0; i < sPendingAddList.size(); i++) {
3104 completeAdd(sPendingAddList.get(i));
3105 }
3106 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003107 }
3108
3109 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003110 * Updates the icons on the launcher that are affected by changes to the package list
3111 * on the device.
3112 */
3113 private void updateIconsAffectedByPackageManagerChanges() {
3114 updateAppMarketIcon();
3115 updateGlobalSearchIcon();
3116 updateVoiceSearchIcon();
3117 }
3118
3119 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 * Add the icons for all apps.
3121 *
3122 * Implementation of the method from LauncherModel.Callbacks.
3123 */
3124 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003125 if (mAppsCustomizeContent != null) {
3126 mAppsCustomizeContent.setApps(apps);
3127 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003128 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 }
3130
3131 /**
3132 * A package was installed.
3133 *
3134 * Implementation of the method from LauncherModel.Callbacks.
3135 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003136 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003137 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003139
Winson Chung785d2eb2011-04-14 16:08:02 -07003140 if (mAppsCustomizeContent != null) {
3141 mAppsCustomizeContent.addApps(apps);
3142 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003143 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003144 }
3145
3146 /**
3147 * A package was updated.
3148 *
3149 * Implementation of the method from LauncherModel.Callbacks.
3150 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003151 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003152 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003153 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003154 if (mWorkspace != null) {
3155 mWorkspace.updateShortcuts(apps);
3156 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003157
Winson Chung785d2eb2011-04-14 16:08:02 -07003158 if (mAppsCustomizeContent != null) {
3159 mAppsCustomizeContent.updateApps(apps);
3160 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003161 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003162 }
3163
3164 /**
3165 * A package was uninstalled.
3166 *
3167 * Implementation of the method from LauncherModel.Callbacks.
3168 */
Joe Onorato36115782010-06-17 13:28:48 -04003169 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003170 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003171 if (permanent) {
3172 mWorkspace.removeItems(apps);
3173 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003174
Winson Chung785d2eb2011-04-14 16:08:02 -07003175 if (mAppsCustomizeContent != null) {
3176 mAppsCustomizeContent.removeApps(apps);
3177 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003178 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003179 }
Joe Onoratobe386092009-11-17 17:32:16 -08003180
3181 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003182 * A number of packages were updated.
3183 */
3184 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003185
Winson Chung785d2eb2011-04-14 16:08:02 -07003186 if (mAppsCustomizeContent != null) {
3187 mAppsCustomizeContent.onPackagesUpdated();
3188 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003189 }
3190
Winson Chung400438b2011-01-16 17:53:48 -08003191 private int mapConfigurationOriActivityInfoOri(int configOri) {
3192 final Display d = getWindowManager().getDefaultDisplay();
3193 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3194 switch (d.getRotation()) {
3195 case Surface.ROTATION_0:
3196 case Surface.ROTATION_180:
3197 // We are currently in the same basic orientation as the natural orientation
3198 naturalOri = configOri;
3199 break;
3200 case Surface.ROTATION_90:
3201 case Surface.ROTATION_270:
3202 // We are currently in the other basic orientation to the natural orientation
3203 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3204 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3205 break;
3206 }
3207
3208 int[] oriMap = {
3209 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3210 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3211 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3212 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3213 };
3214 // Since the map starts at portrait, we need to offset if this device's natural orientation
3215 // is landscape.
3216 int indexOffset = 0;
3217 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3218 indexOffset = 1;
3219 }
3220 return oriMap[(d.getRotation() + indexOffset) % 4];
3221 }
3222 public void lockScreenOrientation() {
3223 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3224 .getConfiguration().orientation));
3225 }
3226 public void unlockScreenOrientation() {
3227 mHandler.postDelayed(new Runnable() {
3228 public void run() {
3229 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3230 }
3231 }, mRestoreScreenOrientationDelay);
3232 }
3233
Winson Chung80baf5a2010-08-09 16:03:15 -07003234 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003235 * Prints out out state for debugging.
3236 */
3237 public void dumpState() {
3238 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003239 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003240 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3241 Log.d(TAG, "mRestoring=" + mRestoring);
3242 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3243 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3244 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003245 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003246 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003247
Winson Chung785d2eb2011-04-14 16:08:02 -07003248 if (mAppsCustomizeContent != null) {
3249 mAppsCustomizeContent.dumpState();
3250 }
Joe Onoratobe386092009-11-17 17:32:16 -08003251 Log.d(TAG, "END launcher2 dump state");
3252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253}
Michael Jurka2763be32011-02-24 11:19:57 -08003254
Michael Jurkaabded662011-03-04 12:06:57 -08003255interface LauncherTransitionable {
3256 void onLauncherTransitionStart(Animator animation);
3257 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003258}