blob: 695dfd20e8307cc5af49b3009b4c7cfdd68fa45e [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
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
Bjorn Bringert85f418d2013-09-06 12:50:05 +010048import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070054import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070072import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070090import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070091import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700106import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700108import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.IOException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.io.InputStream;
111import java.io.OutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700115import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700118import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120/**
121 * Default launcher application.
122 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100123public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700124 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700125 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800126 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700127 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Daniel Sandlerf061f822013-06-27 13:59:36 -0400129 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400130 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700131 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400132 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700133 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chung2672ff92012-05-04 16:22:30 -0700161 // The Intent extra that defines whether to ignore the launch animation
162 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400163 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
166 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700167 // Type: int
168 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
171 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: boolean
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
179 // Type: long
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
185 // Type: parcelable
186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100188 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700189 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700191 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100192 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700193
Adam Cohen39a06042013-07-19 14:30:12 -0700194 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700195 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700196
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700197 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700198 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700199 private State mState = State.WORKSPACE;
200 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700203 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
204 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chungaf40f202013-09-18 18:26:31 -0700205 private static final int SHOW_CLING_DURATION = 250;
206 private static final int DISMISS_CLING_DURATION = 200;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800209 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
212 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700213 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700214
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800215 private final BroadcastReceiver mCloseSystemDialogsReceiver
216 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800217 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private LayoutInflater mInflater;
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700222 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800223 private DragLayer mDragLayer;
224 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700225 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700226
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700227 private AppWidgetManager mAppWidgetManager;
228 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700229
Michael Jurkac9d95c52011-08-29 14:03:34 -0700230 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700231 private AppWidgetProviderInfo mPendingAddWidgetInfo;
232
Michael Jurka0280c3b2010-09-17 15:00:07 -0700233 private int[] mTmpAddItemCellCoordinates = new int[2];
234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private FolderInfo mFolderInfo;
236
Winson Chung3d503fb2011-07-13 17:25:49 -0700237 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700238 private View mOverviewPanel;
239
Michael Jurka838a4ca2011-02-07 13:33:06 -0800240 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700241
Winson Chungc51db6a2011-10-05 11:44:49 -0700242 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700243 private AppsCustomizeTabHost mAppsCustomizeTabHost;
244 private AppsCustomizePagedView mAppsCustomizeContent;
245 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100246 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700249 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
250 // scroll issues (because the workspace may not have been measured yet) and extra work.
251 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
252 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253
254 private SpannableStringBuilder mDefaultKeySsb = null;
255
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 private boolean mWorkspaceLoading = true;
257
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800258 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 private boolean mRestoring;
260 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700261 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
Michael Jurka1e2f4652013-07-08 18:03:46 -0700263 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700264 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
265
Winson Chung4a2afa32012-07-19 14:53:05 -0700266 // Keep track of whether the user has left launcher
267 private static boolean sPausedFromUserAction = false;
268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private Bundle mSavedInstanceState;
270
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800272 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800273 private boolean mUserPresent = true;
274 private boolean mVisible = false;
275 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700276 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700278 private static LocaleConfiguration sLocaleConfiguration = null;
279
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700280 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700281
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700282 private Intent mAppMarketIntent = null;
283
Adam Cohended9f8d2010-11-03 13:25:16 -0700284 // Related to the auto-advancing of widgets
285 private final int ADVANCE_MSG = 1;
286 private final int mAdvanceInterval = 20000;
287 private final int mAdvanceStagger = 250;
288 private long mAutoAdvanceSentTime;
289 private long mAutoAdvanceTimeLeft = -1;
290 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
291 new HashMap<View, AppWidgetProviderInfo>();
292
Winson Chung400438b2011-01-16 17:53:48 -0800293 // Determines how long to wait after a rotation before restoring the screen orientation to
294 // match the sensor state.
295 private final int mRestoreScreenOrientationDelay = 500;
296
Michael Jurka4ef207b2010-11-29 17:05:45 -0800297 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700298 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
299 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
300 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800301
Craig Mautner360310b2012-10-26 15:13:08 -0700302 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700303
Adam Cohen1462de32012-07-24 22:34:36 -0700304 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
305
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700306 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700307 static Date sDateStamp = new Date();
308 static DateFormat sDateFormat =
309 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
310 static long sRunStart = System.currentTimeMillis();
311 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700312
Winson Chung46353de2012-02-16 14:05:10 -0800313 // We only want to get the SharedPreferences once since it does an FS stat each time we get
314 // it from the context.
315 private SharedPreferences mSharedPrefs;
316
Adam Cohene25af792013-06-06 23:08:25 -0700317 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
318
Winson Chungf0c6ae02012-03-21 16:10:31 -0700319 // Holds the page that we need to animate to, and the icon views that we need to animate up
320 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700321 private ImageView mFolderIconImageView;
322 private Bitmap mFolderIconBitmap;
323 private Canvas mFolderIconCanvas;
324 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700325
Michael Jurkaddd62e92011-02-16 17:49:14 -0800326 private BubbleTextView mWaitingForResume;
327
Michael Jurka22143132012-10-04 17:42:38 +0200328 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
329 = new HideFromAccessibilityHelper();
330
Michael Jurkac1f5d262011-09-30 19:32:27 -0700331 private Runnable mBuildLayersRunnable = new Runnable() {
332 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700333 if (mWorkspace != null) {
334 mWorkspace.buildPageHardwareLayers();
335 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700336 }
337 };
338
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800339 private static ArrayList<PendingAddArguments> sPendingAddList
340 = new ArrayList<PendingAddArguments>();
341
Michael Jurka0a457bf2012-11-19 14:05:05 -0800342 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
343
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800344 private static class PendingAddArguments {
345 int requestCode;
346 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700347 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700348 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800349 int cellX;
350 int cellY;
351 }
352
Daniel Sandlerff02d492013-08-05 02:12:05 -0400353 private Stats mStats;
354
Michael Jurka0a457bf2012-11-19 14:05:05 -0800355 private static boolean isPropertyEnabled(String propertyName) {
356 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700357 }
358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 @Override
360 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700361 if (DEBUG_STRICT_MODE) {
362 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
363 .detectDiskReads()
364 .detectDiskWrites()
365 .detectNetwork() // or .detectAll() for all detectable problems
366 .penaltyLog()
367 .build());
368 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
369 .detectLeakedSqlLiteObjects()
370 .detectLeakedClosableObjects()
371 .penaltyLog()
372 .penaltyDeath()
373 .build());
374 }
375
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400377
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400378 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400379 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700380
381 // Determine the dynamic grid properties
382 Point smallestSize = new Point();
383 Point largestSize = new Point();
384 Point realSize = new Point();
385 Display display = getWindowManager().getDefaultDisplay();
386 display.getCurrentSizeRange(smallestSize, largestSize);
387 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700388 DisplayMetrics dm = new DisplayMetrics();
389 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700390 // Lazy-initialize the dynamic grid
391 DeviceProfile grid = app.initDynamicGrid(this,
392 Math.min(smallestSize.x, smallestSize.y),
393 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700394 realSize.x, realSize.y,
395 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700396
397 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400398 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700399 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800400 mModel = app.setLauncher(this);
401 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400402 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700404
Daniel Sandlerff02d492013-08-05 02:12:05 -0400405 mStats = new Stats(this);
406
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700407 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700408 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
409 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700410
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700411 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
412 // this also ensures that any synchronous binding below doesn't re-trigger another
413 // LauncherModel load.
414 mPaused = false;
415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200417 android.os.Debug.startMethodTracing(
418 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 }
420
421 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 setContentView(R.layout.launcher);
423 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100424 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800426 registerContentObservers();
427
Joe Onorato7c312c12009-08-13 21:36:53 -0700428 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 mSavedState = savedInstanceState;
431 restoreState(mSavedState);
432
Winson Chunga12a2502010-12-20 14:41:35 -0800433 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700434 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200435 mAppsCustomizeContent.onPackagesUpdated(
436 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700437 }
Winson Chunga12a2502010-12-20 14:41:35 -0800438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 if (PROFILE_STARTUP) {
440 android.os.Debug.stopMethodTracing();
441 }
442
443 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700444 if (sPausedFromUserAction) {
445 // If the user leaves launcher, then we should just load items asynchronously when
446 // they return.
447 mModel.startLoader(true, -1);
448 } else {
449 // We only load the page synchronously if the user rotates (or triggers a
450 // configuration change) while launcher is in the foreground
451 mModel.startLoader(true, mWorkspace.getCurrentPage());
452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454
455 // For handling default keys
456 mDefaultKeySsb = new SpannableStringBuilder();
457 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800458
459 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
460 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800461
Adam Cohenf9426d52012-06-04 17:26:21 -0700462 updateGlobalIcons();
463
464 // On large interfaces, we want the screen to auto-rotate based on the current orientation
465 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700466
467 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700468 }
469
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 protected void onUserLeaveHint() {
471 super.onUserLeaveHint();
472 sPausedFromUserAction = true;
473 }
474
Winson Chung98ca0f72013-07-29 12:58:51 -0700475 /** To be overriden by subclasses to hint to Launcher that we have custom content */
476 protected boolean hasCustomContentToLeft() {
477 return false;
478 }
479
Dave Hawkeya8881582013-09-17 15:55:33 -0600480 /**
481 * To be overridden by subclasses to create the custom content and call
482 * {@link #addToCustomContentPage}. This will only be invoked if
483 * {@link #hasCustomContentToLeft()} is {@code true}.
484 */
485 protected void addCustomContentToLeft() {
486 }
487
488 /**
489 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
490 * ensure the custom content page is added or removed if necessary.
491 */
492 protected void invalidateHasCustomContentToLeft() {
493 if (mWorkspace.getScreenOrder().isEmpty()) {
494 // Not bound yet, wait for bindScreens to be called.
495 return;
496 }
497
498 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
499 // Create the custom content page and call the subclass to populate it.
500 mWorkspace.createCustomContentPage();
501 addCustomContentToLeft();
502 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
503 mWorkspace.removeCustomContentPage();
504 }
505 }
506
Adam Cohenf9426d52012-06-04 17:26:21 -0700507 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700508 boolean searchVisible = false;
509 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800510 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700511 int coi = getCurrentOrientationIndexForGlobalIcons();
512 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
513 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700514 updateAppMarketIcon();
515 searchVisible = updateGlobalSearchIcon();
516 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700517 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700518 if (sGlobalSearchIcon[coi] != null) {
519 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700520 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700521 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700522 if (sVoiceSearchIcon[coi] != null) {
523 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700524 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700525 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700526 if (sAppMarketIcon[coi] != null) {
527 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800528 }
Winson Chungadf0c182012-08-23 14:59:07 -0700529 if (mSearchDropTargetBar != null) {
530 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
531 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 }
Romain Guycbb89e42009-06-08 15:52:54 -0700533
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700535 if (sLocaleConfiguration == null) {
536 new AsyncTask<Void, Void, LocaleConfiguration>() {
537 @Override
538 protected LocaleConfiguration doInBackground(Void... unused) {
539 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
540 readConfiguration(Launcher.this, localeConfiguration);
541 return localeConfiguration;
542 }
543
544 @Override
545 protected void onPostExecute(LocaleConfiguration result) {
546 sLocaleConfiguration = result;
547 checkForLocaleChange(); // recursive, but now with a locale configuration
548 }
549 }.execute();
550 return;
551 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 final Configuration configuration = getResources().getConfiguration();
554
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700555 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 final String locale = configuration.locale.toString();
557
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700558 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 final int mcc = configuration.mcc;
560
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700561 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 final int mnc = configuration.mnc;
563
Romain Guy84f296c2009-11-04 15:00:44 -0800564 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565
Romain Guy84f296c2009-11-04 15:00:44 -0800566 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700567 sLocaleConfiguration.locale = locale;
568 sLocaleConfiguration.mcc = mcc;
569 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700570
Joe Onorato0589f0f2010-02-08 13:44:00 -0800571 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700572
573 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
574 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700575 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700576 public void run() {
577 writeConfiguration(Launcher.this, localeConfiguration);
578 }
579 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700580 }
581 }
582
583 private static class LocaleConfiguration {
584 public String locale;
585 public int mcc = -1;
586 public int mnc = -1;
587 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700588
Romain Guy98d01652009-06-30 16:21:04 -0700589 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
590 DataInputStream in = null;
591 try {
592 in = new DataInputStream(context.openFileInput(PREFERENCES));
593 configuration.locale = in.readUTF();
594 configuration.mcc = in.readInt();
595 configuration.mnc = in.readInt();
596 } catch (FileNotFoundException e) {
597 // Ignore
598 } catch (IOException e) {
599 // Ignore
600 } finally {
601 if (in != null) {
602 try {
603 in.close();
604 } catch (IOException e) {
605 // Ignore
606 }
607 }
608 }
609 }
610
611 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
612 DataOutputStream out = null;
613 try {
614 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
615 out.writeUTF(configuration.locale);
616 out.writeInt(configuration.mcc);
617 out.writeInt(configuration.mnc);
618 out.flush();
619 } catch (FileNotFoundException e) {
620 // Ignore
621 } catch (IOException e) {
622 //noinspection ResultOfMethodCallIgnored
623 context.getFileStreamPath(PREFERENCES).delete();
624 } finally {
625 if (out != null) {
626 try {
627 out.close();
628 } catch (IOException e) {
629 // Ignore
630 }
631 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 }
633 }
634
Bjorn Bringertc459e522013-06-07 19:36:01 +0100635 public LayoutInflater getInflater() {
636 return mInflater;
637 }
638
Adam Cohen716b51e2011-06-30 12:09:54 -0700639 public DragLayer getDragLayer() {
640 return mDragLayer;
641 }
642
Winson Chung36a62fe2012-05-06 18:04:42 -0700643 boolean isDraggingEnabled() {
644 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
645 // that is subsequently removed from the workspace in startBinding().
646 return !mModel.isLoadingWorkspace();
647 }
648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 static int getScreen() {
650 synchronized (sLock) {
651 return sScreen;
652 }
653 }
654
655 static void setScreen(int screen) {
656 synchronized (sLock) {
657 sScreen = screen;
658 }
659 }
660
Winson Chung557d6ed2011-07-08 15:34:52 -0700661 /**
662 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
663 * a configuration step, this allows the proper animations to run after other transitions.
664 */
665 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700666 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800667 switch (args.requestCode) {
668 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700669 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700670 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800671 break;
672 case REQUEST_PICK_SHORTCUT:
673 processShortcut(args.intent);
674 break;
675 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700676 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700677 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700678 result = true;
679 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 case REQUEST_CREATE_APPWIDGET:
681 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700682 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700683 result = true;
684 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 }
Michael Jurka27614d22012-04-02 06:40:22 -0700686 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
687 // if you turned the screen off and then back while in All Apps, Launcher would not
688 // return to the workspace. Clearing mAddInfo.container here fixes this issue
689 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700690 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 }
692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700694 protected void onActivityResult(
695 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700696 // Reset the startActivity waiting flag
697 mWaitingForResult = false;
698
Michael Jurka8b805b12012-04-18 14:23:14 -0700699 if (requestCode == REQUEST_BIND_APPWIDGET) {
700 int appWidgetId = data != null ?
701 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
702 if (resultCode == RESULT_CANCELED) {
703 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
704 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700705 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 }
707 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200708 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
709 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
710 mWorkspace.exitOverviewMode(false);
711 }
712 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200714
Winson Chung557d6ed2011-07-08 15:34:52 -0700715 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800716 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
717 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700718
Adam Cohened66b2b2012-01-23 17:28:51 -0800719 // We have special handling for widgets
720 if (isWidgetDrop) {
721 int appWidgetId = data != null ?
722 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700723 if (appWidgetId < 0) {
724 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
725 "widget configuration activity.");
726 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
727 } else {
728 completeTwoStageWidgetDrop(resultCode, appWidgetId);
729 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 return;
731 }
732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 // The pattern used here is that a user PICKs a specific application,
734 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
737 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700738 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 final PendingAddArguments args = new PendingAddArguments();
740 args.requestCode = requestCode;
741 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700742 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700743 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700744 args.cellX = mPendingAddInfo.cellX;
745 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800746 if (isWorkspaceLocked()) {
747 sPendingAddList.add(args);
748 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700749 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800752 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700753 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
755 null);
756 }
757
758 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700760 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800761 Runnable onCompleteRunnable = null;
762 int animationType = 0;
763
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700764 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 if (resultCode == RESULT_OK) {
766 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
767 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700768 mPendingAddWidgetInfo);
769 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800770 onCompleteRunnable = new Runnable() {
771 @Override
772 public void run() {
773 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
776 null);
777 }
778 };
779 } else if (resultCode == RESULT_CANCELED) {
780 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
781 onCompleteRunnable = new Runnable() {
782 @Override
783 public void run() {
784 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
785 null);
786 }
787 };
788 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700789 if (mDragLayer.getAnimatedView() != null) {
790 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
791 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
792 animationType, boundWidget, true);
793 } else {
794 // The animated view may be null in the case of a rotation during widget configuration
795 onCompleteRunnable.run();
796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 }
798
799 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700800 protected void onStop() {
801 super.onStop();
802 FirstFrameAnimatorHelper.setIsVisible(false);
803 }
804
805 @Override
806 protected void onStart() {
807 super.onStart();
808 FirstFrameAnimatorHelper.setIsVisible(true);
809 }
810
811 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200813 long startTime = 0;
814 if (DEBUG_RESUME_TIME) {
815 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400816 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700819
Winson Chung4a2afa32012-07-19 14:53:05 -0700820 // Restore the previous launcher state
821 if (mOnResumeState == State.WORKSPACE) {
822 showWorkspace(false);
823 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chungc58497e2013-09-03 17:48:37 -0700824 showAllApps(false, AppsCustomizePagedView.ContentType.Applications);
Winson Chung4a2afa32012-07-19 14:53:05 -0700825 }
826 mOnResumeState = State.NONE;
827
Craig Mautner360310b2012-10-26 15:13:08 -0700828 // Background was set to gradient in onPause(), restore to black if in all apps.
829 setWorkspaceBackground(mState == State.WORKSPACE);
830
Winson Chungde0fb8f2012-05-08 14:37:08 -0700831 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700832 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700833
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800834 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700835 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700836 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700838 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700840 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700842 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200843 // We might have postponed some bind calls until onResume (see waitUntilResume) --
844 // execute them here
845 long startTimeCallbacks = 0;
846 if (DEBUG_RESUME_TIME) {
847 startTimeCallbacks = System.currentTimeMillis();
848 }
849
850 if (mAppsCustomizeContent != null) {
851 mAppsCustomizeContent.setBulkBind(true);
852 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700853 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
854 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200855 }
856 if (mAppsCustomizeContent != null) {
857 mAppsCustomizeContent.setBulkBind(false);
858 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700859 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200860 if (DEBUG_RESUME_TIME) {
861 Log.d(TAG, "Time spent processing callbacks in onResume: " +
862 (System.currentTimeMillis() - startTimeCallbacks));
863 }
Michael Jurka447bf842013-05-15 14:52:15 +0200864 }
Michael Jurka54554252013-08-01 12:52:23 +0200865 if (mOnResumeCallbacks.size() > 0) {
866 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
867 mOnResumeCallbacks.get(i).run();
868 }
869 mOnResumeCallbacks.clear();
870 }
Winson Chunge4e50662012-01-23 14:45:13 -0800871
872 // Reset the pressed state of icons that were locked in the press state while activities
873 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800874 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800875 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800876 mWaitingForResume.setStayPressed(false);
877 }
Winson Chunge4e50662012-01-23 14:45:13 -0800878 if (mAppsCustomizeContent != null) {
879 // Resets the previous all apps icon press state
880 mAppsCustomizeContent.resetDrawableState();
881 }
Adam Cohen06dff352012-06-01 17:17:08 -0700882 // It is possible that widgets can receive updates while launcher is not in the foreground.
883 // Consequently, the widgets will be inflated in the orientation of the foreground activity
884 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
885 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700886 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700887
888 // Again, as with the above scenario, it's possible that one or more of the global icons
889 // were updated in the wrong orientation.
890 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200891 if (DEBUG_RESUME_TIME) {
892 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
893 }
Adam Cohen06dff352012-06-01 17:17:08 -0700894 }
895
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700897 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700898 // Ensure that items added to Launcher are queued until Launcher returns
899 InstallShortcutReceiver.enableInstallQueue();
900
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700901 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700902 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800903 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700904 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700905 }
Romain Guycbb89e42009-06-08 15:52:54 -0700906
Adam Cohen66a01fd2013-06-11 12:48:00 -0700907 protected void onFinishBindingItems() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600908 if (hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
909 addCustomContentToLeft();
910 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700911 }
912
Adam Cohenbffe7452013-07-22 18:21:45 -0700913 QSBScroller mQsbScroller = new QSBScroller() {
914 int scrollY = 0;
915
916 @Override
917 public void setScrollY(int scroll) {
918 scrollY = scroll;
919
920 if (mWorkspace.isOnOrMovingToCustomContent()) {
921 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +0100922 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -0700923 }
924 }
925 };
926
927 public void resetQSBScroll() {
928 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +0100929 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -0700930 }
931
932 public interface CustomContentCallbacks {
933 // Custom content is completely shown
934 public void onShow();
935
936 // Custom content is completely hidden
937 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700938
939 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
940 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700941 }
942
Adam Cohen30bacb22013-08-29 14:25:25 -0700943 protected void startSettings() {
944 }
945
Adam Cohenbffe7452013-07-22 18:21:45 -0700946 public interface QSBScroller {
947 public void setScrollY(int scrollY);
948 }
949
Adam Cohen66a01fd2013-06-11 12:48:00 -0700950 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700951 public QSBScroller addToCustomContentPage(View customContent) {
952 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700953 }
954
Winson Chung98ca0f72013-07-29 12:58:51 -0700955 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700956 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700957 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700958 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700959 }
960
Adam Cohenedb40762013-07-18 16:45:45 -0700961 // The custom content needs to offset its content to account for the QSB
962 public int getTopOffsetForCustomContent() {
963 return mWorkspace.getPaddingTop();
964 }
965
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700966 @Override
967 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 // Flag the loader to stop early before switching
969 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700970 if (mAppsCustomizeContent != null) {
971 mAppsCustomizeContent.surrender();
972 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800973 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700974 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700975
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800976 // We can't hide the IME if it was forced open. So don't bother
977 /*
978 @Override
979 public void onWindowFocusChanged(boolean hasFocus) {
980 super.onWindowFocusChanged(hasFocus);
981
982 if (hasFocus) {
983 final InputMethodManager inputManager = (InputMethodManager)
984 getSystemService(Context.INPUT_METHOD_SERVICE);
985 WindowManager.LayoutParams lp = getWindow().getAttributes();
986 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
987 android.os.Handler()) {
988 protected void onReceiveResult(int resultCode, Bundle resultData) {
989 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
990 }
991 });
992 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
993 }
994 }
995 */
996
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 private boolean acceptFilter() {
998 final InputMethodManager inputManager = (InputMethodManager)
999 getSystemService(Context.INPUT_METHOD_SERVICE);
1000 return !inputManager.isFullscreenMode();
1001 }
1002
1003 @Override
1004 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001005 final int uniChar = event.getUnicodeChar();
1006 final boolean handled = super.onKeyDown(keyCode, event);
1007 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1008 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1010 keyCode, event);
1011 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001012 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001013 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001014 // showSearchDialog()
1015 // If there are multiple keystrokes before the search dialog takes focus,
1016 // onSearchRequested() will be called for every keystroke,
1017 // but it is idempotent, so it's fine.
1018 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 }
1020 }
1021
Joe Onorato8a9625e2010-01-28 15:55:35 -08001022 // Eat the long press event so the keyboard doesn't come up.
1023 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1024 return true;
1025 }
1026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 return handled;
1028 }
1029
Karl Rosaen138a0412009-04-23 19:00:21 -07001030 private String getTypedText() {
1031 return mDefaultKeySsb.toString();
1032 }
1033
1034 private void clearTypedText() {
1035 mDefaultKeySsb.clear();
1036 mDefaultKeySsb.clearSpans();
1037 Selection.setSelection(mDefaultKeySsb, 0);
1038 }
1039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001041 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1042 * State
1043 */
1044 private static State intToState(int stateOrdinal) {
1045 State state = State.WORKSPACE;
1046 final State[] stateValues = State.values();
1047 for (int i = 0; i < stateValues.length; i++) {
1048 if (stateValues[i].ordinal() == stateOrdinal) {
1049 state = stateValues[i];
1050 break;
1051 }
1052 }
1053 return state;
1054 }
1055
1056 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 * Restores the previous state, if it exists.
1058 *
1059 * @param savedState The previous state.
1060 */
1061 private void restoreState(Bundle savedState) {
1062 if (savedState == null) {
1063 return;
1064 }
1065
Michael Jurka883f55b2010-10-21 15:47:14 -07001066 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001067 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001068 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001069 }
1070
Winson Chungf0c6ae02012-03-21 16:10:31 -07001071 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001073 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
1075
Winson Chung3d503fb2011-07-13 17:25:49 -07001076 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001077 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078
Winson Chung3d503fb2011-07-13 17:25:49 -07001079 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1080 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001082 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1083 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001084 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1085 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1086 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001087 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 mRestoring = true;
1089 }
1090
1091 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1092 if (renameFolder) {
1093 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001094 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 mRestoring = true;
1096 }
Winson Chunga12a2502010-12-20 14:41:35 -08001097
Winson Chung785d2eb2011-04-14 16:08:02 -07001098 // Restore the AppsCustomize tab
1099 if (mAppsCustomizeTabHost != null) {
1100 String curTab = savedState.getString("apps_customize_currentTab");
1101 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001102 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001103 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001104 mAppsCustomizeContent.loadAssociatedPages(
1105 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001106 }
1107
Winson Chung5afbf7b2011-07-25 11:53:08 -07001108 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1109 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112
1113 /**
1114 * Finds all the views we need and configure them properly.
1115 */
1116 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001117 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001118
Craig Mautner360310b2012-10-26 15:13:08 -07001119 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001120 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1121 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001122
John Spurlock77e1f472013-09-11 10:09:51 -04001123 mLauncherView.setSystemUiVisibility(
1124 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001125 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126
Winson Chung4c98d922011-05-31 16:50:48 -07001127 // Setup the drag layer
1128 mDragLayer.setup(this, dragController);
1129
Winson Chung3d503fb2011-07-13 17:25:49 -07001130 // Setup the hotseat
1131 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1132 if (mHotseat != null) {
1133 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001134 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001135 }
1136
Adam Cohenf358a4b2013-07-23 16:47:31 -07001137 mOverviewPanel = findViewById(R.id.overview_panel);
1138 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1139 @Override
1140 public void onClick(View arg0) {
Winson Chungc58497e2013-09-03 17:48:37 -07001141 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001142 }
1143 });
1144 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1145 @Override
1146 public void onClick(View arg0) {
1147 startWallpaper();
1148 }
1149 });
Adam Cohen30bacb22013-08-29 14:25:25 -07001150 findViewById(R.id.settings_button).setOnClickListener(new OnClickListener() {
1151 @Override
1152 public void onClick(View arg0) {
1153 startSettings();
1154 }
1155 });
Adam Cohendbdff6b2013-09-18 19:09:15 -07001156 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001157
Winson Chung4c98d922011-05-31 16:50:48 -07001158 // Setup the workspace
1159 mWorkspace.setHapticFeedbackEnabled(false);
1160 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001161 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001162 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001163
Winson Chungf0ea4d32011-06-06 14:27:16 -07001164 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001165 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001166
Winson Chungf0ea4d32011-06-06 14:27:16 -07001167 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001168 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001169 mAppsCustomizeContent = (AppsCustomizePagedView)
1170 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1171 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001172
Winson Chung3d503fb2011-07-13 17:25:49 -07001173 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001174 dragController.setDragScoller(mWorkspace);
1175 dragController.setScrollView(mDragLayer);
1176 dragController.setMoveTarget(mWorkspace);
1177 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001178 if (mSearchDropTargetBar != null) {
1179 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001180 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001181
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001182 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001183 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001184 mWeightWatcher = new WeightWatcher(this);
1185 mWeightWatcher.setAlpha(0.5f);
1186 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001187 new FrameLayout.LayoutParams(
1188 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001189 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001190 Gravity.BOTTOM)
1191 );
Adam Cohen39a06042013-07-19 14:30:12 -07001192
1193 boolean show = shouldShowWeightWatcher();
1194 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001195 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
1197
1198 /**
1199 * Creates a view representing a shortcut.
1200 *
1201 * @param info The data structure describing the shortcut.
1202 *
1203 * @return A View inflated from R.layout.application.
1204 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001205 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001207 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 }
1209
1210 /**
1211 * Creates a view representing a shortcut inflated from the specified resource.
1212 *
1213 * @param layoutResId The id of the XML layout used to create the shortcut.
1214 * @param parent The group the shortcut belongs to.
1215 * @param info The data structure describing the shortcut.
1216 *
1217 * @return A View inflated from layoutResId.
1218 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001219 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001220 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1221 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 return favorite;
1224 }
1225
1226 /**
1227 * Add an application shortcut to the workspace.
1228 *
1229 * @param data The intent describing the application.
1230 * @param cellInfo The position on screen where to create the shortcut.
1231 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001232 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001233 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001234 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001235
Adam Cohenfbba09b2011-07-18 21:43:05 -07001236 // First we check if we already know the exact location where we want to add this item.
1237 if (cellX >= 0 && cellY >= 0) {
1238 cellXY[0] = cellX;
1239 cellXY[1] = cellY;
1240 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001241 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 return;
1243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001245 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001246
Romain Guy73b979d2009-06-09 12:57:21 -07001247 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001248 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001250 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001251 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001252 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001253 } else {
1254 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
1256 }
Romain Guycbb89e42009-06-08 15:52:54 -07001257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 /**
1259 * Add a shortcut to the workspace.
1260 *
1261 * @param data The intent describing the shortcut.
1262 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001264 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001265 int cellY) {
1266 int[] cellXY = mTmpAddItemCellCoordinates;
1267 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001268 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001269
Michael Jurkad3ef3062010-11-23 16:23:58 -08001270 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001271
Adam Cohen558baaf2011-08-15 15:22:57 -07001272 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001273 if (info == null) {
1274 return;
1275 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001276 final View view = createShortcut(info);
1277
Adam Cohenfbba09b2011-07-18 21:43:05 -07001278 // First we check if we already know the exact location where we want to add this item.
1279 if (cellX >= 0 && cellY >= 0) {
1280 cellXY[0] = cellX;
1281 cellXY[1] = cellY;
1282 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001283
1284 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001285 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001286 true, null,null)) {
1287 return;
1288 }
1289 DragObject dragObject = new DragObject();
1290 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001291 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1292 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001293 return;
1294 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001295 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001296 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001297 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001298 foundCellSpan = (result != null);
1299 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001300 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001301 }
1302
1303 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001304 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001305 return;
1306 }
1307
Adam Cohendcd297f2013-06-18 13:13:40 -07001308 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309
1310 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001311 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314 }
1315
Adam Cohen2f093b62012-04-30 18:59:53 -07001316 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1317 int minHeight) {
1318 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001319 // We want to account for the extra amount of padding that we are adding to the widget
1320 // to ensure that it gets the full amount of space that it has requested
1321 int requiredWidth = minWidth + padding.left + padding.right;
1322 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001323 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001324 }
1325
Adam Cohen2f093b62012-04-30 18:59:53 -07001326 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1327 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001328 }
1329
Adam Cohen2f093b62012-04-30 18:59:53 -07001330 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1331 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001332 }
1333
Adam Cohen2f093b62012-04-30 18:59:53 -07001334 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1335 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001336 }
1337
Adam Cohen2f093b62012-04-30 18:59:53 -07001338 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1339 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001340 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001341 }
1342
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001344 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001346 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001347 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001349 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001350 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1351 if (appWidgetInfo == null) {
1352 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1353 }
Romain Guycbb89e42009-06-08 15:52:54 -07001354
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001355 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001356 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001357
Adam Cohen2f093b62012-04-30 18:59:53 -07001358 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1359 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001360
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001362 // We have saved the position to which the widget was dragged-- this really only matters
1363 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001364 int[] cellXY = mTmpAddItemCellCoordinates;
1365 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001366 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001367 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001368 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1369 cellXY[0] = mPendingAddInfo.cellX;
1370 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001371 spanXY[0] = mPendingAddInfo.spanX;
1372 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001373 foundCellSpan = true;
1374 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001375 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001376 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001377 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1378 spanXY[1], cellXY, finalSpan);
1379 spanXY[0] = finalSpan[0];
1380 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001381 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001382 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001383 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001384 }
1385
Michael Jurka0280c3b2010-09-17 15:00:07 -07001386 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001387 if (appWidgetId != -1) {
1388 // Deleting an app widget ID is a void call but writes to disk before returning
1389 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001390 new Thread("deleteAppWidgetId") {
1391 public void run() {
1392 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1393 }
1394 }.start();
1395 }
Winson Chung93eef082012-03-23 15:59:27 -07001396 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001397 return;
1398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001400 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001401 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1402 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001403 launcherInfo.spanX = spanXY[0];
1404 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001405 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1406 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001409 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410
1411 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001412 if (hostView == null) {
1413 // Perform actual inflation because we're live
1414 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1415 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1416 } else {
1417 // The AppWidgetHostView has already been inflated and instantiated
1418 launcherInfo.hostView = hostView;
1419 }
Romain Guycbb89e42009-06-08 15:52:54 -07001420
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001422 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001423 launcherInfo.notifyWidgetSizeChanged(this);
1424
Adam Cohendcd297f2013-06-18 13:13:40 -07001425 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001426 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001427
1428 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001430 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 }
Romain Guycbb89e42009-06-08 15:52:54 -07001432
Adam Cohended9f8d2010-11-03 13:25:16 -07001433 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1434 @Override
1435 public void onReceive(Context context, Intent intent) {
1436 final String action = intent.getAction();
1437 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1438 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001439 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001440 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001441
Winson Chungc100e8e2011-08-09 16:02:43 -07001442 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001443 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001444 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001445 showWorkspaceAndExitOverviewMode(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001446 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001447 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1448 mUserPresent = true;
1449 updateRunning();
1450 }
1451 }
1452 };
1453
1454 @Override
1455 public void onAttachedToWindow() {
1456 super.onAttachedToWindow();
1457
1458 // Listen for broadcasts related to user-presence
1459 final IntentFilter filter = new IntentFilter();
1460 filter.addAction(Intent.ACTION_SCREEN_OFF);
1461 filter.addAction(Intent.ACTION_USER_PRESENT);
1462 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001463 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001464 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001465 mVisible = true;
1466 }
1467
1468 @Override
1469 public void onDetachedFromWindow() {
1470 super.onDetachedFromWindow();
1471 mVisible = false;
1472
Adam Cohend113e0c2010-11-11 10:48:05 -08001473 if (mAttached) {
1474 unregisterReceiver(mReceiver);
1475 mAttached = false;
1476 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001477 updateRunning();
1478 }
1479
1480 public void onWindowVisibilityChanged(int visibility) {
1481 mVisible = visibility == View.VISIBLE;
1482 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001483 // The following code used to be in onResume, but it turns out onResume is called when
1484 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1485 // is a more appropriate event to handle
1486 if (mVisible) {
1487 mAppsCustomizeTabHost.onWindowVisible();
1488 if (!mWorkspaceLoading) {
1489 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001490 // We want to let Launcher draw itself at least once before we force it to build
1491 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001492 // apps is nice and speedy.
1493 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001494 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001495 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001496 if (mStarted) return;
1497 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001498 // We delay the layer building a bit in order to give
1499 // other message processing a time to run. In particular
1500 // this avoids a delay in hiding the IME if it was
1501 // currently shown, because doing that may involve
1502 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001503 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001504 final ViewTreeObserver.OnDrawListener listener = this;
1505 mWorkspace.post(new Runnable() {
1506 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001507 if (mWorkspace != null &&
1508 mWorkspace.getViewTreeObserver() != null) {
1509 mWorkspace.getViewTreeObserver().
1510 removeOnDrawListener(listener);
1511 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001512 }
1513 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001514 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001515 }
1516 });
1517 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001518 // When Launcher comes back to foreground, a different Activity might be responsible for
1519 // the app market intent, so refresh the icon
1520 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001521 clearTypedText();
1522 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001523 }
1524
1525 private void sendAdvanceMessage(long delay) {
1526 mHandler.removeMessages(ADVANCE_MSG);
1527 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1528 mHandler.sendMessageDelayed(msg, delay);
1529 mAutoAdvanceSentTime = System.currentTimeMillis();
1530 }
1531
1532 private void updateRunning() {
1533 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1534 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1535 mAutoAdvanceRunning = autoAdvanceRunning;
1536 if (autoAdvanceRunning) {
1537 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1538 sendAdvanceMessage(delay);
1539 } else {
1540 if (!mWidgetsToAdvance.isEmpty()) {
1541 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1542 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1543 }
1544 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001545 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001546 }
1547 }
1548 }
1549
1550 private final Handler mHandler = new Handler() {
1551 @Override
1552 public void handleMessage(Message msg) {
1553 if (msg.what == ADVANCE_MSG) {
1554 int i = 0;
1555 for (View key: mWidgetsToAdvance.keySet()) {
1556 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1557 final int delay = mAdvanceStagger * i;
1558 if (v instanceof Advanceable) {
1559 postDelayed(new Runnable() {
1560 public void run() {
1561 ((Advanceable) v).advance();
1562 }
1563 }, delay);
1564 }
1565 i++;
1566 }
1567 sendAdvanceMessage(mAdvanceInterval);
1568 }
1569 }
1570 };
1571
1572 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001573 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001574 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1575 if (v instanceof Advanceable) {
1576 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001577 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001578 updateRunning();
1579 }
1580 }
1581
1582 void removeWidgetToAutoAdvance(View hostView) {
1583 if (mWidgetsToAdvance.containsKey(hostView)) {
1584 mWidgetsToAdvance.remove(hostView);
1585 updateRunning();
1586 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001587 }
1588
Joe Onorato9c1289c2009-08-17 11:03:03 -04001589 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001591 launcherInfo.hostView = null;
1592 }
1593
Winson Chung93eef082012-03-23 15:59:27 -07001594 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1595 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1596 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001597 }
1598
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001599 public LauncherAppWidgetHost getAppWidgetHost() {
1600 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 }
Romain Guycbb89e42009-06-08 15:52:54 -07001602
Winson Chunga9abd0e2010-10-27 17:18:37 -07001603 public LauncherModel getModel() {
1604 return mModel;
1605 }
1606
Bjorn Bringertc459e522013-06-07 19:36:01 +01001607 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001608 getWindow().closeAllPanels();
1609
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001610 // Whatever we were doing is hereby canceled.
1611 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001612 }
1613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 @Override
1615 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001616 long startTime = 0;
1617 if (DEBUG_RESUME_TIME) {
1618 startTime = System.currentTimeMillis();
1619 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 super.onNewIntent(intent);
1621
1622 // Close the menu
1623 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001624 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001625 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001626
Jamie Genniscb222e82012-10-23 15:44:26 -07001627 final boolean alreadyOnHome =
1628 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001629 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001630
Jamie Genniscb222e82012-10-23 15:44:26 -07001631 Runnable processIntent = new Runnable() {
1632 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001633 if (mWorkspace == null) {
1634 // Can be cases where mWorkspace is null, this prevents a NPE
1635 return;
1636 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001637 Folder openFolder = mWorkspace.getOpenFolder();
1638 // In all these cases, only animate if we're already on home
1639 mWorkspace.exitWidgetResizeMode();
1640 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1641 openFolder == null) {
1642 mWorkspace.moveToDefaultScreen(true);
1643 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001644
Jamie Genniscb222e82012-10-23 15:44:26 -07001645 closeFolder();
1646 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001647
Jamie Genniscb222e82012-10-23 15:44:26 -07001648 // If we are already on home, then just animate back to the workspace,
1649 // otherwise, just wait until onResume to set the state back to Workspace
1650 if (alreadyOnHome) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001651 showWorkspaceAndExitOverviewMode(true);
Jamie Genniscb222e82012-10-23 15:44:26 -07001652 } else {
1653 mOnResumeState = State.WORKSPACE;
1654 }
1655
1656 final View v = getWindow().peekDecorView();
1657 if (v != null && v.getWindowToken() != null) {
1658 InputMethodManager imm = (InputMethodManager)getSystemService(
1659 INPUT_METHOD_SERVICE);
1660 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1661 }
1662
Winson Chung7819a562013-09-19 15:55:45 -07001663 // Reset the apps customize page
1664 if (mAppsCustomizeTabHost != null) {
Jamie Genniscb222e82012-10-23 15:44:26 -07001665 mAppsCustomizeTabHost.reset();
1666 }
1667 }
1668 };
1669
1670 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1671 // Delay processing of the intent to allow the status bar animation to finish
1672 // first in order to avoid janky animations.
1673 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001674 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001675 // Process the intent immediately.
1676 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001677 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
Michael Jurka447bf842013-05-15 14:52:15 +02001680 if (DEBUG_RESUME_TIME) {
1681 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1682 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 }
1684
Adam Cohen040a5d22013-09-16 17:09:33 -07001685 protected void showWorkspaceAndExitOverviewMode(boolean animate) {
1686 showWorkspace(animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001687 if (mWorkspace.isInOverviewMode()) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001688 mWorkspace.exitOverviewMode(animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001689 }
1690 }
Adam Cohen040a5d22013-09-16 17:09:33 -07001691 protected void showWorkspaceAndExitOverviewMode() {
1692 showWorkspaceAndExitOverviewMode(true);
1693 }
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001696 public void onRestoreInstanceState(Bundle state) {
1697 super.onRestoreInstanceState(state);
1698 for (int page: mSynchronouslyBoundPages) {
1699 mWorkspace.restoreInstanceStateForChild(page);
1700 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 }
1702
1703 @Override
1704 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001705 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001706 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707
Michael Jurka883f55b2010-10-21 15:47:14 -07001708 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001709 // We close any open folder since it will not be re-opened, and we need to make sure
1710 // this state is reflected.
1711 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712
Adam Cohendcd297f2013-06-18 13:13:40 -07001713 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001714 mWaitingForResult) {
1715 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001716 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001717 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1718 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001719 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1720 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1721 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 }
1723
1724 if (mFolderInfo != null && mWaitingForResult) {
1725 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1726 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1727 }
Michael Jurka946ad472010-07-09 18:05:18 -07001728
Winson Chung785d2eb2011-04-14 16:08:02 -07001729 // Save the current AppsCustomize tab
1730 if (mAppsCustomizeTabHost != null) {
1731 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1732 if (currentTabTag != null) {
1733 outState.putString("apps_customize_currentTab", currentTabTag);
1734 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001735 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1736 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 }
1739
1740 @Override
1741 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001743
Winson Chunge7a03942011-08-05 15:05:12 -07001744 // Remove all pending runnables
1745 mHandler.removeMessages(ADVANCE_MSG);
1746 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001747 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001748
Winson Chungcd2b0142011-06-08 16:02:26 -07001749 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001750 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001751 mModel.stopLoader();
1752 app.setLauncher(null);
1753
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001755 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001757 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001759 mAppWidgetHost = null;
1760
1761 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762
1763 TextKeyListener.getInstance().release();
1764
Winson Chung81b52252012-08-27 15:34:29 -07001765 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1766 // to prevent leaking Launcher activities on orientation change.
1767 if (mModel != null) {
1768 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1769 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001770
1771 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001772 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001773
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001774 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001775 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1776 mWorkspace.removeAllViews();
1777 mWorkspace = null;
1778 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001779
Michael Jurka2ecf9952012-06-18 12:52:28 -07001780 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 }
1782
Adam Cohena9cf38f2011-05-02 15:36:58 -07001783 public DragController getDragController() {
1784 return mDragController;
1785 }
1786
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 @Override
1788 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001789 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 super.startActivityForResult(intent, requestCode);
1791 }
1792
Winson Chung70d72102011-08-12 11:24:35 -07001793 /**
1794 * Indicates that we want global search for this activity by setting the globalSearch
1795 * argument for {@link #startSearch} to true.
1796 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001798 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001800
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001801 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001802
Karl Rosaen138a0412009-04-23 19:00:21 -07001803 if (initialQuery == null) {
1804 // Use any text typed in the launcher as the initial query
1805 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 if (appSearchData == null) {
1808 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001809 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 }
Winson Chungadf0c182012-08-23 14:59:07 -07001811 Rect sourceBounds = new Rect();
1812 if (mSearchDropTargetBar != null) {
1813 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1814 }
Romain Guycbb89e42009-06-08 15:52:54 -07001815
Bjorn Bringertc459e522013-06-07 19:36:01 +01001816 startSearch(initialQuery, selectInitialQuery,
1817 appSearchData, sourceBounds);
1818 }
1819
1820 public void startSearch(String initialQuery,
1821 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001822 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001823 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001824 }
1825
1826 /**
1827 * Starts the global search activity. This code is a copied from SearchManager
1828 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001829 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001830 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001831 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001832 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1833 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1834 if (globalSearchActivity == null) {
1835 Log.w(TAG, "No global search activity found.");
1836 return;
1837 }
1838 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1839 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1840 intent.setComponent(globalSearchActivity);
1841 // Make sure that we have a Bundle to put source in
1842 if (appSearchData == null) {
1843 appSearchData = new Bundle();
1844 } else {
1845 appSearchData = new Bundle(appSearchData);
1846 }
1847 // Set source to package name of app that starts global search, if not set already.
1848 if (!appSearchData.containsKey("source")) {
1849 appSearchData.putString("source", getPackageName());
1850 }
1851 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1852 if (!TextUtils.isEmpty(initialQuery)) {
1853 intent.putExtra(SearchManager.QUERY, initialQuery);
1854 }
1855 if (selectInitialQuery) {
1856 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1857 }
1858 intent.setSourceBounds(sourceBounds);
1859 try {
1860 startActivity(intent);
1861 } catch (ActivityNotFoundException ex) {
1862 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 }
1865
Winson Chung70d72102011-08-12 11:24:35 -07001866 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001867 public boolean onPrepareOptionsMenu(Menu menu) {
1868 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001869 if (!mWorkspace.isInOverviewMode()) {
1870 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001871 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001872 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001875 @Override
1876 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001877 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001878 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001879 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001880 }
1881
Joe Onorato9c1289c2009-08-17 11:03:03 -04001882 public boolean isWorkspaceLocked() {
1883 return mWorkspaceLoading || mWaitingForResult;
1884 }
1885
Michael Jurka0280c3b2010-09-17 15:00:07 -07001886 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001887 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001888 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001889 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1890 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001891 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001892 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001893 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001894
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001895 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1896 AppWidgetProviderInfo appWidgetInfo) {
1897 if (appWidgetInfo.configure != null) {
1898 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001899
Bjorn Bringert7984c942009-12-09 15:38:25 +00001900 // Launch over to configure widget, if needed
1901 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001902 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001903 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001904 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001906 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001907 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001908 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001909 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001910 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
1912 }
Romain Guycbb89e42009-06-08 15:52:54 -07001913
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001914 protected void moveToCustomContentScreen(boolean animate) {
1915 mWorkspace.moveToCustomContentScreen(animate);
1916 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001917 /**
1918 * Process a shortcut drop.
1919 *
1920 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001921 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001922 * @param cell The cell it should be added to, optional
1923 * @param position The location on the screen where it was dropped, optional
1924 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001925 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001926 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001927 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001928 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001929 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001930 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001931
1932 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001933 mPendingAddInfo.cellX = cell[0];
1934 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001935 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001936
1937 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1938 createShortcutIntent.setComponent(componentName);
1939 processShortcut(createShortcutIntent);
1940 }
1941
Adam Cohenfbba09b2011-07-18 21:43:05 -07001942 /**
1943 * Process a widget drop.
1944 *
1945 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001946 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001947 * @param cell The cell it should be added to, optional
1948 * @param position The location on the screen where it was dropped, optional
1949 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001950 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001951 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001952 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001953 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001954 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001955 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001956 mPendingAddInfo.minSpanX = info.minSpanX;
1957 mPendingAddInfo.minSpanY = info.minSpanY;
1958
Adam Cohenfbba09b2011-07-18 21:43:05 -07001959 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001960 mPendingAddInfo.cellX = cell[0];
1961 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001962 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001963 if (span != null) {
1964 mPendingAddInfo.spanX = span[0];
1965 mPendingAddInfo.spanY = span[1];
1966 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001967
Adam Cohened66b2b2012-01-23 17:28:51 -08001968 AppWidgetHostView hostView = info.boundWidget;
1969 int appWidgetId;
1970 if (hostView != null) {
1971 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001972 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001973 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001974 // In this case, we either need to start an activity to get permission to bind
1975 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001976 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001977 Bundle options = info.bindOptions;
1978
1979 boolean success = false;
1980 if (options != null) {
1981 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1982 info.componentName, options);
1983 } else {
1984 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1985 info.componentName);
1986 }
1987 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001988 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001989 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001990 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001991 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1992 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1993 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001994 // TODO: we need to make sure that this accounts for the options bundle.
1995 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001996 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1997 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001998 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001999 }
2000
Joe Onoratodeb98af2010-02-19 14:59:39 -08002001 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002002 // Handle case where user selected "Applications"
2003 String applicationName = getResources().getString(R.string.group_applications);
2004 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002005
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002006 if (applicationName != null && applicationName.equals(shortcutName)) {
2007 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2008 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002009
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002010 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2011 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002012 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002013 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002014 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002015 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
Winson Chung24ab2f12010-09-16 14:10:47 -07002019 void processWallpaper(Intent intent) {
2020 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2021 }
2022
Adam Cohendcd297f2013-06-18 13:13:40 -07002023 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002024 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002025 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 folderInfo.title = getText(R.string.folder_name);
2027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002029 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002030 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002031 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032
2033 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002034 FolderIcon newFolder =
2035 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002036 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002037 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002038 // Force measure the new folder icon
2039 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2040 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002041 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
Romain Guycbb89e42009-06-08 15:52:54 -07002043
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002045 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002046 }
2047
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002048 protected void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002049 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002051 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002052 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 }
2054
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002055 protected ComponentName getWallpaperPickerComponent() {
2056 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2057 }
2058
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002059 /**
2060 * Registers various content observers. The current implementation registers
2061 * only a favorites observer to keep track of the favorites applications.
2062 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002063 private void registerContentObservers() {
2064 ContentResolver resolver = getContentResolver();
2065 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2066 true, mWidgetObserver);
2067 }
2068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 @Override
2070 public boolean dispatchKeyEvent(KeyEvent event) {
2071 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2072 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002074 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002075 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002076 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002077 dumpState();
2078 return true;
2079 }
2080 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002081 }
2082 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2083 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002084 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 return true;
2086 }
2087 }
2088
2089 return super.dispatchKeyEvent(event);
2090 }
2091
Joe Onorato88ec0992009-11-19 13:16:06 -08002092 @Override
2093 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002094 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002095 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002096 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002097 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002098 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002099 Folder openFolder = mWorkspace.getOpenFolder();
2100 if (openFolder.isEditingName()) {
2101 openFolder.dismissEditingName();
2102 } else {
2103 closeFolder();
2104 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002105 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002106 mWorkspace.exitWidgetResizeMode();
2107
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002108 // Back button is a no-op here, but give at least some feedback for the button press
2109 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002110 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002111 }
2112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002114 * Re-listen when widgets are reset.
2115 */
2116 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002117 if (mAppWidgetHost != null) {
2118 mAppWidgetHost.startListening();
2119 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002120 }
2121
2122 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 * Launches the intent referred by the clicked shortcut.
2124 *
2125 * @param v The view representing the clicked shortcut.
2126 */
2127 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002128 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2129 // view has detached (it's possible for this to happen if the view is removed mid touch).
2130 if (v.getWindowToken() == null) {
2131 return;
2132 }
2133
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002134 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002135 return;
2136 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002137
Adam Cohen1697b792013-09-17 19:08:21 -07002138 if (v instanceof Workspace) {
2139 if (mWorkspace.isInOverviewMode()) {
2140 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002141 }
2142 return;
2143 }
2144
2145 if (v instanceof CellLayout) {
2146 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002147 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002148 }
2149 }
2150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002152 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002154 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2155 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002156
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002157 // Check for special shortcuts
2158 if (intent.getComponent() != null) {
2159 final String shortcutClass = intent.getComponent().getClassName();
2160
2161 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chungc58497e2013-09-03 17:48:37 -07002162 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002163 return;
2164 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2165 MemoryDumpActivity.startDump(this);
2166 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002167 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2168 toggleShowWeightWatcher();
2169 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002170 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002171 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002172
2173 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002174 int[] pos = new int[2];
2175 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002176 intent.setSourceBounds(new Rect(pos[0], pos[1],
2177 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002178
2179 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002180
Daniel Sandlerff02d492013-08-05 02:12:05 -04002181 mStats.recordLaunch(intent, shortcut);
2182
Michael Jurkaddd62e92011-02-16 17:49:14 -08002183 if (success && v instanceof BubbleTextView) {
2184 mWaitingForResume = (BubbleTextView) v;
2185 mWaitingForResume.setStayPressed(true);
2186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002188 if (v instanceof FolderIcon) {
2189 FolderIcon fi = (FolderIcon) v;
2190 handleFolderClick(fi);
2191 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002192 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002193 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002194 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002195 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002196 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002197 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 }
2199 }
2200
Michael Jurka0e260592010-06-30 17:07:39 -07002201 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002202 return false;
2203 }
2204
Michael Jurkaaf442092010-06-10 17:01:57 -07002205 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002206 * Event handler for the search button
2207 *
2208 * @param v The view that was clicked.
2209 */
2210 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002211 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2212
Amith Yamasania135ba82011-08-09 17:42:01 -07002213 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002214 }
2215
2216 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002217 * Event handler for the voice button
2218 *
2219 * @param v The view that was clicked.
2220 */
2221 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002222 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002223
Bjorn Bringertc459e522013-06-07 19:36:01 +01002224 startVoice();
2225 }
2226
2227 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002228 try {
2229 final SearchManager searchManager =
2230 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2231 ComponentName activityName = searchManager.getGlobalSearchActivity();
2232 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002233 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002234 if (activityName != null) {
2235 intent.setPackage(activityName.getPackageName());
2236 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002237 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002238 } catch (ActivityNotFoundException e) {
2239 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002240 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002241 startActivitySafely(null, intent, "onClickVoiceButton");
2242 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002243 }
2244
2245 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002246 * Event handler for the "grid" button that appears on the home screen, which
2247 * enters all apps mode.
2248 *
2249 * @param v The view that was clicked.
2250 */
2251 public void onClickAllAppsButton(View v) {
Winson Chungc58497e2013-09-03 17:48:37 -07002252 showAllApps(true, AppsCustomizePagedView.ContentType.Applications);
Michael Jurka5130e402011-10-13 04:55:35 -07002253 }
2254
2255 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002256 // Provide the same haptic feedback that the system offers for virtual keys.
2257 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002258 }
2259
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002260 public void onClickAppMarketButton(View v) {
2261 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002262 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002263 } else {
2264 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002265 }
2266 }
2267
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002268 /**
2269 * Called when the user stops interacting with the launcher.
2270 * This implies that the user is now on the homescreen and is not doing housekeeping.
2271 */
2272 protected void onInteractionEnd() {}
2273
2274 /**
2275 * Called when the user starts interacting with the launcher.
2276 * The possible interactions are:
2277 * - open all apps
2278 * - reorder an app shortcut, or a widget
2279 * - open the overview mode.
2280 * This is a good time to stop doing things that only make sense
2281 * when the user is on the homescreen and not doing housekeeping.
2282 */
2283 protected void onInteractionBegin() {}
2284
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002285 void startApplicationDetailsActivity(ComponentName componentName) {
2286 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002287 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2288 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002289 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002290 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002291 }
2292
Michael Jurka1e2f4652013-07-08 18:03:46 -07002293 // returns true if the activity was started
2294 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002295 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002296 // System applications cannot be installed. For now, show a toast explaining that.
2297 // We may give them the option of disabling apps this way.
2298 int messageId = R.string.uninstall_system_app_text;
2299 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002300 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002301 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002302 String packageName = componentName.getPackageName();
2303 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002304 Intent intent = new Intent(
2305 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002306 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2307 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002308 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002309 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002310 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002311 }
2312
Michael Jurka86a720e2012-05-09 11:23:23 -07002313 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002317 // Only launch using the new animation if the shortcut has not opted out (this is a
2318 // private contract between launcher and may be ignored in the future).
2319 boolean useLaunchAnimation = (v != null) &&
2320 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2321 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002322 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2323 v.getMeasuredWidth(), v.getMeasuredHeight());
2324
2325 startActivity(intent, opts.toBundle());
2326 } else {
2327 startActivity(intent);
2328 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002329 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 } catch (SecurityException e) {
2331 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002332 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002334 "or use the exported attribute for this activity. "
2335 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002337 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002339
Michael Jurka86a720e2012-05-09 11:23:23 -07002340 boolean startActivitySafely(View v, Intent intent, Object tag) {
2341 boolean success = false;
2342 try {
2343 success = startActivity(v, intent, tag);
2344 } catch (ActivityNotFoundException e) {
2345 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2346 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2347 }
2348 return success;
2349 }
2350
Adam Cohena9cf38f2011-05-02 15:36:58 -07002351 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002352 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002353 Folder openFolder = mWorkspace.getFolderForTag(info);
2354
2355 // If the folder info reports that the associated folder is open, then verify that
2356 // it is actually opened. There have been a few instances where this gets out of sync.
2357 if (info.opened && openFolder == null) {
2358 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002359 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002360 info.opened = false;
2361 }
2362
Adam Cohenfb91f302012-06-11 15:45:18 -07002363 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 // Close any open folder
2365 closeFolder();
2366 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002367 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 } else {
2369 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 int folderScreen;
2371 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002372 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373 // .. and close it
2374 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002375 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 // Close any folder open on the current screen
2377 closeFolder();
2378 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002379 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 }
2381 }
2382 }
2383 }
2384
Adam Cohen268c4752012-06-06 17:47:33 -07002385 /**
2386 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2387 * in the DragLayer in the exact absolute location of the original FolderIcon.
2388 */
2389 private void copyFolderIconToImage(FolderIcon fi) {
2390 final int width = fi.getMeasuredWidth();
2391 final int height = fi.getMeasuredHeight();
2392
2393 // Lazy load ImageView, Bitmap and Canvas
2394 if (mFolderIconImageView == null) {
2395 mFolderIconImageView = new ImageView(this);
2396 }
2397 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2398 mFolderIconBitmap.getHeight() != height) {
2399 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2400 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2401 }
2402
2403 DragLayer.LayoutParams lp;
2404 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2405 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2406 } else {
2407 lp = new DragLayer.LayoutParams(width, height);
2408 }
2409
Adam Cohen307fe232012-08-16 17:55:58 -07002410 // The layout from which the folder is being opened may be scaled, adjust the starting
2411 // view size by this scale factor.
2412 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002413 lp.customPosition = true;
2414 lp.x = mRectForFolderAnimation.left;
2415 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002416 lp.width = (int) (scale * width);
2417 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002418
2419 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2420 fi.draw(mFolderIconCanvas);
2421 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002422 if (fi.getFolder() != null) {
2423 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2424 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002425 }
Adam Cohen268c4752012-06-06 17:47:33 -07002426 // Just in case this image view is still in the drag layer from a previous animation,
2427 // we remove it and re-add it.
2428 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2429 mDragLayer.removeView(mFolderIconImageView);
2430 }
2431 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002432 if (fi.getFolder() != null) {
2433 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002434 }
Adam Cohen268c4752012-06-06 17:47:33 -07002435 }
2436
Adam Cohen2801caf2011-05-13 20:57:39 -07002437 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002438 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002439 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2440 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2441 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2442
Adam Cohenc51934b2011-07-26 21:07:43 -07002443 FolderInfo info = (FolderInfo) fi.getTag();
2444 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2445 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002446 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2447 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002448 }
2449
Adam Cohen268c4752012-06-06 17:47:33 -07002450 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2451 copyFolderIconToImage(fi);
2452 fi.setVisibility(View.INVISIBLE);
2453
Michael Jurka2ecf9952012-06-18 12:52:28 -07002454 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002455 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002456 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2457 oa.start();
2458 }
2459
Adam Cohen268c4752012-06-06 17:47:33 -07002460 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002461 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002462 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2463 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2464 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2465
Adam Cohen268c4752012-06-06 17:47:33 -07002466 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002467
Adam Cohen268c4752012-06-06 17:47:33 -07002468 // We remove and re-draw the FolderIcon in-case it has changed
2469 mDragLayer.removeView(mFolderIconImageView);
2470 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002471 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002472 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002473 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002474 oa.addListener(new AnimatorListenerAdapter() {
2475 @Override
2476 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002477 if (cl != null) {
2478 cl.clearFolderLeaveBehind();
2479 // Remove the ImageView copy of the FolderIcon and make the original visible.
2480 mDragLayer.removeView(mFolderIconImageView);
2481 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002482 }
2483 }
2484 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002485 oa.start();
2486 }
2487
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002489 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 * is animated relative to the specified View. If the View is null, no animation
2491 * is played.
2492 *
2493 * @param folderInfo The FolderInfo describing the folder to open.
2494 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002495 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002496 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002497 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498
Adam Cohena9cf38f2011-05-02 15:36:58 -07002499 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500
Adam Cohen4554ee12011-08-03 16:13:21 -07002501 // Just verify that the folder hasn't already been added to the DragLayer.
2502 // There was a one-off crash where the folder had a parent already.
2503 if (folder.getParent() == null) {
2504 mDragLayer.addView(folder);
2505 mDragController.addDropTarget((DropTarget) folder);
2506 } else {
2507 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2508 folder.getParent() + ").");
2509 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002510 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002511 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002512
2513 // Notify the accessibility manager that this folder "window" has appeared and occluded
2514 // the workspace items
2515 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2516 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002517 }
2518
2519 public void closeFolder() {
2520 Folder folder = mWorkspace.getOpenFolder();
2521 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002522 if (folder.isEditingName()) {
2523 folder.dismissEditingName();
2524 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002525 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002526
2527 // Dismiss the folder cling
2528 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002529 }
2530 }
2531
2532 void closeFolder(Folder folder) {
2533 folder.getInfo().opened = false;
2534
2535 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2536 if (parent != null) {
2537 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2538 shrinkAndFadeInFolderIcon(fi);
2539 }
2540 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002541
2542 // Notify the accessibility manager that this folder "window" has disappeard and no
2543 // longer occludeds the workspace items
2544 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 }
2546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002548 if (!isDraggingEnabled()) return false;
2549 if (isWorkspaceLocked()) return false;
2550 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551
Adam Cohen1697b792013-09-17 19:08:21 -07002552 if (v instanceof Workspace) {
2553 if (!mWorkspace.isInOverviewMode()) {
2554 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2555 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2556 mWorkspace.enterOverviewMode();
2557 }
2558 return true;
2559 }
2560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002562 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 }
2564
Michael Jurka0280c3b2010-09-17 15:00:07 -07002565 resetAddInfo();
2566 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002568 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 return true;
2570 }
2571
Winson Chung3d503fb2011-07-13 17:25:49 -07002572 // The hotseat touch handling does not go through Workspace, and we always allow long press
2573 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002574 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002575 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2576 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002577 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002578 // User long pressed on empty space
2579 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2580 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2581 // Disabling reordering until we sort out some issues.
2582 if (mWorkspace.isInOverviewMode()) {
2583 mWorkspace.startReordering(v);
2584 } else {
2585 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002588 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002590 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 }
2592 }
2593 }
2594 return true;
2595 }
2596
Winson Chung3d503fb2011-07-13 17:25:49 -07002597 boolean isHotseatLayout(View layout) {
2598 return mHotseat != null && layout != null &&
2599 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2600 }
2601 Hotseat getHotseat() {
2602 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002603 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002604 View getOverviewPanel() {
2605 return mOverviewPanel;
2606 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002607 SearchDropTargetBar getSearchBar() {
2608 return mSearchDropTargetBar;
2609 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002610
Winson Chung3d503fb2011-07-13 17:25:49 -07002611 /**
2612 * Returns the CellLayout of the specified container at the specified screen.
2613 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002614 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002615 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2616 if (mHotseat != null) {
2617 return mHotseat.getLayout();
2618 } else {
2619 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002620 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002621 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002622 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002623 }
2624 }
2625
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 Workspace getWorkspace() {
2627 return mWorkspace;
2628 }
2629
Daniel Sandler843e8602010-06-07 14:59:01 -04002630 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002631 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002632 }
2633
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002634 /**
2635 * Helper method for the cameraZoomIn/cameraZoomOut animations
2636 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002637 * @param scaleFactor The scale factor used for the zoom
2638 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002639 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002640 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002641 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002642 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002643
Craig Mautner360310b2012-10-26 15:13:08 -07002644 private void setWorkspaceBackground(boolean workspace) {
2645 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002646 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002647 }
2648
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002649 void updateWallpaperVisibility(boolean visible) {
2650 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2651 int curflags = getWindow().getAttributes().flags
2652 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2653 if (wpflags != curflags) {
2654 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2655 }
Craig Mautner360310b2012-10-26 15:13:08 -07002656 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002657 }
2658
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002659 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2660 if (v instanceof LauncherTransitionable) {
2661 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2662 }
2663 }
2664
Michael Jurkabed61d22012-02-14 22:51:29 -08002665 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2666 if (v instanceof LauncherTransitionable) {
2667 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2668 }
Winson Chung70442722012-02-10 15:43:22 -08002669
2670 // Update the workspace transition step as well
2671 dispatchOnLauncherTransitionStep(v, 0f);
2672 }
2673
2674 private void dispatchOnLauncherTransitionStep(View v, float t) {
2675 if (v instanceof LauncherTransitionable) {
2676 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2677 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002678 }
2679
2680 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2681 if (v instanceof LauncherTransitionable) {
2682 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2683 }
Winson Chung70442722012-02-10 15:43:22 -08002684
2685 // Update the workspace transition step as well
2686 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002687 }
2688
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002689 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002690 * Things to test when changing the following seven functions.
2691 * - Home from workspace
2692 * - from center screen
2693 * - from other screens
2694 * - Home from all apps
2695 * - from center screen
2696 * - from other screens
2697 * - Back from all apps
2698 * - from center screen
2699 * - from other screens
2700 * - Launch app from workspace and quit
2701 * - with back
2702 * - with home
2703 * - Launch app from all apps and quit
2704 * - with back
2705 * - with home
2706 * - Go to a screen that's not the default, then all
2707 * apps, and launch and app, and go back
2708 * - with back
2709 * -with home
2710 * - On workspace, long press power and go back
2711 * - with back
2712 * - with home
2713 * - On all apps, long press power and go back
2714 * - with back
2715 * - with home
2716 * - On workspace, power off
2717 * - On all apps, power off
2718 * - Launch an app and turn off the screen while in that app
2719 * - Go back with home key
2720 * - Go back with back key TODO: make this not go to workspace
2721 * - From all apps
2722 * - From workspace
2723 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2724 * - From all apps
2725 * - From the center workspace
2726 * - From another workspace
2727 */
2728
2729 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002730 * Zoom the camera out from the workspace to reveal 'toView'.
2731 * Assumes that the view to show is anchored at either the very top or very bottom
2732 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002733 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002734 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002735 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2736 showAppsCustomizeHelper(animated, springLoaded, contentType);
2737 }
2738 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2739 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002740 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002741 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002742 mStateAnimation.cancel();
2743 mStateAnimation = null;
2744 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002745 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002746
Winson Chungf0ea4d32011-06-06 14:27:16 -07002747 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2748 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2749 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002750 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002751 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002752 final int startDelay =
2753 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002754
Michael Jurkab3e22d92011-10-31 15:58:33 -07002755 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002756
Michael Jurkad74c9842011-07-10 12:44:21 -07002757 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002758 Animator workspaceAnim =
2759 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002760 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2761 // Set the content type for the all apps space
2762 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2763 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002764
2765 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002766 toView.setScaleX(scale);
2767 toView.setScaleY(scale);
2768 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2769 scaleAnim.
2770 scaleX(1f).scaleY(1f).
2771 setDuration(duration).
2772 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002773
Winson Chungf0ea4d32011-06-06 14:27:16 -07002774 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002775 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002776 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002777 .ofFloat(toView, "alpha", 0f, 1f)
2778 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002779 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002780 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2781 @Override
2782 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002783 if (animation == null) {
2784 throw new RuntimeException("animation is null");
2785 }
Winson Chung70442722012-02-10 15:43:22 -08002786 float t = (Float) animation.getAnimatedValue();
2787 dispatchOnLauncherTransitionStep(fromView, t);
2788 dispatchOnLauncherTransitionStep(toView, t);
2789 }
2790 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002791
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002792 // toView should appear right at the end of the workspace shrink
2793 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002794 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002795 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002796 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002797
2798 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002799 boolean animationCancelled = false;
2800
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002801 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002802 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803 // Prepare the position
2804 toView.setTranslationX(0.0f);
2805 toView.setTranslationY(0.0f);
2806 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002807 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002808 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002809 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002810 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002811 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2812 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002813
Winson Chungc7d2b602012-05-16 17:02:20 -07002814 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002815 if (mSearchDropTargetBar != null) {
2816 mSearchDropTargetBar.hideSearchBar(false);
2817 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002818 }
2819
Adam Cohencff6af82011-09-13 14:51:53 -07002820 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002821 public void onAnimationCancel(Animator animation) {
2822 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002823 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002824 });
2825
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002826 if (workspaceAnim != null) {
2827 mStateAnimation.play(workspaceAnim);
2828 }
Michael Jurka1899a362011-11-03 13:50:45 -07002829
2830 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002831
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002832 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2833 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002834
2835 // If any of the objects being animated haven't been measured/laid out
2836 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002837 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002838 (mWorkspace.getMeasuredWidth() == 0) ||
2839 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002840 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002841 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002842
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002843 final AnimatorSet stateAnimation = mStateAnimation;
2844 final Runnable startAnimRunnable = new Runnable() {
2845 public void run() {
2846 // Check that mStateAnimation hasn't changed while
2847 // we waited for a layout/draw pass
2848 if (mStateAnimation != stateAnimation)
2849 return;
2850 setPivotsForZoom(toView, scale);
2851 dispatchOnLauncherTransitionStart(fromView, animated, false);
2852 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002853 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002854 }
2855 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002856 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002857 final ViewTreeObserver observer = toView.getViewTreeObserver();
2858 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2859 public void onGlobalLayout() {
2860 startAnimRunnable.run();
2861 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2862 }
2863 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002864 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002865 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002866 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002867 } else {
2868 toView.setTranslationX(0.0f);
2869 toView.setTranslationY(0.0f);
2870 toView.setScaleX(1.0f);
2871 toView.setScaleY(1.0f);
2872 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002873 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002874
Daniel Sandlere4f98912013-06-25 15:13:26 -04002875 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002876 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002877 if (mSearchDropTargetBar != null) {
2878 mSearchDropTargetBar.hideSearchBar(false);
2879 }
Michael Jurkaabded662011-03-04 12:06:57 -08002880 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002881 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002882 dispatchOnLauncherTransitionStart(fromView, animated, false);
2883 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002884 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002885 dispatchOnLauncherTransitionStart(toView, animated, false);
2886 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002887 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002888 }
2889
2890 /**
2891 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002892 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002893 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002894 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002895 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2896 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002897
Michael Jurkab3e22d92011-10-31 15:58:33 -07002898 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002899 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002900 mStateAnimation.cancel();
2901 mStateAnimation = null;
2902 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002903 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002904
Winson Chungf0ea4d32011-06-06 14:27:16 -07002905 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002906 final int fadeOutDuration =
2907 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002908 final float scaleFactor = (float)
2909 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2910 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002911 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002912 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002913 if (toState == State.WORKSPACE) {
2914 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2915 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002916 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002917 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2918 workspaceAnim = mWorkspace.getChangeStateAnimation(
2919 Workspace.State.SPRING_LOADED, animated);
2920 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002921
Michael Jurkab3e22d92011-10-31 15:58:33 -07002922 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07002923 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002924 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002925 final LauncherViewPropertyAnimator scaleAnim =
2926 new LauncherViewPropertyAnimator(fromView);
2927 scaleAnim.
2928 scaleX(scaleFactor).scaleY(scaleFactor).
2929 setDuration(duration).
2930 setInterpolator(new Workspace.ZoomInInterpolator());
2931
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002932 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002933 .ofFloat(fromView, "alpha", 1f, 0f)
2934 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002935 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002936 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2937 @Override
2938 public void onAnimationUpdate(ValueAnimator animation) {
2939 float t = 1f - (Float) animation.getAnimatedValue();
2940 dispatchOnLauncherTransitionStep(fromView, t);
2941 dispatchOnLauncherTransitionStep(toView, t);
2942 }
2943 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002944
Michael Jurka2ecf9952012-06-18 12:52:28 -07002945 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002946
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002947 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2948 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002949 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002950
2951 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002952 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002953 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002954 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002955 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2956 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002957 if (onCompleteRunnable != null) {
2958 onCompleteRunnable.run();
2959 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002960 mAppsCustomizeContent.updateCurrentPageScroll();
2961 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002962 }
2963 });
2964
Winson Chungf0ea4d32011-06-06 14:27:16 -07002965 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002966 if (workspaceAnim != null) {
2967 mStateAnimation.play(workspaceAnim);
2968 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002969 dispatchOnLauncherTransitionStart(fromView, animated, true);
2970 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002971 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002972 } else {
2973 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002974 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002975 dispatchOnLauncherTransitionStart(fromView, animated, true);
2976 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002977 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002978 dispatchOnLauncherTransitionStart(toView, animated, true);
2979 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002980 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002981 }
2982
Michael Jurkae326f182011-11-21 14:05:46 -08002983 @Override
2984 public void onTrimMemory(int level) {
2985 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002986 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002987 mAppsCustomizeTabHost.onTrimMemory();
2988 }
2989 }
2990
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002991 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002992 showWorkspace(animated, null);
2993 }
2994
2995 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002996 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07002997 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002998 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002999 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003000
Winson Chungc7d2b602012-05-16 17:02:20 -07003001 // Show the search bar (only animate if we were showing the drop target bar in spring
3002 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003003 if (mSearchDropTargetBar != null) {
3004 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3005 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003006
Michael Jurkab3e22d92011-10-31 15:58:33 -07003007 // Set focus to the AppsCustomize button
3008 if (mAllAppsButton != null) {
3009 mAllAppsButton.requestFocus();
3010 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003011 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003012
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003013 // Change the state *after* we've called all the transition code
3014 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003015
Winson Chung5fb63472011-02-02 17:03:37 -08003016 // Resume the auto-advance of widgets
3017 mUserPresent = true;
3018 updateRunning();
3019
alanv1d4fde62012-10-17 13:15:47 -07003020 // Send an accessibility event to announce the context change
3021 getWindow().getDecorView()
3022 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003023
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003024 onWorkspaceShown(animated);
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07003025 onInteractionEnd();
Bjorn Bringertc459e522013-06-07 19:36:01 +01003026 }
3027
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003028 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003029 }
3030
Winson Chungc58497e2013-09-03 17:48:37 -07003031 void showAllApps(boolean animated,
3032 AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003033 if (mState != State.WORKSPACE) return;
3034
Winson Chungc58497e2013-09-03 17:48:37 -07003035 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003036 mAppsCustomizeTabHost.requestFocus();
3037
Michael Jurkab3e22d92011-10-31 15:58:33 -07003038 // Change the state *after* we've called all the transition code
3039 mState = State.APPS_CUSTOMIZE;
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07003040 onInteractionBegin();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003041
3042 // Pause the auto-advance of widgets until we are out of AllApps
3043 mUserPresent = false;
3044 updateRunning();
3045 closeFolder();
3046
3047 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003048 getWindow().getDecorView()
3049 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003050 }
3051
Adam Cohen7777d962011-08-18 18:58:38 -07003052 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003053 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003054 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003055 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003056 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003057 }
Adam Cohen7777d962011-08-18 18:58:38 -07003058
Adam Cohened66b2b2012-01-23 17:28:51 -08003059 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3060 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003061 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3062
Winson Chunge7a03942011-08-05 15:05:12 -07003063 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003064 @Override
3065 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003066 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003067 // Before we show workspace, hide all apps again because
3068 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3069 // clean up our state transition functions
3070 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003071 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003072 } else {
3073 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003074 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003075 }
3076 }, (extendedDelay ?
3077 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3078 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3079 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003080
Michael Jurkad3ef3062010-11-23 16:23:58 -08003081 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003082 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003083 final boolean animated = true;
3084 final boolean springLoaded = true;
3085 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003086 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003087 }
3088 // Otherwise, we are not in spring loaded mode, so don't do anything.
3089 }
3090
Michael Jurkab3e22d92011-10-31 15:58:33 -07003091 void lockAllApps() {
3092 // TODO
3093 }
3094
3095 void unlockAllApps() {
3096 // TODO
3097 }
3098
Winson Chungf0ea4d32011-06-06 14:27:16 -07003099 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003100 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003101 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003102 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003103 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003104 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003105 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003106 int duration = 0;
3107 if (mSearchDropTargetBar != null) {
3108 duration = mSearchDropTargetBar.getTransitionInDuration();
3109 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003110 mHotseat.animate().alpha(1f).setDuration(duration);
3111 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003112 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003113 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003114 }
3115 }
3116 }
3117
3118 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003119 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003120 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003121 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003122 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003123 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003124 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003125 int duration = 0;
3126 if (mSearchDropTargetBar != null) {
3127 duration = mSearchDropTargetBar.getTransitionOutDuration();
3128 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003129 mHotseat.animate().alpha(0f).setDuration(duration);
3130 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003131 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003132 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003133 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003134 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003135 }
3136
Patrick Dubroy5f445422011-02-18 14:35:21 -08003137 /**
3138 * Add an item from all apps or customize onto the given workspace screen.
3139 * If layout is null, add to the current screen.
3140 */
3141 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003142 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003143 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003144 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003145 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003146
Winson Chungdff8ebb2011-09-08 17:25:31 -07003147 /** Maps the current orientation to an index for referencing orientation correct global icons */
3148 private int getCurrentOrientationIndexForGlobalIcons() {
3149 // default - 0, landscape - 1
3150 switch (getResources().getConfiguration().orientation) {
3151 case Configuration.ORIENTATION_LANDSCAPE:
3152 return 1;
3153 default:
3154 return 0;
3155 }
3156 }
3157
Michael Jurkaae65ee32012-04-30 11:45:54 -07003158 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003159 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003160 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003161 // Look for the toolbar icon specified in the activity meta-data
3162 Bundle metaData = packageManager.getActivityInfo(
3163 activityName, PackageManager.GET_META_DATA).metaData;
3164 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003165 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003166 if (iconResId != 0) {
3167 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003168 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003169 }
3170 }
3171 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003172 // This can happen if the activity defines an invalid drawable
3173 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3174 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003175 } catch (Resources.NotFoundException nfe) {
3176 // This can happen if the activity defines an invalid drawable
3177 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3178 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003179 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003180 return null;
3181 }
3182
3183 // if successful in getting icon, return it; otherwise, set button to use default drawable
3184 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003185 int buttonId, ComponentName activityName, int fallbackDrawableId,
3186 String toolbarResourceName) {
3187 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003188 Resources r = getResources();
3189 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3190 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003191
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003192 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003193 // If we were unable to find the icon via the meta-data, use a generic one
3194 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003195 toolbarIcon = r.getDrawable(fallbackDrawableId);
3196 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003197 if (button != null) {
3198 button.setCompoundDrawables(toolbarIcon, null, null, null);
3199 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003200 return null;
3201 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003202 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003203 if (button != null) {
3204 button.setCompoundDrawables(toolbarIcon, null, null, null);
3205 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003206 return toolbarIcon.getConstantState();
3207 }
3208 }
3209
3210 // if successful in getting icon, return it; otherwise, set button to use default drawable
3211 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003212 int buttonId, ComponentName activityName, int fallbackDrawableId,
3213 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003214 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003215 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003216
Michael Jurka19e0fc52011-07-22 18:00:21 -07003217 if (button != null) {
3218 // If we were unable to find the icon via the meta-data, use a
3219 // generic one
3220 if (toolbarIcon == null) {
3221 button.setImageResource(fallbackDrawableId);
3222 } else {
3223 button.setImageDrawable(toolbarIcon);
3224 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003225 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003226
3227 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3228
Michael Jurka0423dcf2010-10-05 14:56:18 -07003229 }
3230
Winson Chung1b884092012-06-08 17:13:02 -07003231 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003232 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003233 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003234 }
3235
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003236 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003237 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003238 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003239 }
3240
Winson Chungbb185bd2011-11-21 12:31:42 -08003241 private void invalidatePressedFocusedStates(View container, View button) {
3242 if (container instanceof HolographicLinearLayout) {
3243 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3244 layout.invalidatePressedFocusedStates();
3245 } else if (button instanceof HolographicImageView) {
3246 HolographicImageView view = (HolographicImageView) button;
3247 view.invalidatePressedFocusedStates();
3248 }
3249 }
3250
Cristina Stancu476493b2013-08-07 17:20:14 +01003251 public View getQsbBar() {
3252 if (mQsbBar == null) {
3253 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3254 }
3255 return mQsbBar;
3256 }
3257
3258 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003259 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003260 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003262 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003263
Winson Chung1cad91e2011-05-25 17:41:01 -07003264 final SearchManager searchManager =
3265 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3266 ComponentName activityName = searchManager.getGlobalSearchActivity();
3267 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003268 int coi = getCurrentOrientationIndexForGlobalIcons();
3269 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003270 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3271 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3272 if (sGlobalSearchIcon[coi] == null) {
3273 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3274 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3275 TOOLBAR_ICON_METADATA_NAME);
3276 }
3277
Winson Chungc51db6a2011-10-05 11:44:49 -07003278 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3279 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003280 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003281 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003282 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003283 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003284 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3285 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003286 if (searchButton != null) searchButton.setVisibility(View.GONE);
3287 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003288 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003289 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003290 }
3291 }
3292
Cristina Stancu476493b2013-08-07 17:20:14 +01003293 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003294 final View searchButtonContainer = findViewById(R.id.search_button_container);
3295 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003296 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003297 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003298 }
3299
Cristina Stancu476493b2013-08-07 17:20:14 +01003300 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003301 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003302 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003303
Winson Chungc51db6a2011-10-05 11:44:49 -07003304 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003305 final SearchManager searchManager =
3306 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3307 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3308
3309 ComponentName activityName = null;
3310 if (globalSearchActivity != null) {
3311 // Check if the global search activity handles voice search
3312 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3313 intent.setPackage(globalSearchActivity.getPackageName());
3314 activityName = intent.resolveActivity(getPackageManager());
3315 }
3316
3317 if (activityName == null) {
3318 // Fallback: check if an activity other than the global search activity
3319 // resolves this
3320 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3321 activityName = intent.resolveActivity(getPackageManager());
3322 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003323 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003324 int coi = getCurrentOrientationIndexForGlobalIcons();
3325 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003326 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3327 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3328 if (sVoiceSearchIcon[coi] == null) {
3329 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3330 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3331 TOOLBAR_ICON_METADATA_NAME);
3332 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003333 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003334 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003335 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003336 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003337 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003338 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003339 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003340 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003341 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003342 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003343 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003344 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003345
Cristina Stancu476493b2013-08-07 17:20:14 +01003346 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003347 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3348 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003349 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003350 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003351 }
3352
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003353 public void setVoiceButtonProxyVisible(boolean visible) {
3354 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3355 if (voiceButtonProxy != null) {
3356 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3357 }
3358 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003359 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003360 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003361 */
3362 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003363 final View marketButton = findViewById(R.id.market_button);
3364 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3365 // Find the app market activity by resolving an intent.
3366 // (If multiple app markets are installed, it will return the ResolverActivity.)
3367 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003368 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003369 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003370 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003371 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003372 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3373 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003374 marketButton.setVisibility(View.VISIBLE);
3375 } else {
3376 // We should hide and disable the view so that we don't try and restore the visibility
3377 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3378 marketButton.setVisibility(View.GONE);
3379 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003380 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003381 }
3382
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003383 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003384 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3385 Resources r = getResources();
3386 Drawable marketIconDrawable = d.newDrawable(r);
3387 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3388 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3389 marketIconDrawable.setBounds(0, 0, w, h);
3390
3391 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003392 }
3393
Michael Jurkad7c28052012-04-27 15:43:36 -07003394 @Override
3395 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003396 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003397 final List<CharSequence> text = event.getText();
3398 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003399 // Populate event with a fake title based on the current state.
3400 if (mState == State.APPS_CUSTOMIZE) {
3401 text.add(getString(R.string.all_apps_button_label));
3402 } else {
3403 text.add(getString(R.string.all_apps_home_button_label));
3404 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003405 return result;
3406 }
3407
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003408 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003409 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003410 */
3411 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3412 @Override
3413 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003414 closeSystemDialogs();
3415 }
3416 }
3417
3418 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003419 * Receives notifications whenever the appwidgets are reset.
3420 */
3421 private class AppWidgetResetObserver extends ContentObserver {
3422 public AppWidgetResetObserver() {
3423 super(new Handler());
3424 }
3425
3426 @Override
3427 public void onChange(boolean selfChange) {
3428 onAppWidgetReset();
3429 }
3430 }
3431
3432 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003433 * If the activity is currently paused, signal that we need to run the passed Runnable
3434 * in onResume.
3435 *
3436 * This needs to be called from incoming places where resources might have been loaded
3437 * while we are paused. That is becaues the Configuration might be wrong
3438 * when we're not running, and if it comes back to what it was when we
3439 * were paused, we are not restarted.
3440 *
3441 * Implementation of the method from LauncherModel.Callbacks.
3442 *
3443 * @return true if we are currently paused. The caller might be able to
3444 * skip some work in that case since we will come back again.
3445 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003446 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003447 if (mPaused) {
3448 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003449 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003450 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003451 }
3452 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003453 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003454 return true;
3455 } else {
3456 return false;
3457 }
3458 }
3459
Michael Jurkac402cd92013-05-20 15:49:32 +02003460 private boolean waitUntilResume(Runnable run) {
3461 return waitUntilResume(run, false);
3462 }
3463
Michael Jurka1e2f4652013-07-08 18:03:46 -07003464 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003465 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003466 }
3467
Michael Jurka7607c2f2013-04-03 14:33:19 -07003468 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003469 * If the activity is currently paused, signal that we need to re-run the loader
3470 * in onResume.
3471 *
3472 * This needs to be called from incoming places where resources might have been loaded
3473 * while we are paused. That is becaues the Configuration might be wrong
3474 * when we're not running, and if it comes back to what it was when we
3475 * were paused, we are not restarted.
3476 *
3477 * Implementation of the method from LauncherModel.Callbacks.
3478 *
3479 * @return true if we are currently paused. The caller might be able to
3480 * skip some work in that case since we will come back again.
3481 */
3482 public boolean setLoadOnResume() {
3483 if (mPaused) {
3484 Log.i(TAG, "setLoadOnResume");
3485 mOnResumeNeedsLoad = true;
3486 return true;
3487 } else {
3488 return false;
3489 }
3490 }
3491
3492 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003494 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003495 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003496 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003497 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003498 } else {
3499 return SCREEN_COUNT / 2;
3500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003502
Joe Onorato9c1289c2009-08-17 11:03:03 -04003503 /**
3504 * Refreshes the shortcuts shown on the workspace.
3505 *
3506 * Implementation of the method from LauncherModel.Callbacks.
3507 */
3508 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003509 // If we're starting binding all over again, clear any bind calls we'd postponed in
3510 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3511 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003512 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003513
Winson Chungd64d1762013-08-20 14:37:16 -07003514 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003515 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003516 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003517
Michael Jurka05bf6442011-11-30 20:28:53 -08003518 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003519 if (mHotseat != null) {
3520 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003521 }
3522 }
3523
Adam Cohendcd297f2013-06-18 13:13:40 -07003524 @Override
3525 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003526 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003527
Adam Cohen5084cba2013-09-03 12:01:16 -07003528 // If there are no screens, we need to have an empty screen
3529 if (orderedScreenIds.size() == 0) {
3530 mWorkspace.addExtraEmptyScreen();
3531 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003532
3533 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003534 // setCurrentPage() so ensure that all pages are added before calling this).
3535 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003536 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3537 mWorkspace.createCustomContentPage();
3538 }
Winson Chung64359a52013-07-08 17:17:08 -07003539 }
3540
3541 @Override
3542 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003543 int count = orderedScreenIds.size();
3544 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003545 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003546 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003547 }
3548
Adam Cohen39a06042013-07-19 14:30:12 -07003549 private boolean shouldShowWeightWatcher() {
3550 String spKey = LauncherAppState.getSharedPreferencesKey();
3551 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003552 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003553
3554 return show;
3555 }
3556
Adam Cohen4caf2982013-08-20 18:54:31 -07003557 private boolean emailSent() {
3558 String spKey = LauncherAppState.getSharedPreferencesKey();
3559 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3560 boolean show = sp.getBoolean(CORRUPTION_EMAIL_SENT_KEY, false);
3561 return show;
3562 }
3563
3564 private void setEmailSent(boolean sent) {
3565 String spKey = LauncherAppState.getSharedPreferencesKey();
3566 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3567
3568 SharedPreferences.Editor editor = sp.edit();
3569 editor.putBoolean(CORRUPTION_EMAIL_SENT_KEY, sent);
3570 editor.commit();
3571 }
3572
Adam Cohen39a06042013-07-19 14:30:12 -07003573 private void toggleShowWeightWatcher() {
3574 String spKey = LauncherAppState.getSharedPreferencesKey();
3575 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3576 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3577
3578 show = !show;
3579
3580 SharedPreferences.Editor editor = sp.edit();
3581 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3582 editor.commit();
3583
3584 if (mWeightWatcher != null) {
3585 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3586 }
3587 }
3588
Winson Chungd64d1762013-08-20 14:37:16 -07003589 public void bindAppsAdded(final ArrayList<Long> newScreens,
3590 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003591 final ArrayList<ItemInfo> addAnimated,
3592 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003593 Runnable r = new Runnable() {
3594 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003595 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003596 }
3597 };
3598 if (waitUntilResume(r)) {
3599 return;
3600 }
3601
Winson Chungd64d1762013-08-20 14:37:16 -07003602 // Add the new screens
3603 bindAddScreens(newScreens);
3604
3605 // We add the items without animation on non-visible pages, and with
3606 // animations on the new page (which we will try and snap to).
3607 if (!addNotAnimated.isEmpty()) {
3608 bindItems(addNotAnimated, 0,
3609 addNotAnimated.size(), false);
3610 }
3611 if (!addAnimated.isEmpty()) {
3612 bindItems(addAnimated, 0,
3613 addAnimated.size(), true);
3614 }
Winson Chungc58497e2013-09-03 17:48:37 -07003615
3616 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3617 addedApps != null && mAppsCustomizeContent != null) {
3618 mAppsCustomizeContent.addApps(addedApps);
3619 }
Winson Chungd64d1762013-08-20 14:37:16 -07003620 }
3621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003622 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 * Bind the items start-end from the list.
3624 *
3625 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003626 */
Winson Chung64359a52013-07-08 17:17:08 -07003627 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3628 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003629 Runnable r = new Runnable() {
3630 public void run() {
3631 bindItems(shortcuts, start, end, forceAnimateIcons);
3632 }
3633 };
3634 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003635 return;
3636 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003637
Winson Chungf0c6ae02012-03-21 16:10:31 -07003638 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003639 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3640 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003641 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003642 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003643 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003644 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003645 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003646
3647 // Short circuit if we are loading dock items for a configuration which has no dock
3648 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3649 mHotseat == null) {
3650 continue;
3651 }
3652
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 switch (item.itemType) {
3654 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3655 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003656 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003657 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003658
Winson Chung64359a52013-07-08 17:17:08 -07003659 /*
3660 * TODO: FIX collision case
3661 */
Winson Chungce376632013-07-11 16:12:41 -07003662 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3663 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3664 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3665 throw new RuntimeException("OCCUPIED");
3666 }
Winson Chung64359a52013-07-08 17:17:08 -07003667 }
3668
Adam Cohendcd297f2013-06-18 13:13:40 -07003669 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3670 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003671 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003672 // Animate all the applications up now
3673 shortcut.setAlpha(0f);
3674 shortcut.setScaleX(0f);
3675 shortcut.setScaleY(0f);
3676 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003677 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003678 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003679 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003680 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003681 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003682 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003683 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003684 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3685 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003687 default:
3688 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003690 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003691
Winson Chung997a9232013-07-24 15:33:46 -07003692 if (animateIcons) {
3693 // Animate to the correct page
3694 if (newShortcutsScreenId > -1) {
3695 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3696 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3697 if (newShortcutsScreenId != currentScreenId) {
3698 mWorkspace.snapToPage(newScreenIndex);
3699 }
3700 }
3701
Winson Chung64359a52013-07-08 17:17:08 -07003702 // We post the animation slightly delayed to prevent slowdowns when we are loading
3703 // right after we return to launcher.
3704 mWorkspace.postDelayed(new Runnable() {
3705 public void run() {
3706 anim.playTogether(bounceAnims);
3707 anim.start();
3708 }
3709 }, NEW_APPS_ANIMATION_DELAY);
3710 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003711 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003712 }
3713
Joe Onorato9c1289c2009-08-17 11:03:03 -04003714 /**
3715 * Implementation of the method from LauncherModel.Callbacks.
3716 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003717 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003718 Runnable r = new Runnable() {
3719 public void run() {
3720 bindFolders(folders);
3721 }
3722 };
3723 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003724 return;
3725 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003726 sFolders.clear();
3727 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003728 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729
3730 /**
3731 * Add the views for a widget to the workspace.
3732 *
3733 * Implementation of the method from LauncherModel.Callbacks.
3734 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003735 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003736 Runnable r = new Runnable() {
3737 public void run() {
3738 bindAppWidget(item);
3739 }
3740 };
3741 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003742 return;
3743 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003744
Daniel Sandler843e8602010-06-07 14:59:01 -04003745 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3746 if (DEBUG_WIDGETS) {
3747 Log.d(TAG, "bindAppWidget: " + item);
3748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003749 final Workspace workspace = mWorkspace;
3750
3751 final int appWidgetId = item.appWidgetId;
3752 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003753 if (DEBUG_WIDGETS) {
3754 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3755 }
3756
Joe Onorato9c1289c2009-08-17 11:03:03 -04003757 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3758
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003760 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761
Adam Cohendcd297f2013-06-18 13:13:40 -07003762 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003763 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003764 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3765
Joe Onorato9c1289c2009-08-17 11:03:03 -04003766 workspace.requestLayout();
3767
Daniel Sandler843e8602010-06-07 14:59:01 -04003768 if (DEBUG_WIDGETS) {
3769 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3770 + (SystemClock.uptimeMillis()-start) + "ms");
3771 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003772 }
3773
Adam Cohen1462de32012-07-24 22:34:36 -07003774 public void onPageBoundSynchronously(int page) {
3775 mSynchronouslyBoundPages.add(page);
3776 }
3777
Joe Onorato9c1289c2009-08-17 11:03:03 -04003778 /**
3779 * Callback saying that there aren't any more items to bind.
3780 *
3781 * Implementation of the method from LauncherModel.Callbacks.
3782 */
Adam Cohene25af792013-06-06 23:08:25 -07003783 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003784 Runnable r = new Runnable() {
3785 public void run() {
3786 finishBindingItems(upgradePath);
3787 }
3788 };
3789 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003790 return;
3791 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 if (mSavedState != null) {
3793 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003794 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 mSavedState = null;
3797 }
3798
Adam Cohen1462de32012-07-24 22:34:36 -07003799 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003801 // If we received the result of any pending adds while the loader was running (e.g. the
3802 // widget configuration forced an orientation change), process them now.
3803 for (int i = 0; i < sPendingAddList.size(); i++) {
3804 completeAdd(sPendingAddList.get(i));
3805 }
3806 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807
Winson Chungc51db6a2011-10-05 11:44:49 -07003808 // Update the market app icon as necessary (the other icons will be managed in response to
3809 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003810 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003811
Winson Chungf0c6ae02012-03-21 16:10:31 -07003812 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003813 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003814 mWorkspace.getUniqueComponents(true, null);
3815 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003816 }
Adam Cohen99894d92013-06-14 11:22:59 -07003817
Adam Cohen66a01fd2013-06-11 12:48:00 -07003818 mWorkspace.post(new Runnable() {
3819 @Override
3820 public void run() {
3821 onFinishBindingItems();
3822 }
3823 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003824 }
3825
Winson Chunga2413752012-04-03 14:22:34 -07003826 private boolean canRunNewAppsAnimation() {
3827 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3828 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3829 }
3830
Winson Chungc9168342013-06-26 14:54:55 -07003831 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3832 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3833 PropertyValuesHolder.ofFloat("alpha", 1f),
3834 PropertyValuesHolder.ofFloat("scaleX", 1f),
3835 PropertyValuesHolder.ofFloat("scaleY", 1f));
3836 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3837 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3838 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3839 return bounceAnim;
3840 }
3841
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003842 @Override
3843 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003844 boolean searchVisible = updateGlobalSearchIcon();
3845 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003846 if (mSearchDropTargetBar != null) {
3847 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3848 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003849 }
3850
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003851 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 * Add the icons for all apps.
3853 *
3854 * Implementation of the method from LauncherModel.Callbacks.
3855 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003856 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003857 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3858 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3859 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3860 getHotseat().addAllAppsFolder(mIconCache, apps,
3861 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3862 }
3863 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003864 }
Winson Chungc58497e2013-09-03 17:48:37 -07003865 } else {
3866 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3867 mAppsCustomizeContent != null) {
3868 mAppsCustomizeContent.setApps(apps);
3869 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003871 }
3872
Bjorn Bringert85f418d2013-09-06 12:50:05 +01003873 @Override
3874 public boolean shouldShowApp(ResolveInfo app) {
3875 return true;
3876 }
3877
Joe Onorato9c1289c2009-08-17 11:03:03 -04003878 /**
3879 * A package was updated.
3880 *
3881 * Implementation of the method from LauncherModel.Callbacks.
3882 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003883 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003884 Runnable r = new Runnable() {
3885 public void run() {
3886 bindAppsUpdated(apps);
3887 }
3888 };
3889 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003890 return;
3891 }
3892
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003893 if (mWorkspace != null) {
3894 mWorkspace.updateShortcuts(apps);
3895 }
Winson Chungc58497e2013-09-03 17:48:37 -07003896
3897 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3898 mAppsCustomizeContent != null) {
3899 mAppsCustomizeContent.updateApps(apps);
3900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 }
3902
3903 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003904 * A package was uninstalled. We take both the super set of packageNames
3905 * in addition to specific applications to remove, the reason being that
3906 * this can be called when a package is updated as well. In that scenario,
3907 * we only remove specific components from the workspace, where as
3908 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003909 *
3910 * Implementation of the method from LauncherModel.Callbacks.
3911 */
Winson Chung83892cc2013-05-01 16:53:33 -07003912 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003913 final ArrayList<AppInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003914 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003915 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003916 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003917 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003918 }
Winson Chungd64d1762013-08-20 14:37:16 -07003919 };
3920 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003921 return;
3922 }
3923
Winson Chung64359a52013-07-08 17:17:08 -07003924 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003925 mWorkspace.removeItemsByPackageName(packageNames);
3926 } else {
3927 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003928 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003929
Winson Chunga1820962011-10-03 16:31:06 -07003930 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003931 mDragController.onAppsRemoved(appInfos, this);
Winson Chungc58497e2013-09-03 17:48:37 -07003932
3933 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3934 mAppsCustomizeContent != null) {
3935 mAppsCustomizeContent.removeApps(appInfos);
3936 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003937 }
Joe Onoratobe386092009-11-17 17:32:16 -08003938
3939 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003940 * A number of packages were updated.
3941 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003942 private ArrayList<Object> mWidgetsAndShortcuts;
3943 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003944 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003945 bindPackagesUpdated(mWidgetsAndShortcuts);
3946 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003947 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003948 };
3949
3950 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3951 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3952 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003953 return;
3954 }
3955
Winson Chung64359a52013-07-08 17:17:08 -07003956 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07003957 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3958 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003959 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003960 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003961 }
3962
Winson Chung400438b2011-01-16 17:53:48 -08003963 private int mapConfigurationOriActivityInfoOri(int configOri) {
3964 final Display d = getWindowManager().getDefaultDisplay();
3965 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3966 switch (d.getRotation()) {
3967 case Surface.ROTATION_0:
3968 case Surface.ROTATION_180:
3969 // We are currently in the same basic orientation as the natural orientation
3970 naturalOri = configOri;
3971 break;
3972 case Surface.ROTATION_90:
3973 case Surface.ROTATION_270:
3974 // We are currently in the other basic orientation to the natural orientation
3975 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3976 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3977 break;
3978 }
3979
3980 int[] oriMap = {
3981 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3982 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3983 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3984 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3985 };
3986 // Since the map starts at portrait, we need to offset if this device's natural orientation
3987 // is landscape.
3988 int indexOffset = 0;
3989 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3990 indexOffset = 1;
3991 }
3992 return oriMap[(d.getRotation() + indexOffset) % 4];
3993 }
Adam Cohen446e9402011-09-15 18:21:21 -07003994
Winson Chung4b919f82012-05-01 10:44:08 -07003995 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003996 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003997 getResources().getBoolean(R.bool.allow_rotation);
3998 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003999 }
Winson Chung4b919f82012-05-01 10:44:08 -07004000 public void lockScreenOrientation() {
4001 if (isRotationEnabled()) {
4002 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4003 .getConfiguration().orientation));
4004 }
4005 }
4006 public void unlockScreenOrientation(boolean immediate) {
4007 if (isRotationEnabled()) {
4008 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004009 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004010 } else {
4011 mHandler.postDelayed(new Runnable() {
4012 public void run() {
4013 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4014 }
4015 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004016 }
Winson Chung4b919f82012-05-01 10:44:08 -07004017 }
Winson Chung400438b2011-01-16 17:53:48 -08004018 }
4019
Winson Chung82f55532011-08-09 14:14:23 -07004020 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004021 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004022 if (DISABLE_CLINGS) {
4023 return false;
4024 }
4025
Brett Chabot2a9e2282011-08-23 15:03:13 -07004026 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004027 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004028
Michael Jurkae233a8b2013-03-19 13:49:20 +01004029 // Restricted secondary users (child mode) will potentially have very few apps
4030 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004031// boolean supportsLimitedUsers =
4032// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4033// Account[] accounts = AccountManager.get(this).getAccounts();
4034// if (supportsLimitedUsers && accounts.length == 0) {
4035// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4036// Bundle restrictions = um.getUserRestrictions();
4037// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4038// return false;
4039// }
4040// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004041 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004042 }
Michael Jurka22143132012-10-04 17:42:38 +02004043
Winson Chungaf40f202013-09-18 18:26:31 -07004044 private Cling initCling(int clingId, int[] positionData, boolean animate,
4045 boolean dimNavBarVisibilty) {
Michael Jurka22143132012-10-04 17:42:38 +02004046 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004047 if (cling != null) {
4048 cling.init(this, positionData);
Winson Chungaf40f202013-09-18 18:26:31 -07004049 cling.show(animate, SHOW_CLING_DURATION);
4050
4051 if (dimNavBarVisibilty) {
4052 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4053 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004054 }
4055 }
4056 return cling;
4057 }
Michael Jurka22143132012-10-04 17:42:38 +02004058
Winson Chungaf40f202013-09-18 18:26:31 -07004059 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4060 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004061 // To catch cases where siblings of top-level views are made invisible, just check whether
4062 // the cling is directly set to GONE before dismissing it.
4063 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004064 final Runnable cleanUpClingCb = new Runnable() {
4065 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004066 cling.cleanup();
Winson Chungaf40f202013-09-18 18:26:31 -07004067 /*
Winson Chung46353de2012-02-16 14:05:10 -08004068 // We should update the shared preferences on a background thread
4069 new Thread("dismissClingThread") {
4070 public void run() {
4071 SharedPreferences.Editor editor = mSharedPrefs.edit();
4072 editor.putBoolean(flag, true);
4073 editor.commit();
4074 }
4075 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004076 */
4077 if (postAnimationCb != null) {
4078 postAnimationCb.run();
4079 }
4080 }
4081 };
4082 if (duration <= 0) {
4083 cleanUpClingCb.run();
4084 } else {
4085 cling.hide(duration, cleanUpClingCb);
4086 }
Michael Jurka22143132012-10-04 17:42:38 +02004087 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004088
4089 if (restoreNavBarVisibilty) {
4090 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4091 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4092 }
Winson Chung82f55532011-08-09 14:14:23 -07004093 }
4094 }
Michael Jurka22143132012-10-04 17:42:38 +02004095
Winson Chung9d9d74f2011-09-19 11:49:12 -07004096 private void removeCling(int id) {
4097 final View cling = findViewById(id);
4098 if (cling != null) {
4099 final ViewGroup parent = (ViewGroup) cling.getParent();
4100 parent.post(new Runnable() {
4101 @Override
4102 public void run() {
4103 parent.removeView(cling);
4104 }
4105 });
Michael Jurka22143132012-10-04 17:42:38 +02004106 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004107 }
4108 }
Michael Jurka974c3862012-05-22 22:00:31 -07004109
4110 private boolean skipCustomClingIfNoAccounts() {
4111 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4112 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4113 if (customCling) {
4114 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004115 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004116 Account[] accounts = am.getAccountsByType("com.google");
4117 return accounts.length == 0;
4118 }
4119 return false;
4120 }
4121
Winson Chungaf40f202013-09-18 18:26:31 -07004122 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004123 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004124 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004125 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004126 // If we're not using the default workspace layout, replace workspace cling
4127 // with a custom workspace cling (usually specified in an overlay)
4128 // For now, only do this on tablets
4129 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004130 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004131 // Use a custom cling
4132 View cling = findViewById(R.id.workspace_cling);
4133 ViewGroup clingParent = (ViewGroup) cling.getParent();
4134 int clingIndex = clingParent.indexOfChild(cling);
4135 clingParent.removeViewAt(clingIndex);
4136 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4137 clingParent.addView(customCling, clingIndex);
4138 customCling.setId(R.id.workspace_cling);
4139 }
Winson Chungaf40f202013-09-18 18:26:31 -07004140 initCling(R.id.first_run_cling, null, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004141 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004142 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004143 }
4144 }
Winson Chungaf40f202013-09-18 18:26:31 -07004145
4146 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004147 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004148 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004149 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
4150 initCling(R.id.workspace_cling, null, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004151 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004152 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004153 }
4154 }
4155 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004156 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004157 if (isClingsEnabled() &&
4158 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungaf40f202013-09-18 18:26:31 -07004159 Cling cling = initCling(R.id.folder_cling, null, true, true);
4160 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004161 } else {
4162 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004163 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004164 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004165 }
Michael Jurka104c4562013-07-08 18:03:46 -07004166 protected SharedPreferences getSharedPrefs() {
4167 return mSharedPrefs;
4168 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004169 public boolean isFolderClingVisible() {
4170 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004171 if (cling != null) {
4172 return cling.getVisibility() == View.VISIBLE;
4173 }
4174 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004175 }
Winson Chungaf40f202013-09-18 18:26:31 -07004176 public void dismissFirstRunCling(View v) {
4177 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4178 Runnable cb = new Runnable() {
4179 public void run() {
4180 // Show the workspace cling next
4181 showFirstRunWorkspaceCling();
4182 }
4183 };
4184 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4185 DISMISS_CLING_DURATION, false);
4186 }
Winson Chung82f55532011-08-09 14:14:23 -07004187 public void dismissWorkspaceCling(View v) {
4188 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004189 Runnable cb = null;
4190 if (v == null) {
4191 cb = new Runnable() {
4192 public void run() {
4193 mWorkspace.enterOverviewMode();
4194 }
4195 };
4196 }
4197 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4198 DISMISS_CLING_DURATION, true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004199 }
4200 public void dismissFolderCling(View v) {
4201 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004202 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4203 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004204 }
4205
Winson Chung80baf5a2010-08-09 16:03:15 -07004206 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004207 * Prints out out state for debugging.
4208 */
4209 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004210 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004211 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004212 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4213 Log.d(TAG, "mRestoring=" + mRestoring);
4214 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4215 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004216 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004217 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004218
Winson Chung785d2eb2011-04-14 16:08:02 -07004219 if (mAppsCustomizeContent != null) {
4220 mAppsCustomizeContent.dumpState();
4221 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004222 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004223 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004224
4225 @Override
4226 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4227 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004228 synchronized (sDumpLogs) {
4229 writer.println(" ");
4230 writer.println("Debug logs: ");
4231 for (int i = 0; i < sDumpLogs.size(); i++) {
4232 writer.println(" " + sDumpLogs.get(i));
4233 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004234 }
4235 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004236
4237 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004238 if (DEBUG_DUMP_LOG) {
4239 synchronized (sDumpLogs) {
4240 Log.d(TAG, "");
4241 Log.d(TAG, "*********************");
4242 Log.d(TAG, "Launcher debug logs: ");
4243 for (int i = 0; i < sDumpLogs.size(); i++) {
4244 Log.d(TAG, " " + sDumpLogs.get(i));
4245 }
4246 Log.d(TAG, "*********************");
4247 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004248 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004249 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004250 }
4251
4252 public static void addDumpLog(String tag, String log, boolean debugLog) {
4253 if (debugLog) {
4254 Log.d(tag, log);
4255 }
Winson Chungede41292013-09-19 16:27:36 -07004256 if (DEBUG_DUMP_LOG) {
4257 sDateStamp.setTime(System.currentTimeMillis());
4258 synchronized (sDumpLogs) {
4259 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004260 }
Winson Chungede41292013-09-19 16:27:36 -07004261 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004262 }
4263
Winson Chungede41292013-09-19 16:27:36 -07004264 public void dumpLogsToLocalData() {
4265 if (DEBUG_DUMP_LOG) {
4266 new Thread("DumpLogsToLocalData") {
4267 @Override
4268 public void run() {
4269 boolean success = false;
4270 sDateStamp.setTime(sRunStart);
4271 String FILENAME = sDateStamp.getMonth() + "-"
4272 + sDateStamp.getDay() + "_"
4273 + sDateStamp.getHours() + "-"
4274 + sDateStamp.getMinutes() + "_"
4275 + sDateStamp.getSeconds() + ".txt";
4276
4277 FileOutputStream fos = null;
4278 File outFile = null;
4279 try {
4280 outFile = new File(getFilesDir(), FILENAME);
4281 outFile.createNewFile();
4282 fos = new FileOutputStream(outFile);
4283 } catch (Exception e) {
4284 e.printStackTrace();
4285 }
4286 if (fos != null) {
4287 PrintWriter writer = new PrintWriter(fos);
4288
4289 writer.println(" ");
4290 writer.println("Debug logs: ");
4291 synchronized (sDumpLogs) {
4292 for (int i = 0; i < sDumpLogs.size(); i++) {
4293 writer.println(" " + sDumpLogs.get(i));
4294 }
4295 }
4296 writer.close();
4297 }
4298 try {
4299 if (fos != null) {
4300 fos.close();
4301 success = true;
4302 }
4303 } catch (IOException e) {
4304 e.printStackTrace();
4305 }
4306 }
4307 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004308 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004310}
Michael Jurka2763be32011-02-24 11:19:57 -08004311
Michael Jurkaabded662011-03-04 12:06:57 -08004312interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004313 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004314 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004315 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004316 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004317 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004318}