blob: 83ca6754f89c76451378665b4bf63ec274e43c45 [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
624 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625 // Flag the loader to stop early before switching
626 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700627 if (mAppsCustomizeContent != null) {
628 mAppsCustomizeContent.surrender();
629 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800630 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700631 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700632
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800633 // We can't hide the IME if it was forced open. So don't bother
634 /*
635 @Override
636 public void onWindowFocusChanged(boolean hasFocus) {
637 super.onWindowFocusChanged(hasFocus);
638
639 if (hasFocus) {
640 final InputMethodManager inputManager = (InputMethodManager)
641 getSystemService(Context.INPUT_METHOD_SERVICE);
642 WindowManager.LayoutParams lp = getWindow().getAttributes();
643 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
644 android.os.Handler()) {
645 protected void onReceiveResult(int resultCode, Bundle resultData) {
646 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
647 }
648 });
649 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
650 }
651 }
652 */
653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 private boolean acceptFilter() {
655 final InputMethodManager inputManager = (InputMethodManager)
656 getSystemService(Context.INPUT_METHOD_SERVICE);
657 return !inputManager.isFullscreenMode();
658 }
659
660 @Override
661 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700662 final int uniChar = event.getUnicodeChar();
663 final boolean handled = super.onKeyDown(keyCode, event);
664 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
665 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
667 keyCode, event);
668 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700669 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700670 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700671 // showSearchDialog()
672 // If there are multiple keystrokes before the search dialog takes focus,
673 // onSearchRequested() will be called for every keystroke,
674 // but it is idempotent, so it's fine.
675 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 }
678
Joe Onorato8a9625e2010-01-28 15:55:35 -0800679 // Eat the long press event so the keyboard doesn't come up.
680 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
681 return true;
682 }
683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 return handled;
685 }
686
Karl Rosaen138a0412009-04-23 19:00:21 -0700687 private String getTypedText() {
688 return mDefaultKeySsb.toString();
689 }
690
691 private void clearTypedText() {
692 mDefaultKeySsb.clear();
693 mDefaultKeySsb.clearSpans();
694 Selection.setSelection(mDefaultKeySsb, 0);
695 }
696
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700698 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
699 * State
700 */
701 private static State intToState(int stateOrdinal) {
702 State state = State.WORKSPACE;
703 final State[] stateValues = State.values();
704 for (int i = 0; i < stateValues.length; i++) {
705 if (stateValues[i].ordinal() == stateOrdinal) {
706 state = stateValues[i];
707 break;
708 }
709 }
710 return state;
711 }
712
713 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 * Restores the previous state, if it exists.
715 *
716 * @param savedState The previous state.
717 */
718 private void restoreState(Bundle savedState) {
719 if (savedState == null) {
720 return;
721 }
722
Michael Jurka883f55b2010-10-21 15:47:14 -0700723 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700724 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800725 showAllApps(false);
726 }
727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
729 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700730 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 }
732
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
734 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700735
Winson Chung3d503fb2011-07-13 17:25:49 -0700736 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
737 mPendingAddInfo.container = pendingAddContainer;
738 mPendingAddInfo.screen = pendingAddScreen;
739 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
740 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 mRestoring = true;
742 }
743
744 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
745 if (renameFolder) {
746 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700747 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 mRestoring = true;
749 }
Winson Chunga12a2502010-12-20 14:41:35 -0800750
Winson Chung785d2eb2011-04-14 16:08:02 -0700751
752 // Restore the AppsCustomize tab
753 if (mAppsCustomizeTabHost != null) {
754 String curTab = savedState.getString("apps_customize_currentTab");
755 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700756 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700757 mAppsCustomizeContent.setContentType(
758 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
759 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700760 mAppsCustomizeContent.loadAssociatedPages(
761 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700762 }
763
Winson Chung5afbf7b2011-07-25 11:53:08 -0700764 int currentIndex = savedState.getInt("apps_customize_currentIndex");
765 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
768
769 /**
770 * Finds all the views we need and configure them properly.
771 */
772 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700773 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400774
Winson Chung4c98d922011-05-31 16:50:48 -0700775 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
776 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800777 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
778 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779
Winson Chung4c98d922011-05-31 16:50:48 -0700780 // Setup the drag layer
781 mDragLayer.setup(this, dragController);
782
Winson Chung3d503fb2011-07-13 17:25:49 -0700783 // Setup the hotseat
784 mHotseat = (Hotseat) findViewById(R.id.hotseat);
785 if (mHotseat != null) {
786 mHotseat.setup(this);
787 }
788
Winson Chung4c98d922011-05-31 16:50:48 -0700789 // Setup the workspace
790 mWorkspace.setHapticFeedbackEnabled(false);
791 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700792 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700793 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700794
Winson Chungf0ea4d32011-06-06 14:27:16 -0700795 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700796 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700797
Winson Chungf0ea4d32011-06-06 14:27:16 -0700798 // Setup AppsCustomize
799 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
800 findViewById(R.id.apps_customize_pane);
801 mAppsCustomizeContent = (AppsCustomizePagedView)
802 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
803 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400804
Michael Jurka5130e402011-10-13 04:55:35 -0700805 // Get the all apps button
806 mAllAppsButton = findViewById(R.id.all_apps_button);
807 if (mAllAppsButton != null) {
808 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
809 @Override
810 public boolean onTouch(View v, MotionEvent event) {
811 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
812 onTouchDownAllAppsButton(v);
813 }
814 return false;
815 }
816 });
817 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700818 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700819 dragController.setDragScoller(mWorkspace);
820 dragController.setScrollView(mDragLayer);
821 dragController.setMoveTarget(mWorkspace);
822 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700823 if (mSearchDropTargetBar != null) {
824 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800825 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 }
827
828 /**
829 * Creates a view representing a shortcut.
830 *
831 * @param info The data structure describing the shortcut.
832 *
833 * @return A View inflated from R.layout.application.
834 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800835 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700837 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
839
840 /**
841 * Creates a view representing a shortcut inflated from the specified resource.
842 *
843 * @param layoutResId The id of the XML layout used to create the shortcut.
844 * @param parent The group the shortcut belongs to.
845 * @param info The data structure describing the shortcut.
846 *
847 * @return A View inflated from layoutResId.
848 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800849 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800850 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
851 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 return favorite;
854 }
855
856 /**
857 * Add an application shortcut to the workspace.
858 *
859 * @param data The intent describing the application.
860 * @param cellInfo The position on screen where to create the shortcut.
861 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700862 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700863 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700864 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700865
Adam Cohenfbba09b2011-07-18 21:43:05 -0700866 // First we check if we already know the exact location where we want to add this item.
867 if (cellX >= 0 && cellY >= 0) {
868 cellXY[0] = cellX;
869 cellXY[1] = cellY;
870 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700871 showOutOfSpaceMessage();
872 return;
873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800875 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800876
Romain Guy73b979d2009-06-09 12:57:21 -0700877 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800878 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800880 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700881 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700882 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800883 } else {
884 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886 }
Romain Guycbb89e42009-06-08 15:52:54 -0700887
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 /**
889 * Add a shortcut to the workspace.
890 *
891 * @param data The intent describing the shortcut.
892 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700894 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
895 int cellY) {
896 int[] cellXY = mTmpAddItemCellCoordinates;
897 int[] touchXY = mPendingAddInfo.dropPos;
898 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700899
Michael Jurkad3ef3062010-11-23 16:23:58 -0800900 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700901
Adam Cohen558baaf2011-08-15 15:22:57 -0700902 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800903 if (info == null) {
904 return;
905 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700906 final View view = createShortcut(info);
907
Adam Cohenfbba09b2011-07-18 21:43:05 -0700908 // First we check if we already know the exact location where we want to add this item.
909 if (cellX >= 0 && cellY >= 0) {
910 cellXY[0] = cellX;
911 cellXY[1] = cellY;
912 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700913
914 // If appropriate, either create a folder or add to an existing folder
915 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
916 true, null,null)) {
917 return;
918 }
919 DragObject dragObject = new DragObject();
920 dragObject.dragInfo = info;
921 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
922 return;
923 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700924 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800925 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700926 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800927 foundCellSpan = (result != null);
928 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700929 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800930 }
931
932 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700933 showOutOfSpaceMessage();
934 return;
935 }
936
Adam Cohen558baaf2011-08-15 15:22:57 -0700937 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938
939 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
941 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943 }
944
Adam Cohenf814aa02011-09-01 13:48:05 -0700945 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
946 if (spanXY == null) {
947 spanXY = new int[2];
948 }
949
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800950 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700951 // We want to account for the extra amount of padding that we are adding to the widget
952 // to ensure that it gets the full amount of space that it has requested
953 int requiredWidth = minWidth + padding.left + padding.right;
954 int requiredHeight = minHeight + padding.top + padding.bottom;
955 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
956 }
957
958 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
959 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
960 }
961
Adam Cohencbf47e32011-09-16 17:32:37 -0700962 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
963 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
964 }
965
Adam Cohenf814aa02011-09-01 13:48:05 -0700966 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
967 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
968 }
969
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700971 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700973 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700974 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 */
Adam Cohened66b2b2012-01-23 17:28:51 -0800976 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
977 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
978 if (appWidgetInfo == null) {
979 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
980 }
Romain Guycbb89e42009-06-08 15:52:54 -0700981
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700982 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700983 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700984
Adam Cohenf814aa02011-09-01 13:48:05 -0700985 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700986
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700988 // We have saved the position to which the widget was dragged-- this really only matters
989 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700990 int[] cellXY = mTmpAddItemCellCoordinates;
991 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700992 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
994 cellXY[0] = mPendingAddInfo.cellX;
995 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700996 foundCellSpan = true;
997 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700998 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700999 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -07001000 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001001 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001002 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001003 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001004 }
1005
Michael Jurka0280c3b2010-09-17 15:00:07 -07001006 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001007 if (appWidgetId != -1) {
1008 // Deleting an app widget ID is a void call but writes to disk before returning
1009 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001010 new Thread("deleteAppWidgetId") {
1011 public void run() {
1012 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1013 }
1014 }.start();
1015 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001016 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001017 return;
1018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001020 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001021 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001022 launcherInfo.spanX = spanXY[0];
1023 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001026 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027
1028 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001029 if (hostView == null) {
1030 // Perform actual inflation because we're live
1031 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1032 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1033 } else {
1034 // The AppWidgetHostView has already been inflated and instantiated
1035 launcherInfo.hostView = hostView;
1036 }
Romain Guycbb89e42009-06-08 15:52:54 -07001037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 launcherInfo.hostView.setTag(launcherInfo);
Winson Chung3d503fb2011-07-13 17:25:49 -07001039 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001041
1042 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001044 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 }
Romain Guycbb89e42009-06-08 15:52:54 -07001046
Adam Cohended9f8d2010-11-03 13:25:16 -07001047 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1048 @Override
1049 public void onReceive(Context context, Intent intent) {
1050 final String action = intent.getAction();
1051 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1052 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001053 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001054 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001055
Winson Chungc100e8e2011-08-09 16:02:43 -07001056 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001057 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001058 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1059 mAppsCustomizeTabHost.reset();
1060 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001061 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001062 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1063 mUserPresent = true;
1064 updateRunning();
1065 }
1066 }
1067 };
1068
1069 @Override
1070 public void onAttachedToWindow() {
1071 super.onAttachedToWindow();
1072
1073 // Listen for broadcasts related to user-presence
1074 final IntentFilter filter = new IntentFilter();
1075 filter.addAction(Intent.ACTION_SCREEN_OFF);
1076 filter.addAction(Intent.ACTION_USER_PRESENT);
1077 registerReceiver(mReceiver, filter);
1078
Adam Cohend113e0c2010-11-11 10:48:05 -08001079 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001080 mVisible = true;
1081 }
1082
1083 @Override
1084 public void onDetachedFromWindow() {
1085 super.onDetachedFromWindow();
1086 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001087 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001088
Adam Cohend113e0c2010-11-11 10:48:05 -08001089 if (mAttached) {
1090 unregisterReceiver(mReceiver);
1091 mAttached = false;
1092 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001093 updateRunning();
1094 }
1095
1096 public void onWindowVisibilityChanged(int visibility) {
1097 mVisible = visibility == View.VISIBLE;
1098 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001099 // The following code used to be in onResume, but it turns out onResume is called when
1100 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1101 // is a more appropriate event to handle
1102 if (mVisible) {
1103 mAppsCustomizeTabHost.onWindowVisible();
1104 if (!mWorkspaceLoading) {
1105 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001106 // We want to let Launcher draw itself at least once before we force it to build
1107 // layers on all the workspace pages, so that transitioning to Launcher from other
1108 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1109 // a true draw so we wait until the second preDraw call to be safe
1110 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001111 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001112 // We delay the layer building a bit in order to give
1113 // other message processing a time to run. In particular
1114 // this avoids a delay in hiding the IME if it was
1115 // currently shown, because doing that may involve
1116 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001117 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001118 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001119 return true;
1120 }
1121 });
1122 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001123 // When Launcher comes back to foreground, a different Activity might be responsible for
1124 // the app market intent, so refresh the icon
1125 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001126 clearTypedText();
1127 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 }
1129
1130 private void sendAdvanceMessage(long delay) {
1131 mHandler.removeMessages(ADVANCE_MSG);
1132 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1133 mHandler.sendMessageDelayed(msg, delay);
1134 mAutoAdvanceSentTime = System.currentTimeMillis();
1135 }
1136
1137 private void updateRunning() {
1138 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1139 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1140 mAutoAdvanceRunning = autoAdvanceRunning;
1141 if (autoAdvanceRunning) {
1142 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1143 sendAdvanceMessage(delay);
1144 } else {
1145 if (!mWidgetsToAdvance.isEmpty()) {
1146 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1147 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1148 }
1149 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001150 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001151 }
1152 }
1153 }
1154
1155 private final Handler mHandler = new Handler() {
1156 @Override
1157 public void handleMessage(Message msg) {
1158 if (msg.what == ADVANCE_MSG) {
1159 int i = 0;
1160 for (View key: mWidgetsToAdvance.keySet()) {
1161 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1162 final int delay = mAdvanceStagger * i;
1163 if (v instanceof Advanceable) {
1164 postDelayed(new Runnable() {
1165 public void run() {
1166 ((Advanceable) v).advance();
1167 }
1168 }, delay);
1169 }
1170 i++;
1171 }
1172 sendAdvanceMessage(mAdvanceInterval);
1173 }
1174 }
1175 };
1176
1177 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001178 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001179 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1180 if (v instanceof Advanceable) {
1181 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001182 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001183 updateRunning();
1184 }
1185 }
1186
1187 void removeWidgetToAutoAdvance(View hostView) {
1188 if (mWidgetsToAdvance.containsKey(hostView)) {
1189 mWidgetsToAdvance.remove(hostView);
1190 updateRunning();
1191 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001192 }
1193
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001195 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196 launcherInfo.hostView = null;
1197 }
1198
Adam Cohended9f8d2010-11-03 13:25:16 -07001199 void showOutOfSpaceMessage() {
1200 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1201 }
1202
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001203 public LauncherAppWidgetHost getAppWidgetHost() {
1204 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
Romain Guycbb89e42009-06-08 15:52:54 -07001206
Winson Chunga9abd0e2010-10-27 17:18:37 -07001207 public LauncherModel getModel() {
1208 return mModel;
1209 }
1210
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001211 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001212 getWindow().closeAllPanels();
1213
Winson Chung87acb482011-08-23 17:28:05 -07001214 /**
1215 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001216 try {
1217 dismissDialog(DIALOG_CREATE_SHORTCUT);
1218 // Unlock the workspace if the dialog was showing
1219 } catch (Exception e) {
1220 // An exception is thrown if the dialog is not visible, which is fine
1221 }
1222
1223 try {
1224 dismissDialog(DIALOG_RENAME_FOLDER);
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 }
Winson Chung87acb482011-08-23 17:28:05 -07001229 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001230
1231 // Whatever we were doing is hereby canceled.
1232 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001233 }
1234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 @Override
1236 protected void onNewIntent(Intent intent) {
1237 super.onNewIntent(intent);
1238
1239 // Close the menu
1240 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001241 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001242 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001243
Joe Onorato14f122b2009-11-19 14:06:36 -08001244 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1245 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001246
Adam Cohenac56cff2011-09-28 20:45:37 -07001247 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001248 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001249 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001250 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1251 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001252 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001253 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001254
1255 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001256 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001257 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001258
Joe Onorato3a8820b2009-11-10 15:06:42 -08001259 final View v = getWindow().peekDecorView();
1260 if (v != null && v.getWindowToken() != null) {
1261 InputMethodManager imm = (InputMethodManager)getSystemService(
1262 INPUT_METHOD_SERVICE);
1263 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001265
Michael Jurka35aa14d2011-07-07 17:01:08 -07001266 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001267 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001268 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
1271 }
1272
1273 @Override
1274 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1275 // Do not call super here
1276 mSavedInstanceState = savedInstanceState;
1277 }
1278
1279 @Override
1280 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001281 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001282 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283
Michael Jurka883f55b2010-10-21 15:47:14 -07001284 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001285 // We close any open folder since it will not be re-opened, and we need to make sure
1286 // this state is reflected.
1287 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288
Winson Chung3d503fb2011-07-13 17:25:49 -07001289 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1290 mWaitingForResult) {
1291 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1292 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1293 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1294 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
1296
1297 if (mFolderInfo != null && mWaitingForResult) {
1298 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1299 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1300 }
Michael Jurka946ad472010-07-09 18:05:18 -07001301
Winson Chung785d2eb2011-04-14 16:08:02 -07001302 // Save the current AppsCustomize tab
1303 if (mAppsCustomizeTabHost != null) {
1304 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1305 if (currentTabTag != null) {
1306 outState.putString("apps_customize_currentTab", currentTabTag);
1307 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001308 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1309 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312
1313 @Override
1314 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001316
Winson Chunge7a03942011-08-05 15:05:12 -07001317 // Remove all pending runnables
1318 mHandler.removeMessages(ADVANCE_MSG);
1319 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001320 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001321
Winson Chungcd2b0142011-06-08 16:02:26 -07001322 // Stop callbacks from LauncherModel
1323 LauncherApplication app = ((LauncherApplication) getApplication());
1324 mModel.stopLoader();
1325 app.setLauncher(null);
1326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001328 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001330 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001332 mAppWidgetHost = null;
1333
1334 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335
1336 TextKeyListener.getInstance().release();
1337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001340
1341 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001342 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001343
1344 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1345 mWorkspace.removeAllViews();
1346 mWorkspace = null;
1347 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001348
1349 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 }
1351
Adam Cohena9cf38f2011-05-02 15:36:58 -07001352 public DragController getDragController() {
1353 return mDragController;
1354 }
1355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 @Override
1357 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001358 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 super.startActivityForResult(intent, requestCode);
1360 }
1361
Winson Chung70d72102011-08-12 11:24:35 -07001362 /**
1363 * Indicates that we want global search for this activity by setting the globalSearch
1364 * argument for {@link #startSearch} to true.
1365 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001367 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001369
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001370 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001371
Karl Rosaen138a0412009-04-23 19:00:21 -07001372 if (initialQuery == null) {
1373 // Use any text typed in the launcher as the initial query
1374 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001375 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 if (appSearchData == null) {
1377 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001378 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001380 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001381
Karl Rosaen138a0412009-04-23 19:00:21 -07001382 final SearchManager searchManager =
1383 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001384 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001385 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
1387
Winson Chung70d72102011-08-12 11:24:35 -07001388 @Override
1389 public boolean onCreateOptionsMenu(Menu menu) {
1390 if (isWorkspaceLocked()) {
1391 return false;
1392 }
1393
1394 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001395
1396 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1397 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1398 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001399 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1400 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1401 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001402 String helpUrl = getString(R.string.help_url);
1403 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001404 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1405 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1406
Winson Chung70d72102011-08-12 11:24:35 -07001407 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1408 .setIcon(android.R.drawable.ic_menu_gallery)
1409 .setAlphabeticShortcut('W');
1410 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1411 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001412 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001413 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001414 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1415 .setIcon(android.R.drawable.ic_menu_preferences)
1416 .setIntent(settings)
1417 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001418 if (!helpUrl.isEmpty()) {
1419 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1420 .setIcon(android.R.drawable.ic_menu_help)
1421 .setIntent(help)
1422 .setAlphabeticShortcut('H');
1423 }
Winson Chung70d72102011-08-12 11:24:35 -07001424 return true;
1425 }
1426
1427 @Override
1428 public boolean onPrepareOptionsMenu(Menu menu) {
1429 super.onPrepareOptionsMenu(menu);
1430
1431 if (mAppsCustomizeTabHost.isTransitioning()) {
1432 return false;
1433 }
1434 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1435 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1436
1437 return true;
1438 }
1439
1440 @Override
1441 public boolean onOptionsItemSelected(MenuItem item) {
1442 switch (item.getItemId()) {
1443 case MENU_WALLPAPER_SETTINGS:
1444 startWallpaper();
1445 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001446 }
1447
1448 return super.onOptionsItemSelected(item);
1449 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001451 @Override
1452 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001453 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001454 // Use a custom animation for launching search
1455 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001456 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001457 }
1458
Joe Onorato9c1289c2009-08-17 11:03:03 -04001459 public boolean isWorkspaceLocked() {
1460 return mWorkspaceLoading || mWaitingForResult;
1461 }
1462
Michael Jurka0280c3b2010-09-17 15:00:07 -07001463 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001464 mPendingAddInfo.container = ItemInfo.NO_ID;
1465 mPendingAddInfo.screen = -1;
1466 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1467 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1468 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001469 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001470
Michael Jurkaaf442092010-06-10 17:01:57 -07001471 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001472 // TODO: catch bad widget exception when sent
1473 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001474 // TODO: Is this log message meaningful?
1475 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001476 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001477 }
1478
Adam Cohened66b2b2012-01-23 17:28:51 -08001479 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1480 final AppWidgetProviderInfo appWidget = info.info;
1481 Runnable configurationActivity = null;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001482 if (appWidget.configure != null) {
1483 // Launch over to configure widget, if needed
1484 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1485 intent.setComponent(appWidget.configure);
1486 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001487 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001488 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001489 intent.putExtra(InstallWidgetReceiver.
1490 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001491
1492 final String mimeType = info.mimeType;
1493 final ClipData clipData = (ClipData) info.configurationData;
1494 final ClipDescription clipDesc = clipData.getDescription();
1495 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1496 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001497 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001498 final CharSequence stringData = item.getText();
1499 final Uri uriData = item.getUri();
1500 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001501 final String key = InstallWidgetReceiver.
1502 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001503 if (uriData != null) {
1504 intent.putExtra(key, uriData);
1505 } else if (intentData != null) {
1506 intent.putExtra(key, intentData);
1507 } else if (stringData != null) {
1508 intent.putExtra(key, stringData);
1509 }
1510 break;
1511 }
1512 }
1513 }
Winson Chung55cef262010-10-28 14:14:18 -07001514 }
Romain Guy8e633c52010-03-04 12:51:36 -08001515 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001516 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001518 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001520 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001521 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 }
1523 }
Romain Guycbb89e42009-06-08 15:52:54 -07001524
Adam Cohenfbba09b2011-07-18 21:43:05 -07001525 /**
1526 * Process a shortcut drop.
1527 *
1528 * @param componentName The name of the component
1529 * @param screen The screen where it should be added
1530 * @param cell The cell it should be added to, optional
1531 * @param position The location on the screen where it was dropped, optional
1532 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001533 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1534 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001535 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001536 mPendingAddInfo.container = container;
1537 mPendingAddInfo.screen = screen;
1538 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001539
1540 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001541 mPendingAddInfo.cellX = cell[0];
1542 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001543 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001544
1545 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1546 createShortcutIntent.setComponent(componentName);
1547 processShortcut(createShortcutIntent);
1548 }
1549
Adam Cohenfbba09b2011-07-18 21:43:05 -07001550 /**
1551 * Process a widget drop.
1552 *
1553 * @param info The PendingAppWidgetInfo of the widget being added.
1554 * @param screen The screen where it should be added
1555 * @param cell The cell it should be added to, optional
1556 * @param position The location on the screen where it was dropped, optional
1557 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1559 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001560 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 mPendingAddInfo.container = info.container = container;
1562 mPendingAddInfo.screen = info.screen = screen;
1563 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001564 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001565 mPendingAddInfo.cellX = cell[0];
1566 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001567 }
1568
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 AppWidgetHostView hostView = info.boundWidget;
1570 int appWidgetId;
1571 if (hostView != null) {
1572 appWidgetId = hostView.getAppWidgetId();
1573 } else {
1574 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1575 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1576 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001577 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001578
Adam Cohenfbba09b2011-07-18 21:43:05 -07001579 }
1580
Joe Onoratodeb98af2010-02-19 14:59:39 -08001581 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001582 // Handle case where user selected "Applications"
1583 String applicationName = getResources().getString(R.string.group_applications);
1584 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001585
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001586 if (applicationName != null && applicationName.equals(shortcutName)) {
1587 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1588 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001589
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001590 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1591 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001592 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001593 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001594 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001595 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
1598
Winson Chung24ab2f12010-09-16 14:10:47 -07001599 void processWallpaper(Intent intent) {
1600 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1601 }
1602
Winson Chung3d503fb2011-07-13 17:25:49 -07001603 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1604 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001605 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 folderInfo.title = getText(R.string.folder_name);
1607
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001609 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1610 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001611 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
1613 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001614 FolderIcon newFolder =
1615 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1616 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1617 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001618 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
Romain Guycbb89e42009-06-08 15:52:54 -07001620
Joe Onorato9c1289c2009-08-17 11:03:03 -04001621 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001622 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001623 }
1624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001626 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001628 Intent chooser = Intent.createChooser(pickWallpaper,
1629 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001630 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1631 // Removed in Eclair MR1
1632// WallpaperManager wm = (WallpaperManager)
1633// getSystemService(Context.WALLPAPER_SERVICE);
1634// WallpaperInfo wi = wm.getWallpaperInfo();
1635// if (wi != null && wi.getSettingsActivity() != null) {
1636// LabeledIntent li = new LabeledIntent(getPackageName(),
1637// R.string.configure_wallpaper, 0);
1638// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1639// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1640// }
Mike Clerona0618e42009-10-22 13:55:21 -07001641 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 }
1643
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001644 /**
1645 * Registers various content observers. The current implementation registers
1646 * only a favorites observer to keep track of the favorites applications.
1647 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001648 private void registerContentObservers() {
1649 ContentResolver resolver = getContentResolver();
1650 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1651 true, mWidgetObserver);
1652 }
1653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 @Override
1655 public boolean dispatchKeyEvent(KeyEvent event) {
1656 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1657 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001659 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001660 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001661 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001662 dumpState();
1663 return true;
1664 }
1665 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001666 }
1667 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1668 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001669 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 return true;
1671 }
1672 }
1673
1674 return super.dispatchKeyEvent(event);
1675 }
1676
Joe Onorato88ec0992009-11-19 13:16:06 -08001677 @Override
1678 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001679 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001680 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001681 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001682 Folder openFolder = mWorkspace.getOpenFolder();
1683 if (openFolder.isEditingName()) {
1684 openFolder.dismissEditingName();
1685 } else {
1686 closeFolder();
1687 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001688 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001689 mWorkspace.exitWidgetResizeMode();
1690
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001691 // Back button is a no-op here, but give at least some feedback for the button press
1692 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001693 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001694 }
1695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001697 * Re-listen when widgets are reset.
1698 */
1699 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001700 if (mAppWidgetHost != null) {
1701 mAppWidgetHost.startListening();
1702 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001703 }
1704
1705 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001706 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1707 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001709 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001710 if (mModel != null) {
1711 mModel.unbindWorkspaceItems();
1712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001714
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 /**
1716 * Launches the intent referred by the clicked shortcut.
1717 *
1718 * @param v The view representing the clicked shortcut.
1719 */
1720 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001721 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1722 // view has detached (it's possible for this to happen if the view is removed mid touch).
1723 if (v.getWindowToken() == null) {
1724 return;
1725 }
1726
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001727 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001728 return;
1729 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001732 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001734 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001735 int[] pos = new int[2];
1736 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001737 intent.setSourceBounds(new Rect(pos[0], pos[1],
1738 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001739 boolean success = startActivitySafely(intent, tag);
1740
1741 if (success && v instanceof BubbleTextView) {
1742 mWaitingForResume = (BubbleTextView) v;
1743 mWaitingForResume.setStayPressed(true);
1744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001746 if (v instanceof FolderIcon) {
1747 FolderIcon fi = (FolderIcon) v;
1748 handleFolderClick(fi);
1749 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001750 } else if (v == mAllAppsButton) {
1751 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001752 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001753 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001754 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001755 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
1757 }
1758
Michael Jurka0e260592010-06-30 17:07:39 -07001759 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001760 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001761 // clicking anywhere on the workspace causes the customization drawer to slide down
1762 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001763 return false;
1764 }
1765
Michael Jurkaaf442092010-06-10 17:01:57 -07001766 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001767 * Event handler for the search button
1768 *
1769 * @param v The view that was clicked.
1770 */
1771 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001772 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1773
Amith Yamasania135ba82011-08-09 17:42:01 -07001774 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001775 }
1776
1777 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001778 * Event handler for the voice button
1779 *
1780 * @param v The view that was clicked.
1781 */
1782 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001783 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001784
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001785 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001786 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001787 startActivity(intent);
1788 }
1789
1790 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001791 * Event handler for the "grid" button that appears on the home screen, which
1792 * enters all apps mode.
1793 *
1794 * @param v The view that was clicked.
1795 */
1796 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001797 showAllApps(true);
1798 }
1799
1800 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001801 // Provide the same haptic feedback that the system offers for virtual keys.
1802 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001803 }
1804
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001805 public void onClickAppMarketButton(View v) {
1806 if (mAppMarketIntent != null) {
1807 startActivitySafely(mAppMarketIntent, "app market");
1808 }
1809 }
1810
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001811 void startApplicationDetailsActivity(ComponentName componentName) {
1812 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001813 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1814 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001815 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001816 startActivity(intent);
1817 }
1818
Patrick Dubroy5539af72010-09-07 15:22:01 -07001819 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1820 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1821 // System applications cannot be installed. For now, show a toast explaining that.
1822 // We may give them the option of disabling apps this way.
1823 int messageId = R.string.uninstall_system_app_text;
1824 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1825 } else {
1826 String packageName = appInfo.componentName.getPackageName();
1827 String className = appInfo.componentName.getClassName();
1828 Intent intent = new Intent(
1829 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001830 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1831 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001832 startActivity(intent);
1833 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001834 }
1835
Michael Jurkaddd62e92011-02-16 17:49:14 -08001836 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1838 try {
1839 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001840 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 } catch (ActivityNotFoundException e) {
1842 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001843 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 } catch (SecurityException e) {
1845 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001846 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001848 "or use the exported attribute for this activity. "
1849 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001851 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001853
Romain Guy8e633c52010-03-04 12:51:36 -08001854 void startActivityForResultSafely(Intent intent, int requestCode) {
1855 try {
1856 startActivityForResult(intent, requestCode);
1857 } catch (ActivityNotFoundException e) {
1858 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1859 } catch (SecurityException e) {
1860 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1861 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1862 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1863 "or use the exported attribute for this activity.", e);
1864 }
1865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866
Adam Cohena9cf38f2011-05-02 15:36:58 -07001867 private void handleFolderClick(FolderIcon folderIcon) {
1868 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001869 Folder openFolder = mWorkspace.getFolderForTag(info);
1870
1871 // If the folder info reports that the associated folder is open, then verify that
1872 // it is actually opened. There have been a few instances where this gets out of sync.
1873 if (info.opened && openFolder == null) {
1874 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1875 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1876 info.opened = false;
1877 }
1878
Adam Cohena9cf38f2011-05-02 15:36:58 -07001879 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 // Close any open folder
1881 closeFolder();
1882 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001883 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 } else {
1885 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 int folderScreen;
1887 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001888 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 // .. and close it
1890 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001891 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 // Close any folder open on the current screen
1893 closeFolder();
1894 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001895 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 }
1897 }
1898 }
1899 }
1900
Adam Cohen2801caf2011-05-13 20:57:39 -07001901 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001902 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001903 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1904 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1905 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1906
Adam Cohenc51934b2011-07-26 21:07:43 -07001907 FolderInfo info = (FolderInfo) fi.getTag();
1908 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1909 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001910 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1911 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001912 }
1913
Adam Cohen2801caf2011-05-13 20:57:39 -07001914 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1915 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1916 oa.start();
1917 }
1918
1919 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001920 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001921 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1922 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1923 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1924
Adam Cohenc51934b2011-07-26 21:07:43 -07001925 FolderInfo info = (FolderInfo) fi.getTag();
1926 CellLayout cl = null;
1927 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1928 cl = (CellLayout) fi.getParent().getParent();
1929 }
1930
1931 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001932 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1933 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001934 oa.addListener(new AnimatorListenerAdapter() {
1935 @Override
1936 public void onAnimationEnd(Animator animation) {
1937 if (layout != null) {
1938 layout.clearFolderLeaveBehind();
1939 }
1940 }
1941 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001942 oa.start();
1943 }
1944
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001946 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 * is animated relative to the specified View. If the View is null, no animation
1948 * is played.
1949 *
1950 * @param folderInfo The FolderInfo describing the folder to open.
1951 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001952 public void openFolder(FolderIcon folderIcon) {
1953 Folder folder = folderIcon.mFolder;
1954 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955
Adam Cohen2801caf2011-05-13 20:57:39 -07001956 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001957 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958
Adam Cohen4554ee12011-08-03 16:13:21 -07001959 // Just verify that the folder hasn't already been added to the DragLayer.
1960 // There was a one-off crash where the folder had a parent already.
1961 if (folder.getParent() == null) {
1962 mDragLayer.addView(folder);
1963 mDragController.addDropTarget((DropTarget) folder);
1964 } else {
1965 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1966 folder.getParent() + ").");
1967 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001968 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001969 }
1970
1971 public void closeFolder() {
1972 Folder folder = mWorkspace.getOpenFolder();
1973 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001974 if (folder.isEditingName()) {
1975 folder.dismissEditingName();
1976 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001977 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001978
1979 // Dismiss the folder cling
1980 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001981 }
1982 }
1983
1984 void closeFolder(Folder folder) {
1985 folder.getInfo().opened = false;
1986
1987 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1988 if (parent != null) {
1989 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1990 shrinkAndFadeInFolderIcon(fi);
1991 }
1992 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 }
1994
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001996 if (mState != State.WORKSPACE) {
1997 return false;
1998 }
1999
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 return false;
2002 }
2003
2004 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002005 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 }
2007
Michael Jurka0280c3b2010-09-17 15:00:07 -07002008 resetAddInfo();
2009 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002011 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 return true;
2013 }
2014
Winson Chung3d503fb2011-07-13 17:25:49 -07002015 // The hotseat touch handling does not go through Workspace, and we always allow long press
2016 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002017 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002018 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2019 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002020 if (itemUnderLongClick == null) {
2021 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002022 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2023 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002024 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002026 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002028 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
2030 }
2031 }
2032 return true;
2033 }
2034
Winson Chung3d503fb2011-07-13 17:25:49 -07002035 boolean isHotseatLayout(View layout) {
2036 return mHotseat != null && layout != null &&
2037 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2038 }
2039 Hotseat getHotseat() {
2040 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002041 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002042 SearchDropTargetBar getSearchBar() {
2043 return mSearchDropTargetBar;
2044 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002045
Winson Chung3d503fb2011-07-13 17:25:49 -07002046 /**
2047 * Returns the CellLayout of the specified container at the specified screen.
2048 */
2049 CellLayout getCellLayout(long container, int screen) {
2050 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2051 if (mHotseat != null) {
2052 return mHotseat.getLayout();
2053 } else {
2054 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002055 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002056 } else {
2057 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002058 }
2059 }
2060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 Workspace getWorkspace() {
2062 return mWorkspace;
2063 }
2064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 @Override
2066 protected Dialog onCreateDialog(int id) {
2067 switch (id) {
2068 case DIALOG_CREATE_SHORTCUT:
2069 return new CreateShortcut().createDialog();
2070 case DIALOG_RENAME_FOLDER:
2071 return new RenameFolder().createDialog();
2072 }
2073
2074 return super.onCreateDialog(id);
2075 }
2076
2077 @Override
2078 protected void onPrepareDialog(int id, Dialog dialog) {
2079 switch (id) {
2080 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 break;
2082 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002083 if (mFolderInfo != null) {
2084 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2085 final CharSequence text = mFolderInfo.title;
2086 input.setText(text);
2087 input.setSelection(0, text.length());
2088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 break;
2090 }
2091 }
2092
2093 void showRenameDialog(FolderInfo info) {
2094 mFolderInfo = info;
2095 mWaitingForResult = true;
2096 showDialog(DIALOG_RENAME_FOLDER);
2097 }
2098
Adam Cohenfbba09b2011-07-18 21:43:05 -07002099 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002100 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002101 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2102 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 mWaitingForResult = true;
2104 showDialog(DIALOG_CREATE_SHORTCUT);
2105 }
2106
2107 private class RenameFolder {
2108 private EditText mInput;
2109
2110 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2112 mInput = (EditText) layout.findViewById(R.id.folder_name);
2113
2114 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2115 builder.setIcon(0);
2116 builder.setTitle(getString(R.string.rename_folder_title));
2117 builder.setCancelable(true);
2118 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2119 public void onCancel(DialogInterface dialog) {
2120 cleanup();
2121 }
2122 });
2123 builder.setNegativeButton(getString(R.string.cancel_action),
2124 new Dialog.OnClickListener() {
2125 public void onClick(DialogInterface dialog, int which) {
2126 cleanup();
2127 }
2128 }
2129 );
2130 builder.setPositiveButton(getString(R.string.rename_action),
2131 new Dialog.OnClickListener() {
2132 public void onClick(DialogInterface dialog, int which) {
2133 changeFolderName();
2134 }
2135 }
2136 );
2137 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002138
2139 final AlertDialog dialog = builder.create();
2140 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2141 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002142 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002143 mInput.requestFocus();
2144 InputMethodManager inputManager = (InputMethodManager)
2145 getSystemService(Context.INPUT_METHOD_SERVICE);
2146 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002147 }
2148 });
2149
2150 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 }
2152
2153 private void changeFolderName() {
2154 final String name = mInput.getText().toString();
2155 if (!TextUtils.isEmpty(name)) {
2156 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002157 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 mFolderInfo.title = name;
2159 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2160
Joe Onorato9c1289c2009-08-17 11:03:03 -04002161 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002162 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 } else {
2165 final FolderIcon folderIcon = (FolderIcon)
2166 mWorkspace.getViewForTag(mFolderInfo);
2167 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002168 // TODO: At some point we'll probably want some version of setting
2169 // the text for a folder icon.
2170 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 getWorkspace().requestLayout();
2172 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002173 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002174 mWorkspaceLoading = true;
2175 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 }
2177 }
2178 }
2179 cleanup();
2180 }
2181
2182 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 dismissDialog(DIALOG_RENAME_FOLDER);
2184 mWaitingForResult = false;
2185 mFolderInfo = null;
2186 }
2187 }
2188
Daniel Sandler843e8602010-06-07 14:59:01 -04002189 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2190 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002191 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002192 }
2193
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002194 // AllAppsView.Watcher
2195 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002196 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002197 mWorkspace.setVisibility(View.GONE);
2198 }
2199 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002200
2201 /**
2202 * Helper method for the cameraZoomIn/cameraZoomOut animations
2203 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002204 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002205 * @param scaleFactor The scale factor used for the zoom
2206 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002207 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002208 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002209 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002210 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002211
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002212 void updateWallpaperVisibility(boolean visible) {
2213 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2214 int curflags = getWindow().getAttributes().flags
2215 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2216 if (wpflags != curflags) {
2217 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2218 }
2219 }
2220
Michael Jurkabed61d22012-02-14 22:51:29 -08002221 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2222 if (v instanceof LauncherTransitionable) {
2223 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2224 }
Winson Chung70442722012-02-10 15:43:22 -08002225
2226 // Update the workspace transition step as well
2227 dispatchOnLauncherTransitionStep(v, 0f);
2228 }
2229
2230 private void dispatchOnLauncherTransitionStep(View v, float t) {
2231 if (v instanceof LauncherTransitionable) {
2232 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2233 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002234 }
2235
2236 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2237 if (v instanceof LauncherTransitionable) {
2238 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2239 }
Winson Chung70442722012-02-10 15:43:22 -08002240
2241 // Update the workspace transition step as well
2242 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002243 }
2244
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002245 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002246 * Things to test when changing the following seven functions.
2247 * - Home from workspace
2248 * - from center screen
2249 * - from other screens
2250 * - Home from all apps
2251 * - from center screen
2252 * - from other screens
2253 * - Back from all apps
2254 * - from center screen
2255 * - from other screens
2256 * - Launch app from workspace and quit
2257 * - with back
2258 * - with home
2259 * - Launch app from all apps and quit
2260 * - with back
2261 * - with home
2262 * - Go to a screen that's not the default, then all
2263 * apps, and launch and app, and go back
2264 * - with back
2265 * -with home
2266 * - On workspace, long press power and go back
2267 * - with back
2268 * - with home
2269 * - On all apps, long press power and go back
2270 * - with back
2271 * - with home
2272 * - On workspace, power off
2273 * - On all apps, power off
2274 * - Launch an app and turn off the screen while in that app
2275 * - Go back with home key
2276 * - Go back with back key TODO: make this not go to workspace
2277 * - From all apps
2278 * - From workspace
2279 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2280 * - From all apps
2281 * - From the center workspace
2282 * - From another workspace
2283 */
2284
2285 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002286 * Zoom the camera out from the workspace to reveal 'toView'.
2287 * Assumes that the view to show is anchored at either the very top or very bottom
2288 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002289 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002290 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002291 if (mStateAnimation != null) {
2292 mStateAnimation.cancel();
2293 mStateAnimation = null;
2294 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002295 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002296 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002297
Winson Chungf0ea4d32011-06-06 14:27:16 -07002298 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2299 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2300 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002301 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002302 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002303 final int startDelay =
2304 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002305
Michael Jurkab3e22d92011-10-31 15:58:33 -07002306 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002307
Michael Jurkad74c9842011-07-10 12:44:21 -07002308 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002309 Animator workspaceAnim =
2310 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002311
2312 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002313 toView.setScaleX(scale);
2314 toView.setScaleY(scale);
2315 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2316 scaleAnim.
2317 scaleX(1f).scaleY(1f).
2318 setDuration(duration).
2319 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002320
Winson Chungf0ea4d32011-06-06 14:27:16 -07002321 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002322 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002323 final ObjectAnimator alphaAnim = ObjectAnimator
2324 .ofFloat(toView, "alpha", 0f, 1f)
2325 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002326 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002327 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2328 @Override
2329 public void onAnimationUpdate(ValueAnimator animation) {
2330 float t = (Float) animation.getAnimatedValue();
2331 dispatchOnLauncherTransitionStep(fromView, t);
2332 dispatchOnLauncherTransitionStep(toView, t);
2333 }
2334 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002335
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002336 // toView should appear right at the end of the workspace shrink
2337 // animation
2338 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002339 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002340 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002341
2342 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002343 boolean animationCancelled = false;
2344
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002345 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002346 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002347 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002348 // Prepare the position
2349 toView.setTranslationX(0.0f);
2350 toView.setTranslationY(0.0f);
2351 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002352 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002353 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002354 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002355 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002356 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2357 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002358
2359 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2360 // Hide the workspace scrollbar
2361 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002362 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002363 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002364 if (!animationCancelled) {
2365 updateWallpaperVisibility(false);
2366 }
2367 }
2368
Adam Cohencff6af82011-09-13 14:51:53 -07002369 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002370 public void onAnimationCancel(Animator animation) {
2371 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002372 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002373 });
2374
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002375 if (workspaceAnim != null) {
2376 mStateAnimation.play(workspaceAnim);
2377 }
Michael Jurka1899a362011-11-03 13:50:45 -07002378
2379 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002380 final ViewTreeObserver observer;
2381
Michael Jurkabed61d22012-02-14 22:51:29 -08002382 dispatchOnLauncherTransitionStart(fromView, animated, false);
2383 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002384
2385 // If any of the objects being animated haven't been measured/laid out
2386 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002387 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002388 (mWorkspace.getMeasuredWidth() == 0) ||
2389 (toView.getMeasuredWidth() == 0)) {
2390 observer = mWorkspace.getViewTreeObserver();
2391 delayAnim = true;
2392 } else {
2393 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002394 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002395
2396 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002397 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002398 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2399 public void onGlobalLayout() {
2400 mWorkspace.post(new Runnable() {
2401 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002402 // Check that mStateAnimation hasn't changed while
2403 // we waited for a layout pass
2404 if (mStateAnimation == stateAnimation) {
2405 // Need to update pivots for zoom if layout changed
2406 setPivotsForZoom(toView, scale);
2407 mStateAnimation.start();
2408 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002409 }
2410 });
2411 observer.removeGlobalOnLayoutListener(this);
2412 }
2413 };
2414 observer.addOnGlobalLayoutListener(delayedStart);
2415 } else {
2416 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002417 mStateAnimation.start();
2418 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002419 } else {
2420 toView.setTranslationX(0.0f);
2421 toView.setTranslationY(0.0f);
2422 toView.setScaleX(1.0f);
2423 toView.setScaleY(1.0f);
2424 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002425 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002426
Michael Jurkabed61d22012-02-14 22:51:29 -08002427 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2428 // Hide the workspace scrollbar
2429 mWorkspace.hideScrollingIndicator(true);
2430 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002431 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002432 dispatchOnLauncherTransitionStart(fromView, animated, false);
2433 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2434 dispatchOnLauncherTransitionStart(toView, animated, false);
2435 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002436 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002437 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002438 }
2439
2440 /**
2441 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002442 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002443 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002444 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002445 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2446 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002447
Michael Jurkab3e22d92011-10-31 15:58:33 -07002448 if (mStateAnimation != null) {
2449 mStateAnimation.cancel();
2450 mStateAnimation = null;
2451 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002452 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002453
Winson Chungf0ea4d32011-06-06 14:27:16 -07002454 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002455 final int fadeOutDuration =
2456 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002457 final float scaleFactor = (float)
2458 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2459 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002460 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002461 Animator workspaceAnim = null;
2462
2463 if (toState == State.WORKSPACE) {
2464 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2465 workspaceAnim = mWorkspace.getChangeStateAnimation(
2466 Workspace.State.NORMAL, animated, stagger);
2467 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2468 workspaceAnim = mWorkspace.getChangeStateAnimation(
2469 Workspace.State.SPRING_LOADED, animated);
2470 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002471
Michael Jurkab3e22d92011-10-31 15:58:33 -07002472 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002473 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002474 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002475 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002476 final float oldScaleX = fromView.getScaleX();
2477 final float oldScaleY = fromView.getScaleY();
2478
Michael Jurka159b4cc2012-01-17 03:00:35 -08002479 final LauncherViewPropertyAnimator scaleAnim =
2480 new LauncherViewPropertyAnimator(fromView);
2481 scaleAnim.
2482 scaleX(scaleFactor).scaleY(scaleFactor).
2483 setDuration(duration).
2484 setInterpolator(new Workspace.ZoomInInterpolator());
2485
2486 final ObjectAnimator alphaAnim = ObjectAnimator
2487 .ofFloat(fromView, "alpha", 1f, 0f)
2488 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002489 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002490 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2491 @Override
2492 public void onAnimationUpdate(ValueAnimator animation) {
2493 float t = 1f - (Float) animation.getAnimatedValue();
2494 dispatchOnLauncherTransitionStep(fromView, t);
2495 dispatchOnLauncherTransitionStep(toView, t);
2496 }
2497 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002498
Michael Jurkabed61d22012-02-14 22:51:29 -08002499 mStateAnimation = new AnimatorSet();
2500
2501 dispatchOnLauncherTransitionStart(fromView, animated, true);
2502 dispatchOnLauncherTransitionStart(toView, animated, true);
2503
2504 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002505 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002506 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002507 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002508 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002509 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2510 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002511 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002512 if (onCompleteRunnable != null) {
2513 onCompleteRunnable.run();
2514 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002515 }
2516 });
2517
Winson Chungf0ea4d32011-06-06 14:27:16 -07002518 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002519 if (workspaceAnim != null) {
2520 mStateAnimation.play(workspaceAnim);
2521 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002522 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002523 } else {
2524 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002525 dispatchOnLauncherTransitionStart(fromView, animated, true);
2526 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2527 dispatchOnLauncherTransitionStart(toView, animated, true);
2528 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002529 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002530 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002531 }
2532
Michael Jurkae326f182011-11-21 14:05:46 -08002533 @Override
2534 public void onTrimMemory(int level) {
2535 super.onTrimMemory(level);
2536 if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2537 mAppsCustomizeTabHost.onTrimMemory();
2538 }
2539 }
2540
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002541 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002542 showWorkspace(animated, null);
2543 }
2544
2545 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002546 if (mState != State.WORKSPACE) {
2547 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002548 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002549
2550 // Show the search bar and hotseat
2551 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002552 // We only need to animate in the dock divider if we're going from spring loaded mode
2553 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002554
2555 // Set focus to the AppsCustomize button
2556 if (mAllAppsButton != null) {
2557 mAllAppsButton.requestFocus();
2558 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002559 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002560
Michael Jurkab737ee62011-11-15 15:57:22 -08002561 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002562
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002563 // Change the state *after* we've called all the transition code
2564 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002565
Winson Chung5fb63472011-02-02 17:03:37 -08002566 // Resume the auto-advance of widgets
2567 mUserPresent = true;
2568 updateRunning();
2569
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002570 // send an accessibility event to announce the context change
2571 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002572 }
2573
Michael Jurkab3e22d92011-10-31 15:58:33 -07002574 void showAllApps(boolean animated) {
2575 if (mState != State.WORKSPACE) return;
2576
2577 showAppsCustomizeHelper(animated, false);
2578 mAppsCustomizeTabHost.requestFocus();
2579
2580 // Hide the search bar and hotseat
2581 mSearchDropTargetBar.hideSearchBar(animated);
2582
2583 // Change the state *after* we've called all the transition code
2584 mState = State.APPS_CUSTOMIZE;
2585
2586 // Pause the auto-advance of widgets until we are out of AllApps
2587 mUserPresent = false;
2588 updateRunning();
2589 closeFolder();
2590
2591 // Send an accessibility event to announce the context change
2592 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2593 }
2594
Adam Cohen7777d962011-08-18 18:58:38 -07002595 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002596 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002597 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002598 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002599 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002600 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002601 }
Adam Cohen7777d962011-08-18 18:58:38 -07002602
Adam Cohened66b2b2012-01-23 17:28:51 -08002603 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2604 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002605 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2606
Winson Chunge7a03942011-08-05 15:05:12 -07002607 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002608 @Override
2609 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002610 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002611 // Before we show workspace, hide all apps again because
2612 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2613 // clean up our state transition functions
2614 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002615 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002616 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002617 } else {
2618 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002619 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002620 }
2621 }, (extendedDelay ?
2622 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2623 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2624 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002625
Michael Jurkad3ef3062010-11-23 16:23:58 -08002626 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002627 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002628 final boolean animated = true;
2629 final boolean springLoaded = true;
2630 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002631 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002632 }
2633 // Otherwise, we are not in spring loaded mode, so don't do anything.
2634 }
2635
Michael Jurkab737ee62011-11-15 15:57:22 -08002636 void hideDockDivider() {
2637 if (mQsbDivider != null && mDockDivider != null) {
2638 mQsbDivider.setVisibility(View.INVISIBLE);
2639 mDockDivider.setVisibility(View.INVISIBLE);
2640 }
2641 }
2642
2643 void showDockDivider(boolean animated) {
2644 if (mQsbDivider != null && mDockDivider != null) {
2645 mQsbDivider.setVisibility(View.VISIBLE);
2646 mDockDivider.setVisibility(View.VISIBLE);
2647 if (mDividerAnimator != null) {
2648 mDividerAnimator.cancel();
2649 mQsbDivider.setAlpha(1f);
2650 mDockDivider.setAlpha(1f);
2651 mDividerAnimator = null;
2652 }
2653 if (animated) {
2654 mDividerAnimator = new AnimatorSet();
2655 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2656 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2657 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2658 mDividerAnimator.start();
2659 }
2660 }
2661 }
2662
Michael Jurkab3e22d92011-10-31 15:58:33 -07002663 void lockAllApps() {
2664 // TODO
2665 }
2666
2667 void unlockAllApps() {
2668 // TODO
2669 }
2670
Adam Cohenfc53cd22011-07-20 15:45:11 -07002671 public boolean isAllAppsCustomizeOpen() {
2672 return mState == State.APPS_CUSTOMIZE;
2673 }
2674
Winson Chungf0ea4d32011-06-06 14:27:16 -07002675 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002676 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002677 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002678 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002679 if (!LauncherApplication.isScreenLarge()) {
2680 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002681 if (mHotseat.getAlpha() != 1f) {
2682 int duration = mSearchDropTargetBar.getTransitionInDuration();
2683 mHotseat.animate().alpha(1f).setDuration(duration);
2684 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002685 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002686 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002687 }
2688 }
2689 }
2690
2691 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002692 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002693 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002694 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002695 if (!LauncherApplication.isScreenLarge()) {
2696 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002697 if (mHotseat.getAlpha() != 0f) {
2698 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2699 mHotseat.animate().alpha(0f).setDuration(duration);
2700 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002701 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002702 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002703 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002704 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002705 }
2706
Patrick Dubroy5f445422011-02-18 14:35:21 -08002707 /**
2708 * Add an item from all apps or customize onto the given workspace screen.
2709 * If layout is null, add to the current screen.
2710 */
2711 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002712 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2713 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002714 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002715 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002716
Winson Chungdff8ebb2011-09-08 17:25:31 -07002717 /** Maps the current orientation to an index for referencing orientation correct global icons */
2718 private int getCurrentOrientationIndexForGlobalIcons() {
2719 // default - 0, landscape - 1
2720 switch (getResources().getConfiguration().orientation) {
2721 case Configuration.ORIENTATION_LANDSCAPE:
2722 return 1;
2723 default:
2724 return 0;
2725 }
2726 }
2727
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002728 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002729 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002730 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002731 // Look for the toolbar icon specified in the activity meta-data
2732 Bundle metaData = packageManager.getActivityInfo(
2733 activityName, PackageManager.GET_META_DATA).metaData;
2734 if (metaData != null) {
2735 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2736 if (iconResId != 0) {
2737 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002738 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002739 }
2740 }
2741 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002742 // This can happen if the activity defines an invalid drawable
2743 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2744 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002745 } catch (Resources.NotFoundException nfe) {
2746 // This can happen if the activity defines an invalid drawable
2747 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2748 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002749 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002750 return null;
2751 }
2752
2753 // if successful in getting icon, return it; otherwise, set button to use default drawable
2754 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2755 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002756 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002757 Resources r = getResources();
2758 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2759 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002760
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002761 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002762 // If we were unable to find the icon via the meta-data, use a generic one
2763 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002764 toolbarIcon = r.getDrawable(fallbackDrawableId);
2765 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002766 if (button != null) {
2767 button.setCompoundDrawables(toolbarIcon, null, null, null);
2768 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002769 return null;
2770 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002771 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002772 if (button != null) {
2773 button.setCompoundDrawables(toolbarIcon, null, null, null);
2774 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002775 return toolbarIcon.getConstantState();
2776 }
2777 }
2778
2779 // if successful in getting icon, return it; otherwise, set button to use default drawable
2780 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2781 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2782 ImageView button = (ImageView) findViewById(buttonId);
2783 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2784
Michael Jurka19e0fc52011-07-22 18:00:21 -07002785 if (button != null) {
2786 // If we were unable to find the icon via the meta-data, use a
2787 // generic one
2788 if (toolbarIcon == null) {
2789 button.setImageResource(fallbackDrawableId);
2790 } else {
2791 button.setImageDrawable(toolbarIcon);
2792 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002793 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002794
2795 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2796
Michael Jurka0423dcf2010-10-05 14:56:18 -07002797 }
2798
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002799 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2800 TextView button = (TextView) findViewById(buttonId);
2801 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2802 }
2803
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002804 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002805 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002806 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002807 }
2808
Winson Chungbb185bd2011-11-21 12:31:42 -08002809 private void invalidatePressedFocusedStates(View container, View button) {
2810 if (container instanceof HolographicLinearLayout) {
2811 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2812 layout.invalidatePressedFocusedStates();
2813 } else if (button instanceof HolographicImageView) {
2814 HolographicImageView view = (HolographicImageView) button;
2815 view.invalidatePressedFocusedStates();
2816 }
2817 }
2818
Winson Chungc51db6a2011-10-05 11:44:49 -07002819 private boolean updateGlobalSearchIcon() {
2820 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002821 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2822 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002823 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002824 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002825
Winson Chung1cad91e2011-05-25 17:41:01 -07002826 final SearchManager searchManager =
2827 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2828 ComponentName activityName = searchManager.getGlobalSearchActivity();
2829 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002830 int coi = getCurrentOrientationIndexForGlobalIcons();
2831 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002832 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002833 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002834 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2835 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002836 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002837 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002838 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002839 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002840 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002841 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2842 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2843 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002844 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002845 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002846 }
2847 }
2848
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002849 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002850 final View searchButtonContainer = findViewById(R.id.search_button_container);
2851 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002852 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002853 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002854 }
2855
Winson Chungc51db6a2011-10-05 11:44:49 -07002856 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002857 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002858 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002859 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002860
Winson Chungc51db6a2011-10-05 11:44:49 -07002861 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002862 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2863 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002864 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002865 int coi = getCurrentOrientationIndexForGlobalIcons();
2866 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002867 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002868 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002869 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002870 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002871 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002872 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002873 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002874 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002875 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002876 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002877 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002878 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002879 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002880
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002881 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002882 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2883 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002884 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002885 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002886 }
2887
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002888 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002889 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002890 */
2891 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002892 final View marketButton = findViewById(R.id.market_button);
2893 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2894 // Find the app market activity by resolving an intent.
2895 // (If multiple app markets are installed, it will return the ResolverActivity.)
2896 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002897 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002898 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002899 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002900 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002901 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002902 marketButton.setVisibility(View.VISIBLE);
2903 } else {
2904 // We should hide and disable the view so that we don't try and restore the visibility
2905 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2906 marketButton.setVisibility(View.GONE);
2907 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002908 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002909 }
2910
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002911 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002912 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002913 }
2914
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002915 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 * Displays the shortcut creation dialog and launches, if necessary, the
2917 * appropriate activity.
2918 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002919 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002920 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2921 DialogInterface.OnShowListener {
2922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002924
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002925 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002927
Winson Chung55b65502011-05-26 12:03:43 -07002928 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2929 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002930 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 AlertDialog dialog = builder.create();
2933 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002934 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002935 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 return dialog;
2938 }
2939
2940 public void onCancel(DialogInterface dialog) {
2941 mWaitingForResult = false;
2942 cleanup();
2943 }
2944
Romain Guycbb89e42009-06-08 15:52:54 -07002945 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002946 mWaitingForResult = false;
2947 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002948 }
2949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002951 try {
2952 dismissDialog(DIALOG_CREATE_SHORTCUT);
2953 } catch (Exception e) {
2954 // An exception is thrown if the dialog is not visible, which is fine
2955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 }
2957
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002958 /**
2959 * Handle the action clicked in the "Add to home" dialog.
2960 */
2961 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002962 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002963
Winson Chung55b65502011-05-26 12:03:43 -07002964 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2965 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002966 case AddAdapter.ITEM_APPLICATION: {
2967 if (mAppsCustomizeTabHost != null) {
2968 mAppsCustomizeTabHost.selectAppsTab();
2969 }
2970 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002971 break;
2972 }
Winson Chung55b65502011-05-26 12:03:43 -07002973 case AddAdapter.ITEM_APPWIDGET: {
2974 if (mAppsCustomizeTabHost != null) {
2975 mAppsCustomizeTabHost.selectWidgetsTab();
2976 }
2977 showAllApps(true);
2978 break;
2979 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002980 case AddAdapter.ITEM_WALLPAPER: {
2981 startWallpaper();
2982 break;
2983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984 }
2985 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002986
2987 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002988 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002990 }
2991
2992 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002993 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002994 */
2995 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2996 @Override
2997 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002998 closeSystemDialogs();
2999 }
3000 }
3001
3002 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003003 * Receives notifications whenever the appwidgets are reset.
3004 */
3005 private class AppWidgetResetObserver extends ContentObserver {
3006 public AppWidgetResetObserver() {
3007 super(new Handler());
3008 }
3009
3010 @Override
3011 public void onChange(boolean selfChange) {
3012 onAppWidgetReset();
3013 }
3014 }
3015
3016 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003017 * If the activity is currently paused, signal that we need to re-run the loader
3018 * in onResume.
3019 *
3020 * This needs to be called from incoming places where resources might have been loaded
3021 * while we are paused. That is becaues the Configuration might be wrong
3022 * when we're not running, and if it comes back to what it was when we
3023 * were paused, we are not restarted.
3024 *
3025 * Implementation of the method from LauncherModel.Callbacks.
3026 *
3027 * @return true if we are currently paused. The caller might be able to
3028 * skip some work in that case since we will come back again.
3029 */
3030 public boolean setLoadOnResume() {
3031 if (mPaused) {
3032 Log.i(TAG, "setLoadOnResume");
3033 mOnResumeNeedsLoad = true;
3034 return true;
3035 } else {
3036 return false;
3037 }
3038 }
3039
3040 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003041 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003043 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003044 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003045 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003046 } else {
3047 return SCREEN_COUNT / 2;
3048 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003050
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003051
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 /**
3053 * Refreshes the shortcuts shown on the workspace.
3054 *
3055 * Implementation of the method from LauncherModel.Callbacks.
3056 */
3057 public void startBinding() {
3058 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003059
3060 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003061 int count = workspace.getChildCount();
3062 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003063 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003064 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3065 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003066 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003067 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003068 if (mHotseat != null) {
3069 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003070 }
3071 }
3072
3073 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003074 * Bind the items start-end from the list.
3075 *
3076 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003078 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003079 setLoadOnResume();
3080
Joe Onorato9c1289c2009-08-17 11:03:03 -04003081 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003082 for (int i=start; i<end; i++) {
3083 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003084
3085 // Short circuit if we are loading dock items for a configuration which has no dock
3086 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3087 mHotseat == null) {
3088 continue;
3089 }
3090
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 switch (item.itemType) {
3092 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3093 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07003094 View shortcut = createShortcut((ShortcutInfo)item);
3095 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3096 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003097 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003098 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003099 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003100 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003101 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003102 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3103 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003107 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108 }
3109
Joe Onorato9c1289c2009-08-17 11:03:03 -04003110 /**
3111 * Implementation of the method from LauncherModel.Callbacks.
3112 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003113 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003114 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003115 sFolders.clear();
3116 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003118
3119 /**
3120 * Add the views for a widget to the workspace.
3121 *
3122 * Implementation of the method from LauncherModel.Callbacks.
3123 */
3124 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003125 setLoadOnResume();
3126
Daniel Sandler843e8602010-06-07 14:59:01 -04003127 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3128 if (DEBUG_WIDGETS) {
3129 Log.d(TAG, "bindAppWidget: " + item);
3130 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003131 final Workspace workspace = mWorkspace;
3132
3133 final int appWidgetId = item.appWidgetId;
3134 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003135 if (DEBUG_WIDGETS) {
3136 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3137 }
3138
Joe Onorato9c1289c2009-08-17 11:03:03 -04003139 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3140
Joe Onorato9c1289c2009-08-17 11:03:03 -04003141 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3142 item.hostView.setTag(item);
3143
Winson Chung3d503fb2011-07-13 17:25:49 -07003144 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145 item.cellY, item.spanX, item.spanY, false);
3146
Adam Cohended9f8d2010-11-03 13:25:16 -07003147 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3148
Joe Onorato9c1289c2009-08-17 11:03:03 -04003149 workspace.requestLayout();
3150
Daniel Sandler843e8602010-06-07 14:59:01 -04003151 if (DEBUG_WIDGETS) {
3152 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3153 + (SystemClock.uptimeMillis()-start) + "ms");
3154 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003155 }
3156
3157 /**
3158 * Callback saying that there aren't any more items to bind.
3159 *
3160 * Implementation of the method from LauncherModel.Callbacks.
3161 */
3162 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003163 setLoadOnResume();
3164
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165 if (mSavedState != null) {
3166 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003167 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003169 mSavedState = null;
3170 }
3171
3172 if (mSavedInstanceState != null) {
3173 super.onRestoreInstanceState(mSavedInstanceState);
3174 mSavedInstanceState = null;
3175 }
3176
Joe Onorato9c1289c2009-08-17 11:03:03 -04003177 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003178
3179 // If we received the result of any pending adds while the loader was running (e.g. the
3180 // widget configuration forced an orientation change), process them now.
3181 for (int i = 0; i < sPendingAddList.size(); i++) {
3182 completeAdd(sPendingAddList.get(i));
3183 }
3184 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003185
Winson Chungc51db6a2011-10-05 11:44:49 -07003186 // Update the market app icon as necessary (the other icons will be managed in response to
3187 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003188 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003189
Michael Jurka6ee21d22012-02-21 18:20:27 -08003190 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003191 }
3192
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003193 @Override
3194 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003195 boolean searchVisible = updateGlobalSearchIcon();
3196 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3197 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003198 }
3199
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003200 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003201 * Add the icons for all apps.
3202 *
3203 * Implementation of the method from LauncherModel.Callbacks.
3204 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003205 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3206 // Remove the progress bar entirely; we could also make it GONE
3207 // but better to remove it since we know it's not going to be used
3208 View progressBar = mAppsCustomizeTabHost.
3209 findViewById(R.id.apps_customize_progress_bar);
3210 if (progressBar != null) {
3211 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003212 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003213 // We just post the call to setApps so the user sees the progress bar
3214 // disappear-- otherwise, it just looks like the progress bar froze
3215 // which doesn't look great
3216 mAppsCustomizeTabHost.post(new Runnable() {
3217 public void run() {
3218 if (mAppsCustomizeContent != null) {
3219 mAppsCustomizeContent.setApps(apps);
3220 }
3221 }
3222 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003223 }
3224
3225 /**
3226 * A package was installed.
3227 *
3228 * Implementation of the method from LauncherModel.Callbacks.
3229 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003230 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003231 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003232 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003233
Winson Chung785d2eb2011-04-14 16:08:02 -07003234 if (mAppsCustomizeContent != null) {
3235 mAppsCustomizeContent.addApps(apps);
3236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 }
3238
3239 /**
3240 * A package was updated.
3241 *
3242 * Implementation of the method from LauncherModel.Callbacks.
3243 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003244 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003245 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003246 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003247 if (mWorkspace != null) {
3248 mWorkspace.updateShortcuts(apps);
3249 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003250
Winson Chung785d2eb2011-04-14 16:08:02 -07003251 if (mAppsCustomizeContent != null) {
3252 mAppsCustomizeContent.updateApps(apps);
3253 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003254 }
3255
3256 /**
3257 * A package was uninstalled.
3258 *
3259 * Implementation of the method from LauncherModel.Callbacks.
3260 */
Joe Onorato36115782010-06-17 13:28:48 -04003261 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003262 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003263 if (permanent) {
3264 mWorkspace.removeItems(apps);
3265 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003266
Winson Chung785d2eb2011-04-14 16:08:02 -07003267 if (mAppsCustomizeContent != null) {
3268 mAppsCustomizeContent.removeApps(apps);
3269 }
Winson Chunga1820962011-10-03 16:31:06 -07003270
3271 // Notify the drag controller
3272 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003273 }
Joe Onoratobe386092009-11-17 17:32:16 -08003274
3275 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003276 * A number of packages were updated.
3277 */
3278 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003279 if (mAppsCustomizeContent != null) {
3280 mAppsCustomizeContent.onPackagesUpdated();
3281 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003282 }
3283
Winson Chung400438b2011-01-16 17:53:48 -08003284 private int mapConfigurationOriActivityInfoOri(int configOri) {
3285 final Display d = getWindowManager().getDefaultDisplay();
3286 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3287 switch (d.getRotation()) {
3288 case Surface.ROTATION_0:
3289 case Surface.ROTATION_180:
3290 // We are currently in the same basic orientation as the natural orientation
3291 naturalOri = configOri;
3292 break;
3293 case Surface.ROTATION_90:
3294 case Surface.ROTATION_270:
3295 // We are currently in the other basic orientation to the natural orientation
3296 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3297 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3298 break;
3299 }
3300
3301 int[] oriMap = {
3302 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3303 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3304 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3305 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3306 };
3307 // Since the map starts at portrait, we need to offset if this device's natural orientation
3308 // is landscape.
3309 int indexOffset = 0;
3310 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3311 indexOffset = 1;
3312 }
3313 return oriMap[(d.getRotation() + indexOffset) % 4];
3314 }
Adam Cohen446e9402011-09-15 18:21:21 -07003315
3316 public void lockScreenOrientationOnLargeUI() {
3317 if (LauncherApplication.isScreenLarge()) {
3318 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3319 .getConfiguration().orientation));
3320 }
Winson Chung400438b2011-01-16 17:53:48 -08003321 }
Adam Cohen446e9402011-09-15 18:21:21 -07003322 public void unlockScreenOrientationOnLargeUI() {
3323 if (LauncherApplication.isScreenLarge()) {
3324 mHandler.postDelayed(new Runnable() {
3325 public void run() {
3326 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3327 }
3328 }, mRestoreScreenOrientationDelay);
3329 }
Winson Chung400438b2011-01-16 17:53:48 -08003330 }
3331
Winson Chung82f55532011-08-09 14:14:23 -07003332 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003333 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3334 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003335 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003336 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003337
Winson Chung7d7541e2011-09-16 20:14:36 -07003338 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003339 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003340 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3341 Cling cling = (Cling) findViewById(clingId);
3342 if (cling != null) {
3343 cling.init(this, positionData);
3344 cling.setVisibility(View.VISIBLE);
3345 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3346 if (animate) {
3347 cling.buildLayer();
3348 cling.setAlpha(0f);
3349 cling.animate()
3350 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003351 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003352 .setDuration(SHOW_CLING_DURATION)
3353 .setStartDelay(delay)
3354 .start();
3355 } else {
3356 cling.setAlpha(1f);
3357 }
3358 }
3359 return cling;
3360 }
3361 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003362 if (cling != null) {
3363 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003364 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003365 anim.addListener(new AnimatorListenerAdapter() {
3366 public void onAnimationEnd(Animator animation) {
3367 cling.setVisibility(View.GONE);
3368 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003369 // We should update the shared preferences on a background thread
3370 new Thread("dismissClingThread") {
3371 public void run() {
3372 SharedPreferences.Editor editor = mSharedPrefs.edit();
3373 editor.putBoolean(flag, true);
3374 editor.commit();
3375 }
3376 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003377 };
3378 });
3379 anim.start();
3380 }
3381 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003382 private void removeCling(int id) {
3383 final View cling = findViewById(id);
3384 if (cling != null) {
3385 final ViewGroup parent = (ViewGroup) cling.getParent();
3386 parent.post(new Runnable() {
3387 @Override
3388 public void run() {
3389 parent.removeView(cling);
3390 }
3391 });
3392 }
3393 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003394 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003395 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003396 if (isClingsEnabled() &&
3397 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003398 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003399 } else {
3400 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003401 }
3402 }
3403 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003404 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003405 if (isClingsEnabled() &&
3406 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003407 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003408 } else {
3409 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003410 }
3411 }
3412 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003413 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003414 if (isClingsEnabled() &&
3415 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3416 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003417 } else {
3418 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003419 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003420 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003421 }
3422 public boolean isFolderClingVisible() {
3423 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003424 if (cling != null) {
3425 return cling.getVisibility() == View.VISIBLE;
3426 }
3427 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003428 }
Winson Chung82f55532011-08-09 14:14:23 -07003429 public void dismissWorkspaceCling(View v) {
3430 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003431 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003432 }
3433 public void dismissAllAppsCling(View v) {
3434 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003435 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3436 }
3437 public void dismissFolderCling(View v) {
3438 Cling cling = (Cling) findViewById(R.id.folder_cling);
3439 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003440 }
3441
Winson Chung80baf5a2010-08-09 16:03:15 -07003442 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003443 * Prints out out state for debugging.
3444 */
3445 public void dumpState() {
3446 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003447 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003448 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3449 Log.d(TAG, "mRestoring=" + mRestoring);
3450 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3451 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003452 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003453 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003454
Winson Chung785d2eb2011-04-14 16:08:02 -07003455 if (mAppsCustomizeContent != null) {
3456 mAppsCustomizeContent.dumpState();
3457 }
Joe Onoratobe386092009-11-17 17:32:16 -08003458 Log.d(TAG, "END launcher2 dump state");
3459 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003460
3461 @Override
3462 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3463 super.dump(prefix, fd, writer, args);
3464 writer.println(" ");
3465 writer.println("Debug logs: ");
3466 for (int i = 0; i < sDumpLogs.size(); i++) {
3467 writer.println(" " + sDumpLogs.get(i));
3468 }
3469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003470}
Michael Jurka2763be32011-02-24 11:19:57 -08003471
Michael Jurkaabded662011-03-04 12:06:57 -08003472interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003473 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003474 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003475 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003476 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003477}