blob: bd8fd8a861d0d62064eb6f6e0a216cdf7d12b1ca [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;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.Dialog;
31import android.app.SearchManager;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020058import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070060import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040061import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080062import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070063import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080064import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.Selection;
66import android.text.SpannableStringBuilder;
67import android.text.TextUtils;
68import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070069import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080070import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080071import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.KeyEvent;
73import android.view.LayoutInflater;
74import android.view.Menu;
75import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070076import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080077import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080079import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070080import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080081import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080082import android.view.ViewTreeObserver.OnGlobalLayoutListener;
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";
Adam Cohen23a4d302011-12-01 17:26:44 -0800142 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
144 // Type: int
145 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700146 // Type: int
147 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700149 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
150 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
152 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700153 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700155 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: boolean
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
158 // Type: long
159 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
160
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700161 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
162
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700163 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700164 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700165 private State mState = State.WORKSPACE;
166 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800167 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700170 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
171 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700172 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700173 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800176 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800178 private final BroadcastReceiver mCloseSystemDialogsReceiver
179 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800180 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private LayoutInflater mInflater;
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800185 private View mQsbDivider;
186 private View mDockDivider;
187 private DragLayer mDragLayer;
188 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700189
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700190 private AppWidgetManager mAppWidgetManager;
191 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700192
Michael Jurkac9d95c52011-08-29 14:03:34 -0700193 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700194 private int[] mTmpAddItemCellCoordinates = new int[2];
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private FolderInfo mFolderInfo;
197
Winson Chung3d503fb2011-07-13 17:25:49 -0700198 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800199 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700200
Winson Chungc51db6a2011-10-05 11:44:49 -0700201 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700202 private AppsCustomizeTabHost mAppsCustomizeTabHost;
203 private AppsCustomizePagedView mAppsCustomizeContent;
204 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private Bundle mSavedState;
207
208 private SpannableStringBuilder mDefaultKeySsb = null;
209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 private boolean mWorkspaceLoading = true;
211
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800212 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 private boolean mRestoring;
214 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700215 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
217 private Bundle mSavedInstanceState;
218
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800221 private boolean mUserPresent = true;
222 private boolean mVisible = false;
223 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700225 private static LocaleConfiguration sLocaleConfiguration = null;
226
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700227 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700229 private Intent mAppMarketIntent = null;
230
Adam Cohended9f8d2010-11-03 13:25:16 -0700231 // Related to the auto-advancing of widgets
232 private final int ADVANCE_MSG = 1;
233 private final int mAdvanceInterval = 20000;
234 private final int mAdvanceStagger = 250;
235 private long mAutoAdvanceSentTime;
236 private long mAutoAdvanceTimeLeft = -1;
237 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
238 new HashMap<View, AppWidgetProviderInfo>();
239
Winson Chung400438b2011-01-16 17:53:48 -0800240 // Determines how long to wait after a rotation before restoring the screen orientation to
241 // match the sensor state.
242 private final int mRestoreScreenOrientationDelay = 500;
243
Michael Jurka4ef207b2010-11-29 17:05:45 -0800244 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700245 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
246 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
247 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800248
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700249 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohened66b2b2012-01-23 17:28:51 -0800250 PendingAddWidgetInfo mWidgetBeingConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700251
Winson Chung46353de2012-02-16 14:05:10 -0800252 // We only want to get the SharedPreferences once since it does an FS stat each time we get
253 // it from the context.
254 private SharedPreferences mSharedPrefs;
255
Adam Cohen2801caf2011-05-13 20:57:39 -0700256
Michael Jurkaddd62e92011-02-16 17:49:14 -0800257 private BubbleTextView mWaitingForResume;
258
Michael Jurkac1f5d262011-09-30 19:32:27 -0700259 private Runnable mBuildLayersRunnable = new Runnable() {
260 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700261 if (mWorkspace != null) {
262 mWorkspace.buildPageHardwareLayers();
263 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700264 }
265 };
266
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800267 private static ArrayList<PendingAddArguments> sPendingAddList
268 = new ArrayList<PendingAddArguments>();
269
270 private static class PendingAddArguments {
271 int requestCode;
272 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700273 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800274 int screen;
275 int cellX;
276 int cellY;
277 }
278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 @Override
280 protected void onCreate(Bundle savedInstanceState) {
281 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800282 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chung46353de2012-02-16 14:05:10 -0800283 mSharedPrefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800284 mModel = app.setLauncher(this);
285 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400286 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700288
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700289 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700290 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
291 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200294 android.os.Debug.startMethodTracing(
295 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 }
297
298 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 setContentView(R.layout.launcher);
300 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700301 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800303 registerContentObservers();
304
Joe Onorato7c312c12009-08-13 21:36:53 -0700305 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 mSavedState = savedInstanceState;
308 restoreState(mSavedState);
309
Winson Chunga12a2502010-12-20 14:41:35 -0800310 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700311 if (mAppsCustomizeContent != null) {
312 mAppsCustomizeContent.onPackagesUpdated();
313 }
Winson Chunga12a2502010-12-20 14:41:35 -0800314
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 if (PROFILE_STARTUP) {
316 android.os.Debug.stopMethodTracing();
317 }
318
319 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400320 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322
Michael Jurkac57b7a82011-08-09 22:02:20 -0700323 if (!mModel.isAllAppsLoaded()) {
324 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
325 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
326 }
327
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 // For handling default keys
329 mDefaultKeySsb = new SpannableStringBuilder();
330 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800331
332 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
333 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800334
Winson Chungc51db6a2011-10-05 11:44:49 -0700335 boolean searchVisible = false;
336 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800337 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700338 int coi = getCurrentOrientationIndexForGlobalIcons();
339 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
340 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700341 updateAppMarketIcon();
342 searchVisible = updateGlobalSearchIcon();
343 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700344 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700345 if (sGlobalSearchIcon[coi] != null) {
346 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700347 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700348 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700349 if (sVoiceSearchIcon[coi] != null) {
350 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700351 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700352 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700353 if (sAppMarketIcon[coi] != null) {
354 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800355 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700356 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700357
Adam Cohen23a4d302011-12-01 17:26:44 -0800358 final String forceEnableRotation =
359 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
360
Adam Cohen446e9402011-09-15 18:21:21 -0700361 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohen23a4d302011-12-01 17:26:44 -0800362 if (LauncherApplication.isScreenLarge() || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700363 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
364 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 }
Romain Guycbb89e42009-06-08 15:52:54 -0700366
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700368 if (sLocaleConfiguration == null) {
369 new AsyncTask<Void, Void, LocaleConfiguration>() {
370 @Override
371 protected LocaleConfiguration doInBackground(Void... unused) {
372 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
373 readConfiguration(Launcher.this, localeConfiguration);
374 return localeConfiguration;
375 }
376
377 @Override
378 protected void onPostExecute(LocaleConfiguration result) {
379 sLocaleConfiguration = result;
380 checkForLocaleChange(); // recursive, but now with a locale configuration
381 }
382 }.execute();
383 return;
384 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 final Configuration configuration = getResources().getConfiguration();
387
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700388 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 final String locale = configuration.locale.toString();
390
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700391 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 final int mcc = configuration.mcc;
393
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700394 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 final int mnc = configuration.mnc;
396
Romain Guy84f296c2009-11-04 15:00:44 -0800397 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398
Romain Guy84f296c2009-11-04 15:00:44 -0800399 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700400 sLocaleConfiguration.locale = locale;
401 sLocaleConfiguration.mcc = mcc;
402 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700403
Joe Onorato0589f0f2010-02-08 13:44:00 -0800404 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405
406 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
407 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700408 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700409 public void run() {
410 writeConfiguration(Launcher.this, localeConfiguration);
411 }
412 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700413 }
414 }
415
416 private static class LocaleConfiguration {
417 public String locale;
418 public int mcc = -1;
419 public int mnc = -1;
420 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700421
Romain Guy98d01652009-06-30 16:21:04 -0700422 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
423 DataInputStream in = null;
424 try {
425 in = new DataInputStream(context.openFileInput(PREFERENCES));
426 configuration.locale = in.readUTF();
427 configuration.mcc = in.readInt();
428 configuration.mnc = in.readInt();
429 } catch (FileNotFoundException e) {
430 // Ignore
431 } catch (IOException e) {
432 // Ignore
433 } finally {
434 if (in != null) {
435 try {
436 in.close();
437 } catch (IOException e) {
438 // Ignore
439 }
440 }
441 }
442 }
443
444 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
445 DataOutputStream out = null;
446 try {
447 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
448 out.writeUTF(configuration.locale);
449 out.writeInt(configuration.mcc);
450 out.writeInt(configuration.mnc);
451 out.flush();
452 } catch (FileNotFoundException e) {
453 // Ignore
454 } catch (IOException e) {
455 //noinspection ResultOfMethodCallIgnored
456 context.getFileStreamPath(PREFERENCES).delete();
457 } finally {
458 if (out != null) {
459 try {
460 out.close();
461 } catch (IOException e) {
462 // Ignore
463 }
464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
466 }
467
Adam Cohen716b51e2011-06-30 12:09:54 -0700468 public DragLayer getDragLayer() {
469 return mDragLayer;
470 }
471
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 static int getScreen() {
473 synchronized (sLock) {
474 return sScreen;
475 }
476 }
477
478 static void setScreen(int screen) {
479 synchronized (sLock) {
480 sScreen = screen;
481 }
482 }
483
Winson Chung557d6ed2011-07-08 15:34:52 -0700484 /**
485 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
486 * a configuration step, this allows the proper animations to run after other transitions.
487 */
488 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700489 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800490 switch (args.requestCode) {
491 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700492 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
493 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800494 break;
495 case REQUEST_PICK_SHORTCUT:
496 processShortcut(args.intent);
497 break;
498 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700499 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
500 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700501 result = true;
502 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800503 case REQUEST_PICK_APPWIDGET:
504 addAppWidgetFromPick(args.intent);
505 break;
506 case REQUEST_CREATE_APPWIDGET:
507 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800508 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700509 result = true;
510 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800511 case REQUEST_PICK_WALLPAPER:
512 // We just wanted the activity result here so we can clear mWaitingForResult
513 break;
514 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700515 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800516 }
517
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800519 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700520 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800521 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
522 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700523 mWaitingForResult = false;
524
Adam Cohened66b2b2012-01-23 17:28:51 -0800525 // We have special handling for widgets
526 if (isWidgetDrop) {
527 int appWidgetId = data != null ?
528 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
529 completeTwoStageWidgetDrop(resultCode, appWidgetId);
530 return;
531 }
532
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 // The pattern used here is that a user PICKs a specific application,
534 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700535
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
537 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700538 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 final PendingAddArguments args = new PendingAddArguments();
540 args.requestCode = requestCode;
541 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700542 args.container = mPendingAddInfo.container;
543 args.screen = mPendingAddInfo.screen;
544 args.cellX = mPendingAddInfo.cellX;
545 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800546 if (isWorkspaceLocked()) {
547 sPendingAddList.add(args);
548 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700549 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800552 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700553 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800554 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
555 null);
556 }
557
558 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
559 CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
560 Runnable onCompleteRunnable = null;
561 int animationType = 0;
562
563 if (resultCode == RESULT_OK) {
564 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
565 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
566 mWidgetBeingConfigured.info);
567 mWidgetBeingConfigured.boundWidget = layout;
568 onCompleteRunnable = new Runnable() {
569 @Override
570 public void run() {
571 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
572 mPendingAddInfo.screen, layout, null);
573 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
574 null);
575 }
576 };
577 } else if (resultCode == RESULT_CANCELED) {
578 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
579 onCompleteRunnable = new Runnable() {
580 @Override
581 public void run() {
582 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
583 null);
584 }
585 };
586 }
587 mWorkspace.animateExternalDrop(mWidgetBeingConfigured, cellLayout,
588 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
589 animationType);
Adam Cohen1b36dc32012-02-13 19:27:37 -0800590 mWidgetBeingConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 }
592
593 @Override
594 protected void onResume() {
595 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800596 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700597 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400598 mWorkspaceLoading = true;
599 mModel.startLoader(this, true);
600 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700601 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 }
Winson Chunge4e50662012-01-23 14:45:13 -0800603
604 // Reset the pressed state of icons that were locked in the press state while activities
605 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800606 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800607 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800608 mWaitingForResume.setStayPressed(false);
609 }
Winson Chunge4e50662012-01-23 14:45:13 -0800610 if (mAppsCustomizeContent != null) {
611 // Resets the previous all apps icon press state
612 mAppsCustomizeContent.resetDrawableState();
613 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 }
615
616 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700617 protected void onPause() {
618 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700619 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800620 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700621 }
Romain Guycbb89e42009-06-08 15:52:54 -0700622
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700623 @Override
Michael Jurkabed61d22012-02-14 22:51:29 -0800624 protected void onStart() {
625 super.onStart();
626 // When Launcher comes back to foreground, a different Activity might be responsible for
627 // the app market intent, so refresh the icon
628 // We don't do this in onResume() because onResume() is triggered every time the home
629 // button is pressed (even if we were already in Launcher)
630 updateAppMarketIcon();
631 }
632
633 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700634 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 // Flag the loader to stop early before switching
636 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700637 if (mAppsCustomizeContent != null) {
638 mAppsCustomizeContent.surrender();
639 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800640 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700641 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700642
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800643 // We can't hide the IME if it was forced open. So don't bother
644 /*
645 @Override
646 public void onWindowFocusChanged(boolean hasFocus) {
647 super.onWindowFocusChanged(hasFocus);
648
649 if (hasFocus) {
650 final InputMethodManager inputManager = (InputMethodManager)
651 getSystemService(Context.INPUT_METHOD_SERVICE);
652 WindowManager.LayoutParams lp = getWindow().getAttributes();
653 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
654 android.os.Handler()) {
655 protected void onReceiveResult(int resultCode, Bundle resultData) {
656 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
657 }
658 });
659 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
660 }
661 }
662 */
663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 private boolean acceptFilter() {
665 final InputMethodManager inputManager = (InputMethodManager)
666 getSystemService(Context.INPUT_METHOD_SERVICE);
667 return !inputManager.isFullscreenMode();
668 }
669
670 @Override
671 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700672 final int uniChar = event.getUnicodeChar();
673 final boolean handled = super.onKeyDown(keyCode, event);
674 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
675 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
677 keyCode, event);
678 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700679 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700680 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700681 // showSearchDialog()
682 // If there are multiple keystrokes before the search dialog takes focus,
683 // onSearchRequested() will be called for every keystroke,
684 // but it is idempotent, so it's fine.
685 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
687 }
688
Joe Onorato8a9625e2010-01-28 15:55:35 -0800689 // Eat the long press event so the keyboard doesn't come up.
690 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
691 return true;
692 }
693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 return handled;
695 }
696
Karl Rosaen138a0412009-04-23 19:00:21 -0700697 private String getTypedText() {
698 return mDefaultKeySsb.toString();
699 }
700
701 private void clearTypedText() {
702 mDefaultKeySsb.clear();
703 mDefaultKeySsb.clearSpans();
704 Selection.setSelection(mDefaultKeySsb, 0);
705 }
706
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700708 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
709 * State
710 */
711 private static State intToState(int stateOrdinal) {
712 State state = State.WORKSPACE;
713 final State[] stateValues = State.values();
714 for (int i = 0; i < stateValues.length; i++) {
715 if (stateValues[i].ordinal() == stateOrdinal) {
716 state = stateValues[i];
717 break;
718 }
719 }
720 return state;
721 }
722
723 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 * Restores the previous state, if it exists.
725 *
726 * @param savedState The previous state.
727 */
728 private void restoreState(Bundle savedState) {
729 if (savedState == null) {
730 return;
731 }
732
Michael Jurka883f55b2010-10-21 15:47:14 -0700733 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700734 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800735 showAllApps(false);
736 }
737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
739 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700740 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
742
Winson Chung3d503fb2011-07-13 17:25:49 -0700743 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
744 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700745
Winson Chung3d503fb2011-07-13 17:25:49 -0700746 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
747 mPendingAddInfo.container = pendingAddContainer;
748 mPendingAddInfo.screen = pendingAddScreen;
749 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
750 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 mRestoring = true;
752 }
753
754 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
755 if (renameFolder) {
756 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700757 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 mRestoring = true;
759 }
Winson Chunga12a2502010-12-20 14:41:35 -0800760
Winson Chung785d2eb2011-04-14 16:08:02 -0700761
762 // Restore the AppsCustomize tab
763 if (mAppsCustomizeTabHost != null) {
764 String curTab = savedState.getString("apps_customize_currentTab");
765 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700766 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700767 mAppsCustomizeContent.setContentType(
768 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
769 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700770 mAppsCustomizeContent.loadAssociatedPages(
771 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700772 }
773
Winson Chung5afbf7b2011-07-25 11:53:08 -0700774 int currentIndex = savedState.getInt("apps_customize_currentIndex");
775 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700776 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 }
778
779 /**
780 * Finds all the views we need and configure them properly.
781 */
782 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700783 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400784
Winson Chung4c98d922011-05-31 16:50:48 -0700785 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
786 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800787 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
788 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789
Winson Chung4c98d922011-05-31 16:50:48 -0700790 // Setup the drag layer
791 mDragLayer.setup(this, dragController);
792
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 // Setup the hotseat
794 mHotseat = (Hotseat) findViewById(R.id.hotseat);
795 if (mHotseat != null) {
796 mHotseat.setup(this);
797 }
798
Winson Chung4c98d922011-05-31 16:50:48 -0700799 // Setup the workspace
800 mWorkspace.setHapticFeedbackEnabled(false);
801 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700802 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700803 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700804
Winson Chungf0ea4d32011-06-06 14:27:16 -0700805 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700806 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700807
Winson Chungf0ea4d32011-06-06 14:27:16 -0700808 // Setup AppsCustomize
809 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
810 findViewById(R.id.apps_customize_pane);
811 mAppsCustomizeContent = (AppsCustomizePagedView)
812 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
813 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400814
Michael Jurka5130e402011-10-13 04:55:35 -0700815 // Get the all apps button
816 mAllAppsButton = findViewById(R.id.all_apps_button);
817 if (mAllAppsButton != null) {
818 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
819 @Override
820 public boolean onTouch(View v, MotionEvent event) {
821 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
822 onTouchDownAllAppsButton(v);
823 }
824 return false;
825 }
826 });
827 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700828 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700829 dragController.setDragScoller(mWorkspace);
830 dragController.setScrollView(mDragLayer);
831 dragController.setMoveTarget(mWorkspace);
832 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700833 if (mSearchDropTargetBar != null) {
834 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
837
838 /**
839 * Creates a view representing a shortcut.
840 *
841 * @param info The data structure describing the shortcut.
842 *
843 * @return A View inflated from R.layout.application.
844 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800845 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700847 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 }
849
850 /**
851 * Creates a view representing a shortcut inflated from the specified resource.
852 *
853 * @param layoutResId The id of the XML layout used to create the shortcut.
854 * @param parent The group the shortcut belongs to.
855 * @param info The data structure describing the shortcut.
856 *
857 * @return A View inflated from layoutResId.
858 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800859 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800860 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
861 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 return favorite;
864 }
865
866 /**
867 * Add an application shortcut to the workspace.
868 *
869 * @param data The intent describing the application.
870 * @param cellInfo The position on screen where to create the shortcut.
871 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700872 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700873 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700874 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700875
Adam Cohenfbba09b2011-07-18 21:43:05 -0700876 // First we check if we already know the exact location where we want to add this item.
877 if (cellX >= 0 && cellY >= 0) {
878 cellXY[0] = cellX;
879 cellXY[1] = cellY;
880 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700881 showOutOfSpaceMessage();
882 return;
883 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800885 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800886
Romain Guy73b979d2009-06-09 12:57:21 -0700887 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800888 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800890 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700891 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700892 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800893 } else {
894 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896 }
Romain Guycbb89e42009-06-08 15:52:54 -0700897
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 /**
899 * Add a shortcut to the workspace.
900 *
901 * @param data The intent describing the shortcut.
902 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700904 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
905 int cellY) {
906 int[] cellXY = mTmpAddItemCellCoordinates;
907 int[] touchXY = mPendingAddInfo.dropPos;
908 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700909
Michael Jurkad3ef3062010-11-23 16:23:58 -0800910 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700911
Adam Cohen558baaf2011-08-15 15:22:57 -0700912 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800913 if (info == null) {
914 return;
915 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700916 final View view = createShortcut(info);
917
Adam Cohenfbba09b2011-07-18 21:43:05 -0700918 // First we check if we already know the exact location where we want to add this item.
919 if (cellX >= 0 && cellY >= 0) {
920 cellXY[0] = cellX;
921 cellXY[1] = cellY;
922 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700923
924 // If appropriate, either create a folder or add to an existing folder
925 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
926 true, null,null)) {
927 return;
928 }
929 DragObject dragObject = new DragObject();
930 dragObject.dragInfo = info;
931 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
932 return;
933 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700934 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800935 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800937 foundCellSpan = (result != null);
938 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700939 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800940 }
941
942 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700943 showOutOfSpaceMessage();
944 return;
945 }
946
Adam Cohen558baaf2011-08-15 15:22:57 -0700947 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948
949 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700950 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
951 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 }
953 }
954
Adam Cohenf814aa02011-09-01 13:48:05 -0700955 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
956 if (spanXY == null) {
957 spanXY = new int[2];
958 }
959
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800960 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700961 // We want to account for the extra amount of padding that we are adding to the widget
962 // to ensure that it gets the full amount of space that it has requested
963 int requiredWidth = minWidth + padding.left + padding.right;
964 int requiredHeight = minHeight + padding.top + padding.bottom;
965 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
966 }
967
968 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
969 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
970 }
971
Adam Cohencbf47e32011-09-16 17:32:37 -0700972 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
973 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
974 }
975
Adam Cohenf814aa02011-09-01 13:48:05 -0700976 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
977 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
978 }
979
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700981 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700983 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700984 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 */
Adam Cohened66b2b2012-01-23 17:28:51 -0800986 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
987 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
988 if (appWidgetInfo == null) {
989 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
990 }
Romain Guycbb89e42009-06-08 15:52:54 -0700991
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700992 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700994
Adam Cohenf814aa02011-09-01 13:48:05 -0700995 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700996
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700998 // We have saved the position to which the widget was dragged-- this really only matters
999 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 int[] cellXY = mTmpAddItemCellCoordinates;
1001 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001002 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001003 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1004 cellXY[0] = mPendingAddInfo.cellX;
1005 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001006 foundCellSpan = true;
1007 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001008 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001009 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -07001010 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001011 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001012 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001013 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001014 }
1015
Michael Jurka0280c3b2010-09-17 15:00:07 -07001016 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001017 if (appWidgetId != -1) {
1018 // Deleting an app widget ID is a void call but writes to disk before returning
1019 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001020 new Thread("deleteAppWidgetId") {
1021 public void run() {
1022 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1023 }
1024 }.start();
1025 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001026 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001027 return;
1028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001030 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001031 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001032 launcherInfo.spanX = spanXY[0];
1033 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001034
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001036 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037
1038 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001039 if (hostView == null) {
1040 // Perform actual inflation because we're live
1041 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1042 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1043 } else {
1044 // The AppWidgetHostView has already been inflated and instantiated
1045 launcherInfo.hostView = hostView;
1046 }
Romain Guycbb89e42009-06-08 15:52:54 -07001047
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 launcherInfo.hostView.setTag(launcherInfo);
Winson Chung3d503fb2011-07-13 17:25:49 -07001049 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001051
1052 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001054 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
Romain Guycbb89e42009-06-08 15:52:54 -07001056
Adam Cohended9f8d2010-11-03 13:25:16 -07001057 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1058 @Override
1059 public void onReceive(Context context, Intent intent) {
1060 final String action = intent.getAction();
1061 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1062 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001063 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001064 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001065
Winson Chungc100e8e2011-08-09 16:02:43 -07001066 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001067 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001068 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1069 mAppsCustomizeTabHost.reset();
1070 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001071 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001072 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1073 mUserPresent = true;
1074 updateRunning();
1075 }
1076 }
1077 };
1078
1079 @Override
1080 public void onAttachedToWindow() {
1081 super.onAttachedToWindow();
1082
1083 // Listen for broadcasts related to user-presence
1084 final IntentFilter filter = new IntentFilter();
1085 filter.addAction(Intent.ACTION_SCREEN_OFF);
1086 filter.addAction(Intent.ACTION_USER_PRESENT);
1087 registerReceiver(mReceiver, filter);
1088
Adam Cohend113e0c2010-11-11 10:48:05 -08001089 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001090 mVisible = true;
1091 }
1092
1093 @Override
1094 public void onDetachedFromWindow() {
1095 super.onDetachedFromWindow();
1096 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001097 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001098
Adam Cohend113e0c2010-11-11 10:48:05 -08001099 if (mAttached) {
1100 unregisterReceiver(mReceiver);
1101 mAttached = false;
1102 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001103 updateRunning();
1104 }
1105
1106 public void onWindowVisibilityChanged(int visibility) {
1107 mVisible = visibility == View.VISIBLE;
1108 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001109 // The following code used to be in onResume, but it turns out onResume is called when
1110 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1111 // is a more appropriate event to handle
1112 if (mVisible) {
1113 mAppsCustomizeTabHost.onWindowVisible();
1114 if (!mWorkspaceLoading) {
1115 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001116 // We want to let Launcher draw itself at least once before we force it to build
1117 // layers on all the workspace pages, so that transitioning to Launcher from other
1118 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1119 // a true draw so we wait until the second preDraw call to be safe
1120 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
1121 boolean mFirstTime = true;
1122 public boolean onPreDraw() {
1123 if (mFirstTime) {
1124 mFirstTime = false;
1125 } else {
1126 //workspace.post(mBuildLayersRunnable);
1127 observer.removeOnPreDrawListener(this);
1128 }
1129 return true;
1130 }
1131 });
1132 }
1133 clearTypedText();
1134 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001135 }
1136
1137 private void sendAdvanceMessage(long delay) {
1138 mHandler.removeMessages(ADVANCE_MSG);
1139 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1140 mHandler.sendMessageDelayed(msg, delay);
1141 mAutoAdvanceSentTime = System.currentTimeMillis();
1142 }
1143
1144 private void updateRunning() {
1145 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1146 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1147 mAutoAdvanceRunning = autoAdvanceRunning;
1148 if (autoAdvanceRunning) {
1149 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1150 sendAdvanceMessage(delay);
1151 } else {
1152 if (!mWidgetsToAdvance.isEmpty()) {
1153 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1154 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1155 }
1156 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001157 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001158 }
1159 }
1160 }
1161
1162 private final Handler mHandler = new Handler() {
1163 @Override
1164 public void handleMessage(Message msg) {
1165 if (msg.what == ADVANCE_MSG) {
1166 int i = 0;
1167 for (View key: mWidgetsToAdvance.keySet()) {
1168 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1169 final int delay = mAdvanceStagger * i;
1170 if (v instanceof Advanceable) {
1171 postDelayed(new Runnable() {
1172 public void run() {
1173 ((Advanceable) v).advance();
1174 }
1175 }, delay);
1176 }
1177 i++;
1178 }
1179 sendAdvanceMessage(mAdvanceInterval);
1180 }
1181 }
1182 };
1183
1184 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001185 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001186 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1187 if (v instanceof Advanceable) {
1188 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001189 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001190 updateRunning();
1191 }
1192 }
1193
1194 void removeWidgetToAutoAdvance(View hostView) {
1195 if (mWidgetsToAdvance.containsKey(hostView)) {
1196 mWidgetsToAdvance.remove(hostView);
1197 updateRunning();
1198 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001199 }
1200
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001202 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203 launcherInfo.hostView = null;
1204 }
1205
Adam Cohended9f8d2010-11-03 13:25:16 -07001206 void showOutOfSpaceMessage() {
1207 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1208 }
1209
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001210 public LauncherAppWidgetHost getAppWidgetHost() {
1211 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
Romain Guycbb89e42009-06-08 15:52:54 -07001213
Winson Chunga9abd0e2010-10-27 17:18:37 -07001214 public LauncherModel getModel() {
1215 return mModel;
1216 }
1217
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001218 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001219 getWindow().closeAllPanels();
1220
Winson Chung87acb482011-08-23 17:28:05 -07001221 /**
1222 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001223 try {
1224 dismissDialog(DIALOG_CREATE_SHORTCUT);
1225 // Unlock the workspace if the dialog was showing
1226 } catch (Exception e) {
1227 // An exception is thrown if the dialog is not visible, which is fine
1228 }
1229
1230 try {
1231 dismissDialog(DIALOG_RENAME_FOLDER);
1232 // Unlock the workspace if the dialog was showing
1233 } catch (Exception e) {
1234 // An exception is thrown if the dialog is not visible, which is fine
1235 }
Winson Chung87acb482011-08-23 17:28:05 -07001236 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001237
1238 // Whatever we were doing is hereby canceled.
1239 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001240 }
1241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 @Override
1243 protected void onNewIntent(Intent intent) {
1244 super.onNewIntent(intent);
1245
1246 // Close the menu
1247 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001248 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001249 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001250
Joe Onorato14f122b2009-11-19 14:06:36 -08001251 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1252 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001253
Adam Cohenac56cff2011-09-28 20:45:37 -07001254 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001255 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001256 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001257 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1258 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001259 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001260 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001261
1262 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001263 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001264 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001265
Joe Onorato3a8820b2009-11-10 15:06:42 -08001266 final View v = getWindow().peekDecorView();
1267 if (v != null && v.getWindowToken() != null) {
1268 InputMethodManager imm = (InputMethodManager)getSystemService(
1269 INPUT_METHOD_SERVICE);
1270 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001272
Michael Jurka35aa14d2011-07-07 17:01:08 -07001273 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001274 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001275 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001276 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
1278 }
1279
1280 @Override
1281 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1282 // Do not call super here
1283 mSavedInstanceState = savedInstanceState;
1284 }
1285
1286 @Override
1287 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001288 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001289 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290
Michael Jurka883f55b2010-10-21 15:47:14 -07001291 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001292 // We close any open folder since it will not be re-opened, and we need to make sure
1293 // this state is reflected.
1294 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295
Winson Chung3d503fb2011-07-13 17:25:49 -07001296 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1297 mWaitingForResult) {
1298 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1299 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1300 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1301 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
1303
1304 if (mFolderInfo != null && mWaitingForResult) {
1305 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1306 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1307 }
Michael Jurka946ad472010-07-09 18:05:18 -07001308
Winson Chung785d2eb2011-04-14 16:08:02 -07001309 // Save the current AppsCustomize tab
1310 if (mAppsCustomizeTabHost != null) {
1311 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1312 if (currentTabTag != null) {
1313 outState.putString("apps_customize_currentTab", currentTabTag);
1314 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001315 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1316 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319
1320 @Override
1321 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001323
Winson Chunge7a03942011-08-05 15:05:12 -07001324 // Remove all pending runnables
1325 mHandler.removeMessages(ADVANCE_MSG);
1326 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001327 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001328
Winson Chungcd2b0142011-06-08 16:02:26 -07001329 // Stop callbacks from LauncherModel
1330 LauncherApplication app = ((LauncherApplication) getApplication());
1331 mModel.stopLoader();
1332 app.setLauncher(null);
1333
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001335 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001337 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001339 mAppWidgetHost = null;
1340
1341 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342
1343 TextKeyListener.getInstance().release();
1344
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345
Winson Chung3d503fb2011-07-13 17:25:49 -07001346 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001347
1348 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001349 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001350
1351 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1352 mWorkspace.removeAllViews();
1353 mWorkspace = null;
1354 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001355
1356 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358
Adam Cohena9cf38f2011-05-02 15:36:58 -07001359 public DragController getDragController() {
1360 return mDragController;
1361 }
1362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 @Override
1364 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001365 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 super.startActivityForResult(intent, requestCode);
1367 }
1368
Winson Chung70d72102011-08-12 11:24:35 -07001369 /**
1370 * Indicates that we want global search for this activity by setting the globalSearch
1371 * argument for {@link #startSearch} to true.
1372 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001374 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001376
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001377 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001378
Karl Rosaen138a0412009-04-23 19:00:21 -07001379 if (initialQuery == null) {
1380 // Use any text typed in the launcher as the initial query
1381 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 if (appSearchData == null) {
1384 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001385 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001387 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001388
Karl Rosaen138a0412009-04-23 19:00:21 -07001389 final SearchManager searchManager =
1390 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001391 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001392 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
1394
Winson Chung70d72102011-08-12 11:24:35 -07001395 @Override
1396 public boolean onCreateOptionsMenu(Menu menu) {
1397 if (isWorkspaceLocked()) {
1398 return false;
1399 }
1400
1401 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001402
1403 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1404 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1405 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001406 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1407 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1408 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001409 String helpUrl = getString(R.string.help_url);
1410 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001411 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1412 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1413
Winson Chung70d72102011-08-12 11:24:35 -07001414 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1415 .setIcon(android.R.drawable.ic_menu_gallery)
1416 .setAlphabeticShortcut('W');
1417 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1418 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001419 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001420 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001421 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1422 .setIcon(android.R.drawable.ic_menu_preferences)
1423 .setIntent(settings)
1424 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001425 if (!helpUrl.isEmpty()) {
1426 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1427 .setIcon(android.R.drawable.ic_menu_help)
1428 .setIntent(help)
1429 .setAlphabeticShortcut('H');
1430 }
Winson Chung70d72102011-08-12 11:24:35 -07001431 return true;
1432 }
1433
1434 @Override
1435 public boolean onPrepareOptionsMenu(Menu menu) {
1436 super.onPrepareOptionsMenu(menu);
1437
1438 if (mAppsCustomizeTabHost.isTransitioning()) {
1439 return false;
1440 }
1441 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1442 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1443
1444 return true;
1445 }
1446
1447 @Override
1448 public boolean onOptionsItemSelected(MenuItem item) {
1449 switch (item.getItemId()) {
1450 case MENU_WALLPAPER_SETTINGS:
1451 startWallpaper();
1452 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001453 }
1454
1455 return super.onOptionsItemSelected(item);
1456 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001458 @Override
1459 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001460 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001461 // Use a custom animation for launching search
1462 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001463 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001464 }
1465
Joe Onorato9c1289c2009-08-17 11:03:03 -04001466 public boolean isWorkspaceLocked() {
1467 return mWorkspaceLoading || mWaitingForResult;
1468 }
1469
Michael Jurka0280c3b2010-09-17 15:00:07 -07001470 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001471 mPendingAddInfo.container = ItemInfo.NO_ID;
1472 mPendingAddInfo.screen = -1;
1473 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1474 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1475 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001476 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001477
Michael Jurkaaf442092010-06-10 17:01:57 -07001478 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001479 // TODO: catch bad widget exception when sent
1480 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001481 // TODO: Is this log message meaningful?
1482 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001483 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001484 }
1485
Adam Cohened66b2b2012-01-23 17:28:51 -08001486 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1487 final AppWidgetProviderInfo appWidget = info.info;
1488 Runnable configurationActivity = null;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001489 if (appWidget.configure != null) {
1490 // Launch over to configure widget, if needed
1491 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1492 intent.setComponent(appWidget.configure);
1493 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001494 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001495 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001496 intent.putExtra(InstallWidgetReceiver.
1497 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001498
1499 final String mimeType = info.mimeType;
1500 final ClipData clipData = (ClipData) info.configurationData;
1501 final ClipDescription clipDesc = clipData.getDescription();
1502 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1503 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001504 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001505 final CharSequence stringData = item.getText();
1506 final Uri uriData = item.getUri();
1507 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001508 final String key = InstallWidgetReceiver.
1509 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001510 if (uriData != null) {
1511 intent.putExtra(key, uriData);
1512 } else if (intentData != null) {
1513 intent.putExtra(key, intentData);
1514 } else if (stringData != null) {
1515 intent.putExtra(key, stringData);
1516 }
1517 break;
1518 }
1519 }
1520 }
Winson Chung55cef262010-10-28 14:14:18 -07001521 }
Romain Guy8e633c52010-03-04 12:51:36 -08001522 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001523 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001525 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001526 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001527 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001528 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
1530 }
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Adam Cohenfbba09b2011-07-18 21:43:05 -07001532 /**
1533 * Process a shortcut drop.
1534 *
1535 * @param componentName The name of the component
1536 * @param screen The screen where it should be added
1537 * @param cell The cell it should be added to, optional
1538 * @param position The location on the screen where it was dropped, optional
1539 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001540 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1541 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001542 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001543 mPendingAddInfo.container = container;
1544 mPendingAddInfo.screen = screen;
1545 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001546
1547 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001548 mPendingAddInfo.cellX = cell[0];
1549 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001550 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001551
1552 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1553 createShortcutIntent.setComponent(componentName);
1554 processShortcut(createShortcutIntent);
1555 }
1556
Adam Cohenfbba09b2011-07-18 21:43:05 -07001557 /**
1558 * Process a widget drop.
1559 *
1560 * @param info The PendingAppWidgetInfo of the widget being added.
1561 * @param screen The screen where it should be added
1562 * @param cell The cell it should be added to, optional
1563 * @param position The location on the screen where it was dropped, optional
1564 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001565 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1566 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001567 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001568 mPendingAddInfo.container = info.container = container;
1569 mPendingAddInfo.screen = info.screen = screen;
1570 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001571 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001572 mPendingAddInfo.cellX = cell[0];
1573 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001574 }
1575
Adam Cohened66b2b2012-01-23 17:28:51 -08001576 AppWidgetHostView hostView = info.boundWidget;
1577 int appWidgetId;
1578 if (hostView != null) {
1579 appWidgetId = hostView.getAppWidgetId();
1580 } else {
1581 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1582 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1583 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001584 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001585
Adam Cohenfbba09b2011-07-18 21:43:05 -07001586 }
1587
Joe Onoratodeb98af2010-02-19 14:59:39 -08001588 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001589 // Handle case where user selected "Applications"
1590 String applicationName = getResources().getString(R.string.group_applications);
1591 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001592
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001593 if (applicationName != null && applicationName.equals(shortcutName)) {
1594 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1595 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001597 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1598 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001599 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001600 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001601 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001602 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 }
1605
Winson Chung24ab2f12010-09-16 14:10:47 -07001606 void processWallpaper(Intent intent) {
1607 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1608 }
1609
Winson Chung3d503fb2011-07-13 17:25:49 -07001610 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1611 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001612 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 folderInfo.title = getText(R.string.folder_name);
1614
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001616 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1617 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001618 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619
1620 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001621 FolderIcon newFolder =
1622 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1623 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1624 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001625 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 }
Romain Guycbb89e42009-06-08 15:52:54 -07001627
Joe Onorato9c1289c2009-08-17 11:03:03 -04001628 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001629 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630 }
1631
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001633 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001635 Intent chooser = Intent.createChooser(pickWallpaper,
1636 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001637 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1638 // Removed in Eclair MR1
1639// WallpaperManager wm = (WallpaperManager)
1640// getSystemService(Context.WALLPAPER_SERVICE);
1641// WallpaperInfo wi = wm.getWallpaperInfo();
1642// if (wi != null && wi.getSettingsActivity() != null) {
1643// LabeledIntent li = new LabeledIntent(getPackageName(),
1644// R.string.configure_wallpaper, 0);
1645// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1646// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1647// }
Mike Clerona0618e42009-10-22 13:55:21 -07001648 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 }
1650
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001651 /**
1652 * Registers various content observers. The current implementation registers
1653 * only a favorites observer to keep track of the favorites applications.
1654 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001655 private void registerContentObservers() {
1656 ContentResolver resolver = getContentResolver();
1657 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1658 true, mWidgetObserver);
1659 }
1660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 @Override
1662 public boolean dispatchKeyEvent(KeyEvent event) {
1663 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1664 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001666 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001667 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001668 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001669 dumpState();
1670 return true;
1671 }
1672 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001673 }
1674 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1675 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001676 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 return true;
1678 }
1679 }
1680
1681 return super.dispatchKeyEvent(event);
1682 }
1683
Joe Onorato88ec0992009-11-19 13:16:06 -08001684 @Override
1685 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001686 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001687 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001688 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001689 Folder openFolder = mWorkspace.getOpenFolder();
1690 if (openFolder.isEditingName()) {
1691 openFolder.dismissEditingName();
1692 } else {
1693 closeFolder();
1694 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001695 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001696 mWorkspace.exitWidgetResizeMode();
1697
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001698 // Back button is a no-op here, but give at least some feedback for the button press
1699 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001700 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001701 }
1702
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001704 * Re-listen when widgets are reset.
1705 */
1706 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001707 if (mAppWidgetHost != null) {
1708 mAppWidgetHost.startListening();
1709 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001710 }
1711
1712 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001713 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1714 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001716 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001717 if (mModel != null) {
1718 mModel.unbindWorkspaceItems();
1719 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001721
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 /**
1723 * Launches the intent referred by the clicked shortcut.
1724 *
1725 * @param v The view representing the clicked shortcut.
1726 */
1727 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001728 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1729 // view has detached (it's possible for this to happen if the view is removed mid touch).
1730 if (v.getWindowToken() == null) {
1731 return;
1732 }
1733
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001734 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001735 return;
1736 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001737
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001739 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001741 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001742 int[] pos = new int[2];
1743 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001744 intent.setSourceBounds(new Rect(pos[0], pos[1],
1745 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001746 boolean success = startActivitySafely(intent, tag);
1747
1748 if (success && v instanceof BubbleTextView) {
1749 mWaitingForResume = (BubbleTextView) v;
1750 mWaitingForResume.setStayPressed(true);
1751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001753 if (v instanceof FolderIcon) {
1754 FolderIcon fi = (FolderIcon) v;
1755 handleFolderClick(fi);
1756 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001757 } else if (v == mAllAppsButton) {
1758 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001759 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001760 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001761 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 }
1764 }
1765
Michael Jurka0e260592010-06-30 17:07:39 -07001766 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001767 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001768 // clicking anywhere on the workspace causes the customization drawer to slide down
1769 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001770 return false;
1771 }
1772
Michael Jurkaaf442092010-06-10 17:01:57 -07001773 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001774 * Event handler for the search button
1775 *
1776 * @param v The view that was clicked.
1777 */
1778 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001779 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1780
Amith Yamasania135ba82011-08-09 17:42:01 -07001781 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001782 }
1783
1784 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001785 * Event handler for the voice button
1786 *
1787 * @param v The view that was clicked.
1788 */
1789 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001790 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001791
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001792 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001793 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001794 startActivity(intent);
1795 }
1796
1797 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001798 * Event handler for the "grid" button that appears on the home screen, which
1799 * enters all apps mode.
1800 *
1801 * @param v The view that was clicked.
1802 */
1803 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001804 showAllApps(true);
1805 }
1806
1807 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001808 // Provide the same haptic feedback that the system offers for virtual keys.
1809 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001810 }
1811
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001812 public void onClickAppMarketButton(View v) {
1813 if (mAppMarketIntent != null) {
1814 startActivitySafely(mAppMarketIntent, "app market");
1815 }
1816 }
1817
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001818 void startApplicationDetailsActivity(ComponentName componentName) {
1819 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001820 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1821 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001822 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001823 startActivity(intent);
1824 }
1825
Patrick Dubroy5539af72010-09-07 15:22:01 -07001826 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1827 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1828 // System applications cannot be installed. For now, show a toast explaining that.
1829 // We may give them the option of disabling apps this way.
1830 int messageId = R.string.uninstall_system_app_text;
1831 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1832 } else {
1833 String packageName = appInfo.componentName.getPackageName();
1834 String className = appInfo.componentName.getClassName();
1835 Intent intent = new Intent(
1836 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001837 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1838 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001839 startActivity(intent);
1840 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001841 }
1842
Michael Jurkaddd62e92011-02-16 17:49:14 -08001843 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1845 try {
1846 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001847 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 } catch (ActivityNotFoundException e) {
1849 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001850 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 } catch (SecurityException e) {
1852 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001853 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001855 "or use the exported attribute for this activity. "
1856 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001858 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001860
Romain Guy8e633c52010-03-04 12:51:36 -08001861 void startActivityForResultSafely(Intent intent, int requestCode) {
1862 try {
1863 startActivityForResult(intent, requestCode);
1864 } catch (ActivityNotFoundException e) {
1865 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1866 } catch (SecurityException e) {
1867 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1868 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1869 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1870 "or use the exported attribute for this activity.", e);
1871 }
1872 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873
Adam Cohena9cf38f2011-05-02 15:36:58 -07001874 private void handleFolderClick(FolderIcon folderIcon) {
1875 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001876 Folder openFolder = mWorkspace.getFolderForTag(info);
1877
1878 // If the folder info reports that the associated folder is open, then verify that
1879 // it is actually opened. There have been a few instances where this gets out of sync.
1880 if (info.opened && openFolder == null) {
1881 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1882 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1883 info.opened = false;
1884 }
1885
Adam Cohena9cf38f2011-05-02 15:36:58 -07001886 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 // Close any open folder
1888 closeFolder();
1889 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001890 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 } else {
1892 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 int folderScreen;
1894 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001895 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 // .. and close it
1897 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001898 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 // Close any folder open on the current screen
1900 closeFolder();
1901 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001902 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 }
1904 }
1905 }
1906 }
1907
Adam Cohen2801caf2011-05-13 20:57:39 -07001908 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001909 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001910 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1911 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1912 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1913
Adam Cohenc51934b2011-07-26 21:07:43 -07001914 FolderInfo info = (FolderInfo) fi.getTag();
1915 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1916 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001917 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1918 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001919 }
1920
Adam Cohen2801caf2011-05-13 20:57:39 -07001921 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1922 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1923 oa.start();
1924 }
1925
1926 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001927 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001928 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1929 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1930 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1931
Adam Cohenc51934b2011-07-26 21:07:43 -07001932 FolderInfo info = (FolderInfo) fi.getTag();
1933 CellLayout cl = null;
1934 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1935 cl = (CellLayout) fi.getParent().getParent();
1936 }
1937
1938 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001939 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1940 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001941 oa.addListener(new AnimatorListenerAdapter() {
1942 @Override
1943 public void onAnimationEnd(Animator animation) {
1944 if (layout != null) {
1945 layout.clearFolderLeaveBehind();
1946 }
1947 }
1948 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001949 oa.start();
1950 }
1951
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001953 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 * is animated relative to the specified View. If the View is null, no animation
1955 * is played.
1956 *
1957 * @param folderInfo The FolderInfo describing the folder to open.
1958 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001959 public void openFolder(FolderIcon folderIcon) {
1960 Folder folder = folderIcon.mFolder;
1961 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962
Adam Cohen2801caf2011-05-13 20:57:39 -07001963 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001964 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965
Adam Cohen4554ee12011-08-03 16:13:21 -07001966 // Just verify that the folder hasn't already been added to the DragLayer.
1967 // There was a one-off crash where the folder had a parent already.
1968 if (folder.getParent() == null) {
1969 mDragLayer.addView(folder);
1970 mDragController.addDropTarget((DropTarget) folder);
1971 } else {
1972 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1973 folder.getParent() + ").");
1974 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001975 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001976 }
1977
1978 public void closeFolder() {
1979 Folder folder = mWorkspace.getOpenFolder();
1980 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001981 if (folder.isEditingName()) {
1982 folder.dismissEditingName();
1983 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001984 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001985
1986 // Dismiss the folder cling
1987 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001988 }
1989 }
1990
1991 void closeFolder(Folder folder) {
1992 folder.getInfo().opened = false;
1993
1994 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1995 if (parent != null) {
1996 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1997 shrinkAndFadeInFolderIcon(fi);
1998 }
1999 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
2001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002003 if (mState != State.WORKSPACE) {
2004 return false;
2005 }
2006
Joe Onorato9c1289c2009-08-17 11:03:03 -04002007 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 return false;
2009 }
2010
2011 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002012 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 }
2014
Michael Jurka0280c3b2010-09-17 15:00:07 -07002015 resetAddInfo();
2016 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002018 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 return true;
2020 }
2021
Winson Chung3d503fb2011-07-13 17:25:49 -07002022 // The hotseat touch handling does not go through Workspace, and we always allow long press
2023 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002024 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002025 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2026 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002027 if (itemUnderLongClick == null) {
2028 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002029 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2030 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002031 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002033 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002035 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037 }
2038 }
2039 return true;
2040 }
2041
Winson Chung3d503fb2011-07-13 17:25:49 -07002042 boolean isHotseatLayout(View layout) {
2043 return mHotseat != null && layout != null &&
2044 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2045 }
2046 Hotseat getHotseat() {
2047 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002048 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002049 SearchDropTargetBar getSearchBar() {
2050 return mSearchDropTargetBar;
2051 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002052
Winson Chung3d503fb2011-07-13 17:25:49 -07002053 /**
2054 * Returns the CellLayout of the specified container at the specified screen.
2055 */
2056 CellLayout getCellLayout(long container, int screen) {
2057 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2058 if (mHotseat != null) {
2059 return mHotseat.getLayout();
2060 } else {
2061 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002062 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002063 } else {
2064 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002065 }
2066 }
2067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 Workspace getWorkspace() {
2069 return mWorkspace;
2070 }
2071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 @Override
2073 protected Dialog onCreateDialog(int id) {
2074 switch (id) {
2075 case DIALOG_CREATE_SHORTCUT:
2076 return new CreateShortcut().createDialog();
2077 case DIALOG_RENAME_FOLDER:
2078 return new RenameFolder().createDialog();
2079 }
2080
2081 return super.onCreateDialog(id);
2082 }
2083
2084 @Override
2085 protected void onPrepareDialog(int id, Dialog dialog) {
2086 switch (id) {
2087 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 break;
2089 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002090 if (mFolderInfo != null) {
2091 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2092 final CharSequence text = mFolderInfo.title;
2093 input.setText(text);
2094 input.setSelection(0, text.length());
2095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 break;
2097 }
2098 }
2099
2100 void showRenameDialog(FolderInfo info) {
2101 mFolderInfo = info;
2102 mWaitingForResult = true;
2103 showDialog(DIALOG_RENAME_FOLDER);
2104 }
2105
Adam Cohenfbba09b2011-07-18 21:43:05 -07002106 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002107 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002108 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2109 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 mWaitingForResult = true;
2111 showDialog(DIALOG_CREATE_SHORTCUT);
2112 }
2113
2114 private class RenameFolder {
2115 private EditText mInput;
2116
2117 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2119 mInput = (EditText) layout.findViewById(R.id.folder_name);
2120
2121 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2122 builder.setIcon(0);
2123 builder.setTitle(getString(R.string.rename_folder_title));
2124 builder.setCancelable(true);
2125 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2126 public void onCancel(DialogInterface dialog) {
2127 cleanup();
2128 }
2129 });
2130 builder.setNegativeButton(getString(R.string.cancel_action),
2131 new Dialog.OnClickListener() {
2132 public void onClick(DialogInterface dialog, int which) {
2133 cleanup();
2134 }
2135 }
2136 );
2137 builder.setPositiveButton(getString(R.string.rename_action),
2138 new Dialog.OnClickListener() {
2139 public void onClick(DialogInterface dialog, int which) {
2140 changeFolderName();
2141 }
2142 }
2143 );
2144 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002145
2146 final AlertDialog dialog = builder.create();
2147 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2148 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002149 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002150 mInput.requestFocus();
2151 InputMethodManager inputManager = (InputMethodManager)
2152 getSystemService(Context.INPUT_METHOD_SERVICE);
2153 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002154 }
2155 });
2156
2157 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 }
2159
2160 private void changeFolderName() {
2161 final String name = mInput.getText().toString();
2162 if (!TextUtils.isEmpty(name)) {
2163 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002164 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 mFolderInfo.title = name;
2166 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2167
Joe Onorato9c1289c2009-08-17 11:03:03 -04002168 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002169 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 } else {
2172 final FolderIcon folderIcon = (FolderIcon)
2173 mWorkspace.getViewForTag(mFolderInfo);
2174 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002175 // TODO: At some point we'll probably want some version of setting
2176 // the text for a folder icon.
2177 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 getWorkspace().requestLayout();
2179 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002180 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 mWorkspaceLoading = true;
2182 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 }
2184 }
2185 }
2186 cleanup();
2187 }
2188
2189 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 dismissDialog(DIALOG_RENAME_FOLDER);
2191 mWaitingForResult = false;
2192 mFolderInfo = null;
2193 }
2194 }
2195
Daniel Sandler843e8602010-06-07 14:59:01 -04002196 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2197 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002198 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002199 }
2200
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002201 // AllAppsView.Watcher
2202 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002203 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002204 mWorkspace.setVisibility(View.GONE);
2205 }
2206 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002207
2208 /**
2209 * Helper method for the cameraZoomIn/cameraZoomOut animations
2210 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002211 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002212 * @param scaleFactor The scale factor used for the zoom
2213 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002214 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002215 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002216 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002217 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002218
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002219 void updateWallpaperVisibility(boolean visible) {
2220 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2221 int curflags = getWindow().getAttributes().flags
2222 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2223 if (wpflags != curflags) {
2224 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2225 }
2226 }
2227
Michael Jurkabed61d22012-02-14 22:51:29 -08002228 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2229 if (v instanceof LauncherTransitionable) {
2230 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2231 }
Winson Chung70442722012-02-10 15:43:22 -08002232
2233 // Update the workspace transition step as well
2234 dispatchOnLauncherTransitionStep(v, 0f);
2235 }
2236
2237 private void dispatchOnLauncherTransitionStep(View v, float t) {
2238 if (v instanceof LauncherTransitionable) {
2239 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2240 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002241 }
2242
2243 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2244 if (v instanceof LauncherTransitionable) {
2245 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2246 }
Winson Chung70442722012-02-10 15:43:22 -08002247
2248 // Update the workspace transition step as well
2249 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002250 }
2251
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002252 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002253 * Things to test when changing the following seven functions.
2254 * - Home from workspace
2255 * - from center screen
2256 * - from other screens
2257 * - Home from all apps
2258 * - from center screen
2259 * - from other screens
2260 * - Back from all apps
2261 * - from center screen
2262 * - from other screens
2263 * - Launch app from workspace and quit
2264 * - with back
2265 * - with home
2266 * - Launch app from all apps and quit
2267 * - with back
2268 * - with home
2269 * - Go to a screen that's not the default, then all
2270 * apps, and launch and app, and go back
2271 * - with back
2272 * -with home
2273 * - On workspace, long press power and go back
2274 * - with back
2275 * - with home
2276 * - On all apps, long press power and go back
2277 * - with back
2278 * - with home
2279 * - On workspace, power off
2280 * - On all apps, power off
2281 * - Launch an app and turn off the screen while in that app
2282 * - Go back with home key
2283 * - Go back with back key TODO: make this not go to workspace
2284 * - From all apps
2285 * - From workspace
2286 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2287 * - From all apps
2288 * - From the center workspace
2289 * - From another workspace
2290 */
2291
2292 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002293 * Zoom the camera out from the workspace to reveal 'toView'.
2294 * Assumes that the view to show is anchored at either the very top or very bottom
2295 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002296 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002297 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002298 if (mStateAnimation != null) {
2299 mStateAnimation.cancel();
2300 mStateAnimation = null;
2301 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002302 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002303 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002304
Winson Chungf0ea4d32011-06-06 14:27:16 -07002305 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2306 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2307 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002308 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002309 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002310 final int startDelay =
2311 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002312
Michael Jurkab3e22d92011-10-31 15:58:33 -07002313 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002314
Michael Jurkad74c9842011-07-10 12:44:21 -07002315 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002316 Animator workspaceAnim =
2317 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002318
2319 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002320 toView.setScaleX(scale);
2321 toView.setScaleY(scale);
2322 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2323 scaleAnim.
2324 scaleX(1f).scaleY(1f).
2325 setDuration(duration).
2326 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002327
Winson Chungf0ea4d32011-06-06 14:27:16 -07002328 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002329 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002330 final ObjectAnimator alphaAnim = ObjectAnimator
2331 .ofFloat(toView, "alpha", 0f, 1f)
2332 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002333 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002334 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2335 @Override
2336 public void onAnimationUpdate(ValueAnimator animation) {
2337 float t = (Float) animation.getAnimatedValue();
2338 dispatchOnLauncherTransitionStep(fromView, t);
2339 dispatchOnLauncherTransitionStep(toView, t);
2340 }
2341 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002342
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002343 // toView should appear right at the end of the workspace shrink
2344 // animation
2345 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002346 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002347 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002348
2349 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002350 boolean animationCancelled = false;
2351
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002352 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002353 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002354 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002355 // Prepare the position
2356 toView.setTranslationX(0.0f);
2357 toView.setTranslationY(0.0f);
2358 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002359 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002360 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002361 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002362 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002363 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2364 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002365
2366 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2367 // Hide the workspace scrollbar
2368 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002369 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002370 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002371 if (!animationCancelled) {
2372 updateWallpaperVisibility(false);
2373 }
2374 }
2375
Adam Cohencff6af82011-09-13 14:51:53 -07002376 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002377 public void onAnimationCancel(Animator animation) {
2378 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002379 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002380 });
2381
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002382 if (workspaceAnim != null) {
2383 mStateAnimation.play(workspaceAnim);
2384 }
Michael Jurka1899a362011-11-03 13:50:45 -07002385
2386 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002387 final ViewTreeObserver observer;
2388
Michael Jurkabed61d22012-02-14 22:51:29 -08002389 dispatchOnLauncherTransitionStart(fromView, animated, false);
2390 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002391
2392 // If any of the objects being animated haven't been measured/laid out
2393 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002394 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002395 (mWorkspace.getMeasuredWidth() == 0) ||
2396 (toView.getMeasuredWidth() == 0)) {
2397 observer = mWorkspace.getViewTreeObserver();
2398 delayAnim = true;
2399 } else {
2400 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002401 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002402
2403 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002404 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002405 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2406 public void onGlobalLayout() {
2407 mWorkspace.post(new Runnable() {
2408 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002409 // Check that mStateAnimation hasn't changed while
2410 // we waited for a layout pass
2411 if (mStateAnimation == stateAnimation) {
2412 // Need to update pivots for zoom if layout changed
2413 setPivotsForZoom(toView, scale);
2414 mStateAnimation.start();
2415 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002416 }
2417 });
2418 observer.removeGlobalOnLayoutListener(this);
2419 }
2420 };
2421 observer.addOnGlobalLayoutListener(delayedStart);
2422 } else {
2423 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002424 mStateAnimation.start();
2425 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002426 } else {
2427 toView.setTranslationX(0.0f);
2428 toView.setTranslationY(0.0f);
2429 toView.setScaleX(1.0f);
2430 toView.setScaleY(1.0f);
2431 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002432 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002433
Michael Jurkabed61d22012-02-14 22:51:29 -08002434 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2435 // Hide the workspace scrollbar
2436 mWorkspace.hideScrollingIndicator(true);
2437 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002438 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002439 dispatchOnLauncherTransitionStart(fromView, animated, false);
2440 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2441 dispatchOnLauncherTransitionStart(toView, animated, false);
2442 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002443 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002444 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002445 }
2446
2447 /**
2448 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002449 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002450 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002451 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002452 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2453 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002454
Michael Jurkab3e22d92011-10-31 15:58:33 -07002455 if (mStateAnimation != null) {
2456 mStateAnimation.cancel();
2457 mStateAnimation = null;
2458 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002459 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002460
Winson Chungf0ea4d32011-06-06 14:27:16 -07002461 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002462 final int fadeOutDuration =
2463 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002464 final float scaleFactor = (float)
2465 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2466 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002467 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002468 Animator workspaceAnim = null;
2469
2470 if (toState == State.WORKSPACE) {
2471 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2472 workspaceAnim = mWorkspace.getChangeStateAnimation(
2473 Workspace.State.NORMAL, animated, stagger);
2474 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2475 workspaceAnim = mWorkspace.getChangeStateAnimation(
2476 Workspace.State.SPRING_LOADED, animated);
2477 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002478
Michael Jurkab3e22d92011-10-31 15:58:33 -07002479 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002480 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002481 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002482 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002483 final float oldScaleX = fromView.getScaleX();
2484 final float oldScaleY = fromView.getScaleY();
2485
Michael Jurka159b4cc2012-01-17 03:00:35 -08002486 final LauncherViewPropertyAnimator scaleAnim =
2487 new LauncherViewPropertyAnimator(fromView);
2488 scaleAnim.
2489 scaleX(scaleFactor).scaleY(scaleFactor).
2490 setDuration(duration).
2491 setInterpolator(new Workspace.ZoomInInterpolator());
2492
2493 final ObjectAnimator alphaAnim = ObjectAnimator
2494 .ofFloat(fromView, "alpha", 1f, 0f)
2495 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002496 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002497 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2498 @Override
2499 public void onAnimationUpdate(ValueAnimator animation) {
2500 float t = 1f - (Float) animation.getAnimatedValue();
2501 dispatchOnLauncherTransitionStep(fromView, t);
2502 dispatchOnLauncherTransitionStep(toView, t);
2503 }
2504 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002505
Michael Jurkabed61d22012-02-14 22:51:29 -08002506 mStateAnimation = new AnimatorSet();
2507
2508 dispatchOnLauncherTransitionStart(fromView, animated, true);
2509 dispatchOnLauncherTransitionStart(toView, animated, true);
2510
2511 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002512 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002513 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002514 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002515 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002516 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2517 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002518 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002519 if (onCompleteRunnable != null) {
2520 onCompleteRunnable.run();
2521 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002522 }
2523 });
2524
Winson Chungf0ea4d32011-06-06 14:27:16 -07002525 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002526 if (workspaceAnim != null) {
2527 mStateAnimation.play(workspaceAnim);
2528 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002529 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002530 } else {
2531 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002532 dispatchOnLauncherTransitionStart(fromView, animated, true);
2533 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2534 dispatchOnLauncherTransitionStart(toView, animated, true);
2535 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002536 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002537 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002538 }
2539
Michael Jurkae326f182011-11-21 14:05:46 -08002540 @Override
2541 public void onTrimMemory(int level) {
2542 super.onTrimMemory(level);
2543 if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2544 mAppsCustomizeTabHost.onTrimMemory();
2545 }
2546 }
2547
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002548 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002549 showWorkspace(animated, null);
2550 }
2551
2552 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002553 if (mState != State.WORKSPACE) {
2554 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002555 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002556
2557 // Show the search bar and hotseat
2558 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002559 // We only need to animate in the dock divider if we're going from spring loaded mode
2560 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002561
2562 // Set focus to the AppsCustomize button
2563 if (mAllAppsButton != null) {
2564 mAllAppsButton.requestFocus();
2565 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002566 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002567
Michael Jurkab737ee62011-11-15 15:57:22 -08002568 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002569
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002570 // Change the state *after* we've called all the transition code
2571 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002572
Winson Chung5fb63472011-02-02 17:03:37 -08002573 // Resume the auto-advance of widgets
2574 mUserPresent = true;
2575 updateRunning();
2576
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002577 // send an accessibility event to announce the context change
2578 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002579 }
2580
Michael Jurkab3e22d92011-10-31 15:58:33 -07002581 void showAllApps(boolean animated) {
2582 if (mState != State.WORKSPACE) return;
2583
2584 showAppsCustomizeHelper(animated, false);
2585 mAppsCustomizeTabHost.requestFocus();
2586
2587 // Hide the search bar and hotseat
2588 mSearchDropTargetBar.hideSearchBar(animated);
2589
2590 // Change the state *after* we've called all the transition code
2591 mState = State.APPS_CUSTOMIZE;
2592
2593 // Pause the auto-advance of widgets until we are out of AllApps
2594 mUserPresent = false;
2595 updateRunning();
2596 closeFolder();
2597
2598 // Send an accessibility event to announce the context change
2599 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2600 }
2601
Adam Cohen7777d962011-08-18 18:58:38 -07002602 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002603 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002604 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002605 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002606 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002607 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002608 }
Adam Cohen7777d962011-08-18 18:58:38 -07002609
Adam Cohened66b2b2012-01-23 17:28:51 -08002610 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2611 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002612 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2613
Winson Chunge7a03942011-08-05 15:05:12 -07002614 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002615 @Override
2616 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002617 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002618 // Before we show workspace, hide all apps again because
2619 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2620 // clean up our state transition functions
2621 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002622 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002623 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002624 } else {
2625 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002626 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002627 }
2628 }, (extendedDelay ?
2629 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2630 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2631 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002632
Michael Jurkad3ef3062010-11-23 16:23:58 -08002633 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002634 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002635 final boolean animated = true;
2636 final boolean springLoaded = true;
2637 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002638 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002639 }
2640 // Otherwise, we are not in spring loaded mode, so don't do anything.
2641 }
2642
Michael Jurkab737ee62011-11-15 15:57:22 -08002643 void hideDockDivider() {
2644 if (mQsbDivider != null && mDockDivider != null) {
2645 mQsbDivider.setVisibility(View.INVISIBLE);
2646 mDockDivider.setVisibility(View.INVISIBLE);
2647 }
2648 }
2649
2650 void showDockDivider(boolean animated) {
2651 if (mQsbDivider != null && mDockDivider != null) {
2652 mQsbDivider.setVisibility(View.VISIBLE);
2653 mDockDivider.setVisibility(View.VISIBLE);
2654 if (mDividerAnimator != null) {
2655 mDividerAnimator.cancel();
2656 mQsbDivider.setAlpha(1f);
2657 mDockDivider.setAlpha(1f);
2658 mDividerAnimator = null;
2659 }
2660 if (animated) {
2661 mDividerAnimator = new AnimatorSet();
2662 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2663 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2664 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2665 mDividerAnimator.start();
2666 }
2667 }
2668 }
2669
Michael Jurkab3e22d92011-10-31 15:58:33 -07002670 void lockAllApps() {
2671 // TODO
2672 }
2673
2674 void unlockAllApps() {
2675 // TODO
2676 }
2677
Adam Cohenfc53cd22011-07-20 15:45:11 -07002678 public boolean isAllAppsCustomizeOpen() {
2679 return mState == State.APPS_CUSTOMIZE;
2680 }
2681
Winson Chungf0ea4d32011-06-06 14:27:16 -07002682 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002683 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002684 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002685 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002686 if (!LauncherApplication.isScreenLarge()) {
2687 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002688 if (mHotseat.getAlpha() != 1f) {
2689 int duration = mSearchDropTargetBar.getTransitionInDuration();
2690 mHotseat.animate().alpha(1f).setDuration(duration);
2691 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002692 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002693 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002694 }
2695 }
2696 }
2697
2698 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002699 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002700 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002701 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002702 if (!LauncherApplication.isScreenLarge()) {
2703 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002704 if (mHotseat.getAlpha() != 0f) {
2705 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2706 mHotseat.animate().alpha(0f).setDuration(duration);
2707 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002708 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002709 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002710 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002711 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002712 }
2713
Patrick Dubroy5f445422011-02-18 14:35:21 -08002714 /**
2715 * Add an item from all apps or customize onto the given workspace screen.
2716 * If layout is null, add to the current screen.
2717 */
2718 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002719 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2720 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002721 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002722 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002723
Winson Chungdff8ebb2011-09-08 17:25:31 -07002724 /** Maps the current orientation to an index for referencing orientation correct global icons */
2725 private int getCurrentOrientationIndexForGlobalIcons() {
2726 // default - 0, landscape - 1
2727 switch (getResources().getConfiguration().orientation) {
2728 case Configuration.ORIENTATION_LANDSCAPE:
2729 return 1;
2730 default:
2731 return 0;
2732 }
2733 }
2734
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002735 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002736 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002737 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002738 // Look for the toolbar icon specified in the activity meta-data
2739 Bundle metaData = packageManager.getActivityInfo(
2740 activityName, PackageManager.GET_META_DATA).metaData;
2741 if (metaData != null) {
2742 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2743 if (iconResId != 0) {
2744 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002745 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002746 }
2747 }
2748 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002749 // This can happen if the activity defines an invalid drawable
2750 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2751 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002752 } catch (Resources.NotFoundException nfe) {
2753 // This can happen if the activity defines an invalid drawable
2754 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2755 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002756 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002757 return null;
2758 }
2759
2760 // if successful in getting icon, return it; otherwise, set button to use default drawable
2761 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2762 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002763 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002764 Resources r = getResources();
2765 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2766 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002767
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002768 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002769 // If we were unable to find the icon via the meta-data, use a generic one
2770 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002771 toolbarIcon = r.getDrawable(fallbackDrawableId);
2772 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002773 if (button != null) {
2774 button.setCompoundDrawables(toolbarIcon, null, null, null);
2775 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002776 return null;
2777 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002778 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002779 if (button != null) {
2780 button.setCompoundDrawables(toolbarIcon, null, null, null);
2781 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002782 return toolbarIcon.getConstantState();
2783 }
2784 }
2785
2786 // if successful in getting icon, return it; otherwise, set button to use default drawable
2787 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2788 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2789 ImageView button = (ImageView) findViewById(buttonId);
2790 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2791
Michael Jurka19e0fc52011-07-22 18:00:21 -07002792 if (button != null) {
2793 // If we were unable to find the icon via the meta-data, use a
2794 // generic one
2795 if (toolbarIcon == null) {
2796 button.setImageResource(fallbackDrawableId);
2797 } else {
2798 button.setImageDrawable(toolbarIcon);
2799 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002800 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002801
2802 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2803
Michael Jurka0423dcf2010-10-05 14:56:18 -07002804 }
2805
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002806 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2807 TextView button = (TextView) findViewById(buttonId);
2808 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2809 }
2810
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002811 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002812 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002813 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002814 }
2815
Winson Chungbb185bd2011-11-21 12:31:42 -08002816 private void invalidatePressedFocusedStates(View container, View button) {
2817 if (container instanceof HolographicLinearLayout) {
2818 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2819 layout.invalidatePressedFocusedStates();
2820 } else if (button instanceof HolographicImageView) {
2821 HolographicImageView view = (HolographicImageView) button;
2822 view.invalidatePressedFocusedStates();
2823 }
2824 }
2825
Winson Chungc51db6a2011-10-05 11:44:49 -07002826 private boolean updateGlobalSearchIcon() {
2827 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002828 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2829 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002830 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002831 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002832
Winson Chung1cad91e2011-05-25 17:41:01 -07002833 final SearchManager searchManager =
2834 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2835 ComponentName activityName = searchManager.getGlobalSearchActivity();
2836 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002837 int coi = getCurrentOrientationIndexForGlobalIcons();
2838 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002839 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002840 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002841 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2842 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002843 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002844 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002845 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002846 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002847 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002848 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2849 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2850 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002851 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002852 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002853 }
2854 }
2855
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002856 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002857 final View searchButtonContainer = findViewById(R.id.search_button_container);
2858 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002859 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002860 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002861 }
2862
Winson Chungc51db6a2011-10-05 11:44:49 -07002863 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002864 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002865 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002866 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002867
Winson Chungc51db6a2011-10-05 11:44:49 -07002868 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002869 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2870 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002871 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002872 int coi = getCurrentOrientationIndexForGlobalIcons();
2873 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002874 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002875 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002876 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002877 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002878 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002879 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002880 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002881 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002882 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002883 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002884 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002885 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002886 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002887
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002888 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002889 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2890 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002891 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002892 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002893 }
2894
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002895 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002896 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002897 */
2898 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002899 final View marketButton = findViewById(R.id.market_button);
2900 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2901 // Find the app market activity by resolving an intent.
2902 // (If multiple app markets are installed, it will return the ResolverActivity.)
2903 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002904 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002905 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002906 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002907 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002908 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002909 marketButton.setVisibility(View.VISIBLE);
2910 } else {
2911 // We should hide and disable the view so that we don't try and restore the visibility
2912 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2913 marketButton.setVisibility(View.GONE);
2914 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002915 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002916 }
2917
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002918 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002919 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002920 }
2921
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002922 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923 * Displays the shortcut creation dialog and launches, if necessary, the
2924 * appropriate activity.
2925 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002926 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002927 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2928 DialogInterface.OnShowListener {
2929
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002930 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002933 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002934
Winson Chung55b65502011-05-26 12:03:43 -07002935 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2936 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002937 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002938
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 AlertDialog dialog = builder.create();
2940 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002941 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002942 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 return dialog;
2945 }
2946
2947 public void onCancel(DialogInterface dialog) {
2948 mWaitingForResult = false;
2949 cleanup();
2950 }
2951
Romain Guycbb89e42009-06-08 15:52:54 -07002952 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002953 mWaitingForResult = false;
2954 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002955 }
2956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002957 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002958 try {
2959 dismissDialog(DIALOG_CREATE_SHORTCUT);
2960 } catch (Exception e) {
2961 // An exception is thrown if the dialog is not visible, which is fine
2962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 }
2964
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002965 /**
2966 * Handle the action clicked in the "Add to home" dialog.
2967 */
2968 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002969 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002970
Winson Chung55b65502011-05-26 12:03:43 -07002971 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2972 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002973 case AddAdapter.ITEM_APPLICATION: {
2974 if (mAppsCustomizeTabHost != null) {
2975 mAppsCustomizeTabHost.selectAppsTab();
2976 }
2977 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002978 break;
2979 }
Winson Chung55b65502011-05-26 12:03:43 -07002980 case AddAdapter.ITEM_APPWIDGET: {
2981 if (mAppsCustomizeTabHost != null) {
2982 mAppsCustomizeTabHost.selectWidgetsTab();
2983 }
2984 showAllApps(true);
2985 break;
2986 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002987 case AddAdapter.ITEM_WALLPAPER: {
2988 startWallpaper();
2989 break;
2990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002991 }
2992 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002993
2994 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002995 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002996 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997 }
2998
2999 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003000 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003001 */
3002 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3003 @Override
3004 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003005 closeSystemDialogs();
3006 }
3007 }
3008
3009 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003010 * Receives notifications whenever the appwidgets are reset.
3011 */
3012 private class AppWidgetResetObserver extends ContentObserver {
3013 public AppWidgetResetObserver() {
3014 super(new Handler());
3015 }
3016
3017 @Override
3018 public void onChange(boolean selfChange) {
3019 onAppWidgetReset();
3020 }
3021 }
3022
3023 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003024 * If the activity is currently paused, signal that we need to re-run the loader
3025 * in onResume.
3026 *
3027 * This needs to be called from incoming places where resources might have been loaded
3028 * while we are paused. That is becaues the Configuration might be wrong
3029 * when we're not running, and if it comes back to what it was when we
3030 * were paused, we are not restarted.
3031 *
3032 * Implementation of the method from LauncherModel.Callbacks.
3033 *
3034 * @return true if we are currently paused. The caller might be able to
3035 * skip some work in that case since we will come back again.
3036 */
3037 public boolean setLoadOnResume() {
3038 if (mPaused) {
3039 Log.i(TAG, "setLoadOnResume");
3040 mOnResumeNeedsLoad = true;
3041 return true;
3042 } else {
3043 return false;
3044 }
3045 }
3046
3047 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003050 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003051 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003052 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003053 } else {
3054 return SCREEN_COUNT / 2;
3055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003056 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003057
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003058
Joe Onorato9c1289c2009-08-17 11:03:03 -04003059 /**
3060 * Refreshes the shortcuts shown on the workspace.
3061 *
3062 * Implementation of the method from LauncherModel.Callbacks.
3063 */
3064 public void startBinding() {
3065 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003066
3067 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003068 int count = workspace.getChildCount();
3069 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003070 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003071 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3072 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003074 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003075 if (mHotseat != null) {
3076 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077 }
3078 }
3079
3080 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003081 * Bind the items start-end from the list.
3082 *
3083 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003086 setLoadOnResume();
3087
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003089 for (int i=start; i<end; i++) {
3090 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003091
3092 // Short circuit if we are loading dock items for a configuration which has no dock
3093 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3094 mHotseat == null) {
3095 continue;
3096 }
3097
Joe Onorato9c1289c2009-08-17 11:03:03 -04003098 switch (item.itemType) {
3099 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3100 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07003101 View shortcut = createShortcut((ShortcutInfo)item);
3102 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3103 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003105 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003106 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003107 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003108 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003109 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3110 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003113 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003114 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003115 }
3116
Joe Onorato9c1289c2009-08-17 11:03:03 -04003117 /**
3118 * Implementation of the method from LauncherModel.Callbacks.
3119 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003120 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003121 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003122 sFolders.clear();
3123 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003125
3126 /**
3127 * Add the views for a widget to the workspace.
3128 *
3129 * Implementation of the method from LauncherModel.Callbacks.
3130 */
3131 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003132 setLoadOnResume();
3133
Daniel Sandler843e8602010-06-07 14:59:01 -04003134 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3135 if (DEBUG_WIDGETS) {
3136 Log.d(TAG, "bindAppWidget: " + item);
3137 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 final Workspace workspace = mWorkspace;
3139
3140 final int appWidgetId = item.appWidgetId;
3141 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003142 if (DEBUG_WIDGETS) {
3143 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3144 }
3145
Joe Onorato9c1289c2009-08-17 11:03:03 -04003146 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3147
Joe Onorato9c1289c2009-08-17 11:03:03 -04003148 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3149 item.hostView.setTag(item);
3150
Winson Chung3d503fb2011-07-13 17:25:49 -07003151 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003152 item.cellY, item.spanX, item.spanY, false);
3153
Adam Cohended9f8d2010-11-03 13:25:16 -07003154 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3155
Joe Onorato9c1289c2009-08-17 11:03:03 -04003156 workspace.requestLayout();
3157
Daniel Sandler843e8602010-06-07 14:59:01 -04003158 if (DEBUG_WIDGETS) {
3159 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3160 + (SystemClock.uptimeMillis()-start) + "ms");
3161 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003162 }
3163
3164 /**
3165 * Callback saying that there aren't any more items to bind.
3166 *
3167 * Implementation of the method from LauncherModel.Callbacks.
3168 */
3169 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003170 setLoadOnResume();
3171
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172 if (mSavedState != null) {
3173 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003174 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003175 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003176 mSavedState = null;
3177 }
3178
3179 if (mSavedInstanceState != null) {
3180 super.onRestoreInstanceState(mSavedInstanceState);
3181 mSavedInstanceState = null;
3182 }
3183
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003185
3186 // If we received the result of any pending adds while the loader was running (e.g. the
3187 // widget configuration forced an orientation change), process them now.
3188 for (int i = 0; i < sPendingAddList.size(); i++) {
3189 completeAdd(sPendingAddList.get(i));
3190 }
3191 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003192
Winson Chungc51db6a2011-10-05 11:44:49 -07003193 // Update the market app icon as necessary (the other icons will be managed in response to
3194 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003195 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003196
3197 mWorkspace.post(mBuildLayersRunnable);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003198 }
3199
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003200 @Override
3201 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003202 boolean searchVisible = updateGlobalSearchIcon();
3203 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3204 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003205 }
3206
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003207 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003208 * Add the icons for all apps.
3209 *
3210 * Implementation of the method from LauncherModel.Callbacks.
3211 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003212 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3213 // Remove the progress bar entirely; we could also make it GONE
3214 // but better to remove it since we know it's not going to be used
3215 View progressBar = mAppsCustomizeTabHost.
3216 findViewById(R.id.apps_customize_progress_bar);
3217 if (progressBar != null) {
3218 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003219 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003220 // We just post the call to setApps so the user sees the progress bar
3221 // disappear-- otherwise, it just looks like the progress bar froze
3222 // which doesn't look great
3223 mAppsCustomizeTabHost.post(new Runnable() {
3224 public void run() {
3225 if (mAppsCustomizeContent != null) {
3226 mAppsCustomizeContent.setApps(apps);
3227 }
3228 }
3229 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003230 }
3231
3232 /**
3233 * A package was installed.
3234 *
3235 * Implementation of the method from LauncherModel.Callbacks.
3236 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003237 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003238 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003239 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003240
Winson Chung785d2eb2011-04-14 16:08:02 -07003241 if (mAppsCustomizeContent != null) {
3242 mAppsCustomizeContent.addApps(apps);
3243 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003244 }
3245
3246 /**
3247 * A package was updated.
3248 *
3249 * Implementation of the method from LauncherModel.Callbacks.
3250 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003251 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003252 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003253 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003254 if (mWorkspace != null) {
3255 mWorkspace.updateShortcuts(apps);
3256 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003257
Winson Chung785d2eb2011-04-14 16:08:02 -07003258 if (mAppsCustomizeContent != null) {
3259 mAppsCustomizeContent.updateApps(apps);
3260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003261 }
3262
3263 /**
3264 * A package was uninstalled.
3265 *
3266 * Implementation of the method from LauncherModel.Callbacks.
3267 */
Joe Onorato36115782010-06-17 13:28:48 -04003268 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003269 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003270 if (permanent) {
3271 mWorkspace.removeItems(apps);
3272 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003273
Winson Chung785d2eb2011-04-14 16:08:02 -07003274 if (mAppsCustomizeContent != null) {
3275 mAppsCustomizeContent.removeApps(apps);
3276 }
Winson Chunga1820962011-10-03 16:31:06 -07003277
3278 // Notify the drag controller
3279 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003280 }
Joe Onoratobe386092009-11-17 17:32:16 -08003281
3282 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003283 * A number of packages were updated.
3284 */
3285 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003286 if (mAppsCustomizeContent != null) {
3287 mAppsCustomizeContent.onPackagesUpdated();
3288 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003289 }
3290
Winson Chung400438b2011-01-16 17:53:48 -08003291 private int mapConfigurationOriActivityInfoOri(int configOri) {
3292 final Display d = getWindowManager().getDefaultDisplay();
3293 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3294 switch (d.getRotation()) {
3295 case Surface.ROTATION_0:
3296 case Surface.ROTATION_180:
3297 // We are currently in the same basic orientation as the natural orientation
3298 naturalOri = configOri;
3299 break;
3300 case Surface.ROTATION_90:
3301 case Surface.ROTATION_270:
3302 // We are currently in the other basic orientation to the natural orientation
3303 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3304 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3305 break;
3306 }
3307
3308 int[] oriMap = {
3309 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3310 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3311 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3312 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3313 };
3314 // Since the map starts at portrait, we need to offset if this device's natural orientation
3315 // is landscape.
3316 int indexOffset = 0;
3317 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3318 indexOffset = 1;
3319 }
3320 return oriMap[(d.getRotation() + indexOffset) % 4];
3321 }
Adam Cohen446e9402011-09-15 18:21:21 -07003322
3323 public void lockScreenOrientationOnLargeUI() {
3324 if (LauncherApplication.isScreenLarge()) {
3325 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3326 .getConfiguration().orientation));
3327 }
Winson Chung400438b2011-01-16 17:53:48 -08003328 }
Adam Cohen446e9402011-09-15 18:21:21 -07003329 public void unlockScreenOrientationOnLargeUI() {
3330 if (LauncherApplication.isScreenLarge()) {
3331 mHandler.postDelayed(new Runnable() {
3332 public void run() {
3333 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3334 }
3335 }, mRestoreScreenOrientationDelay);
3336 }
Winson Chung400438b2011-01-16 17:53:48 -08003337 }
3338
Winson Chung82f55532011-08-09 14:14:23 -07003339 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003340 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3341 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003342 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003343 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003344
Winson Chung7d7541e2011-09-16 20:14:36 -07003345 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003346 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003347 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3348 Cling cling = (Cling) findViewById(clingId);
3349 if (cling != null) {
3350 cling.init(this, positionData);
3351 cling.setVisibility(View.VISIBLE);
3352 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3353 if (animate) {
3354 cling.buildLayer();
3355 cling.setAlpha(0f);
3356 cling.animate()
3357 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003358 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003359 .setDuration(SHOW_CLING_DURATION)
3360 .setStartDelay(delay)
3361 .start();
3362 } else {
3363 cling.setAlpha(1f);
3364 }
3365 }
3366 return cling;
3367 }
3368 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003369 if (cling != null) {
3370 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003371 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003372 anim.addListener(new AnimatorListenerAdapter() {
3373 public void onAnimationEnd(Animator animation) {
3374 cling.setVisibility(View.GONE);
3375 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003376 // We should update the shared preferences on a background thread
3377 new Thread("dismissClingThread") {
3378 public void run() {
3379 SharedPreferences.Editor editor = mSharedPrefs.edit();
3380 editor.putBoolean(flag, true);
3381 editor.commit();
3382 }
3383 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003384 };
3385 });
3386 anim.start();
3387 }
3388 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003389 private void removeCling(int id) {
3390 final View cling = findViewById(id);
3391 if (cling != null) {
3392 final ViewGroup parent = (ViewGroup) cling.getParent();
3393 parent.post(new Runnable() {
3394 @Override
3395 public void run() {
3396 parent.removeView(cling);
3397 }
3398 });
3399 }
3400 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003401 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003402 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003403 if (isClingsEnabled() &&
3404 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003405 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003406 } else {
3407 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003408 }
3409 }
3410 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003411 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003412 if (isClingsEnabled() &&
3413 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003414 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003415 } else {
3416 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003417 }
3418 }
3419 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003420 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003421 if (isClingsEnabled() &&
3422 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3423 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003424 } else {
3425 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003426 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003427 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003428 }
3429 public boolean isFolderClingVisible() {
3430 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003431 if (cling != null) {
3432 return cling.getVisibility() == View.VISIBLE;
3433 }
3434 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003435 }
Winson Chung82f55532011-08-09 14:14:23 -07003436 public void dismissWorkspaceCling(View v) {
3437 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003438 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003439 }
3440 public void dismissAllAppsCling(View v) {
3441 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003442 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3443 }
3444 public void dismissFolderCling(View v) {
3445 Cling cling = (Cling) findViewById(R.id.folder_cling);
3446 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003447 }
3448
Winson Chung80baf5a2010-08-09 16:03:15 -07003449 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003450 * Prints out out state for debugging.
3451 */
3452 public void dumpState() {
3453 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003454 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003455 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3456 Log.d(TAG, "mRestoring=" + mRestoring);
3457 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3458 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003459 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003460 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003461
Winson Chung785d2eb2011-04-14 16:08:02 -07003462 if (mAppsCustomizeContent != null) {
3463 mAppsCustomizeContent.dumpState();
3464 }
Joe Onoratobe386092009-11-17 17:32:16 -08003465 Log.d(TAG, "END launcher2 dump state");
3466 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003467
3468 @Override
3469 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3470 super.dump(prefix, fd, writer, args);
3471 writer.println(" ");
3472 writer.println("Debug logs: ");
3473 for (int i = 0; i < sDumpLogs.size(); i++) {
3474 writer.println(" " + sDumpLogs.get(i));
3475 }
3476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003477}
Michael Jurka2763be32011-02-24 11:19:57 -08003478
Michael Jurkaabded662011-03-04 12:06:57 -08003479interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003480 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003481 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003482 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003483 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003484}