blob: c917aec1ad8bb62d873a165bfede54c7097fcf0c [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;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070037import android.content.ClipData;
38import android.content.ClipDescription;
Michael Jurkae326f182011-11-21 14:05:46 -080039import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040040import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070045import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040055import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070056import android.os.AsyncTask;
Adam Cohen50370f32011-08-25 18:57:14 -070057import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040062import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080063import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
68import android.text.TextUtils;
69import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080071import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080072import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.KeyEvent;
74import android.view.LayoutInflater;
75import android.view.Menu;
76import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070077import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080078import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070081import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080082import android.view.ViewTreeObserver;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070083import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070085import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070086import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080087import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070089import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070091import android.widget.ImageView;
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;
Adam Cohen558baaf2011-08-15 15:22:57 -070097import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.FileNotFoundException;
103import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.util.ArrayList;
106import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108/**
109 * Default launcher application.
110 */
Michael Jurka946ad472010-07-09 18:05:18 -0700111public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700112 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
113 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800114 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700115 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400118 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700119
Winson Chung70d72102011-08-12 11:24:35 -0700120 private static final int MENU_GROUP_WALLPAPER = 1;
121 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
122 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700123 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
124 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700127 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 private static final int REQUEST_PICK_APPLICATION = 6;
129 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700130 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700131 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
134
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800135 static final int SCREEN_COUNT = 5;
136 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Joe Onorato7c312c12009-08-13 21:36:53 -0700138 static final int DIALOG_CREATE_SHORTCUT = 1;
139 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Romain Guy98d01652009-06-30 16:21:04 -0700141 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
143 // Type: int
144 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700145 // Type: int
146 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700148 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
149 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
151 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700152 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700154 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: boolean
156 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
157 // Type: long
158 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
159
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700160 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
161
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700162 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700163 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700164 private State mState = State.WORKSPACE;
165 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800166 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700167
Joe Onorato9c1289c2009-08-17 11:03:03 -0400168 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700169 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
170 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700171 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700172 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800175 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800177 private final BroadcastReceiver mCloseSystemDialogsReceiver
178 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800179 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private LayoutInflater mInflater;
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800184 private View mQsbDivider;
185 private View mDockDivider;
186 private DragLayer mDragLayer;
187 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700188
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700189 private AppWidgetManager mAppWidgetManager;
190 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700191
Michael Jurkac9d95c52011-08-29 14:03:34 -0700192 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700193 private int[] mTmpAddItemCellCoordinates = new int[2];
194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private FolderInfo mFolderInfo;
196
Winson Chung3d503fb2011-07-13 17:25:49 -0700197 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800198 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700199
Winson Chungc51db6a2011-10-05 11:44:49 -0700200 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700201 private AppsCustomizeTabHost mAppsCustomizeTabHost;
202 private AppsCustomizePagedView mAppsCustomizeContent;
203 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private Bundle mSavedState;
206
207 private SpannableStringBuilder mDefaultKeySsb = null;
208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 private boolean mWorkspaceLoading = true;
210
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800211 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private boolean mRestoring;
213 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700214 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
216 private Bundle mSavedInstanceState;
217
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800220 private boolean mUserPresent = true;
221 private boolean mVisible = false;
222 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700224 private static LocaleConfiguration sLocaleConfiguration = null;
225
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700226 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700228 private Intent mAppMarketIntent = null;
229
Adam Cohended9f8d2010-11-03 13:25:16 -0700230 // Related to the auto-advancing of widgets
231 private final int ADVANCE_MSG = 1;
232 private final int mAdvanceInterval = 20000;
233 private final int mAdvanceStagger = 250;
234 private long mAutoAdvanceSentTime;
235 private long mAutoAdvanceTimeLeft = -1;
236 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
237 new HashMap<View, AppWidgetProviderInfo>();
238
Winson Chung400438b2011-01-16 17:53:48 -0800239 // Determines how long to wait after a rotation before restoring the screen orientation to
240 // match the sensor state.
241 private final int mRestoreScreenOrientationDelay = 500;
242
Michael Jurka4ef207b2010-11-29 17:05:45 -0800243 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700244 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
245 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
246 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800247
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700248 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
249
Adam Cohen2801caf2011-05-13 20:57:39 -0700250
Michael Jurkaddd62e92011-02-16 17:49:14 -0800251 private BubbleTextView mWaitingForResume;
252
Michael Jurkac1f5d262011-09-30 19:32:27 -0700253 private Runnable mBuildLayersRunnable = new Runnable() {
254 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700255 if (mWorkspace != null) {
256 mWorkspace.buildPageHardwareLayers();
257 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700258 }
259 };
260
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800261 private static ArrayList<PendingAddArguments> sPendingAddList
262 = new ArrayList<PendingAddArguments>();
263
264 private static class PendingAddArguments {
265 int requestCode;
266 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700267 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800268 int screen;
269 int cellX;
270 int cellY;
271 }
272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 @Override
274 protected void onCreate(Bundle savedInstanceState) {
275 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800276 LauncherApplication app = ((LauncherApplication)getApplication());
277 mModel = app.setLauncher(this);
278 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400279 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700281
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700282 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700283 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
284 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200287 android.os.Debug.startMethodTracing(
288 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 }
290
291 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 setContentView(R.layout.launcher);
293 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700294 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800296 registerContentObservers();
297
Joe Onorato7c312c12009-08-13 21:36:53 -0700298 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 mSavedState = savedInstanceState;
301 restoreState(mSavedState);
302
Winson Chunga12a2502010-12-20 14:41:35 -0800303 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700304 if (mAppsCustomizeContent != null) {
305 mAppsCustomizeContent.onPackagesUpdated();
306 }
Winson Chunga12a2502010-12-20 14:41:35 -0800307
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 if (PROFILE_STARTUP) {
309 android.os.Debug.stopMethodTracing();
310 }
311
312 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400313 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 }
315
Michael Jurkac57b7a82011-08-09 22:02:20 -0700316 if (!mModel.isAllAppsLoaded()) {
317 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
318 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
319 }
320
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 // For handling default keys
322 mDefaultKeySsb = new SpannableStringBuilder();
323 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800324
325 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
326 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800327
Winson Chungc51db6a2011-10-05 11:44:49 -0700328 boolean searchVisible = false;
329 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800330 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700331 int coi = getCurrentOrientationIndexForGlobalIcons();
332 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
333 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700334 updateAppMarketIcon();
335 searchVisible = updateGlobalSearchIcon();
336 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700337 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700338 if (sGlobalSearchIcon[coi] != null) {
339 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700340 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700341 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700342 if (sVoiceSearchIcon[coi] != null) {
343 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700344 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700345 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700346 if (sAppMarketIcon[coi] != null) {
347 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800348 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700349 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700350
351 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohendc16b602011-09-26 15:12:33 -0700352 if (LauncherApplication.isScreenLarge() || Build.TYPE.contentEquals("eng")) {
Adam Cohen446e9402011-09-15 18:21:21 -0700353 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 }
Romain Guycbb89e42009-06-08 15:52:54 -0700356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700358 if (sLocaleConfiguration == null) {
359 new AsyncTask<Void, Void, LocaleConfiguration>() {
360 @Override
361 protected LocaleConfiguration doInBackground(Void... unused) {
362 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
363 readConfiguration(Launcher.this, localeConfiguration);
364 return localeConfiguration;
365 }
366
367 @Override
368 protected void onPostExecute(LocaleConfiguration result) {
369 sLocaleConfiguration = result;
370 checkForLocaleChange(); // recursive, but now with a locale configuration
371 }
372 }.execute();
373 return;
374 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700375
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 final Configuration configuration = getResources().getConfiguration();
377
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700378 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 final String locale = configuration.locale.toString();
380
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700381 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 final int mcc = configuration.mcc;
383
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700384 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 final int mnc = configuration.mnc;
386
Romain Guy84f296c2009-11-04 15:00:44 -0800387 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388
Romain Guy84f296c2009-11-04 15:00:44 -0800389 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700390 sLocaleConfiguration.locale = locale;
391 sLocaleConfiguration.mcc = mcc;
392 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700393
Joe Onorato0589f0f2010-02-08 13:44:00 -0800394 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700395
396 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
397 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700398 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700399 public void run() {
400 writeConfiguration(Launcher.this, localeConfiguration);
401 }
402 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700403 }
404 }
405
406 private static class LocaleConfiguration {
407 public String locale;
408 public int mcc = -1;
409 public int mnc = -1;
410 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700411
Romain Guy98d01652009-06-30 16:21:04 -0700412 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
413 DataInputStream in = null;
414 try {
415 in = new DataInputStream(context.openFileInput(PREFERENCES));
416 configuration.locale = in.readUTF();
417 configuration.mcc = in.readInt();
418 configuration.mnc = in.readInt();
419 } catch (FileNotFoundException e) {
420 // Ignore
421 } catch (IOException e) {
422 // Ignore
423 } finally {
424 if (in != null) {
425 try {
426 in.close();
427 } catch (IOException e) {
428 // Ignore
429 }
430 }
431 }
432 }
433
434 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
435 DataOutputStream out = null;
436 try {
437 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
438 out.writeUTF(configuration.locale);
439 out.writeInt(configuration.mcc);
440 out.writeInt(configuration.mnc);
441 out.flush();
442 } catch (FileNotFoundException e) {
443 // Ignore
444 } catch (IOException e) {
445 //noinspection ResultOfMethodCallIgnored
446 context.getFileStreamPath(PREFERENCES).delete();
447 } finally {
448 if (out != null) {
449 try {
450 out.close();
451 } catch (IOException e) {
452 // Ignore
453 }
454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456 }
457
Adam Cohen716b51e2011-06-30 12:09:54 -0700458 public DragLayer getDragLayer() {
459 return mDragLayer;
460 }
461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 static int getScreen() {
463 synchronized (sLock) {
464 return sScreen;
465 }
466 }
467
468 static void setScreen(int screen) {
469 synchronized (sLock) {
470 sScreen = screen;
471 }
472 }
473
Winson Chung557d6ed2011-07-08 15:34:52 -0700474 /**
475 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
476 * a configuration step, this allows the proper animations to run after other transitions.
477 */
478 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700479 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800480 switch (args.requestCode) {
481 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700482 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
483 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800484 break;
485 case REQUEST_PICK_SHORTCUT:
486 processShortcut(args.intent);
487 break;
488 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700489 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
490 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700491 result = true;
492 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800493 case REQUEST_PICK_APPWIDGET:
494 addAppWidgetFromPick(args.intent);
495 break;
496 case REQUEST_CREATE_APPWIDGET:
497 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700498 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700499 result = true;
500 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800501 case REQUEST_PICK_WALLPAPER:
502 // We just wanted the activity result here so we can clear mWaitingForResult
503 break;
504 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700505 // In any situation where we have a multi-step drop, we should reset the add info only after
506 // we complete the drop
507 resetAddInfo();
508 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800509 }
510
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800512 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700513 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700514 mWaitingForResult = false;
515
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516 // The pattern used here is that a user PICKs a specific application,
517 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700518
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800519 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
520 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700521 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800522 final PendingAddArguments args = new PendingAddArguments();
523 args.requestCode = requestCode;
524 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700525 args.container = mPendingAddInfo.container;
526 args.screen = mPendingAddInfo.screen;
527 args.cellX = mPendingAddInfo.cellX;
528 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800529
530 // If the loader is still running, defer the add until it is done.
531 if (isWorkspaceLocked()) {
532 sPendingAddList.add(args);
533 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700534 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 }
Romain Guy18042c82009-11-06 11:44:55 -0800536 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700537 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
538 if (data != null) {
539 // Clean up the appWidgetId if we canceled
540 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
541 if (appWidgetId != -1) {
542 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700546
547 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700548 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 }
550
551 @Override
552 protected void onResume() {
553 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800554 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700555 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400556 mWorkspaceLoading = true;
557 mModel.startLoader(this, true);
558 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700559 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800561 if (mWaitingForResume != null) {
562 mWaitingForResume.setStayPressed(false);
563 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700564 // When we resume Launcher, a different Activity might be responsible for the app
565 // market intent, so refresh the icon
566 updateAppMarketIcon();
Michael Jurkae326f182011-11-21 14:05:46 -0800567 mAppsCustomizeTabHost.onResume();
Michael Jurkac1f5d262011-09-30 19:32:27 -0700568 if (!mWorkspaceLoading) {
Michael Jurkab737ee62011-11-15 15:57:22 -0800569 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
570 final Workspace workspace = mWorkspace;
571 // We want to let Launcher draw itself at least once before we force it to build
572 // layers on all the workspace pages, so that transitioning to Launcher from other
573 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
574 // a true draw so we wait until the second preDraw call to be safe
575 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
576 boolean mFirstTime = true;
577 public boolean onPreDraw() {
578 if (mFirstTime) {
579 mFirstTime = false;
580 } else {
581 workspace.post(mBuildLayersRunnable);
582 observer.removeOnPreDrawListener(this);
583 }
584 return true;
585 }
586 });
Michael Jurkac1f5d262011-09-30 19:32:27 -0700587 }
Mathew Inwood8193f692011-10-12 17:30:58 +0100588 clearTypedText();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 }
590
591 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700592 protected void onPause() {
593 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700594 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800595 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700596 }
Romain Guycbb89e42009-06-08 15:52:54 -0700597
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700598 @Override
599 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400600 // Flag the loader to stop early before switching
601 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700602 if (mAppsCustomizeContent != null) {
603 mAppsCustomizeContent.surrender();
604 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800605 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700606 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700607
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800608 // We can't hide the IME if it was forced open. So don't bother
609 /*
610 @Override
611 public void onWindowFocusChanged(boolean hasFocus) {
612 super.onWindowFocusChanged(hasFocus);
613
614 if (hasFocus) {
615 final InputMethodManager inputManager = (InputMethodManager)
616 getSystemService(Context.INPUT_METHOD_SERVICE);
617 WindowManager.LayoutParams lp = getWindow().getAttributes();
618 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
619 android.os.Handler()) {
620 protected void onReceiveResult(int resultCode, Bundle resultData) {
621 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
622 }
623 });
624 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
625 }
626 }
627 */
628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 private boolean acceptFilter() {
630 final InputMethodManager inputManager = (InputMethodManager)
631 getSystemService(Context.INPUT_METHOD_SERVICE);
632 return !inputManager.isFullscreenMode();
633 }
634
635 @Override
636 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700637 final int uniChar = event.getUnicodeChar();
638 final boolean handled = super.onKeyDown(keyCode, event);
639 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
640 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
642 keyCode, event);
643 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700644 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700645 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700646 // showSearchDialog()
647 // If there are multiple keystrokes before the search dialog takes focus,
648 // onSearchRequested() will be called for every keystroke,
649 // but it is idempotent, so it's fine.
650 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
652 }
653
Joe Onorato8a9625e2010-01-28 15:55:35 -0800654 // Eat the long press event so the keyboard doesn't come up.
655 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
656 return true;
657 }
658
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 return handled;
660 }
661
Karl Rosaen138a0412009-04-23 19:00:21 -0700662 private String getTypedText() {
663 return mDefaultKeySsb.toString();
664 }
665
666 private void clearTypedText() {
667 mDefaultKeySsb.clear();
668 mDefaultKeySsb.clearSpans();
669 Selection.setSelection(mDefaultKeySsb, 0);
670 }
671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700673 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
674 * State
675 */
676 private static State intToState(int stateOrdinal) {
677 State state = State.WORKSPACE;
678 final State[] stateValues = State.values();
679 for (int i = 0; i < stateValues.length; i++) {
680 if (stateValues[i].ordinal() == stateOrdinal) {
681 state = stateValues[i];
682 break;
683 }
684 }
685 return state;
686 }
687
688 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 * Restores the previous state, if it exists.
690 *
691 * @param savedState The previous state.
692 */
693 private void restoreState(Bundle savedState) {
694 if (savedState == null) {
695 return;
696 }
697
Michael Jurka883f55b2010-10-21 15:47:14 -0700698 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700699 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800700 showAllApps(false);
701 }
702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
704 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700705 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 }
707
Winson Chung3d503fb2011-07-13 17:25:49 -0700708 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
709 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700710
Winson Chung3d503fb2011-07-13 17:25:49 -0700711 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
712 mPendingAddInfo.container = pendingAddContainer;
713 mPendingAddInfo.screen = pendingAddScreen;
714 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
715 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 mRestoring = true;
717 }
718
719 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
720 if (renameFolder) {
721 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700722 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 mRestoring = true;
724 }
Winson Chunga12a2502010-12-20 14:41:35 -0800725
Winson Chung785d2eb2011-04-14 16:08:02 -0700726
727 // Restore the AppsCustomize tab
728 if (mAppsCustomizeTabHost != null) {
729 String curTab = savedState.getString("apps_customize_currentTab");
730 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700731 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700732 mAppsCustomizeContent.setContentType(
733 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
734 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700735 mAppsCustomizeContent.loadAssociatedPages(
736 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700737 }
738
Winson Chung5afbf7b2011-07-25 11:53:08 -0700739 int currentIndex = savedState.getInt("apps_customize_currentIndex");
740 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700741 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
743
744 /**
745 * Finds all the views we need and configure them properly.
746 */
747 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700748 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400749
Winson Chung4c98d922011-05-31 16:50:48 -0700750 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
751 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800752 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
753 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754
Winson Chung4c98d922011-05-31 16:50:48 -0700755 // Setup the drag layer
756 mDragLayer.setup(this, dragController);
757
Winson Chung3d503fb2011-07-13 17:25:49 -0700758 // Setup the hotseat
759 mHotseat = (Hotseat) findViewById(R.id.hotseat);
760 if (mHotseat != null) {
761 mHotseat.setup(this);
762 }
763
Winson Chung4c98d922011-05-31 16:50:48 -0700764 // Setup the workspace
765 mWorkspace.setHapticFeedbackEnabled(false);
766 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700767 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700768 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700769
Winson Chungf0ea4d32011-06-06 14:27:16 -0700770 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700771 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700772
Winson Chungf0ea4d32011-06-06 14:27:16 -0700773 // Setup AppsCustomize
774 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
775 findViewById(R.id.apps_customize_pane);
776 mAppsCustomizeContent = (AppsCustomizePagedView)
777 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
778 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400779
Michael Jurka5130e402011-10-13 04:55:35 -0700780 // Get the all apps button
781 mAllAppsButton = findViewById(R.id.all_apps_button);
782 if (mAllAppsButton != null) {
783 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
784 @Override
785 public boolean onTouch(View v, MotionEvent event) {
786 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
787 onTouchDownAllAppsButton(v);
788 }
789 return false;
790 }
791 });
792 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700794 dragController.setDragScoller(mWorkspace);
795 dragController.setScrollView(mDragLayer);
796 dragController.setMoveTarget(mWorkspace);
797 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700798 if (mSearchDropTargetBar != null) {
799 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800800 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 }
802
803 /**
804 * Creates a view representing a shortcut.
805 *
806 * @param info The data structure describing the shortcut.
807 *
808 * @return A View inflated from R.layout.application.
809 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800810 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700812 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
814
815 /**
816 * Creates a view representing a shortcut inflated from the specified resource.
817 *
818 * @param layoutResId The id of the XML layout used to create the shortcut.
819 * @param parent The group the shortcut belongs to.
820 * @param info The data structure describing the shortcut.
821 *
822 * @return A View inflated from layoutResId.
823 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800824 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800825 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
826 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 return favorite;
829 }
830
831 /**
832 * Add an application shortcut to the workspace.
833 *
834 * @param data The intent describing the application.
835 * @param cellInfo The position on screen where to create the shortcut.
836 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700837 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700838 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700839 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700840
Adam Cohenfbba09b2011-07-18 21:43:05 -0700841 // First we check if we already know the exact location where we want to add this item.
842 if (cellX >= 0 && cellY >= 0) {
843 cellXY[0] = cellX;
844 cellXY[1] = cellY;
845 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700846 showOutOfSpaceMessage();
847 return;
848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800850 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800851
Romain Guy73b979d2009-06-09 12:57:21 -0700852 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800853 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800855 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700857 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800858 } else {
859 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
861 }
Romain Guycbb89e42009-06-08 15:52:54 -0700862
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 /**
864 * Add a shortcut to the workspace.
865 *
866 * @param data The intent describing the shortcut.
867 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700869 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
870 int cellY) {
871 int[] cellXY = mTmpAddItemCellCoordinates;
872 int[] touchXY = mPendingAddInfo.dropPos;
873 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700874
Michael Jurkad3ef3062010-11-23 16:23:58 -0800875 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700876
Adam Cohen558baaf2011-08-15 15:22:57 -0700877 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
878 final View view = createShortcut(info);
879
Adam Cohenfbba09b2011-07-18 21:43:05 -0700880 // First we check if we already know the exact location where we want to add this item.
881 if (cellX >= 0 && cellY >= 0) {
882 cellXY[0] = cellX;
883 cellXY[1] = cellY;
884 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700885
886 // If appropriate, either create a folder or add to an existing folder
887 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
888 true, null,null)) {
889 return;
890 }
891 DragObject dragObject = new DragObject();
892 dragObject.dragInfo = info;
893 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
894 return;
895 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700896 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800897 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700898 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800899 foundCellSpan = (result != null);
900 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700901 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800902 }
903
904 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700905 showOutOfSpaceMessage();
906 return;
907 }
908
Adam Cohen558baaf2011-08-15 15:22:57 -0700909 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910
911 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700912 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
913 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 }
915 }
916
Adam Cohenf814aa02011-09-01 13:48:05 -0700917 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
918 if (spanXY == null) {
919 spanXY = new int[2];
920 }
921
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800922 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700923 // We want to account for the extra amount of padding that we are adding to the widget
924 // to ensure that it gets the full amount of space that it has requested
925 int requiredWidth = minWidth + padding.left + padding.right;
926 int requiredHeight = minHeight + padding.top + padding.bottom;
927 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
928 }
929
930 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
931 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
932 }
933
Adam Cohencbf47e32011-09-16 17:32:37 -0700934 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
935 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
936 }
937
Adam Cohenf814aa02011-09-01 13:48:05 -0700938 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
939 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
940 }
941
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700943 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700945 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700946 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700948 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700949 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700950
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700951 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700952 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700953
Adam Cohenf814aa02011-09-01 13:48:05 -0700954 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700955
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700957 // We have saved the position to which the widget was dragged-- this really only matters
958 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 int[] cellXY = mTmpAddItemCellCoordinates;
960 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700961 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700962 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
963 cellXY[0] = mPendingAddInfo.cellX;
964 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700965 foundCellSpan = true;
966 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700967 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700968 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700969 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800970 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700971 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700972 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700973 }
974
Michael Jurka0280c3b2010-09-17 15:00:07 -0700975 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800976 if (appWidgetId != -1) {
977 // Deleting an app widget ID is a void call but writes to disk before returning
978 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800979 new Thread("deleteAppWidgetId") {
980 public void run() {
981 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
982 }
983 }.start();
984 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700985 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800986 return;
987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700989 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700990 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700991 launcherInfo.spanX = spanXY[0];
992 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700993
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700995 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996
997 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700999 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001000
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001001 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001003
Winson Chung3d503fb2011-07-13 17:25:49 -07001004 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001006
1007 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 }
1009 }
Romain Guycbb89e42009-06-08 15:52:54 -07001010
Adam Cohended9f8d2010-11-03 13:25:16 -07001011 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1012 @Override
1013 public void onReceive(Context context, Intent intent) {
1014 final String action = intent.getAction();
1015 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1016 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001017 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001018 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001019
Winson Chungc100e8e2011-08-09 16:02:43 -07001020 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001021 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001022 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1023 mAppsCustomizeTabHost.reset();
1024 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001025 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001026 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1027 mUserPresent = true;
1028 updateRunning();
1029 }
1030 }
1031 };
1032
1033 @Override
1034 public void onAttachedToWindow() {
1035 super.onAttachedToWindow();
1036
1037 // Listen for broadcasts related to user-presence
1038 final IntentFilter filter = new IntentFilter();
1039 filter.addAction(Intent.ACTION_SCREEN_OFF);
1040 filter.addAction(Intent.ACTION_USER_PRESENT);
1041 registerReceiver(mReceiver, filter);
1042
Adam Cohend113e0c2010-11-11 10:48:05 -08001043 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001044 mVisible = true;
1045 }
1046
1047 @Override
1048 public void onDetachedFromWindow() {
1049 super.onDetachedFromWindow();
1050 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001051 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001052
Adam Cohend113e0c2010-11-11 10:48:05 -08001053 if (mAttached) {
1054 unregisterReceiver(mReceiver);
1055 mAttached = false;
1056 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001057 updateRunning();
1058 }
1059
1060 public void onWindowVisibilityChanged(int visibility) {
1061 mVisible = visibility == View.VISIBLE;
1062 updateRunning();
1063 }
1064
1065 private void sendAdvanceMessage(long delay) {
1066 mHandler.removeMessages(ADVANCE_MSG);
1067 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1068 mHandler.sendMessageDelayed(msg, delay);
1069 mAutoAdvanceSentTime = System.currentTimeMillis();
1070 }
1071
1072 private void updateRunning() {
1073 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1074 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1075 mAutoAdvanceRunning = autoAdvanceRunning;
1076 if (autoAdvanceRunning) {
1077 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1078 sendAdvanceMessage(delay);
1079 } else {
1080 if (!mWidgetsToAdvance.isEmpty()) {
1081 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1082 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1083 }
1084 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001085 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001086 }
1087 }
1088 }
1089
1090 private final Handler mHandler = new Handler() {
1091 @Override
1092 public void handleMessage(Message msg) {
1093 if (msg.what == ADVANCE_MSG) {
1094 int i = 0;
1095 for (View key: mWidgetsToAdvance.keySet()) {
1096 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1097 final int delay = mAdvanceStagger * i;
1098 if (v instanceof Advanceable) {
1099 postDelayed(new Runnable() {
1100 public void run() {
1101 ((Advanceable) v).advance();
1102 }
1103 }, delay);
1104 }
1105 i++;
1106 }
1107 sendAdvanceMessage(mAdvanceInterval);
1108 }
1109 }
1110 };
1111
1112 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001113 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001114 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1115 if (v instanceof Advanceable) {
1116 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001117 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001118 updateRunning();
1119 }
1120 }
1121
1122 void removeWidgetToAutoAdvance(View hostView) {
1123 if (mWidgetsToAdvance.containsKey(hostView)) {
1124 mWidgetsToAdvance.remove(hostView);
1125 updateRunning();
1126 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 }
1128
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001130 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 launcherInfo.hostView = null;
1132 }
1133
Adam Cohended9f8d2010-11-03 13:25:16 -07001134 void showOutOfSpaceMessage() {
1135 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1136 }
1137
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001138 public LauncherAppWidgetHost getAppWidgetHost() {
1139 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
Romain Guycbb89e42009-06-08 15:52:54 -07001141
Winson Chunga9abd0e2010-10-27 17:18:37 -07001142 public LauncherModel getModel() {
1143 return mModel;
1144 }
1145
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001146 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001147 getWindow().closeAllPanels();
1148
Winson Chung87acb482011-08-23 17:28:05 -07001149 /**
1150 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001151 try {
1152 dismissDialog(DIALOG_CREATE_SHORTCUT);
1153 // Unlock the workspace if the dialog was showing
1154 } catch (Exception e) {
1155 // An exception is thrown if the dialog is not visible, which is fine
1156 }
1157
1158 try {
1159 dismissDialog(DIALOG_RENAME_FOLDER);
1160 // Unlock the workspace if the dialog was showing
1161 } catch (Exception e) {
1162 // An exception is thrown if the dialog is not visible, which is fine
1163 }
Winson Chung87acb482011-08-23 17:28:05 -07001164 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001165
1166 // Whatever we were doing is hereby canceled.
1167 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001168 }
1169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 @Override
1171 protected void onNewIntent(Intent intent) {
1172 super.onNewIntent(intent);
1173
1174 // Close the menu
1175 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001176 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001177 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001178
Joe Onorato14f122b2009-11-19 14:06:36 -08001179 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1180 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001181
Adam Cohenac56cff2011-09-28 20:45:37 -07001182 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001183 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001184 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001185 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1186 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001187 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001188 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001189
1190 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001191 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001192 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001193
Joe Onorato3a8820b2009-11-10 15:06:42 -08001194 final View v = getWindow().peekDecorView();
1195 if (v != null && v.getWindowToken() != null) {
1196 InputMethodManager imm = (InputMethodManager)getSystemService(
1197 INPUT_METHOD_SERVICE);
1198 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001200
Michael Jurka35aa14d2011-07-07 17:01:08 -07001201 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001202 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001203 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001204 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
1206 }
1207
1208 @Override
1209 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1210 // Do not call super here
1211 mSavedInstanceState = savedInstanceState;
1212 }
1213
1214 @Override
1215 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001216 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001217 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218
Michael Jurka883f55b2010-10-21 15:47:14 -07001219 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001220 // We close any open folder since it will not be re-opened, and we need to make sure
1221 // this state is reflected.
1222 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223
Winson Chung3d503fb2011-07-13 17:25:49 -07001224 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1225 mWaitingForResult) {
1226 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1227 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1228 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1229 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
1231
1232 if (mFolderInfo != null && mWaitingForResult) {
1233 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1234 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1235 }
Michael Jurka946ad472010-07-09 18:05:18 -07001236
Winson Chung785d2eb2011-04-14 16:08:02 -07001237 // Save the current AppsCustomize tab
1238 if (mAppsCustomizeTabHost != null) {
1239 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1240 if (currentTabTag != null) {
1241 outState.putString("apps_customize_currentTab", currentTabTag);
1242 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001243 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1244 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001245 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 }
1247
1248 @Override
1249 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001251
Winson Chunge7a03942011-08-05 15:05:12 -07001252 // Remove all pending runnables
1253 mHandler.removeMessages(ADVANCE_MSG);
1254 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001255 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001256
Winson Chungcd2b0142011-06-08 16:02:26 -07001257 // Stop callbacks from LauncherModel
1258 LauncherApplication app = ((LauncherApplication) getApplication());
1259 mModel.stopLoader();
1260 app.setLauncher(null);
1261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001263 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001265 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001267 mAppWidgetHost = null;
1268
1269 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270
1271 TextKeyListener.getInstance().release();
1272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273
Winson Chung3d503fb2011-07-13 17:25:49 -07001274 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001275
1276 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001277 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001278
1279 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1280 mWorkspace.removeAllViews();
1281 mWorkspace = null;
1282 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001283
1284 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286
Adam Cohena9cf38f2011-05-02 15:36:58 -07001287 public DragController getDragController() {
1288 return mDragController;
1289 }
1290
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 @Override
1292 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001293 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 super.startActivityForResult(intent, requestCode);
1295 }
1296
Winson Chung70d72102011-08-12 11:24:35 -07001297 /**
1298 * Indicates that we want global search for this activity by setting the globalSearch
1299 * argument for {@link #startSearch} to true.
1300 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001302 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001304
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001305 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001306
Karl Rosaen138a0412009-04-23 19:00:21 -07001307 if (initialQuery == null) {
1308 // Use any text typed in the launcher as the initial query
1309 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 if (appSearchData == null) {
1312 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001313 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001315 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001316
Karl Rosaen138a0412009-04-23 19:00:21 -07001317 final SearchManager searchManager =
1318 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001319 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001320 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322
Winson Chung70d72102011-08-12 11:24:35 -07001323 @Override
1324 public boolean onCreateOptionsMenu(Menu menu) {
1325 if (isWorkspaceLocked()) {
1326 return false;
1327 }
1328
1329 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001330
1331 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1332 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1333 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001334 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1335 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1336 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001337 String helpUrl = getString(R.string.help_url);
1338 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001339 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1340 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1341
Winson Chung70d72102011-08-12 11:24:35 -07001342 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1343 .setIcon(android.R.drawable.ic_menu_gallery)
1344 .setAlphabeticShortcut('W');
1345 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1346 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001347 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001348 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001349 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1350 .setIcon(android.R.drawable.ic_menu_preferences)
1351 .setIntent(settings)
1352 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001353 if (!helpUrl.isEmpty()) {
1354 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1355 .setIcon(android.R.drawable.ic_menu_help)
1356 .setIntent(help)
1357 .setAlphabeticShortcut('H');
1358 }
Winson Chung70d72102011-08-12 11:24:35 -07001359 return true;
1360 }
1361
1362 @Override
1363 public boolean onPrepareOptionsMenu(Menu menu) {
1364 super.onPrepareOptionsMenu(menu);
1365
1366 if (mAppsCustomizeTabHost.isTransitioning()) {
1367 return false;
1368 }
1369 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1370 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1371
1372 return true;
1373 }
1374
1375 @Override
1376 public boolean onOptionsItemSelected(MenuItem item) {
1377 switch (item.getItemId()) {
1378 case MENU_WALLPAPER_SETTINGS:
1379 startWallpaper();
1380 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001381 }
1382
1383 return super.onOptionsItemSelected(item);
1384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001386 @Override
1387 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001388 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001389 // Use a custom animation for launching search
1390 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001391 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001392 }
1393
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 public boolean isWorkspaceLocked() {
1395 return mWorkspaceLoading || mWaitingForResult;
1396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001399 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001400 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 }
1402
Michael Jurka0280c3b2010-09-17 15:00:07 -07001403 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001404 mPendingAddInfo.container = ItemInfo.NO_ID;
1405 mPendingAddInfo.screen = -1;
1406 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1407 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1408 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001409 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001410
Michael Jurkaaf442092010-06-10 17:01:57 -07001411 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001412 // TODO: catch bad widget exception when sent
1413 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001414 // TODO: Is this log message meaningful?
1415 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001416 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001417 }
1418
Winson Chung55cef262010-10-28 14:14:18 -07001419 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001420 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421
Bjorn Bringert7984c942009-12-09 15:38:25 +00001422 if (appWidget.configure != null) {
1423 // Launch over to configure widget, if needed
1424 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1425 intent.setComponent(appWidget.configure);
1426 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001427 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001428 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1429 intent.putExtra(
1430 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1431 info.mimeType);
1432
1433 final String mimeType = info.mimeType;
1434 final ClipData clipData = (ClipData) info.configurationData;
1435 final ClipDescription clipDesc = clipData.getDescription();
1436 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1437 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001438 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001439 final CharSequence stringData = item.getText();
1440 final Uri uriData = item.getUri();
1441 final Intent intentData = item.getIntent();
1442 final String key =
1443 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1444 if (uriData != null) {
1445 intent.putExtra(key, uriData);
1446 } else if (intentData != null) {
1447 intent.putExtra(key, intentData);
1448 } else if (stringData != null) {
1449 intent.putExtra(key, stringData);
1450 }
1451 break;
1452 }
1453 }
1454 }
Winson Chung55cef262010-10-28 14:14:18 -07001455 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001456
Romain Guy8e633c52010-03-04 12:51:36 -08001457 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001459 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001460 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001461
1462 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001463 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
1465 }
Romain Guycbb89e42009-06-08 15:52:54 -07001466
Adam Cohenfbba09b2011-07-18 21:43:05 -07001467 /**
1468 * Process a shortcut drop.
1469 *
1470 * @param componentName The name of the component
1471 * @param screen The screen where it should be added
1472 * @param cell The cell it should be added to, optional
1473 * @param position The location on the screen where it was dropped, optional
1474 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001475 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1476 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001477 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001478 mPendingAddInfo.container = container;
1479 mPendingAddInfo.screen = screen;
1480 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001481
1482 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001483 mPendingAddInfo.cellX = cell[0];
1484 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001485 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001486
1487 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1488 createShortcutIntent.setComponent(componentName);
1489 processShortcut(createShortcutIntent);
1490 }
1491
Adam Cohenfbba09b2011-07-18 21:43:05 -07001492 /**
1493 * Process a widget drop.
1494 *
1495 * @param info The PendingAppWidgetInfo of the widget being added.
1496 * @param screen The screen where it should be added
1497 * @param cell The cell it should be added to, optional
1498 * @param position The location on the screen where it was dropped, optional
1499 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001500 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1501 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001502 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001503 mPendingAddInfo.container = info.container = container;
1504 mPendingAddInfo.screen = info.screen = screen;
1505 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001507 mPendingAddInfo.cellX = cell[0];
1508 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001509 }
1510
1511 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1512 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1513 addAppWidgetImpl(appWidgetId, info);
1514 }
1515
Joe Onoratodeb98af2010-02-19 14:59:39 -08001516 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001517 // Handle case where user selected "Applications"
1518 String applicationName = getResources().getString(R.string.group_applications);
1519 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001520
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001521 if (applicationName != null && applicationName.equals(shortcutName)) {
1522 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1523 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001524
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001525 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1526 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001527 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001528 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001529 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001530 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001531 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
1533
Winson Chung24ab2f12010-09-16 14:10:47 -07001534 void processWallpaper(Intent intent) {
1535 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1536 }
1537
Winson Chung3d503fb2011-07-13 17:25:49 -07001538 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1539 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001540 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 folderInfo.title = getText(R.string.folder_name);
1542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001544 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1545 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001546 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547
1548 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001549 FolderIcon newFolder =
1550 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1551 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1552 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001553 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001557 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001558 }
1559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 private void showNotifications() {
1561 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1562 if (statusBar != null) {
1563 statusBar.expand();
1564 }
1565 }
1566
1567 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001568 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001570 Intent chooser = Intent.createChooser(pickWallpaper,
1571 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001572 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1573 // Removed in Eclair MR1
1574// WallpaperManager wm = (WallpaperManager)
1575// getSystemService(Context.WALLPAPER_SERVICE);
1576// WallpaperInfo wi = wm.getWallpaperInfo();
1577// if (wi != null && wi.getSettingsActivity() != null) {
1578// LabeledIntent li = new LabeledIntent(getPackageName(),
1579// R.string.configure_wallpaper, 0);
1580// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1581// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1582// }
Mike Clerona0618e42009-10-22 13:55:21 -07001583 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001586 /**
1587 * Registers various content observers. The current implementation registers
1588 * only a favorites observer to keep track of the favorites applications.
1589 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001590 private void registerContentObservers() {
1591 ContentResolver resolver = getContentResolver();
1592 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1593 true, mWidgetObserver);
1594 }
1595
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 @Override
1597 public boolean dispatchKeyEvent(KeyEvent event) {
1598 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1599 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001601 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001602 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001603 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001604 dumpState();
1605 return true;
1606 }
1607 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001608 }
1609 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1610 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001611 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 return true;
1613 }
1614 }
1615
1616 return super.dispatchKeyEvent(event);
1617 }
1618
Joe Onorato88ec0992009-11-19 13:16:06 -08001619 @Override
1620 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001621 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001622 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001623 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001624 Folder openFolder = mWorkspace.getOpenFolder();
1625 if (openFolder.isEditingName()) {
1626 openFolder.dismissEditingName();
1627 } else {
1628 closeFolder();
1629 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001630 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001631 mWorkspace.exitWidgetResizeMode();
1632
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001633 // Back button is a no-op here, but give at least some feedback for the button press
1634 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001635 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001636 }
1637
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001639 * Re-listen when widgets are reset.
1640 */
1641 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001642 if (mAppWidgetHost != null) {
1643 mAppWidgetHost.startListening();
1644 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001645 }
1646
1647 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001648 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1649 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001651 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001652 if (mModel != null) {
1653 mModel.unbindWorkspaceItems();
1654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 /**
1658 * Launches the intent referred by the clicked shortcut.
1659 *
1660 * @param v The view representing the clicked shortcut.
1661 */
1662 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001663 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1664 // view has detached (it's possible for this to happen if the view is removed mid touch).
1665 if (v.getWindowToken() == null) {
1666 return;
1667 }
1668
1669 if (mWorkspace.isSwitchingState()) {
1670 return;
1671 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001674 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001676 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001677 int[] pos = new int[2];
1678 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001679 intent.setSourceBounds(new Rect(pos[0], pos[1],
1680 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001681 boolean success = startActivitySafely(intent, tag);
1682
1683 if (success && v instanceof BubbleTextView) {
1684 mWaitingForResume = (BubbleTextView) v;
1685 mWaitingForResume.setStayPressed(true);
1686 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001688 if (v instanceof FolderIcon) {
1689 FolderIcon fi = (FolderIcon) v;
1690 handleFolderClick(fi);
1691 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001692 } else if (v == mAllAppsButton) {
1693 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001694 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001695 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001696 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 }
1699 }
1700
Michael Jurka0e260592010-06-30 17:07:39 -07001701 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001702 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001703 // clicking anywhere on the workspace causes the customization drawer to slide down
1704 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001705 return false;
1706 }
1707
Michael Jurkaaf442092010-06-10 17:01:57 -07001708 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001709 * Event handler for the search button
1710 *
1711 * @param v The view that was clicked.
1712 */
1713 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001714 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001715 }
1716
1717 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001718 * Event handler for the voice button
1719 *
1720 * @param v The view that was clicked.
1721 */
1722 public void onClickVoiceButton(View v) {
1723 startVoiceSearch();
1724 }
1725
1726 private void startVoiceSearch() {
1727 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001728 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001729 startActivity(intent);
1730 }
1731
1732 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001733 * Event handler for the "grid" button that appears on the home screen, which
1734 * enters all apps mode.
1735 *
1736 * @param v The view that was clicked.
1737 */
1738 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001739 showAllApps(true);
1740 }
1741
1742 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001743 // Provide the same haptic feedback that the system offers for virtual keys.
1744 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001745 }
1746
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001747 public void onClickAppMarketButton(View v) {
1748 if (mAppMarketIntent != null) {
1749 startActivitySafely(mAppMarketIntent, "app market");
1750 }
1751 }
1752
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001753 void startApplicationDetailsActivity(ComponentName componentName) {
1754 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001755 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1756 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001757 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001758 startActivity(intent);
1759 }
1760
Patrick Dubroy5539af72010-09-07 15:22:01 -07001761 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1762 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1763 // System applications cannot be installed. For now, show a toast explaining that.
1764 // We may give them the option of disabling apps this way.
1765 int messageId = R.string.uninstall_system_app_text;
1766 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1767 } else {
1768 String packageName = appInfo.componentName.getPackageName();
1769 String className = appInfo.componentName.getClassName();
1770 Intent intent = new Intent(
1771 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001772 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1773 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001774 startActivity(intent);
1775 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001776 }
1777
Michael Jurkaddd62e92011-02-16 17:49:14 -08001778 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1780 try {
1781 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001782 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 } catch (ActivityNotFoundException e) {
1784 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001785 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 } catch (SecurityException e) {
1787 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001788 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001790 "or use the exported attribute for this activity. "
1791 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001793 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001795
Romain Guy8e633c52010-03-04 12:51:36 -08001796 void startActivityForResultSafely(Intent intent, int requestCode) {
1797 try {
1798 startActivityForResult(intent, requestCode);
1799 } catch (ActivityNotFoundException e) {
1800 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1801 } catch (SecurityException e) {
1802 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1803 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1804 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1805 "or use the exported attribute for this activity.", e);
1806 }
1807 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808
Adam Cohena9cf38f2011-05-02 15:36:58 -07001809 private void handleFolderClick(FolderIcon folderIcon) {
1810 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001811 Folder openFolder = mWorkspace.getFolderForTag(info);
1812
1813 // If the folder info reports that the associated folder is open, then verify that
1814 // it is actually opened. There have been a few instances where this gets out of sync.
1815 if (info.opened && openFolder == null) {
1816 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1817 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1818 info.opened = false;
1819 }
1820
Adam Cohena9cf38f2011-05-02 15:36:58 -07001821 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 // Close any open folder
1823 closeFolder();
1824 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001825 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 } else {
1827 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 int folderScreen;
1829 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001830 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 // .. and close it
1832 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001833 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 // Close any folder open on the current screen
1835 closeFolder();
1836 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001837 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 }
1839 }
1840 }
1841 }
1842
Adam Cohen2801caf2011-05-13 20:57:39 -07001843 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001844 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001845 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1846 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1847 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1848
Adam Cohenc51934b2011-07-26 21:07:43 -07001849 FolderInfo info = (FolderInfo) fi.getTag();
1850 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1851 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001852 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1853 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001854 }
1855
Adam Cohen2801caf2011-05-13 20:57:39 -07001856 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1857 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1858 oa.start();
1859 }
1860
1861 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001862 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001863 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1864 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1865 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1866
Adam Cohenc51934b2011-07-26 21:07:43 -07001867 FolderInfo info = (FolderInfo) fi.getTag();
1868 CellLayout cl = null;
1869 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1870 cl = (CellLayout) fi.getParent().getParent();
1871 }
1872
1873 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001874 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1875 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001876 oa.addListener(new AnimatorListenerAdapter() {
1877 @Override
1878 public void onAnimationEnd(Animator animation) {
1879 if (layout != null) {
1880 layout.clearFolderLeaveBehind();
1881 }
1882 }
1883 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001884 oa.start();
1885 }
1886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001888 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 * is animated relative to the specified View. If the View is null, no animation
1890 * is played.
1891 *
1892 * @param folderInfo The FolderInfo describing the folder to open.
1893 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001894 public void openFolder(FolderIcon folderIcon) {
1895 Folder folder = folderIcon.mFolder;
1896 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897
Adam Cohen2801caf2011-05-13 20:57:39 -07001898 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001899 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900
Adam Cohen4554ee12011-08-03 16:13:21 -07001901 // Just verify that the folder hasn't already been added to the DragLayer.
1902 // There was a one-off crash where the folder had a parent already.
1903 if (folder.getParent() == null) {
1904 mDragLayer.addView(folder);
1905 mDragController.addDropTarget((DropTarget) folder);
1906 } else {
1907 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1908 folder.getParent() + ").");
1909 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001910 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001911 }
1912
1913 public void closeFolder() {
1914 Folder folder = mWorkspace.getOpenFolder();
1915 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001916 if (folder.isEditingName()) {
1917 folder.dismissEditingName();
1918 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001919 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001920
1921 // Dismiss the folder cling
1922 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001923 }
1924 }
1925
1926 void closeFolder(Folder folder) {
1927 folder.getInfo().opened = false;
1928
1929 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1930 if (parent != null) {
1931 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1932 shrinkAndFadeInFolderIcon(fi);
1933 }
1934 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 }
1936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001938 if (mState != State.WORKSPACE) {
1939 return false;
1940 }
1941
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 return false;
1944 }
1945
1946 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001947 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
Michael Jurka0280c3b2010-09-17 15:00:07 -07001950 resetAddInfo();
1951 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001953 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 return true;
1955 }
1956
Winson Chung3d503fb2011-07-13 17:25:49 -07001957 // The hotseat touch handling does not go through Workspace, and we always allow long press
1958 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001959 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001960 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1961 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001962 if (itemUnderLongClick == null) {
1963 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001964 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1965 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001966 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001968 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001970 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
1972 }
1973 }
1974 return true;
1975 }
1976
Winson Chung3d503fb2011-07-13 17:25:49 -07001977 boolean isHotseatLayout(View layout) {
1978 return mHotseat != null && layout != null &&
1979 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1980 }
1981 Hotseat getHotseat() {
1982 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001983 }
Adam Cohenebea84d2011-11-09 17:20:41 -08001984 SearchDropTargetBar getSearchBar() {
1985 return mSearchDropTargetBar;
1986 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001987
Winson Chung3d503fb2011-07-13 17:25:49 -07001988 /**
1989 * Returns the CellLayout of the specified container at the specified screen.
1990 */
1991 CellLayout getCellLayout(long container, int screen) {
1992 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1993 if (mHotseat != null) {
1994 return mHotseat.getLayout();
1995 } else {
1996 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001997 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001998 } else {
1999 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002000 }
2001 }
2002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 Workspace getWorkspace() {
2004 return mWorkspace;
2005 }
2006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 @Override
2008 protected Dialog onCreateDialog(int id) {
2009 switch (id) {
2010 case DIALOG_CREATE_SHORTCUT:
2011 return new CreateShortcut().createDialog();
2012 case DIALOG_RENAME_FOLDER:
2013 return new RenameFolder().createDialog();
2014 }
2015
2016 return super.onCreateDialog(id);
2017 }
2018
2019 @Override
2020 protected void onPrepareDialog(int id, Dialog dialog) {
2021 switch (id) {
2022 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 break;
2024 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002025 if (mFolderInfo != null) {
2026 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2027 final CharSequence text = mFolderInfo.title;
2028 input.setText(text);
2029 input.setSelection(0, text.length());
2030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 break;
2032 }
2033 }
2034
2035 void showRenameDialog(FolderInfo info) {
2036 mFolderInfo = info;
2037 mWaitingForResult = true;
2038 showDialog(DIALOG_RENAME_FOLDER);
2039 }
2040
Adam Cohenfbba09b2011-07-18 21:43:05 -07002041 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002042 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002043 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2044 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 mWaitingForResult = true;
2046 showDialog(DIALOG_CREATE_SHORTCUT);
2047 }
2048
2049 private class RenameFolder {
2050 private EditText mInput;
2051
2052 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2054 mInput = (EditText) layout.findViewById(R.id.folder_name);
2055
2056 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2057 builder.setIcon(0);
2058 builder.setTitle(getString(R.string.rename_folder_title));
2059 builder.setCancelable(true);
2060 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2061 public void onCancel(DialogInterface dialog) {
2062 cleanup();
2063 }
2064 });
2065 builder.setNegativeButton(getString(R.string.cancel_action),
2066 new Dialog.OnClickListener() {
2067 public void onClick(DialogInterface dialog, int which) {
2068 cleanup();
2069 }
2070 }
2071 );
2072 builder.setPositiveButton(getString(R.string.rename_action),
2073 new Dialog.OnClickListener() {
2074 public void onClick(DialogInterface dialog, int which) {
2075 changeFolderName();
2076 }
2077 }
2078 );
2079 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002080
2081 final AlertDialog dialog = builder.create();
2082 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2083 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002084 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002085 mInput.requestFocus();
2086 InputMethodManager inputManager = (InputMethodManager)
2087 getSystemService(Context.INPUT_METHOD_SERVICE);
2088 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002089 }
2090 });
2091
2092 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 }
2094
2095 private void changeFolderName() {
2096 final String name = mInput.getText().toString();
2097 if (!TextUtils.isEmpty(name)) {
2098 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002099 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 mFolderInfo.title = name;
2101 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2102
Joe Onorato9c1289c2009-08-17 11:03:03 -04002103 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002104 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002105 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 } else {
2107 final FolderIcon folderIcon = (FolderIcon)
2108 mWorkspace.getViewForTag(mFolderInfo);
2109 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002110 // TODO: At some point we'll probably want some version of setting
2111 // the text for a folder icon.
2112 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 getWorkspace().requestLayout();
2114 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002115 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002116 mWorkspaceLoading = true;
2117 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 }
2119 }
2120 }
2121 cleanup();
2122 }
2123
2124 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 dismissDialog(DIALOG_RENAME_FOLDER);
2126 mWaitingForResult = false;
2127 mFolderInfo = null;
2128 }
2129 }
2130
Daniel Sandler843e8602010-06-07 14:59:01 -04002131 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2132 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002133 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002134 }
2135
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002136 // AllAppsView.Watcher
2137 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002138 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002139 mWorkspace.setVisibility(View.GONE);
2140 }
2141 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002142
2143 /**
2144 * Helper method for the cameraZoomIn/cameraZoomOut animations
2145 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002146 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002147 * @param scaleFactor The scale factor used for the zoom
2148 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002149 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002150 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002151 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002152 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002153
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002154 void updateWallpaperVisibility(boolean visible) {
2155 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2156 int curflags = getWindow().getAttributes().flags
2157 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2158 if (wpflags != curflags) {
2159 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2160 }
2161 }
2162
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002163 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002164 * Things to test when changing the following seven functions.
2165 * - Home from workspace
2166 * - from center screen
2167 * - from other screens
2168 * - Home from all apps
2169 * - from center screen
2170 * - from other screens
2171 * - Back from all apps
2172 * - from center screen
2173 * - from other screens
2174 * - Launch app from workspace and quit
2175 * - with back
2176 * - with home
2177 * - Launch app from all apps and quit
2178 * - with back
2179 * - with home
2180 * - Go to a screen that's not the default, then all
2181 * apps, and launch and app, and go back
2182 * - with back
2183 * -with home
2184 * - On workspace, long press power and go back
2185 * - with back
2186 * - with home
2187 * - On all apps, long press power and go back
2188 * - with back
2189 * - with home
2190 * - On workspace, power off
2191 * - On all apps, power off
2192 * - Launch an app and turn off the screen while in that app
2193 * - Go back with home key
2194 * - Go back with back key TODO: make this not go to workspace
2195 * - From all apps
2196 * - From workspace
2197 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2198 * - From all apps
2199 * - From the center workspace
2200 * - From another workspace
2201 */
2202
2203 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002204 * Zoom the camera out from the workspace to reveal 'toView'.
2205 * Assumes that the view to show is anchored at either the very top or very bottom
2206 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002207 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002208 private void showAppsCustomizeHelper(boolean animated, final boolean springLoaded) {
2209 if (mStateAnimation != null) {
2210 mStateAnimation.cancel();
2211 mStateAnimation = null;
2212 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002213 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002214 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002215
Winson Chungf0ea4d32011-06-06 14:27:16 -07002216 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2217 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2218 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2219 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002220 final int startDelay =
2221 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002222
Michael Jurkab3e22d92011-10-31 15:58:33 -07002223 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002224
Michael Jurkad74c9842011-07-10 12:44:21 -07002225 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002226 mWorkspace.changeState(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002227
2228 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002229 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002230 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002231 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2232 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002233 ((View) toView.getParent()).invalidate();
2234 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002235 toView.setFastScaleX(a * scale + b * 1f);
2236 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002237 }
2238 });
Adam Cohen61033d32010-11-15 18:29:44 -08002239
Winson Chungf0ea4d32011-06-06 14:27:16 -07002240 toView.setVisibility(View.VISIBLE);
2241 toView.setFastAlpha(0f);
2242 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2243 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2244 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2245 public void onAnimationUpdate(float a, float b) {
2246 // don't need to invalidate because we do so above
2247 toView.setFastAlpha(a * 0f + b * 1f);
2248 }
2249 });
Adam Cohencff6af82011-09-13 14:51:53 -07002250 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002251 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002252
Michael Jurka8edd75c2010-12-17 20:15:06 -08002253 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002254 boolean animationCancelled = false;
2255
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002256 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002257 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002258 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002259 // Prepare the position
2260 toView.setTranslationX(0.0f);
2261 toView.setTranslationY(0.0f);
2262 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002263 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002264 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002265 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002266 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002267 // If we don't set the final scale values here, if this animation is cancelled
2268 // it will have the wrong scale value and subsequent cameraPan animations will
2269 // not fix that
2270 toView.setScaleX(1.0f);
2271 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002272 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002273 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance,
2274 scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002275 }
Winson Chung32174c82011-07-19 15:47:55 -07002276
2277 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2278 // Hide the workspace scrollbar
2279 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002280 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002281 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002282 if (!animationCancelled) {
2283 updateWallpaperVisibility(false);
2284 }
2285 }
2286
Adam Cohencff6af82011-09-13 14:51:53 -07002287 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002288 public void onAnimationCancel(Animator animation) {
2289 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002290 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002291 });
2292
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002293 // toView should appear right at the end of the workspace shrink animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002294 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002295 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka1899a362011-11-03 13:50:45 -07002296
2297 boolean delayAnim = false;
2298 if (toView instanceof LauncherTransitionable) {
2299 LauncherTransitionable lt = (LauncherTransitionable) toView;
2300 delayAnim = lt.onLauncherTransitionStart(instance, mStateAnimation, false);
2301 }
2302 // if the anim is delayed, the LauncherTransitionable is responsible for starting it
2303 if (!delayAnim) {
2304 // TODO: q-- what if this anim is cancelled before being started? or started after
2305 // being cancelled?
2306 mStateAnimation.start();
2307 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002308 } else {
2309 toView.setTranslationX(0.0f);
2310 toView.setTranslationY(0.0f);
2311 toView.setScaleX(1.0f);
2312 toView.setScaleY(1.0f);
2313 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002314 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002315 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002316 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, null, false);
2317 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance, null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002318
Winson Chungc07918d2011-07-01 15:35:26 -07002319 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2320 // Hide the workspace scrollbar
2321 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002322 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002323 }
Michael Jurkaabded662011-03-04 12:06:57 -08002324 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002325 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002326 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002327 }
2328
2329 /**
2330 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002331 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002332 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002333 */
Michael Jurkab737ee62011-11-15 15:57:22 -08002334 private void hideAppsCustomizeHelper(boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002335 if (mStateAnimation != null) {
2336 mStateAnimation.cancel();
2337 mStateAnimation = null;
2338 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002339 Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002340 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002341
Winson Chungf0ea4d32011-06-06 14:27:16 -07002342 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2343 final float scaleFactor = (float)
2344 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2345 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002346
Michael Jurkab3e22d92011-10-31 15:58:33 -07002347 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002348 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002349 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002350 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002351 final float oldScaleX = fromView.getScaleX();
2352 final float oldScaleY = fromView.getScaleY();
2353
2354 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2355 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002356 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2357 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002358 ((View)fromView.getParent()).fastInvalidate();
2359 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2360 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2361 }
2362 });
Michael Jurkaabded662011-03-04 12:06:57 -08002363 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002364 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002365 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002366 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2367 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002368 // don't need to invalidate because we do so above
2369 fromView.setFastAlpha(a * 1f + b * 0f);
2370 }
2371 });
Michael Jurkaabded662011-03-04 12:06:57 -08002372 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002373 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, alphaAnim,
2374 true);
Michael Jurka2763be32011-02-24 11:19:57 -08002375 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002376 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002377 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002378 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002379 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002380 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002381 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002382 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance,
2383 alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002384 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002385 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002386 }
2387 });
2388
Michael Jurkab3e22d92011-10-31 15:58:33 -07002389 mStateAnimation = new AnimatorSet();
Winson Chungf0ea4d32011-06-06 14:27:16 -07002390 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002391 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002392 } else {
2393 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002394 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002395 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, null, true);
2396 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance, null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002397 }
Michael Jurkab737ee62011-11-15 15:57:22 -08002398 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002399 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002400 }
2401
Michael Jurkae326f182011-11-21 14:05:46 -08002402 @Override
2403 public void onTrimMemory(int level) {
2404 super.onTrimMemory(level);
2405 if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2406 mAppsCustomizeTabHost.onTrimMemory();
2407 }
2408 }
2409
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002410 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002411 Resources res = getResources();
2412 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2413
2414 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002415 if (mState != State.WORKSPACE) {
2416 mWorkspace.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08002417 hideAppsCustomizeHelper(animated, false);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002418
2419 // Show the search bar and hotseat
2420 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002421 // We only need to animate in the dock divider if we're going from spring loaded mode
2422 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002423
2424 // Set focus to the AppsCustomize button
2425 if (mAllAppsButton != null) {
2426 mAllAppsButton.requestFocus();
2427 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002428 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002429
Michael Jurkab737ee62011-11-15 15:57:22 -08002430 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002431
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002432 // Change the state *after* we've called all the transition code
2433 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002434
Winson Chung5fb63472011-02-02 17:03:37 -08002435 // Resume the auto-advance of widgets
2436 mUserPresent = true;
2437 updateRunning();
2438
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002439 // send an accessibility event to announce the context change
2440 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002441 }
2442
Michael Jurkab3e22d92011-10-31 15:58:33 -07002443 void showAllApps(boolean animated) {
2444 if (mState != State.WORKSPACE) return;
2445
2446 showAppsCustomizeHelper(animated, false);
2447 mAppsCustomizeTabHost.requestFocus();
2448
2449 // Hide the search bar and hotseat
2450 mSearchDropTargetBar.hideSearchBar(animated);
2451
2452 // Change the state *after* we've called all the transition code
2453 mState = State.APPS_CUSTOMIZE;
2454
2455 // Pause the auto-advance of widgets until we are out of AllApps
2456 mUserPresent = false;
2457 updateRunning();
2458 closeFolder();
2459
2460 // Send an accessibility event to announce the context change
2461 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2462 }
2463
Adam Cohen7777d962011-08-18 18:58:38 -07002464 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002465 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002466 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Michael Jurkab737ee62011-11-15 15:57:22 -08002467 hideAppsCustomizeHelper(true, true);
2468 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002469 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002470 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002471 }
Adam Cohen7777d962011-08-18 18:58:38 -07002472
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002473 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002474 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2475
Winson Chunge7a03942011-08-05 15:05:12 -07002476 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002477 @Override
2478 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002479 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002480 // Before we show workspace, hide all apps again because
2481 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2482 // clean up our state transition functions
2483 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002484 mSearchDropTargetBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002485 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002486 } else {
2487 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002488 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002489 }
2490 }, (extendedDelay ?
2491 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2492 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2493 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002494
Michael Jurkad3ef3062010-11-23 16:23:58 -08002495 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002496 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002497 final boolean animated = true;
2498 final boolean springLoaded = true;
2499 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002500 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002501 }
2502 // Otherwise, we are not in spring loaded mode, so don't do anything.
2503 }
2504
Michael Jurkab737ee62011-11-15 15:57:22 -08002505 void hideDockDivider() {
2506 if (mQsbDivider != null && mDockDivider != null) {
2507 mQsbDivider.setVisibility(View.INVISIBLE);
2508 mDockDivider.setVisibility(View.INVISIBLE);
2509 }
2510 }
2511
2512 void showDockDivider(boolean animated) {
2513 if (mQsbDivider != null && mDockDivider != null) {
2514 mQsbDivider.setVisibility(View.VISIBLE);
2515 mDockDivider.setVisibility(View.VISIBLE);
2516 if (mDividerAnimator != null) {
2517 mDividerAnimator.cancel();
2518 mQsbDivider.setAlpha(1f);
2519 mDockDivider.setAlpha(1f);
2520 mDividerAnimator = null;
2521 }
2522 if (animated) {
2523 mDividerAnimator = new AnimatorSet();
2524 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2525 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2526 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2527 mDividerAnimator.start();
2528 }
2529 }
2530 }
2531
Michael Jurkab3e22d92011-10-31 15:58:33 -07002532 void lockAllApps() {
2533 // TODO
2534 }
2535
2536 void unlockAllApps() {
2537 // TODO
2538 }
2539
Adam Cohenfc53cd22011-07-20 15:45:11 -07002540 public boolean isAllAppsCustomizeOpen() {
2541 return mState == State.APPS_CUSTOMIZE;
2542 }
2543
Winson Chungf0ea4d32011-06-06 14:27:16 -07002544 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002545 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002546 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002547 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002548 if (!LauncherApplication.isScreenLarge()) {
2549 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002550 int duration = mSearchDropTargetBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002551 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002552 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002553 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002554 }
2555 }
2556 }
2557
2558 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002559 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002560 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002561 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002562 if (!LauncherApplication.isScreenLarge()) {
2563 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002564 int duration = mSearchDropTargetBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002565 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002566 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002567 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002568 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002569 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002570 }
2571
Patrick Dubroy5f445422011-02-18 14:35:21 -08002572 /**
2573 * Add an item from all apps or customize onto the given workspace screen.
2574 * If layout is null, add to the current screen.
2575 */
2576 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002577 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2578 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002579 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002580 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002581
Winson Chungdff8ebb2011-09-08 17:25:31 -07002582 /** Maps the current orientation to an index for referencing orientation correct global icons */
2583 private int getCurrentOrientationIndexForGlobalIcons() {
2584 // default - 0, landscape - 1
2585 switch (getResources().getConfiguration().orientation) {
2586 case Configuration.ORIENTATION_LANDSCAPE:
2587 return 1;
2588 default:
2589 return 0;
2590 }
2591 }
2592
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002593 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002594 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002595 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002596 // Look for the toolbar icon specified in the activity meta-data
2597 Bundle metaData = packageManager.getActivityInfo(
2598 activityName, PackageManager.GET_META_DATA).metaData;
2599 if (metaData != null) {
2600 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2601 if (iconResId != 0) {
2602 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002603 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002604 }
2605 }
2606 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002607 // This can happen if the activity defines an invalid drawable
2608 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2609 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002610 } catch (Resources.NotFoundException nfe) {
2611 // This can happen if the activity defines an invalid drawable
2612 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2613 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002614 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002615 return null;
2616 }
2617
2618 // if successful in getting icon, return it; otherwise, set button to use default drawable
2619 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2620 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002621 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002622 Resources r = getResources();
2623 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2624 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002625
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002626 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002627 // If we were unable to find the icon via the meta-data, use a generic one
2628 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002629 toolbarIcon = r.getDrawable(fallbackDrawableId);
2630 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002631 if (button != null) {
2632 button.setCompoundDrawables(toolbarIcon, null, null, null);
2633 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002634 return null;
2635 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002636 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002637 if (button != null) {
2638 button.setCompoundDrawables(toolbarIcon, null, null, null);
2639 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002640 return toolbarIcon.getConstantState();
2641 }
2642 }
2643
2644 // if successful in getting icon, return it; otherwise, set button to use default drawable
2645 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2646 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2647 ImageView button = (ImageView) findViewById(buttonId);
2648 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2649
Michael Jurka19e0fc52011-07-22 18:00:21 -07002650 if (button != null) {
2651 // If we were unable to find the icon via the meta-data, use a
2652 // generic one
2653 if (toolbarIcon == null) {
2654 button.setImageResource(fallbackDrawableId);
2655 } else {
2656 button.setImageDrawable(toolbarIcon);
2657 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002658 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002659
2660 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2661
Michael Jurka0423dcf2010-10-05 14:56:18 -07002662 }
2663
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002664 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2665 TextView button = (TextView) findViewById(buttonId);
2666 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2667 }
2668
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002669 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002670 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002671 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002672 }
2673
Winson Chungc51db6a2011-10-05 11:44:49 -07002674 private boolean updateGlobalSearchIcon() {
2675 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002676 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2677 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002678 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002679 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002680
Winson Chung1cad91e2011-05-25 17:41:01 -07002681 final SearchManager searchManager =
2682 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2683 ComponentName activityName = searchManager.getGlobalSearchActivity();
2684 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002685 int coi = getCurrentOrientationIndexForGlobalIcons();
2686 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002687 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002688 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002689 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2690 searchButton.setVisibility(View.VISIBLE);
2691 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002692 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002693 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002694 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002695 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2696 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2697 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002698 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002699 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002700 }
2701 }
2702
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002703 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002704 updateButtonWithDrawable(R.id.search_button, d);
2705 }
2706
Winson Chungc51db6a2011-10-05 11:44:49 -07002707 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002708 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002709 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002710 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002711
Winson Chungc51db6a2011-10-05 11:44:49 -07002712 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002713 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2714 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002715 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002716 int coi = getCurrentOrientationIndexForGlobalIcons();
2717 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002718 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002719 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002720 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002721 voiceButton.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002722 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002723 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002724 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002725 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002726 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002727 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002728 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002729 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002730
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002731 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002732 updateButtonWithDrawable(R.id.voice_button, d);
2733 }
2734
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002735 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002736 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002737 */
2738 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002739 final View marketButton = findViewById(R.id.market_button);
2740 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2741 // Find the app market activity by resolving an intent.
2742 // (If multiple app markets are installed, it will return the ResolverActivity.)
2743 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002744 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002745 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002746 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002747 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002748 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002749 marketButton.setVisibility(View.VISIBLE);
2750 } else {
2751 // We should hide and disable the view so that we don't try and restore the visibility
2752 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2753 marketButton.setVisibility(View.GONE);
2754 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002755 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002756 }
2757
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002758 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002759 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002760 }
2761
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002762 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 * Displays the shortcut creation dialog and launches, if necessary, the
2764 * appropriate activity.
2765 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002766 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002767 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2768 DialogInterface.OnShowListener {
2769
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002770 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002771
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002772 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002773 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002774
Winson Chung55b65502011-05-26 12:03:43 -07002775 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2776 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002777 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779 AlertDialog dialog = builder.create();
2780 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002781 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002782 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002783
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784 return dialog;
2785 }
2786
2787 public void onCancel(DialogInterface dialog) {
2788 mWaitingForResult = false;
2789 cleanup();
2790 }
2791
Romain Guycbb89e42009-06-08 15:52:54 -07002792 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002793 mWaitingForResult = false;
2794 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002795 }
2796
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002797 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002798 try {
2799 dismissDialog(DIALOG_CREATE_SHORTCUT);
2800 } catch (Exception e) {
2801 // An exception is thrown if the dialog is not visible, which is fine
2802 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002803 }
2804
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002805 /**
2806 * Handle the action clicked in the "Add to home" dialog.
2807 */
2808 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002809 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002810
Winson Chung55b65502011-05-26 12:03:43 -07002811 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2812 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002813 case AddAdapter.ITEM_APPLICATION: {
2814 if (mAppsCustomizeTabHost != null) {
2815 mAppsCustomizeTabHost.selectAppsTab();
2816 }
2817 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002818 break;
2819 }
Winson Chung55b65502011-05-26 12:03:43 -07002820 case AddAdapter.ITEM_APPWIDGET: {
2821 if (mAppsCustomizeTabHost != null) {
2822 mAppsCustomizeTabHost.selectWidgetsTab();
2823 }
2824 showAllApps(true);
2825 break;
2826 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002827 case AddAdapter.ITEM_WALLPAPER: {
2828 startWallpaper();
2829 break;
2830 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002831 }
2832 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002833
2834 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002835 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002836 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837 }
2838
2839 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002840 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002841 */
2842 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2843 @Override
2844 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002845 closeSystemDialogs();
2846 }
2847 }
2848
2849 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002850 * Receives notifications whenever the appwidgets are reset.
2851 */
2852 private class AppWidgetResetObserver extends ContentObserver {
2853 public AppWidgetResetObserver() {
2854 super(new Handler());
2855 }
2856
2857 @Override
2858 public void onChange(boolean selfChange) {
2859 onAppWidgetReset();
2860 }
2861 }
2862
2863 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002864 * If the activity is currently paused, signal that we need to re-run the loader
2865 * in onResume.
2866 *
2867 * This needs to be called from incoming places where resources might have been loaded
2868 * while we are paused. That is becaues the Configuration might be wrong
2869 * when we're not running, and if it comes back to what it was when we
2870 * were paused, we are not restarted.
2871 *
2872 * Implementation of the method from LauncherModel.Callbacks.
2873 *
2874 * @return true if we are currently paused. The caller might be able to
2875 * skip some work in that case since we will come back again.
2876 */
2877 public boolean setLoadOnResume() {
2878 if (mPaused) {
2879 Log.i(TAG, "setLoadOnResume");
2880 mOnResumeNeedsLoad = true;
2881 return true;
2882 } else {
2883 return false;
2884 }
2885 }
2886
2887 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002891 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002892 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002893 } else {
2894 return SCREEN_COUNT / 2;
2895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002897
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002898
Joe Onorato9c1289c2009-08-17 11:03:03 -04002899 /**
2900 * Refreshes the shortcuts shown on the workspace.
2901 *
2902 * Implementation of the method from LauncherModel.Callbacks.
2903 */
2904 public void startBinding() {
2905 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002906
2907 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908 int count = workspace.getChildCount();
2909 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002910 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002911 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2912 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002913 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002914 if (mHotseat != null) {
2915 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 }
2917 }
2918
2919 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002920 * Bind the items start-end from the list.
2921 *
2922 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002924 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002925 setLoadOnResume();
2926
Joe Onorato9c1289c2009-08-17 11:03:03 -04002927 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 for (int i=start; i<end; i++) {
2929 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002930
2931 // Short circuit if we are loading dock items for a configuration which has no dock
2932 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2933 mHotseat == null) {
2934 continue;
2935 }
2936
Joe Onorato9c1289c2009-08-17 11:03:03 -04002937 switch (item.itemType) {
2938 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2939 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002940 View shortcut = createShortcut((ShortcutInfo)item);
2941 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2942 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002943 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002944 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002945 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002946 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002947 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002948 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2949 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002953 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 }
2955
Joe Onorato9c1289c2009-08-17 11:03:03 -04002956 /**
2957 * Implementation of the method from LauncherModel.Callbacks.
2958 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002959 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002960 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002961 sFolders.clear();
2962 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002964
2965 /**
2966 * Add the views for a widget to the workspace.
2967 *
2968 * Implementation of the method from LauncherModel.Callbacks.
2969 */
2970 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002971 setLoadOnResume();
2972
Daniel Sandler843e8602010-06-07 14:59:01 -04002973 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2974 if (DEBUG_WIDGETS) {
2975 Log.d(TAG, "bindAppWidget: " + item);
2976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002977 final Workspace workspace = mWorkspace;
2978
2979 final int appWidgetId = item.appWidgetId;
2980 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002981 if (DEBUG_WIDGETS) {
2982 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2983 }
2984
Joe Onorato9c1289c2009-08-17 11:03:03 -04002985 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2986
Joe Onorato9c1289c2009-08-17 11:03:03 -04002987 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2988 item.hostView.setTag(item);
2989
Winson Chung3d503fb2011-07-13 17:25:49 -07002990 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002991 item.cellY, item.spanX, item.spanY, false);
2992
Adam Cohended9f8d2010-11-03 13:25:16 -07002993 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2994
Joe Onorato9c1289c2009-08-17 11:03:03 -04002995 workspace.requestLayout();
2996
Daniel Sandler843e8602010-06-07 14:59:01 -04002997 if (DEBUG_WIDGETS) {
2998 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2999 + (SystemClock.uptimeMillis()-start) + "ms");
3000 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003001 }
3002
3003 /**
3004 * Callback saying that there aren't any more items to bind.
3005 *
3006 * Implementation of the method from LauncherModel.Callbacks.
3007 */
3008 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003009 setLoadOnResume();
3010
Joe Onorato9c1289c2009-08-17 11:03:03 -04003011 if (mSavedState != null) {
3012 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003013 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003014 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 mSavedState = null;
3016 }
3017
3018 if (mSavedInstanceState != null) {
3019 super.onRestoreInstanceState(mSavedInstanceState);
3020 mSavedInstanceState = null;
3021 }
3022
Joe Onorato9c1289c2009-08-17 11:03:03 -04003023 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003024
3025 // If we received the result of any pending adds while the loader was running (e.g. the
3026 // widget configuration forced an orientation change), process them now.
3027 for (int i = 0; i < sPendingAddList.size(); i++) {
3028 completeAdd(sPendingAddList.get(i));
3029 }
3030 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003031
Winson Chungc51db6a2011-10-05 11:44:49 -07003032 // Update the market app icon as necessary (the other icons will be managed in response to
3033 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003034 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003035
3036 mWorkspace.post(mBuildLayersRunnable);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003037 }
3038
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003039 @Override
3040 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003041 boolean searchVisible = updateGlobalSearchIcon();
3042 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3043 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003044 }
3045
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003046 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003047 * Add the icons for all apps.
3048 *
3049 * Implementation of the method from LauncherModel.Callbacks.
3050 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003051 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3052 // Remove the progress bar entirely; we could also make it GONE
3053 // but better to remove it since we know it's not going to be used
3054 View progressBar = mAppsCustomizeTabHost.
3055 findViewById(R.id.apps_customize_progress_bar);
3056 if (progressBar != null) {
3057 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003058 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003059 // We just post the call to setApps so the user sees the progress bar
3060 // disappear-- otherwise, it just looks like the progress bar froze
3061 // which doesn't look great
3062 mAppsCustomizeTabHost.post(new Runnable() {
3063 public void run() {
3064 if (mAppsCustomizeContent != null) {
3065 mAppsCustomizeContent.setApps(apps);
3066 }
3067 }
3068 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 }
3070
3071 /**
3072 * A package was installed.
3073 *
3074 * Implementation of the method from LauncherModel.Callbacks.
3075 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003076 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003077 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003078 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003079
Winson Chung785d2eb2011-04-14 16:08:02 -07003080 if (mAppsCustomizeContent != null) {
3081 mAppsCustomizeContent.addApps(apps);
3082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003083 }
3084
3085 /**
3086 * A package was updated.
3087 *
3088 * Implementation of the method from LauncherModel.Callbacks.
3089 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003090 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003091 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003092 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003093 if (mWorkspace != null) {
3094 mWorkspace.updateShortcuts(apps);
3095 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003096
Winson Chung785d2eb2011-04-14 16:08:02 -07003097 if (mAppsCustomizeContent != null) {
3098 mAppsCustomizeContent.updateApps(apps);
3099 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 }
3101
3102 /**
3103 * A package was uninstalled.
3104 *
3105 * Implementation of the method from LauncherModel.Callbacks.
3106 */
Joe Onorato36115782010-06-17 13:28:48 -04003107 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003108 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003109 if (permanent) {
3110 mWorkspace.removeItems(apps);
3111 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003112
Winson Chung785d2eb2011-04-14 16:08:02 -07003113 if (mAppsCustomizeContent != null) {
3114 mAppsCustomizeContent.removeApps(apps);
3115 }
Winson Chunga1820962011-10-03 16:31:06 -07003116
3117 // Notify the drag controller
3118 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003119 }
Joe Onoratobe386092009-11-17 17:32:16 -08003120
3121 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003122 * A number of packages were updated.
3123 */
3124 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003125 if (mAppsCustomizeContent != null) {
3126 mAppsCustomizeContent.onPackagesUpdated();
3127 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003128 }
3129
Winson Chung400438b2011-01-16 17:53:48 -08003130 private int mapConfigurationOriActivityInfoOri(int configOri) {
3131 final Display d = getWindowManager().getDefaultDisplay();
3132 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3133 switch (d.getRotation()) {
3134 case Surface.ROTATION_0:
3135 case Surface.ROTATION_180:
3136 // We are currently in the same basic orientation as the natural orientation
3137 naturalOri = configOri;
3138 break;
3139 case Surface.ROTATION_90:
3140 case Surface.ROTATION_270:
3141 // We are currently in the other basic orientation to the natural orientation
3142 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3143 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3144 break;
3145 }
3146
3147 int[] oriMap = {
3148 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3149 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3150 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3151 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3152 };
3153 // Since the map starts at portrait, we need to offset if this device's natural orientation
3154 // is landscape.
3155 int indexOffset = 0;
3156 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3157 indexOffset = 1;
3158 }
3159 return oriMap[(d.getRotation() + indexOffset) % 4];
3160 }
Adam Cohen446e9402011-09-15 18:21:21 -07003161
3162 public void lockScreenOrientationOnLargeUI() {
3163 if (LauncherApplication.isScreenLarge()) {
3164 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3165 .getConfiguration().orientation));
3166 }
Winson Chung400438b2011-01-16 17:53:48 -08003167 }
Adam Cohen446e9402011-09-15 18:21:21 -07003168 public void unlockScreenOrientationOnLargeUI() {
3169 if (LauncherApplication.isScreenLarge()) {
3170 mHandler.postDelayed(new Runnable() {
3171 public void run() {
3172 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3173 }
3174 }, mRestoreScreenOrientationDelay);
3175 }
Winson Chung400438b2011-01-16 17:53:48 -08003176 }
3177
Winson Chung82f55532011-08-09 14:14:23 -07003178 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003179 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3180 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003181 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003182 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003183
Winson Chung7d7541e2011-09-16 20:14:36 -07003184 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003185 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003186 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3187 Cling cling = (Cling) findViewById(clingId);
3188 if (cling != null) {
3189 cling.init(this, positionData);
3190 cling.setVisibility(View.VISIBLE);
3191 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3192 if (animate) {
3193 cling.buildLayer();
3194 cling.setAlpha(0f);
3195 cling.animate()
3196 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003197 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003198 .setDuration(SHOW_CLING_DURATION)
3199 .setStartDelay(delay)
3200 .start();
3201 } else {
3202 cling.setAlpha(1f);
3203 }
3204 }
3205 return cling;
3206 }
3207 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003208 if (cling != null) {
3209 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003210 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003211 anim.addListener(new AnimatorListenerAdapter() {
3212 public void onAnimationEnd(Animator animation) {
3213 cling.setVisibility(View.GONE);
3214 cling.cleanup();
3215 SharedPreferences prefs =
3216 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3217 SharedPreferences.Editor editor = prefs.edit();
3218 editor.putBoolean(flag, true);
3219 editor.commit();
3220 };
3221 });
3222 anim.start();
3223 }
3224 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003225 private void removeCling(int id) {
3226 final View cling = findViewById(id);
3227 if (cling != null) {
3228 final ViewGroup parent = (ViewGroup) cling.getParent();
3229 parent.post(new Runnable() {
3230 @Override
3231 public void run() {
3232 parent.removeView(cling);
3233 }
3234 });
3235 }
3236 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003237 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003238 // Enable the clings only if they have not been dismissed before
3239 SharedPreferences prefs =
3240 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003241 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003242 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003243 } else {
3244 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003245 }
3246 }
3247 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003248 // Enable the clings only if they have not been dismissed before
3249 SharedPreferences prefs =
3250 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003251 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003252 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003253 } else {
3254 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003255 }
3256 }
3257 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003258 // Enable the clings only if they have not been dismissed before
3259 SharedPreferences prefs =
3260 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3261 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003262 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003263 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003264 } else {
3265 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003266 }
3267 return cling;
3268 }
3269 public boolean isFolderClingVisible() {
3270 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003271 if (cling != null) {
3272 return cling.getVisibility() == View.VISIBLE;
3273 }
3274 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003275 }
Winson Chung82f55532011-08-09 14:14:23 -07003276 public void dismissWorkspaceCling(View v) {
3277 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003278 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003279 }
3280 public void dismissAllAppsCling(View v) {
3281 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003282 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3283 }
3284 public void dismissFolderCling(View v) {
3285 Cling cling = (Cling) findViewById(R.id.folder_cling);
3286 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003287 }
3288
Winson Chung80baf5a2010-08-09 16:03:15 -07003289 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003290 * Prints out out state for debugging.
3291 */
3292 public void dumpState() {
3293 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003294 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003295 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3296 Log.d(TAG, "mRestoring=" + mRestoring);
3297 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3298 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003299 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003300 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003301
Winson Chung785d2eb2011-04-14 16:08:02 -07003302 if (mAppsCustomizeContent != null) {
3303 mAppsCustomizeContent.dumpState();
3304 }
Joe Onoratobe386092009-11-17 17:32:16 -08003305 Log.d(TAG, "END launcher2 dump state");
3306 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003307
3308 @Override
3309 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3310 super.dump(prefix, fd, writer, args);
3311 writer.println(" ");
3312 writer.println("Debug logs: ");
3313 for (int i = 0; i < sDumpLogs.size(); i++) {
3314 writer.println(" " + sDumpLogs.get(i));
3315 }
3316 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003317}
Michael Jurka2763be32011-02-24 11:19:57 -08003318
Michael Jurkaabded662011-03-04 12:06:57 -08003319interface LauncherTransitionable {
Michael Jurka1899a362011-11-03 13:50:45 -07003320 // return true if the callee will take care of start the animation by itself
3321 boolean onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace);
Winson Chung7d7541e2011-09-16 20:14:36 -07003322 void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003323}