blob: ba3ba932758210b7b0e840cf9c3fc56c167eea35 [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;
Mathew Inwood72fbec12013-11-19 15:45:07 +000048import 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;
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -070090import android.view.accessibility.AccessibilityManager;
Adam Cohencff6af82011-09-13 14:51:53 -070091import android.view.animation.AccelerateDecelerateInterpolator;
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 Cohenc0dcf592011-06-01 15:30:43 -0700106import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700109import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700115import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000116import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118/**
119 * Default launcher application.
120 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100121public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700122 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700123 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800124 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400128 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700129 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400130 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700131 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurka8b805b12012-04-18 14:23:14 -0700140 private static final int REQUEST_BIND_APPWIDGET = 11;
141
Mathew Inwood876a8462013-06-14 14:12:41 +0100142 /**
143 * IntentStarter uses request codes starting with this. This must be greater than all activity
144 * request codes used internally.
145 */
146 protected static final int REQUEST_LAST = 100;
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
149
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800150 static final int SCREEN_COUNT = 5;
151 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Romain Guy98d01652009-06-30 16:21:04 -0700153 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800154 // To turn on these properties, type
155 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
156 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
157 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Winson Chung2672ff92012-05-04 16:22:30 -0700159 // The Intent extra that defines whether to ignore the launch animation
160 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400161 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: int
164 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700165 // Type: int
166 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700168 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
169 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
171 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700172 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: boolean
176 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
177 // Type: long
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700179 // Type: int
180 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
183 // Type: parcelable
184 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000185 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000187 // Type: int[]
188 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700191 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100192 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700193 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100194 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700195
Adam Cohen39a06042013-07-19 14:30:12 -0700196 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700197 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700198
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700200 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700201 private State mState = State.WORKSPACE;
202 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700205 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
206 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Winson Chungaf40f202013-09-18 18:26:31 -0700207 private static final int SHOW_CLING_DURATION = 250;
208 private static final int DISMISS_CLING_DURATION = 200;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800211 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000213 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
214 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700217 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700219 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800221 private final BroadcastReceiver mCloseSystemDialogsReceiver
222 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800223 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 private LayoutInflater mInflater;
226
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800229 private DragLayer mDragLayer;
230 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700231 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700233 private AppWidgetManager mAppWidgetManager;
234 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Michael Jurkac9d95c52011-08-29 14:03:34 -0700236 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700237 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800238 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700239
Michael Jurka0280c3b2010-09-17 15:00:07 -0700240 private int[] mTmpAddItemCellCoordinates = new int[2];
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 private FolderInfo mFolderInfo;
243
Winson Chung3d503fb2011-07-13 17:25:49 -0700244 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700245 private View mOverviewPanel;
246
Michael Jurka838a4ca2011-02-07 13:33:06 -0800247 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700248
Winson Chungc51db6a2011-10-05 11:44:49 -0700249 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700250 private AppsCustomizeTabHost mAppsCustomizeTabHost;
251 private AppsCustomizePagedView mAppsCustomizeContent;
252 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100253 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700256 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
257 // scroll issues (because the workspace may not have been measured yet) and extra work.
258 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
259 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
261 private SpannableStringBuilder mDefaultKeySsb = null;
262
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263 private boolean mWorkspaceLoading = true;
264
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800265 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private boolean mRestoring;
267 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700268 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
Michael Jurka1e2f4652013-07-08 18:03:46 -0700270 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700271 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
272
Winson Chung4a2afa32012-07-19 14:53:05 -0700273 // Keep track of whether the user has left launcher
274 private static boolean sPausedFromUserAction = false;
275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private Bundle mSavedInstanceState;
277
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800280 private boolean mUserPresent = true;
281 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700282 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800283 private boolean mAttached = false;
Winson Chungfa545132013-09-26 17:45:32 -0700284 private static final boolean DISABLE_CLINGS = false;
Winson Chunge6eabff2013-09-24 11:01:23 -0700285 private static final boolean DISABLE_CUSTOM_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700287 private static LocaleConfiguration sLocaleConfiguration = null;
288
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700289 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700290
Adam Cohen61f560d2013-09-30 15:58:20 -0700291 private View.OnTouchListener mHapticFeedbackTouchListener;
292
Adam Cohended9f8d2010-11-03 13:25:16 -0700293 // Related to the auto-advancing of widgets
294 private final int ADVANCE_MSG = 1;
295 private final int mAdvanceInterval = 20000;
296 private final int mAdvanceStagger = 250;
297 private long mAutoAdvanceSentTime;
298 private long mAutoAdvanceTimeLeft = -1;
299 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
300 new HashMap<View, AppWidgetProviderInfo>();
301
Winson Chung400438b2011-01-16 17:53:48 -0800302 // Determines how long to wait after a rotation before restoring the screen orientation to
303 // match the sensor state.
304 private final int mRestoreScreenOrientationDelay = 500;
305
Michael Jurka4ef207b2010-11-29 17:05:45 -0800306 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700307 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
308 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
309 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800310
Winson Chungd64a6662013-09-30 11:06:59 -0700311 private Intent mAppMarketIntent = null;
312 private static final boolean DISABLE_MARKET_BUTTON = true;
313
Craig Mautner360310b2012-10-26 15:13:08 -0700314 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700315
Adam Cohen1462de32012-07-24 22:34:36 -0700316 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700317 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700318
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700319 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700320 static Date sDateStamp = new Date();
321 static DateFormat sDateFormat =
322 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
323 static long sRunStart = System.currentTimeMillis();
324 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700325
Winson Chung46353de2012-02-16 14:05:10 -0800326 // We only want to get the SharedPreferences once since it does an FS stat each time we get
327 // it from the context.
328 private SharedPreferences mSharedPrefs;
329
Adam Cohene25af792013-06-06 23:08:25 -0700330 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
331
Winson Chungf0c6ae02012-03-21 16:10:31 -0700332 // Holds the page that we need to animate to, and the icon views that we need to animate up
333 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700334 private ImageView mFolderIconImageView;
335 private Bitmap mFolderIconBitmap;
336 private Canvas mFolderIconCanvas;
337 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700338
Michael Jurkaddd62e92011-02-16 17:49:14 -0800339 private BubbleTextView mWaitingForResume;
340
Michael Jurka22143132012-10-04 17:42:38 +0200341 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
342 = new HideFromAccessibilityHelper();
343
Michael Jurkac1f5d262011-09-30 19:32:27 -0700344 private Runnable mBuildLayersRunnable = new Runnable() {
345 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700346 if (mWorkspace != null) {
347 mWorkspace.buildPageHardwareLayers();
348 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 }
350 };
351
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800352 private static ArrayList<PendingAddArguments> sPendingAddList
353 = new ArrayList<PendingAddArguments>();
354
Michael Jurka7267fa52013-09-26 15:29:57 -0700355 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800356
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357 private static class PendingAddArguments {
358 int requestCode;
359 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700360 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700361 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 int cellX;
363 int cellY;
364 }
365
Daniel Sandlerff02d492013-08-05 02:12:05 -0400366 private Stats mStats;
367
Michael Jurka0a457bf2012-11-19 14:05:05 -0800368 private static boolean isPropertyEnabled(String propertyName) {
369 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700370 }
371
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 @Override
373 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700374 if (DEBUG_STRICT_MODE) {
375 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
376 .detectDiskReads()
377 .detectDiskWrites()
378 .detectNetwork() // or .detectAll() for all detectable problems
379 .penaltyLog()
380 .build());
381 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
382 .detectLeakedSqlLiteObjects()
383 .detectLeakedClosableObjects()
384 .penaltyLog()
385 .penaltyDeath()
386 .build());
387 }
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400390
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400391 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400392 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700393
394 // Determine the dynamic grid properties
395 Point smallestSize = new Point();
396 Point largestSize = new Point();
397 Point realSize = new Point();
398 Display display = getWindowManager().getDefaultDisplay();
399 display.getCurrentSizeRange(smallestSize, largestSize);
400 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700401 DisplayMetrics dm = new DisplayMetrics();
402 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700403
Winson Chung5f8afe62013-08-12 16:19:28 -0700404 // Lazy-initialize the dynamic grid
405 DeviceProfile grid = app.initDynamicGrid(this,
406 Math.min(smallestSize.x, smallestSize.y),
407 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700408 realSize.x, realSize.y,
409 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700410
411 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400412 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700413 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800414 mModel = app.setLauncher(this);
415 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700416 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400417 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700419
Daniel Sandlerff02d492013-08-05 02:12:05 -0400420 mStats = new Stats(this);
421
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700422 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700423
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700424 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
425 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700427 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
428 // this also ensures that any synchronous binding below doesn't re-trigger another
429 // LauncherModel load.
430 mPaused = false;
431
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200433 android.os.Debug.startMethodTracing(
434 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 }
436
Adam Cohen53805212013-10-01 10:39:23 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100442 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800444 registerContentObservers();
445
Joe Onorato7c312c12009-08-13 21:36:53 -0700446 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 mSavedState = savedInstanceState;
449 restoreState(mSavedState);
450
451 if (PROFILE_STARTUP) {
452 android.os.Debug.stopMethodTracing();
453 }
454
455 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700456 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700457 // If the user leaves launcher, then we should just load items asynchronously when
458 // they return.
459 mModel.startLoader(true, -1);
460 } else {
461 // We only load the page synchronously if the user rotates (or triggers a
462 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800463 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
466
467 // For handling default keys
468 mDefaultKeySsb = new SpannableStringBuilder();
469 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800470
471 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
472 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800473
Adam Cohenf9426d52012-06-04 17:26:21 -0700474 updateGlobalIcons();
475
476 // On large interfaces, we want the screen to auto-rotate based on the current orientation
477 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700478
479 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700480 }
481
Winson Chung4a2afa32012-07-19 14:53:05 -0700482 protected void onUserLeaveHint() {
483 super.onUserLeaveHint();
484 sPausedFromUserAction = true;
485 }
486
Winson Chung98ca0f72013-07-29 12:58:51 -0700487 /** To be overriden by subclasses to hint to Launcher that we have custom content */
488 protected boolean hasCustomContentToLeft() {
489 return false;
490 }
491
Dave Hawkeya8881582013-09-17 15:55:33 -0600492 /**
493 * To be overridden by subclasses to create the custom content and call
494 * {@link #addToCustomContentPage}. This will only be invoked if
495 * {@link #hasCustomContentToLeft()} is {@code true}.
496 */
497 protected void addCustomContentToLeft() {
498 }
499
500 /**
501 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
502 * ensure the custom content page is added or removed if necessary.
503 */
504 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600505 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600506 // Not bound yet, wait for bindScreens to be called.
507 return;
508 }
509
510 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
511 // Create the custom content page and call the subclass to populate it.
512 mWorkspace.createCustomContentPage();
513 addCustomContentToLeft();
514 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
515 mWorkspace.removeCustomContentPage();
516 }
517 }
518
Adam Cohenf9426d52012-06-04 17:26:21 -0700519 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700520 boolean searchVisible = false;
521 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800522 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700523 int coi = getCurrentOrientationIndexForGlobalIcons();
524 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
525 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700526 if (!DISABLE_MARKET_BUTTON) {
527 updateAppMarketIcon();
528 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700529 searchVisible = updateGlobalSearchIcon();
530 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700531 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700532 if (sGlobalSearchIcon[coi] != null) {
533 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700534 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700535 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700536 if (sVoiceSearchIcon[coi] != null) {
537 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700538 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700539 }
Winson Chungd64a6662013-09-30 11:06:59 -0700540 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700541 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800542 }
Winson Chungadf0c182012-08-23 14:59:07 -0700543 if (mSearchDropTargetBar != null) {
544 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
545 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 }
Romain Guycbb89e42009-06-08 15:52:54 -0700547
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700549 if (sLocaleConfiguration == null) {
550 new AsyncTask<Void, Void, LocaleConfiguration>() {
551 @Override
552 protected LocaleConfiguration doInBackground(Void... unused) {
553 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
554 readConfiguration(Launcher.this, localeConfiguration);
555 return localeConfiguration;
556 }
557
558 @Override
559 protected void onPostExecute(LocaleConfiguration result) {
560 sLocaleConfiguration = result;
561 checkForLocaleChange(); // recursive, but now with a locale configuration
562 }
563 }.execute();
564 return;
565 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700566
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 final Configuration configuration = getResources().getConfiguration();
568
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700569 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 final String locale = configuration.locale.toString();
571
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700572 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 final int mcc = configuration.mcc;
574
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700575 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 final int mnc = configuration.mnc;
577
Romain Guy84f296c2009-11-04 15:00:44 -0800578 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579
Romain Guy84f296c2009-11-04 15:00:44 -0800580 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581 sLocaleConfiguration.locale = locale;
582 sLocaleConfiguration.mcc = mcc;
583 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700584
Joe Onorato0589f0f2010-02-08 13:44:00 -0800585 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586
587 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
588 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700589 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700590 public void run() {
591 writeConfiguration(Launcher.this, localeConfiguration);
592 }
593 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700594 }
595 }
596
597 private static class LocaleConfiguration {
598 public String locale;
599 public int mcc = -1;
600 public int mnc = -1;
601 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700602
Romain Guy98d01652009-06-30 16:21:04 -0700603 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
604 DataInputStream in = null;
605 try {
606 in = new DataInputStream(context.openFileInput(PREFERENCES));
607 configuration.locale = in.readUTF();
608 configuration.mcc = in.readInt();
609 configuration.mnc = in.readInt();
610 } catch (FileNotFoundException e) {
611 // Ignore
612 } catch (IOException e) {
613 // Ignore
614 } finally {
615 if (in != null) {
616 try {
617 in.close();
618 } catch (IOException e) {
619 // Ignore
620 }
621 }
622 }
623 }
624
625 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
626 DataOutputStream out = null;
627 try {
628 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
629 out.writeUTF(configuration.locale);
630 out.writeInt(configuration.mcc);
631 out.writeInt(configuration.mnc);
632 out.flush();
633 } catch (FileNotFoundException e) {
634 // Ignore
635 } catch (IOException e) {
636 //noinspection ResultOfMethodCallIgnored
637 context.getFileStreamPath(PREFERENCES).delete();
638 } finally {
639 if (out != null) {
640 try {
641 out.close();
642 } catch (IOException e) {
643 // Ignore
644 }
645 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
647 }
648
Anton Hanssona2f665f2013-09-26 12:18:32 +0100649 public Stats getStats() {
650 return mStats;
651 }
652
Bjorn Bringertc459e522013-06-07 19:36:01 +0100653 public LayoutInflater getInflater() {
654 return mInflater;
655 }
656
Adam Cohen716b51e2011-06-30 12:09:54 -0700657 public DragLayer getDragLayer() {
658 return mDragLayer;
659 }
660
Winson Chung36a62fe2012-05-06 18:04:42 -0700661 boolean isDraggingEnabled() {
662 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
663 // that is subsequently removed from the workspace in startBinding().
664 return !mModel.isLoadingWorkspace();
665 }
666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 static int getScreen() {
668 synchronized (sLock) {
669 return sScreen;
670 }
671 }
672
673 static void setScreen(int screen) {
674 synchronized (sLock) {
675 sScreen = screen;
676 }
677 }
678
Winson Chung557d6ed2011-07-08 15:34:52 -0700679 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000680 * Copied from View -- the View version of the method isn't called
681 * anywhere else in our process and only exists for API level 17+,
682 * so it's ok to keep our own version with no API requirement.
683 */
684 public static int generateViewId() {
685 for (;;) {
686 final int result = sNextGeneratedId.get();
687 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
688 int newValue = result + 1;
689 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
690 if (sNextGeneratedId.compareAndSet(result, newValue)) {
691 return result;
692 }
693 }
694 }
695
696 public int getViewIdForItem(ItemInfo info) {
697 // This cast is safe given the > 2B range for int.
698 int itemId = (int) info.id;
699 if (mItemIdToViewId.containsKey(itemId)) {
700 return mItemIdToViewId.get(itemId);
701 }
702 int viewId = generateViewId();
703 mItemIdToViewId.put(itemId, viewId);
704 return viewId;
705 }
706
707 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700708 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
709 * a configuration step, this allows the proper animations to run after other transitions.
710 */
711 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700712 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800713 switch (args.requestCode) {
714 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700715 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700716 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800717 break;
718 case REQUEST_PICK_SHORTCUT:
719 processShortcut(args.intent);
720 break;
721 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700723 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700724 result = true;
725 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800726 case REQUEST_CREATE_APPWIDGET:
727 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700729 result = true;
730 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 }
Michael Jurka27614d22012-04-02 06:40:22 -0700732 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
733 // if you turned the screen off and then back while in All Apps, Launcher would not
734 // return to the workspace. Clearing mAddInfo.container here fixes this issue
735 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700736 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800737 }
738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700740 protected void onActivityResult(
741 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700742 // Reset the startActivity waiting flag
743 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800744 int pendingAddWidgetId = mPendingAddWidgetId;
745 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700746
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 Runnable exitSpringLoaded = new Runnable() {
748 @Override
749 public void run() {
750 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
751 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
752 }
753 };
754
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
758 if (resultCode == RESULT_CANCELED) {
759 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
761 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
764 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 }
766 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200767 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
768 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
769 mWorkspace.exitOverviewMode(false);
770 }
771 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200773
Adam Cohened66b2b2012-01-23 17:28:51 -0800774 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
775 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700776
Adam Cohened66b2b2012-01-23 17:28:51 -0800777 // We have special handling for widgets
778 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800779 final int appWidgetId;
780 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
781 : -1;
782 if (widgetId < 0) {
783 appWidgetId = pendingAddWidgetId;
784 } else {
785 appWidgetId = widgetId;
786 }
787
Adam Cohenad4e15c2013-10-17 16:21:35 -0700788 final int result;
789 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800790 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700791 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
792 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700793 result = RESULT_CANCELED;
794 completeTwoStageWidgetDrop(result, appWidgetId);
795 onComplete = new Runnable() {
796 @Override
797 public void run() {
798 exitSpringLoadedDragModeDelayed(false, 0, null);
799 }
800 };
Winson Chung5aaab772012-04-27 15:24:02 -0700801 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700802 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700803 final CellLayout dropLayout =
804 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
805 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 onComplete = new Runnable() {
807 @Override
808 public void run() {
809 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700810 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700811 }
812 };
Winson Chung5aaab772012-04-27 15:24:02 -0700813 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700814 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
815 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800816 return;
817 }
818
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 // The pattern used here is that a user PICKs a specific application,
820 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
823 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700824 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800825 final PendingAddArguments args = new PendingAddArguments();
826 args.requestCode = requestCode;
827 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700828 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700829 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700830 args.cellX = mPendingAddInfo.cellX;
831 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800832 if (isWorkspaceLocked()) {
833 sPendingAddList.add(args);
834 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700835 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700837 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
838 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700839 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700840 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
841 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800843 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800844 }
845
846 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700847 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800849 Runnable onCompleteRunnable = null;
850 int animationType = 0;
851
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700852 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800853 if (resultCode == RESULT_OK) {
854 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
855 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700856 mPendingAddWidgetInfo);
857 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800858 onCompleteRunnable = new Runnable() {
859 @Override
860 public void run() {
861 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700862 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
864 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 }
866 };
867 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800868 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700871 if (mDragLayer.getAnimatedView() != null) {
872 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
873 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
874 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700875 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700876 // The animated view may be null in the case of a rotation during widget configuration
877 onCompleteRunnable.run();
878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
880
881 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700882 protected void onStop() {
883 super.onStop();
884 FirstFrameAnimatorHelper.setIsVisible(false);
885 }
886
887 @Override
888 protected void onStart() {
889 super.onStart();
890 FirstFrameAnimatorHelper.setIsVisible(true);
891 }
892
893 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200895 long startTime = 0;
896 if (DEBUG_RESUME_TIME) {
897 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400898 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700901
Winson Chung4a2afa32012-07-19 14:53:05 -0700902 // Restore the previous launcher state
903 if (mOnResumeState == State.WORKSPACE) {
904 showWorkspace(false);
905 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -0800906 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700907 }
908 mOnResumeState = State.NONE;
909
Craig Mautner360310b2012-10-26 15:13:08 -0700910 // Background was set to gradient in onPause(), restore to black if in all apps.
911 setWorkspaceBackground(mState == State.WORKSPACE);
912
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800913 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700914 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700915 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700917 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700919 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700921 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200922 // We might have postponed some bind calls until onResume (see waitUntilResume) --
923 // execute them here
924 long startTimeCallbacks = 0;
925 if (DEBUG_RESUME_TIME) {
926 startTimeCallbacks = System.currentTimeMillis();
927 }
928
929 if (mAppsCustomizeContent != null) {
930 mAppsCustomizeContent.setBulkBind(true);
931 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700932 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
933 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200934 }
935 if (mAppsCustomizeContent != null) {
936 mAppsCustomizeContent.setBulkBind(false);
937 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700938 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200939 if (DEBUG_RESUME_TIME) {
940 Log.d(TAG, "Time spent processing callbacks in onResume: " +
941 (System.currentTimeMillis() - startTimeCallbacks));
942 }
Michael Jurka447bf842013-05-15 14:52:15 +0200943 }
Michael Jurka54554252013-08-01 12:52:23 +0200944 if (mOnResumeCallbacks.size() > 0) {
945 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
946 mOnResumeCallbacks.get(i).run();
947 }
948 mOnResumeCallbacks.clear();
949 }
Winson Chunge4e50662012-01-23 14:45:13 -0800950
951 // Reset the pressed state of icons that were locked in the press state while activities
952 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800953 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800954 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800955 mWaitingForResume.setStayPressed(false);
956 }
Winson Chunge4e50662012-01-23 14:45:13 -0800957 if (mAppsCustomizeContent != null) {
958 // Resets the previous all apps icon press state
959 mAppsCustomizeContent.resetDrawableState();
960 }
Winson Chung82963d52013-10-09 11:20:57 -0700961
Adam Cohen06dff352012-06-01 17:17:08 -0700962 // It is possible that widgets can receive updates while launcher is not in the foreground.
963 // Consequently, the widgets will be inflated in the orientation of the foreground activity
964 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
965 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700966 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700967
Winson Chung780fe592013-09-26 14:48:44 -0700968 // Process any items that were added while Launcher was away.
969 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
970
Winson Chung5841efa2013-09-30 18:06:44 -0700971 // Update the voice search button proxy
972 updateVoiceButtonProxyVisible(false);
973
Adam Cohenf9426d52012-06-04 17:26:21 -0700974 // Again, as with the above scenario, it's possible that one or more of the global icons
975 // were updated in the wrong orientation.
976 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200977 if (DEBUG_RESUME_TIME) {
978 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
979 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700980
981 if (mWorkspace.getCustomContentCallbacks() != null) {
982 // If we are resuming and the custom content is the current page, we call onShow().
983 // It is also poassible that onShow will instead be called slightly after first layout
984 // if PagedView#setRestorePage was set to the custom content page in onCreate().
985 if (mWorkspace.isOnOrMovingToCustomContent()) {
986 mWorkspace.getCustomContentCallbacks().onShow();
987 }
988 }
Adam Cohenedaaa302013-10-01 17:33:27 -0700989 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -0700990 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -0700991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700994 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700995 // Ensure that items added to Launcher are queued until Launcher returns
996 InstallShortcutReceiver.enableInstallQueue();
997
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700998 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700999 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001000 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001001 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001002
1003 // We call onHide() aggressively. The custom content callbacks should be able to
1004 // debounce excess onHide calls.
1005 if (mWorkspace.getCustomContentCallbacks() != null) {
1006 mWorkspace.getCustomContentCallbacks().onHide();
1007 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001008 }
Romain Guycbb89e42009-06-08 15:52:54 -07001009
Adam Cohen66a01fd2013-06-11 12:48:00 -07001010 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -07001011 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -06001012 addCustomContentToLeft();
1013 }
Adam Cohen66a01fd2013-06-11 12:48:00 -07001014 }
1015
Adam Cohenbffe7452013-07-22 18:21:45 -07001016 QSBScroller mQsbScroller = new QSBScroller() {
1017 int scrollY = 0;
1018
1019 @Override
1020 public void setScrollY(int scroll) {
1021 scrollY = scroll;
1022
1023 if (mWorkspace.isOnOrMovingToCustomContent()) {
1024 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001025 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001026 }
1027 }
1028 };
1029
1030 public void resetQSBScroll() {
1031 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001032 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001033 }
1034
1035 public interface CustomContentCallbacks {
1036 // Custom content is completely shown
1037 public void onShow();
1038
1039 // Custom content is completely hidden
1040 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001041
1042 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1043 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001044 }
1045
Adam Cohen30bacb22013-08-29 14:25:25 -07001046 protected void startSettings() {
1047 }
1048
Adam Cohenbffe7452013-07-22 18:21:45 -07001049 public interface QSBScroller {
1050 public void setScrollY(int scrollY);
1051 }
1052
Winson Chung98ca0f72013-07-29 12:58:51 -07001053 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001054 CustomContentCallbacks callbacks, String description) {
1055 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001056 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001057 }
1058
Adam Cohenedb40762013-07-18 16:45:45 -07001059 // The custom content needs to offset its content to account for the QSB
1060 public int getTopOffsetForCustomContent() {
1061 return mWorkspace.getPaddingTop();
1062 }
1063
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001064 @Override
1065 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001066 // Flag the loader to stop early before switching
1067 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001068 if (mAppsCustomizeContent != null) {
1069 mAppsCustomizeContent.surrender();
1070 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001071 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001072 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001074 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001075 @Override
1076 public void onWindowFocusChanged(boolean hasFocus) {
1077 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001078 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001079 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 private boolean acceptFilter() {
1082 final InputMethodManager inputManager = (InputMethodManager)
1083 getSystemService(Context.INPUT_METHOD_SERVICE);
1084 return !inputManager.isFullscreenMode();
1085 }
1086
1087 @Override
1088 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001089 final int uniChar = event.getUnicodeChar();
1090 final boolean handled = super.onKeyDown(keyCode, event);
1091 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1092 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1094 keyCode, event);
1095 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001096 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001097 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001098 // showSearchDialog()
1099 // If there are multiple keystrokes before the search dialog takes focus,
1100 // onSearchRequested() will be called for every keystroke,
1101 // but it is idempotent, so it's fine.
1102 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 }
1104 }
1105
Joe Onorato8a9625e2010-01-28 15:55:35 -08001106 // Eat the long press event so the keyboard doesn't come up.
1107 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1108 return true;
1109 }
1110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 return handled;
1112 }
1113
Karl Rosaen138a0412009-04-23 19:00:21 -07001114 private String getTypedText() {
1115 return mDefaultKeySsb.toString();
1116 }
1117
1118 private void clearTypedText() {
1119 mDefaultKeySsb.clear();
1120 mDefaultKeySsb.clearSpans();
1121 Selection.setSelection(mDefaultKeySsb, 0);
1122 }
1123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001125 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1126 * State
1127 */
1128 private static State intToState(int stateOrdinal) {
1129 State state = State.WORKSPACE;
1130 final State[] stateValues = State.values();
1131 for (int i = 0; i < stateValues.length; i++) {
1132 if (stateValues[i].ordinal() == stateOrdinal) {
1133 state = stateValues[i];
1134 break;
1135 }
1136 }
1137 return state;
1138 }
1139
1140 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 * Restores the previous state, if it exists.
1142 *
1143 * @param savedState The previous state.
1144 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001145 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 private void restoreState(Bundle savedState) {
1147 if (savedState == null) {
1148 return;
1149 }
1150
Michael Jurka883f55b2010-10-21 15:47:14 -07001151 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001152 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001153 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001154 }
1155
Adam Cohen21cd0022013-10-09 18:57:02 -07001156 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1157 PagedView.INVALID_RESTORE_PAGE);
1158 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001159 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 }
1161
Winson Chung3d503fb2011-07-13 17:25:49 -07001162 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001163 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001164
Winson Chung3d503fb2011-07-13 17:25:49 -07001165 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1166 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001167 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001168 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1169 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001170 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1171 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1172 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001173 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001174 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 mRestoring = true;
1176 }
1177
1178 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1179 if (renameFolder) {
1180 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001181 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 mRestoring = true;
1183 }
Winson Chunga12a2502010-12-20 14:41:35 -08001184
Winson Chung785d2eb2011-04-14 16:08:02 -07001185 // Restore the AppsCustomize tab
1186 if (mAppsCustomizeTabHost != null) {
1187 String curTab = savedState.getString("apps_customize_currentTab");
1188 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001189 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001190 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001191 mAppsCustomizeContent.loadAssociatedPages(
1192 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001193 }
1194
Winson Chung5afbf7b2011-07-25 11:53:08 -07001195 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1196 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001197 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001198 mItemIdToViewId = (HashMap<Integer, Integer>)
1199 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 }
1201
1202 /**
1203 * Finds all the views we need and configure them properly.
1204 */
1205 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001206 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001207
Craig Mautner360310b2012-10-26 15:13:08 -07001208 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001209 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1210 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001211
John Spurlock77e1f472013-09-11 10:09:51 -04001212 mLauncherView.setSystemUiVisibility(
1213 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001214 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215
Winson Chung4c98d922011-05-31 16:50:48 -07001216 // Setup the drag layer
1217 mDragLayer.setup(this, dragController);
1218
Winson Chung3d503fb2011-07-13 17:25:49 -07001219 // Setup the hotseat
1220 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1221 if (mHotseat != null) {
1222 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001223 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001224 }
1225
Adam Cohenf358a4b2013-07-23 16:47:31 -07001226 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001227 View widgetButton = findViewById(R.id.widget_button);
1228 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001229 @Override
1230 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001231 if (!mWorkspace.isSwitchingState()) {
1232 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1233 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001234 }
1235 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001236 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1237
1238 View wallpaperButton = findViewById(R.id.wallpaper_button);
1239 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001240 @Override
1241 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001242 if (!mWorkspace.isSwitchingState()) {
1243 startWallpaper();
1244 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001245 }
1246 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001247 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1248
1249 View settingsButton = findViewById(R.id.settings_button);
1250 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001251 @Override
1252 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001253 if (!mWorkspace.isSwitchingState()) {
1254 startSettings();
1255 }
Adam Cohen30bacb22013-08-29 14:25:25 -07001256 }
1257 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001258 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001259 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001260
Winson Chung4c98d922011-05-31 16:50:48 -07001261 // Setup the workspace
1262 mWorkspace.setHapticFeedbackEnabled(false);
1263 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001264 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001265 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001266
Winson Chungf0ea4d32011-06-06 14:27:16 -07001267 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001268 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001269
Winson Chungf0ea4d32011-06-06 14:27:16 -07001270 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001271 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001272 mAppsCustomizeContent = (AppsCustomizePagedView)
1273 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1274 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001275
Winson Chung3d503fb2011-07-13 17:25:49 -07001276 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001277 dragController.setDragScoller(mWorkspace);
1278 dragController.setScrollView(mDragLayer);
1279 dragController.setMoveTarget(mWorkspace);
1280 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001281 if (mSearchDropTargetBar != null) {
1282 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001283 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001284
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001285 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001286 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001287 mWeightWatcher = new WeightWatcher(this);
1288 mWeightWatcher.setAlpha(0.5f);
1289 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001290 new FrameLayout.LayoutParams(
1291 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001292 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001293 Gravity.BOTTOM)
1294 );
Adam Cohen39a06042013-07-19 14:30:12 -07001295
1296 boolean show = shouldShowWeightWatcher();
1297 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
1300
1301 /**
1302 * Creates a view representing a shortcut.
1303 *
1304 * @param info The data structure describing the shortcut.
1305 *
1306 * @return A View inflated from R.layout.application.
1307 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001308 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001310 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312
1313 /**
1314 * Creates a view representing a shortcut inflated from the specified resource.
1315 *
1316 * @param layoutResId The id of the XML layout used to create the shortcut.
1317 * @param parent The group the shortcut belongs to.
1318 * @param info The data structure describing the shortcut.
1319 *
1320 * @return A View inflated from layoutResId.
1321 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001322 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001323 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1324 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 return favorite;
1327 }
1328
1329 /**
1330 * Add an application shortcut to the workspace.
1331 *
1332 * @param data The intent describing the application.
1333 * @param cellInfo The position on screen where to create the shortcut.
1334 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001335 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001336 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001337 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001338
Adam Cohenfbba09b2011-07-18 21:43:05 -07001339 // First we check if we already know the exact location where we want to add this item.
1340 if (cellX >= 0 && cellY >= 0) {
1341 cellXY[0] = cellX;
1342 cellXY[1] = cellY;
1343 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001344 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001345 return;
1346 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001348 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001349
Romain Guy73b979d2009-06-09 12:57:21 -07001350 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001351 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001353 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001354 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001355 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001356 } else {
1357 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
1359 }
Romain Guycbb89e42009-06-08 15:52:54 -07001360
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 /**
1362 * Add a shortcut to the workspace.
1363 *
1364 * @param data The intent describing the shortcut.
1365 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001367 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001368 int cellY) {
1369 int[] cellXY = mTmpAddItemCellCoordinates;
1370 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001371 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001372
Michael Jurkad3ef3062010-11-23 16:23:58 -08001373 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001374
Adam Cohen558baaf2011-08-15 15:22:57 -07001375 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001376 if (info == null) {
1377 return;
1378 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001379 final View view = createShortcut(info);
1380
Adam Cohenfbba09b2011-07-18 21:43:05 -07001381 // First we check if we already know the exact location where we want to add this item.
1382 if (cellX >= 0 && cellY >= 0) {
1383 cellXY[0] = cellX;
1384 cellXY[1] = cellY;
1385 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001386
1387 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001388 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001389 true, null,null)) {
1390 return;
1391 }
1392 DragObject dragObject = new DragObject();
1393 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001394 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1395 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001396 return;
1397 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001398 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001399 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001400 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001401 foundCellSpan = (result != null);
1402 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001403 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001404 }
1405
1406 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001407 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001408 return;
1409 }
1410
Adam Cohendcd297f2013-06-18 13:13:40 -07001411 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412
1413 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001414 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001415 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
1417 }
1418
Adam Cohen2f093b62012-04-30 18:59:53 -07001419 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1420 int minHeight) {
1421 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001422 // We want to account for the extra amount of padding that we are adding to the widget
1423 // to ensure that it gets the full amount of space that it has requested
1424 int requiredWidth = minWidth + padding.left + padding.right;
1425 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001426 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001427 }
1428
Adam Cohen2f093b62012-04-30 18:59:53 -07001429 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1430 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001431 }
1432
Adam Cohen2f093b62012-04-30 18:59:53 -07001433 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1434 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001435 }
1436
Adam Cohen2f093b62012-04-30 18:59:53 -07001437 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1438 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001439 }
1440
Adam Cohen2f093b62012-04-30 18:59:53 -07001441 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1442 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001443 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001444 }
1445
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001447 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001449 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001450 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001452 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001453 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1454 if (appWidgetInfo == null) {
1455 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1456 }
Romain Guycbb89e42009-06-08 15:52:54 -07001457
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001458 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001459 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001460
Adam Cohen2f093b62012-04-30 18:59:53 -07001461 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1462 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001463
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001465 // We have saved the position to which the widget was dragged-- this really only matters
1466 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001467 int[] cellXY = mTmpAddItemCellCoordinates;
1468 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001469 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001470 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001471 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1472 cellXY[0] = mPendingAddInfo.cellX;
1473 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001474 spanXY[0] = mPendingAddInfo.spanX;
1475 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001476 foundCellSpan = true;
1477 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001478 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001479 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001480 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1481 spanXY[1], cellXY, finalSpan);
1482 spanXY[0] = finalSpan[0];
1483 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001484 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001485 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001486 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001487 }
1488
Michael Jurka0280c3b2010-09-17 15:00:07 -07001489 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001490 if (appWidgetId != -1) {
1491 // Deleting an app widget ID is a void call but writes to disk before returning
1492 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001493 new Thread("deleteAppWidgetId") {
1494 public void run() {
1495 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1496 }
1497 }.start();
1498 }
Winson Chung93eef082012-03-23 15:59:27 -07001499 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001500 return;
1501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001503 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001504 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1505 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001506 launcherInfo.spanX = spanXY[0];
1507 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001508 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1509 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001510
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001512 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513
1514 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001515 if (hostView == null) {
1516 // Perform actual inflation because we're live
1517 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1518 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1519 } else {
1520 // The AppWidgetHostView has already been inflated and instantiated
1521 launcherInfo.hostView = hostView;
1522 }
Romain Guycbb89e42009-06-08 15:52:54 -07001523
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001525 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001526 launcherInfo.notifyWidgetSizeChanged(this);
1527
Adam Cohendcd297f2013-06-18 13:13:40 -07001528 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001529 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001530
1531 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001533 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
Romain Guycbb89e42009-06-08 15:52:54 -07001535
Adam Cohended9f8d2010-11-03 13:25:16 -07001536 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1537 @Override
1538 public void onReceive(Context context, Intent intent) {
1539 final String action = intent.getAction();
1540 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1541 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001542 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001543 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001544
Winson Chungc100e8e2011-08-09 16:02:43 -07001545 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001546 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001547 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001548 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001549 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001550 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1551 mUserPresent = true;
1552 updateRunning();
1553 }
1554 }
1555 };
1556
1557 @Override
1558 public void onAttachedToWindow() {
1559 super.onAttachedToWindow();
1560
1561 // Listen for broadcasts related to user-presence
1562 final IntentFilter filter = new IntentFilter();
1563 filter.addAction(Intent.ACTION_SCREEN_OFF);
1564 filter.addAction(Intent.ACTION_USER_PRESENT);
1565 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001566 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001567 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001568 mVisible = true;
1569 }
1570
1571 @Override
1572 public void onDetachedFromWindow() {
1573 super.onDetachedFromWindow();
1574 mVisible = false;
1575
Adam Cohend113e0c2010-11-11 10:48:05 -08001576 if (mAttached) {
1577 unregisterReceiver(mReceiver);
1578 mAttached = false;
1579 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001580 updateRunning();
1581 }
1582
1583 public void onWindowVisibilityChanged(int visibility) {
1584 mVisible = visibility == View.VISIBLE;
1585 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001586 // The following code used to be in onResume, but it turns out onResume is called when
1587 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1588 // is a more appropriate event to handle
1589 if (mVisible) {
1590 mAppsCustomizeTabHost.onWindowVisible();
1591 if (!mWorkspaceLoading) {
1592 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001593 // We want to let Launcher draw itself at least once before we force it to build
1594 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001595 // apps is nice and speedy.
1596 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001597 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001598 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001599 if (mStarted) return;
1600 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001601 // We delay the layer building a bit in order to give
1602 // other message processing a time to run. In particular
1603 // this avoids a delay in hiding the IME if it was
1604 // currently shown, because doing that may involve
1605 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001606 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001607 final ViewTreeObserver.OnDrawListener listener = this;
1608 mWorkspace.post(new Runnable() {
1609 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001610 if (mWorkspace != null &&
1611 mWorkspace.getViewTreeObserver() != null) {
1612 mWorkspace.getViewTreeObserver().
1613 removeOnDrawListener(listener);
1614 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001615 }
1616 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001617 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001618 }
1619 });
1620 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001621 // When Launcher comes back to foreground, a different Activity might be responsible for
1622 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001623 if (!DISABLE_MARKET_BUTTON) {
1624 updateAppMarketIcon();
1625 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001626 clearTypedText();
1627 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 }
1629
1630 private void sendAdvanceMessage(long delay) {
1631 mHandler.removeMessages(ADVANCE_MSG);
1632 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1633 mHandler.sendMessageDelayed(msg, delay);
1634 mAutoAdvanceSentTime = System.currentTimeMillis();
1635 }
1636
1637 private void updateRunning() {
1638 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1639 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1640 mAutoAdvanceRunning = autoAdvanceRunning;
1641 if (autoAdvanceRunning) {
1642 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1643 sendAdvanceMessage(delay);
1644 } else {
1645 if (!mWidgetsToAdvance.isEmpty()) {
1646 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1647 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1648 }
1649 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001650 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 }
1652 }
1653 }
1654
1655 private final Handler mHandler = new Handler() {
1656 @Override
1657 public void handleMessage(Message msg) {
1658 if (msg.what == ADVANCE_MSG) {
1659 int i = 0;
1660 for (View key: mWidgetsToAdvance.keySet()) {
1661 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1662 final int delay = mAdvanceStagger * i;
1663 if (v instanceof Advanceable) {
1664 postDelayed(new Runnable() {
1665 public void run() {
1666 ((Advanceable) v).advance();
1667 }
1668 }, delay);
1669 }
1670 i++;
1671 }
1672 sendAdvanceMessage(mAdvanceInterval);
1673 }
1674 }
1675 };
1676
1677 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001678 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1680 if (v instanceof Advanceable) {
1681 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001682 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001683 updateRunning();
1684 }
1685 }
1686
1687 void removeWidgetToAutoAdvance(View hostView) {
1688 if (mWidgetsToAdvance.containsKey(hostView)) {
1689 mWidgetsToAdvance.remove(hostView);
1690 updateRunning();
1691 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001692 }
1693
Joe Onorato9c1289c2009-08-17 11:03:03 -04001694 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001695 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001696 launcherInfo.hostView = null;
1697 }
1698
Winson Chung93eef082012-03-23 15:59:27 -07001699 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1700 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1701 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 }
1703
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001704 public LauncherAppWidgetHost getAppWidgetHost() {
1705 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 }
Romain Guycbb89e42009-06-08 15:52:54 -07001707
Winson Chunga9abd0e2010-10-27 17:18:37 -07001708 public LauncherModel getModel() {
1709 return mModel;
1710 }
1711
Bjorn Bringertc459e522013-06-07 19:36:01 +01001712 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001713 getWindow().closeAllPanels();
1714
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001715 // Whatever we were doing is hereby canceled.
1716 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001717 }
1718
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 @Override
1720 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001721 long startTime = 0;
1722 if (DEBUG_RESUME_TIME) {
1723 startTime = System.currentTimeMillis();
1724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 super.onNewIntent(intent);
1726
1727 // Close the menu
1728 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001729 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001730 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001731
Adam Cohened307df2013-10-02 09:37:31 -07001732 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1733 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1734 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001735
Adam Cohen6fecd412013-10-02 17:41:50 -07001736 if (mWorkspace == null) {
1737 // Can be cases where mWorkspace is null, this prevents a NPE
1738 return;
1739 }
1740 Folder openFolder = mWorkspace.getOpenFolder();
1741 // In all these cases, only animate if we're already on home
1742 mWorkspace.exitWidgetResizeMode();
1743 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001744 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001745 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001746 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001747
Adam Cohen6fecd412013-10-02 17:41:50 -07001748 closeFolder();
1749 exitSpringLoadedDragMode();
1750
1751 // If we are already on home, then just animate back to the workspace,
1752 // otherwise, just wait until onResume to set the state back to Workspace
1753 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001754 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001755 } else {
1756 mOnResumeState = State.WORKSPACE;
1757 }
1758
1759 final View v = getWindow().peekDecorView();
1760 if (v != null && v.getWindowToken() != null) {
1761 InputMethodManager imm = (InputMethodManager)getSystemService(
1762 INPUT_METHOD_SERVICE);
1763 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1764 }
1765
1766 // Reset the apps customize page
1767 if (mAppsCustomizeTabHost != null) {
1768 mAppsCustomizeTabHost.reset();
1769 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001770
1771 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 }
Adam Cohened307df2013-10-02 09:37:31 -07001773
Michael Jurka447bf842013-05-15 14:52:15 +02001774 if (DEBUG_RESUME_TIME) {
1775 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1776 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
1778
Adam Coppa120b8e2013-11-12 16:26:04 +00001779 /**
1780 * Override point for subclasses to prevent movement to the default screen when the home
1781 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1782 */
1783 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1784 return true;
1785 }
1786
1787 /**
1788 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1789 */
1790 protected void onHomeIntent() {
1791 // Do nothing
1792 }
1793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001795 public void onRestoreInstanceState(Bundle state) {
1796 super.onRestoreInstanceState(state);
1797 for (int page: mSynchronouslyBoundPages) {
1798 mWorkspace.restoreInstanceStateForChild(page);
1799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
1801
1802 @Override
1803 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001804 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001805 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1806 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001807 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001808 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809
Michael Jurka883f55b2010-10-21 15:47:14 -07001810 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001811 // We close any open folder since it will not be re-opened, and we need to make sure
1812 // this state is reflected.
1813 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814
Adam Cohendcd297f2013-06-18 13:13:40 -07001815 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001816 mWaitingForResult) {
1817 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001818 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001819 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1820 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001821 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1822 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1823 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001824 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 }
1826
1827 if (mFolderInfo != null && mWaitingForResult) {
1828 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1829 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1830 }
Michael Jurka946ad472010-07-09 18:05:18 -07001831
Winson Chung785d2eb2011-04-14 16:08:02 -07001832 // Save the current AppsCustomize tab
1833 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001834 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1835 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001836 if (currentTabTag != null) {
1837 outState.putString("apps_customize_currentTab", currentTabTag);
1838 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001839 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1840 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001841 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001842 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 }
1844
1845 @Override
1846 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001848
Winson Chunge7a03942011-08-05 15:05:12 -07001849 // Remove all pending runnables
1850 mHandler.removeMessages(ADVANCE_MSG);
1851 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001852 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001853
Winson Chungcd2b0142011-06-08 16:02:26 -07001854 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001855 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001856 mModel.stopLoader();
1857 app.setLauncher(null);
1858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001860 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001862 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001864 mAppWidgetHost = null;
1865
1866 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867
1868 TextKeyListener.getInstance().release();
1869
Winson Chung81b52252012-08-27 15:34:29 -07001870 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1871 // to prevent leaking Launcher activities on orientation change.
1872 if (mModel != null) {
1873 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1874 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001875
1876 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001877 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001878
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001879 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001880 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1881 mWorkspace.removeAllViews();
1882 mWorkspace = null;
1883 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001884
Michael Jurka2ecf9952012-06-18 12:52:28 -07001885 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 }
1887
Adam Cohena9cf38f2011-05-02 15:36:58 -07001888 public DragController getDragController() {
1889 return mDragController;
1890 }
1891
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 @Override
1893 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001894 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 super.startActivityForResult(intent, requestCode);
1896 }
1897
Winson Chung70d72102011-08-12 11:24:35 -07001898 /**
1899 * Indicates that we want global search for this activity by setting the globalSearch
1900 * argument for {@link #startSearch} to true.
1901 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001903 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001905
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001906 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001907
Karl Rosaen138a0412009-04-23 19:00:21 -07001908 if (initialQuery == null) {
1909 // Use any text typed in the launcher as the initial query
1910 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 if (appSearchData == null) {
1913 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001914 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 }
Winson Chungadf0c182012-08-23 14:59:07 -07001916 Rect sourceBounds = new Rect();
1917 if (mSearchDropTargetBar != null) {
1918 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1919 }
Romain Guycbb89e42009-06-08 15:52:54 -07001920
Bjorn Bringertc459e522013-06-07 19:36:01 +01001921 startSearch(initialQuery, selectInitialQuery,
1922 appSearchData, sourceBounds);
1923 }
1924
1925 public void startSearch(String initialQuery,
1926 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001927 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001928 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001929 }
1930
1931 /**
1932 * Starts the global search activity. This code is a copied from SearchManager
1933 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001934 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001935 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001936 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001937 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1938 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1939 if (globalSearchActivity == null) {
1940 Log.w(TAG, "No global search activity found.");
1941 return;
1942 }
1943 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1944 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1945 intent.setComponent(globalSearchActivity);
1946 // Make sure that we have a Bundle to put source in
1947 if (appSearchData == null) {
1948 appSearchData = new Bundle();
1949 } else {
1950 appSearchData = new Bundle(appSearchData);
1951 }
1952 // Set source to package name of app that starts global search, if not set already.
1953 if (!appSearchData.containsKey("source")) {
1954 appSearchData.putString("source", getPackageName());
1955 }
1956 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1957 if (!TextUtils.isEmpty(initialQuery)) {
1958 intent.putExtra(SearchManager.QUERY, initialQuery);
1959 }
1960 if (selectInitialQuery) {
1961 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1962 }
1963 intent.setSourceBounds(sourceBounds);
1964 try {
1965 startActivity(intent);
1966 } catch (ActivityNotFoundException ex) {
1967 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
Winson Chung70d72102011-08-12 11:24:35 -07001971 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001972 public boolean onPrepareOptionsMenu(Menu menu) {
1973 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001974 if (!mWorkspace.isInOverviewMode()) {
1975 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001976 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001977 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001980 @Override
1981 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001982 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001983 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001984 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001985 }
1986
Joe Onorato9c1289c2009-08-17 11:03:03 -04001987 public boolean isWorkspaceLocked() {
1988 return mWorkspaceLoading || mWaitingForResult;
1989 }
1990
Michael Jurka0280c3b2010-09-17 15:00:07 -07001991 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001992 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001993 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001994 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1995 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001996 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001997 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001998 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001999
Adam Cohenad4e15c2013-10-17 16:21:35 -07002000 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2001 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2002 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2003 }
2004
2005 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2006 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2007 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002008 if (appWidgetInfo.configure != null) {
2009 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002010 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002011
Bjorn Bringert7984c942009-12-09 15:38:25 +00002012 // Launch over to configure widget, if needed
2013 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002014 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002015 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002016 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002018 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002019 Runnable onComplete = new Runnable() {
2020 @Override
2021 public void run() {
2022 // Exit spring loaded mode if necessary after adding the widget
2023 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2024 null);
2025 }
2026 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002027 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002028 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002029 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 }
2031 }
Romain Guycbb89e42009-06-08 15:52:54 -07002032
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002033 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002034 // Close any folders that may be open.
2035 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002036 mWorkspace.moveToCustomContentScreen(animate);
2037 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002038 /**
2039 * Process a shortcut drop.
2040 *
2041 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002042 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002043 * @param cell The cell it should be added to, optional
2044 * @param position The location on the screen where it was dropped, optional
2045 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002046 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002047 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002048 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002049 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002050 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002051 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002052
2053 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002054 mPendingAddInfo.cellX = cell[0];
2055 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002056 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002057
2058 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2059 createShortcutIntent.setComponent(componentName);
2060 processShortcut(createShortcutIntent);
2061 }
2062
Adam Cohenfbba09b2011-07-18 21:43:05 -07002063 /**
2064 * Process a widget drop.
2065 *
2066 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002067 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002068 * @param cell The cell it should be added to, optional
2069 * @param position The location on the screen where it was dropped, optional
2070 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002071 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002072 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002073 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002074 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002075 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002076 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002077 mPendingAddInfo.minSpanX = info.minSpanX;
2078 mPendingAddInfo.minSpanY = info.minSpanY;
2079
Adam Cohenfbba09b2011-07-18 21:43:05 -07002080 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002081 mPendingAddInfo.cellX = cell[0];
2082 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002083 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002084 if (span != null) {
2085 mPendingAddInfo.spanX = span[0];
2086 mPendingAddInfo.spanY = span[1];
2087 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002088
Adam Cohened66b2b2012-01-23 17:28:51 -08002089 AppWidgetHostView hostView = info.boundWidget;
2090 int appWidgetId;
2091 if (hostView != null) {
2092 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002093 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002094 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002095 // In this case, we either need to start an activity to get permission to bind
2096 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002097 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002098 Bundle options = info.bindOptions;
2099
2100 boolean success = false;
2101 if (options != null) {
2102 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2103 info.componentName, options);
2104 } else {
2105 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2106 info.componentName);
2107 }
2108 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002109 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002110 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002111 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002112 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2113 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2114 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002115 // TODO: we need to make sure that this accounts for the options bundle.
2116 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002117 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2118 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002119 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002120 }
2121
Joe Onoratodeb98af2010-02-19 14:59:39 -08002122 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002123 // Handle case where user selected "Applications"
2124 String applicationName = getResources().getString(R.string.group_applications);
2125 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002126
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002127 if (applicationName != null && applicationName.equals(shortcutName)) {
2128 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2129 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002130
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002131 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2132 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002133 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002134 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002135 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002136 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 }
2139
Winson Chung24ab2f12010-09-16 14:10:47 -07002140 void processWallpaper(Intent intent) {
2141 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2142 }
2143
Adam Cohendcd297f2013-06-18 13:13:40 -07002144 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002145 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002146 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 folderInfo.title = getText(R.string.folder_name);
2148
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002150 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002151 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002152 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153
2154 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002155 FolderIcon newFolder =
2156 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002157 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002158 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002159 // Force measure the new folder icon
2160 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2161 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002162 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 }
Romain Guycbb89e42009-06-08 15:52:54 -07002164
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002166 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002167 }
2168
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002169 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002171 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002172 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 }
2174
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002175 protected ComponentName getWallpaperPickerComponent() {
2176 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2177 }
2178
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002179 /**
2180 * Registers various content observers. The current implementation registers
2181 * only a favorites observer to keep track of the favorites applications.
2182 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002183 private void registerContentObservers() {
2184 ContentResolver resolver = getContentResolver();
2185 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2186 true, mWidgetObserver);
2187 }
2188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 @Override
2190 public boolean dispatchKeyEvent(KeyEvent event) {
2191 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2192 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002194 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002195 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002196 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002197 dumpState();
2198 return true;
2199 }
2200 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002201 }
2202 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2203 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002204 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 return true;
2206 }
2207 }
2208
2209 return super.dispatchKeyEvent(event);
2210 }
2211
Joe Onorato88ec0992009-11-19 13:16:06 -08002212 @Override
2213 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002214 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002215 if (mAppsCustomizeContent.getContentType() ==
2216 AppsCustomizePagedView.ContentType.Applications) {
2217 showWorkspace(true);
2218 } else {
2219 showOverviewMode(true);
2220 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002221 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002222 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002223 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002224 Folder openFolder = mWorkspace.getOpenFolder();
2225 if (openFolder.isEditingName()) {
2226 openFolder.dismissEditingName();
2227 } else {
2228 closeFolder();
2229 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002230 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002231 mWorkspace.exitWidgetResizeMode();
2232
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002233 // Back button is a no-op here, but give at least some feedback for the button press
2234 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002235 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002236 }
2237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002239 * Re-listen when widgets are reset.
2240 */
2241 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002242 if (mAppWidgetHost != null) {
2243 mAppWidgetHost.startListening();
2244 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002245 }
2246
2247 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 * Launches the intent referred by the clicked shortcut.
2249 *
2250 * @param v The view representing the clicked shortcut.
2251 */
2252 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002253 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2254 // view has detached (it's possible for this to happen if the view is removed mid touch).
2255 if (v.getWindowToken() == null) {
2256 return;
2257 }
2258
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002259 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002260 return;
2261 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002262
Adam Cohen1697b792013-09-17 19:08:21 -07002263 if (v instanceof Workspace) {
2264 if (mWorkspace.isInOverviewMode()) {
2265 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002266 }
2267 return;
2268 }
2269
2270 if (v instanceof CellLayout) {
2271 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002272 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002273 }
2274 }
2275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002277 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002279 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2280 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002281
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002282 // Check for special shortcuts
2283 if (intent.getComponent() != null) {
2284 final String shortcutClass = intent.getComponent().getClassName();
2285
2286 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002287 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002288 return;
2289 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2290 MemoryDumpActivity.startDump(this);
2291 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002292 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2293 toggleShowWeightWatcher();
2294 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002295 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002296 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002297
2298 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002299 int[] pos = new int[2];
2300 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002301 intent.setSourceBounds(new Rect(pos[0], pos[1],
2302 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002303
2304 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002305
Daniel Sandlerff02d492013-08-05 02:12:05 -04002306 mStats.recordLaunch(intent, shortcut);
2307
Michael Jurkaddd62e92011-02-16 17:49:14 -08002308 if (success && v instanceof BubbleTextView) {
2309 mWaitingForResume = (BubbleTextView) v;
2310 mWaitingForResume.setStayPressed(true);
2311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002313 if (v instanceof FolderIcon) {
2314 FolderIcon fi = (FolderIcon) v;
2315 handleFolderClick(fi);
2316 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002317 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002318 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002319 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002320 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002321 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 }
2324 }
2325
Michael Jurka0e260592010-06-30 17:07:39 -07002326 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002327 return false;
2328 }
2329
Michael Jurkaaf442092010-06-10 17:01:57 -07002330 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002331 * Event handler for the search button
2332 *
2333 * @param v The view that was clicked.
2334 */
2335 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002336 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2337
Amith Yamasania135ba82011-08-09 17:42:01 -07002338 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002339 }
2340
2341 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002342 * Event handler for the voice button
2343 *
2344 * @param v The view that was clicked.
2345 */
2346 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002347 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002348
Bjorn Bringertc459e522013-06-07 19:36:01 +01002349 startVoice();
2350 }
2351
2352 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002353 try {
2354 final SearchManager searchManager =
2355 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2356 ComponentName activityName = searchManager.getGlobalSearchActivity();
2357 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002358 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002359 if (activityName != null) {
2360 intent.setPackage(activityName.getPackageName());
2361 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002362 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002363 } catch (ActivityNotFoundException e) {
2364 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002365 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002366 startActivitySafely(null, intent, "onClickVoiceButton");
2367 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002368 }
2369
2370 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002371 * Event handler for the "grid" button that appears on the home screen, which
2372 * enters all apps mode.
2373 *
2374 * @param v The view that was clicked.
2375 */
2376 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002377 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002378 }
2379
2380 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002381 // Provide the same haptic feedback that the system offers for virtual keys.
2382 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002383 }
2384
Adam Cohen61f560d2013-09-30 15:58:20 -07002385 public void performHapticFeedbackOnTouchDown(View v) {
2386 // Provide the same haptic feedback that the system offers for virtual keys.
2387 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2388 }
2389
2390 public View.OnTouchListener getHapticFeedbackTouchListener() {
2391 if (mHapticFeedbackTouchListener == null) {
2392 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2393 @Override
2394 public boolean onTouch(View v, MotionEvent event) {
2395 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2396 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2397 }
2398 return false;
2399 }
2400 };
2401 }
2402 return mHapticFeedbackTouchListener;
2403 }
2404
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002405 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002406 if (!DISABLE_MARKET_BUTTON) {
2407 if (mAppMarketIntent != null) {
2408 startActivitySafely(v, mAppMarketIntent, "app market");
2409 } else {
2410 Log.e(TAG, "Invalid app market intent.");
2411 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002412 }
2413 }
2414
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002415 /**
2416 * Called when the user stops interacting with the launcher.
2417 * This implies that the user is now on the homescreen and is not doing housekeeping.
2418 */
2419 protected void onInteractionEnd() {}
2420
2421 /**
2422 * Called when the user starts interacting with the launcher.
2423 * The possible interactions are:
2424 * - open all apps
2425 * - reorder an app shortcut, or a widget
2426 * - open the overview mode.
2427 * This is a good time to stop doing things that only make sense
2428 * when the user is on the homescreen and not doing housekeeping.
2429 */
2430 protected void onInteractionBegin() {}
2431
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002432 void startApplicationDetailsActivity(ComponentName componentName) {
2433 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002434 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2435 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002436 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002437 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002438 }
2439
Michael Jurka1e2f4652013-07-08 18:03:46 -07002440 // returns true if the activity was started
2441 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002442 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002443 // System applications cannot be installed. For now, show a toast explaining that.
2444 // We may give them the option of disabling apps this way.
2445 int messageId = R.string.uninstall_system_app_text;
2446 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002447 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002448 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002449 String packageName = componentName.getPackageName();
2450 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002451 Intent intent = new Intent(
2452 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002453 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2454 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002455 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002456 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002457 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002458 }
2459
Michael Jurka86a720e2012-05-09 11:23:23 -07002460 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002463 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002464 // Only launch using the new animation if the shortcut has not opted out (this is a
2465 // private contract between launcher and may be ignored in the future).
2466 boolean useLaunchAnimation = (v != null) &&
2467 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2468 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002469 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2470 v.getMeasuredWidth(), v.getMeasuredHeight());
2471
2472 startActivity(intent, opts.toBundle());
2473 } else {
2474 startActivity(intent);
2475 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002476 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 } catch (SecurityException e) {
2478 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002479 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002481 "or use the exported attribute for this activity. "
2482 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002484 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002486
Michael Jurka86a720e2012-05-09 11:23:23 -07002487 boolean startActivitySafely(View v, Intent intent, Object tag) {
2488 boolean success = false;
2489 try {
2490 success = startActivity(v, intent, tag);
2491 } catch (ActivityNotFoundException e) {
2492 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2493 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2494 }
2495 return success;
2496 }
2497
Adam Cohena9cf38f2011-05-02 15:36:58 -07002498 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002499 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002500 Folder openFolder = mWorkspace.getFolderForTag(info);
2501
2502 // If the folder info reports that the associated folder is open, then verify that
2503 // it is actually opened. There have been a few instances where this gets out of sync.
2504 if (info.opened && openFolder == null) {
2505 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002506 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002507 info.opened = false;
2508 }
2509
Adam Cohenfb91f302012-06-11 15:45:18 -07002510 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 // Close any open folder
2512 closeFolder();
2513 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002514 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 } else {
2516 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 int folderScreen;
2518 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002519 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 // .. and close it
2521 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002522 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 // Close any folder open on the current screen
2524 closeFolder();
2525 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002526 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 }
2528 }
2529 }
2530 }
2531
Adam Cohen268c4752012-06-06 17:47:33 -07002532 /**
2533 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2534 * in the DragLayer in the exact absolute location of the original FolderIcon.
2535 */
2536 private void copyFolderIconToImage(FolderIcon fi) {
2537 final int width = fi.getMeasuredWidth();
2538 final int height = fi.getMeasuredHeight();
2539
2540 // Lazy load ImageView, Bitmap and Canvas
2541 if (mFolderIconImageView == null) {
2542 mFolderIconImageView = new ImageView(this);
2543 }
2544 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2545 mFolderIconBitmap.getHeight() != height) {
2546 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2547 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2548 }
2549
2550 DragLayer.LayoutParams lp;
2551 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2552 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2553 } else {
2554 lp = new DragLayer.LayoutParams(width, height);
2555 }
2556
Adam Cohen307fe232012-08-16 17:55:58 -07002557 // The layout from which the folder is being opened may be scaled, adjust the starting
2558 // view size by this scale factor.
2559 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002560 lp.customPosition = true;
2561 lp.x = mRectForFolderAnimation.left;
2562 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002563 lp.width = (int) (scale * width);
2564 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002565
2566 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2567 fi.draw(mFolderIconCanvas);
2568 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002569 if (fi.getFolder() != null) {
2570 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2571 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002572 }
Adam Cohen268c4752012-06-06 17:47:33 -07002573 // Just in case this image view is still in the drag layer from a previous animation,
2574 // we remove it and re-add it.
2575 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2576 mDragLayer.removeView(mFolderIconImageView);
2577 }
2578 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002579 if (fi.getFolder() != null) {
2580 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002581 }
Adam Cohen268c4752012-06-06 17:47:33 -07002582 }
2583
Adam Cohen2801caf2011-05-13 20:57:39 -07002584 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002585 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002586 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2587 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2588 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2589
Adam Cohenc51934b2011-07-26 21:07:43 -07002590 FolderInfo info = (FolderInfo) fi.getTag();
2591 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2592 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002593 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2594 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002595 }
2596
Adam Cohen268c4752012-06-06 17:47:33 -07002597 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2598 copyFolderIconToImage(fi);
2599 fi.setVisibility(View.INVISIBLE);
2600
Michael Jurka2ecf9952012-06-18 12:52:28 -07002601 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002602 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002603 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2604 oa.start();
2605 }
2606
Adam Cohen268c4752012-06-06 17:47:33 -07002607 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002608 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002609 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2610 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2611 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2612
Adam Cohen268c4752012-06-06 17:47:33 -07002613 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002614
Adam Cohen268c4752012-06-06 17:47:33 -07002615 // We remove and re-draw the FolderIcon in-case it has changed
2616 mDragLayer.removeView(mFolderIconImageView);
2617 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002618 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002619 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002620 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002621 oa.addListener(new AnimatorListenerAdapter() {
2622 @Override
2623 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002624 if (cl != null) {
2625 cl.clearFolderLeaveBehind();
2626 // Remove the ImageView copy of the FolderIcon and make the original visible.
2627 mDragLayer.removeView(mFolderIconImageView);
2628 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002629 }
2630 }
2631 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002632 oa.start();
2633 }
2634
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002635 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002636 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 * is animated relative to the specified View. If the View is null, no animation
2638 * is played.
2639 *
2640 * @param folderInfo The FolderInfo describing the folder to open.
2641 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002642 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002643 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002644 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002645
Adam Cohena9cf38f2011-05-02 15:36:58 -07002646 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002647
Adam Cohen4554ee12011-08-03 16:13:21 -07002648 // Just verify that the folder hasn't already been added to the DragLayer.
2649 // There was a one-off crash where the folder had a parent already.
2650 if (folder.getParent() == null) {
2651 mDragLayer.addView(folder);
2652 mDragController.addDropTarget((DropTarget) folder);
2653 } else {
2654 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2655 folder.getParent() + ").");
2656 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002657 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002658 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002659
2660 // Notify the accessibility manager that this folder "window" has appeared and occluded
2661 // the workspace items
2662 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2663 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002664 }
2665
2666 public void closeFolder() {
2667 Folder folder = mWorkspace.getOpenFolder();
2668 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002669 if (folder.isEditingName()) {
2670 folder.dismissEditingName();
2671 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002672 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002673
2674 // Dismiss the folder cling
2675 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002676 }
2677 }
2678
2679 void closeFolder(Folder folder) {
2680 folder.getInfo().opened = false;
2681
2682 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2683 if (parent != null) {
2684 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2685 shrinkAndFadeInFolderIcon(fi);
2686 }
2687 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002688
2689 // Notify the accessibility manager that this folder "window" has disappeard and no
2690 // longer occludeds the workspace items
2691 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 }
2693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002695 if (!isDraggingEnabled()) return false;
2696 if (isWorkspaceLocked()) return false;
2697 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698
Adam Cohen1697b792013-09-17 19:08:21 -07002699 if (v instanceof Workspace) {
2700 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002701 if (mWorkspace.enterOverviewMode()) {
2702 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2703 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2704 return true;
2705 } else {
2706 return false;
2707 }
Adam Cohen1697b792013-09-17 19:08:21 -07002708 }
Adam Cohen1697b792013-09-17 19:08:21 -07002709 }
2710
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002712 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 }
2714
Michael Jurka0280c3b2010-09-17 15:00:07 -07002715 resetAddInfo();
2716 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002718 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002719 return true;
2720 }
2721
Winson Chung3d503fb2011-07-13 17:25:49 -07002722 // The hotseat touch handling does not go through Workspace, and we always allow long press
2723 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002724 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002725 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2726 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002727 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002728 // User long pressed on empty space
2729 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2730 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2731 // Disabling reordering until we sort out some issues.
2732 if (mWorkspace.isInOverviewMode()) {
2733 mWorkspace.startReordering(v);
2734 } else {
2735 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002737 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002738 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002739 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002740 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741 }
2742 }
2743 }
2744 return true;
2745 }
2746
Winson Chung3d503fb2011-07-13 17:25:49 -07002747 boolean isHotseatLayout(View layout) {
2748 return mHotseat != null && layout != null &&
2749 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2750 }
2751 Hotseat getHotseat() {
2752 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002753 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002754 View getOverviewPanel() {
2755 return mOverviewPanel;
2756 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002757 SearchDropTargetBar getSearchBar() {
2758 return mSearchDropTargetBar;
2759 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002760
Winson Chung3d503fb2011-07-13 17:25:49 -07002761 /**
2762 * Returns the CellLayout of the specified container at the specified screen.
2763 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002764 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002765 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2766 if (mHotseat != null) {
2767 return mHotseat.getLayout();
2768 } else {
2769 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002770 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002771 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002772 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002773 }
2774 }
2775
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002776 Workspace getWorkspace() {
2777 return mWorkspace;
2778 }
2779
Daniel Sandler843e8602010-06-07 14:59:01 -04002780 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002781 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002782 }
2783
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002784 /**
2785 * Helper method for the cameraZoomIn/cameraZoomOut animations
2786 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002787 * @param scaleFactor The scale factor used for the zoom
2788 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002789 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002790 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002791 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002792 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002793
Craig Mautner360310b2012-10-26 15:13:08 -07002794 private void setWorkspaceBackground(boolean workspace) {
2795 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002796 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002797 }
2798
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002799 void updateWallpaperVisibility(boolean visible) {
2800 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2801 int curflags = getWindow().getAttributes().flags
2802 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2803 if (wpflags != curflags) {
2804 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2805 }
Craig Mautner360310b2012-10-26 15:13:08 -07002806 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002807 }
2808
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002809 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2810 if (v instanceof LauncherTransitionable) {
2811 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2812 }
2813 }
2814
Michael Jurkabed61d22012-02-14 22:51:29 -08002815 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2816 if (v instanceof LauncherTransitionable) {
2817 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2818 }
Winson Chung70442722012-02-10 15:43:22 -08002819
2820 // Update the workspace transition step as well
2821 dispatchOnLauncherTransitionStep(v, 0f);
2822 }
2823
2824 private void dispatchOnLauncherTransitionStep(View v, float t) {
2825 if (v instanceof LauncherTransitionable) {
2826 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2827 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002828 }
2829
2830 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2831 if (v instanceof LauncherTransitionable) {
2832 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2833 }
Winson Chung70442722012-02-10 15:43:22 -08002834
2835 // Update the workspace transition step as well
2836 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002837 }
2838
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002839 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002840 * Things to test when changing the following seven functions.
2841 * - Home from workspace
2842 * - from center screen
2843 * - from other screens
2844 * - Home from all apps
2845 * - from center screen
2846 * - from other screens
2847 * - Back from all apps
2848 * - from center screen
2849 * - from other screens
2850 * - Launch app from workspace and quit
2851 * - with back
2852 * - with home
2853 * - Launch app from all apps and quit
2854 * - with back
2855 * - with home
2856 * - Go to a screen that's not the default, then all
2857 * apps, and launch and app, and go back
2858 * - with back
2859 * -with home
2860 * - On workspace, long press power and go back
2861 * - with back
2862 * - with home
2863 * - On all apps, long press power and go back
2864 * - with back
2865 * - with home
2866 * - On workspace, power off
2867 * - On all apps, power off
2868 * - Launch an app and turn off the screen while in that app
2869 * - Go back with home key
2870 * - Go back with back key TODO: make this not go to workspace
2871 * - From all apps
2872 * - From workspace
2873 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2874 * - From all apps
2875 * - From the center workspace
2876 * - From another workspace
2877 */
2878
2879 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002880 * Zoom the camera out from the workspace to reveal 'toView'.
2881 * Assumes that the view to show is anchored at either the very top or very bottom
2882 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002883 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002884 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002885 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2886 showAppsCustomizeHelper(animated, springLoaded, contentType);
2887 }
2888 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2889 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002890 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002891 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002892 mStateAnimation.cancel();
2893 mStateAnimation = null;
2894 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002895 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002896
Winson Chungf0ea4d32011-06-06 14:27:16 -07002897 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2898 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2899 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002900 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002901 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002902 final int startDelay =
2903 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002904
Michael Jurkab3e22d92011-10-31 15:58:33 -07002905 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002906
Michael Jurkad74c9842011-07-10 12:44:21 -07002907 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002908 Animator workspaceAnim =
2909 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002910 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2911 // Set the content type for the all apps space
2912 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2913 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002914
2915 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002916 toView.setScaleX(scale);
2917 toView.setScaleY(scale);
2918 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2919 scaleAnim.
2920 scaleX(1f).scaleY(1f).
2921 setDuration(duration).
2922 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002923
Winson Chungf0ea4d32011-06-06 14:27:16 -07002924 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002925 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002926 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002927 .ofFloat(toView, "alpha", 0f, 1f)
2928 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002929 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002930 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2931 @Override
2932 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002933 if (animation == null) {
2934 throw new RuntimeException("animation is null");
2935 }
Winson Chung70442722012-02-10 15:43:22 -08002936 float t = (Float) animation.getAnimatedValue();
2937 dispatchOnLauncherTransitionStep(fromView, t);
2938 dispatchOnLauncherTransitionStep(toView, t);
2939 }
2940 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002941
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002942 // toView should appear right at the end of the workspace shrink
2943 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002944 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002945 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002946 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002947
2948 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002949 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002950 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002951 // Prepare the position
2952 toView.setTranslationX(0.0f);
2953 toView.setTranslationY(0.0f);
2954 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002955 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002956 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002957 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002958 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002959 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2960 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002961
Winson Chungc7d2b602012-05-16 17:02:20 -07002962 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002963 if (mSearchDropTargetBar != null) {
2964 mSearchDropTargetBar.hideSearchBar(false);
2965 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002966 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002967 });
2968
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002969 if (workspaceAnim != null) {
2970 mStateAnimation.play(workspaceAnim);
2971 }
Michael Jurka1899a362011-11-03 13:50:45 -07002972
2973 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002974
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002975 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2976 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002977
2978 // If any of the objects being animated haven't been measured/laid out
2979 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002980 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002981 (mWorkspace.getMeasuredWidth() == 0) ||
2982 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002983 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002984 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002985
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002986 final AnimatorSet stateAnimation = mStateAnimation;
2987 final Runnable startAnimRunnable = new Runnable() {
2988 public void run() {
2989 // Check that mStateAnimation hasn't changed while
2990 // we waited for a layout/draw pass
2991 if (mStateAnimation != stateAnimation)
2992 return;
2993 setPivotsForZoom(toView, scale);
2994 dispatchOnLauncherTransitionStart(fromView, animated, false);
2995 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002996 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002997 }
2998 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002999 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003000 final ViewTreeObserver observer = toView.getViewTreeObserver();
3001 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3002 public void onGlobalLayout() {
3003 startAnimRunnable.run();
3004 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3005 }
3006 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003007 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003008 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003009 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003010 } else {
3011 toView.setTranslationX(0.0f);
3012 toView.setTranslationY(0.0f);
3013 toView.setScaleX(1.0f);
3014 toView.setScaleY(1.0f);
3015 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003016 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003017
Daniel Sandlere4f98912013-06-25 15:13:26 -04003018 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003019 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003020 if (mSearchDropTargetBar != null) {
3021 mSearchDropTargetBar.hideSearchBar(false);
3022 }
Michael Jurkaabded662011-03-04 12:06:57 -08003023 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003024 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003025 dispatchOnLauncherTransitionStart(fromView, animated, false);
3026 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003027 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003028 dispatchOnLauncherTransitionStart(toView, animated, false);
3029 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003030 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003031 }
3032
3033 /**
3034 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003035 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003036 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003037 */
Adam Cohened307df2013-10-02 09:37:31 -07003038 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003039 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003040
Michael Jurkab3e22d92011-10-31 15:58:33 -07003041 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003042 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003043 mStateAnimation.cancel();
3044 mStateAnimation = null;
3045 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003046 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003047
Winson Chungf0ea4d32011-06-06 14:27:16 -07003048 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003049 final int fadeOutDuration =
3050 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003051 final float scaleFactor = (float)
3052 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3053 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003054 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003055 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003056 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003057 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3058 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003059 toState, animated, stagger, -1);
3060 } else if (toState == Workspace.State.SPRING_LOADED ||
3061 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003062 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003063 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003064 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003065
Michael Jurkab3e22d92011-10-31 15:58:33 -07003066 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003067 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003068 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003069 final LauncherViewPropertyAnimator scaleAnim =
3070 new LauncherViewPropertyAnimator(fromView);
3071 scaleAnim.
3072 scaleX(scaleFactor).scaleY(scaleFactor).
3073 setDuration(duration).
3074 setInterpolator(new Workspace.ZoomInInterpolator());
3075
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003076 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003077 .ofFloat(fromView, "alpha", 1f, 0f)
3078 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003079 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003080 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3081 @Override
3082 public void onAnimationUpdate(ValueAnimator animation) {
3083 float t = 1f - (Float) animation.getAnimatedValue();
3084 dispatchOnLauncherTransitionStep(fromView, t);
3085 dispatchOnLauncherTransitionStep(toView, t);
3086 }
3087 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003088
Michael Jurka2ecf9952012-06-18 12:52:28 -07003089 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003090
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003091 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3092 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003093 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003094
3095 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003096 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003097 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003098 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003099 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3100 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003101 if (onCompleteRunnable != null) {
3102 onCompleteRunnable.run();
3103 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003104 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003105 }
3106 });
3107
Winson Chungf0ea4d32011-06-06 14:27:16 -07003108 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003109 if (workspaceAnim != null) {
3110 mStateAnimation.play(workspaceAnim);
3111 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003112 dispatchOnLauncherTransitionStart(fromView, animated, true);
3113 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003114 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003115 } else {
3116 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003117 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003118 dispatchOnLauncherTransitionStart(fromView, animated, true);
3119 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003120 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003121 dispatchOnLauncherTransitionStart(toView, animated, true);
3122 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003123 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003124 }
3125
Michael Jurkae326f182011-11-21 14:05:46 -08003126 @Override
3127 public void onTrimMemory(int level) {
3128 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003129 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003130 mAppsCustomizeTabHost.onTrimMemory();
3131 }
3132 }
3133
Adam Cohened307df2013-10-02 09:37:31 -07003134 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003135 showWorkspace(animated, null);
3136 }
3137
Adam Cohened307df2013-10-02 09:37:31 -07003138 protected void showWorkspace() {
3139 showWorkspace(true);
3140 }
3141
Adam Cohened66b2b2012-01-23 17:28:51 -08003142 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003143 if (mWorkspace.isInOverviewMode()) {
3144 mWorkspace.exitOverviewMode(animated);
3145 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003146 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003147 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003148 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003149 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003150
Winson Chungc7d2b602012-05-16 17:02:20 -07003151 // Show the search bar (only animate if we were showing the drop target bar in spring
3152 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003153 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003154 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003155 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003156
Michael Jurkab3e22d92011-10-31 15:58:33 -07003157 // Set focus to the AppsCustomize button
3158 if (mAllAppsButton != null) {
3159 mAllAppsButton.requestFocus();
3160 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003161 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003162
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003163 // Change the state *after* we've called all the transition code
3164 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003165
Winson Chung5fb63472011-02-02 17:03:37 -08003166 // Resume the auto-advance of widgets
3167 mUserPresent = true;
3168 updateRunning();
3169
alanv1d4fde62012-10-17 13:15:47 -07003170 // Send an accessibility event to announce the context change
3171 getWindow().getDecorView()
3172 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003173
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003174 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003175 }
3176
Adam Cohened307df2013-10-02 09:37:31 -07003177 void showOverviewMode(boolean animated) {
3178 mWorkspace.setVisibility(View.VISIBLE);
3179 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3180 mState = State.WORKSPACE;
3181 onWorkspaceShown(animated);
3182 }
3183
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003184 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003185 }
3186
Winson Chung82963d52013-10-09 11:20:57 -07003187 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3188 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003189 if (mState != State.WORKSPACE) return;
3190
Winson Chung82963d52013-10-09 11:20:57 -07003191 if (resetPageToZero) {
3192 mAppsCustomizeTabHost.reset();
3193 }
Winson Chungc58497e2013-09-03 17:48:37 -07003194 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003195 mAppsCustomizeTabHost.requestFocus();
3196
Michael Jurkab3e22d92011-10-31 15:58:33 -07003197 // Change the state *after* we've called all the transition code
3198 mState = State.APPS_CUSTOMIZE;
3199
3200 // Pause the auto-advance of widgets until we are out of AllApps
3201 mUserPresent = false;
3202 updateRunning();
3203 closeFolder();
3204
3205 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003206 getWindow().getDecorView()
3207 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003208 }
3209
Adam Cohen7777d962011-08-18 18:58:38 -07003210 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003211 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003212 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003213 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003214 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003215 }
Adam Cohen7777d962011-08-18 18:58:38 -07003216
Adam Cohenad4e15c2013-10-17 16:21:35 -07003217 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003218 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003219 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3220
Winson Chunge7a03942011-08-05 15:05:12 -07003221 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003222 @Override
3223 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003224 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003225 // Before we show workspace, hide all apps again because
3226 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3227 // clean up our state transition functions
3228 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003229 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003230 } else {
3231 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003232 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003233 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003234 }, delay);
3235
Winson Chung557d6ed2011-07-08 15:34:52 -07003236 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003237
Michael Jurkad3ef3062010-11-23 16:23:58 -08003238 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003239 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003240 final boolean animated = true;
3241 final boolean springLoaded = true;
3242 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003243 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003244 }
3245 // Otherwise, we are not in spring loaded mode, so don't do anything.
3246 }
3247
Michael Jurkab3e22d92011-10-31 15:58:33 -07003248 void lockAllApps() {
3249 // TODO
3250 }
3251
3252 void unlockAllApps() {
3253 // TODO
3254 }
3255
Winson Chungf0ea4d32011-06-06 14:27:16 -07003256 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003257 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003258 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003259 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003260 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003261 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003262 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003263 int duration = 0;
3264 if (mSearchDropTargetBar != null) {
3265 duration = mSearchDropTargetBar.getTransitionInDuration();
3266 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003267 mHotseat.animate().alpha(1f).setDuration(duration);
3268 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003269 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003270 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003271 }
3272 }
3273 }
3274
3275 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003276 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003277 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003278 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003279 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003280 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003281 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003282 int duration = 0;
3283 if (mSearchDropTargetBar != null) {
3284 duration = mSearchDropTargetBar.getTransitionOutDuration();
3285 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003286 mHotseat.animate().alpha(0f).setDuration(duration);
3287 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003288 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003289 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003290 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003291 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003292 }
3293
Patrick Dubroy5f445422011-02-18 14:35:21 -08003294 /**
3295 * Add an item from all apps or customize onto the given workspace screen.
3296 * If layout is null, add to the current screen.
3297 */
3298 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003299 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003300 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003301 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003302 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003303
Winson Chungdff8ebb2011-09-08 17:25:31 -07003304 /** Maps the current orientation to an index for referencing orientation correct global icons */
3305 private int getCurrentOrientationIndexForGlobalIcons() {
3306 // default - 0, landscape - 1
3307 switch (getResources().getConfiguration().orientation) {
3308 case Configuration.ORIENTATION_LANDSCAPE:
3309 return 1;
3310 default:
3311 return 0;
3312 }
3313 }
3314
Michael Jurkaae65ee32012-04-30 11:45:54 -07003315 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003316 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003317 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003318 // Look for the toolbar icon specified in the activity meta-data
3319 Bundle metaData = packageManager.getActivityInfo(
3320 activityName, PackageManager.GET_META_DATA).metaData;
3321 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003322 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003323 if (iconResId != 0) {
3324 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003325 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003326 }
3327 }
3328 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003329 // This can happen if the activity defines an invalid drawable
3330 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3331 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003332 } catch (Resources.NotFoundException nfe) {
3333 // This can happen if the activity defines an invalid drawable
3334 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3335 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003336 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003337 return null;
3338 }
3339
3340 // if successful in getting icon, return it; otherwise, set button to use default drawable
3341 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003342 int buttonId, ComponentName activityName, int fallbackDrawableId,
3343 String toolbarResourceName) {
3344 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003345 Resources r = getResources();
3346 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3347 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003348
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003349 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003350 // If we were unable to find the icon via the meta-data, use a generic one
3351 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003352 toolbarIcon = r.getDrawable(fallbackDrawableId);
3353 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003354 if (button != null) {
3355 button.setCompoundDrawables(toolbarIcon, null, null, null);
3356 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003357 return null;
3358 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003359 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003360 if (button != null) {
3361 button.setCompoundDrawables(toolbarIcon, null, null, null);
3362 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003363 return toolbarIcon.getConstantState();
3364 }
3365 }
3366
3367 // if successful in getting icon, return it; otherwise, set button to use default drawable
3368 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003369 int buttonId, ComponentName activityName, int fallbackDrawableId,
3370 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003371 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003372 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003373
Michael Jurka19e0fc52011-07-22 18:00:21 -07003374 if (button != null) {
3375 // If we were unable to find the icon via the meta-data, use a
3376 // generic one
3377 if (toolbarIcon == null) {
3378 button.setImageResource(fallbackDrawableId);
3379 } else {
3380 button.setImageDrawable(toolbarIcon);
3381 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003382 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003383
3384 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3385
Michael Jurka0423dcf2010-10-05 14:56:18 -07003386 }
3387
Winson Chung1b884092012-06-08 17:13:02 -07003388 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003389 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003390 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003391 }
3392
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003393 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003394 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003395 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003396 }
3397
Winson Chungbb185bd2011-11-21 12:31:42 -08003398 private void invalidatePressedFocusedStates(View container, View button) {
3399 if (container instanceof HolographicLinearLayout) {
3400 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3401 layout.invalidatePressedFocusedStates();
3402 } else if (button instanceof HolographicImageView) {
3403 HolographicImageView view = (HolographicImageView) button;
3404 view.invalidatePressedFocusedStates();
3405 }
3406 }
3407
Cristina Stancu476493b2013-08-07 17:20:14 +01003408 public View getQsbBar() {
3409 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003410 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3411 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003412 }
3413 return mQsbBar;
3414 }
3415
3416 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003417 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003418 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003419 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003420 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003421
Winson Chung1cad91e2011-05-25 17:41:01 -07003422 final SearchManager searchManager =
3423 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3424 ComponentName activityName = searchManager.getGlobalSearchActivity();
3425 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003426 int coi = getCurrentOrientationIndexForGlobalIcons();
3427 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003428 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3429 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3430 if (sGlobalSearchIcon[coi] == null) {
3431 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3432 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3433 TOOLBAR_ICON_METADATA_NAME);
3434 }
3435
Winson Chungc51db6a2011-10-05 11:44:49 -07003436 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3437 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003438 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003439 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003440 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003441 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003442 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3443 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003444 if (searchButton != null) searchButton.setVisibility(View.GONE);
3445 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003446 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003447 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003448 }
3449 }
3450
Cristina Stancu476493b2013-08-07 17:20:14 +01003451 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003452 final View searchButtonContainer = findViewById(R.id.search_button_container);
3453 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003454 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003455 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003456 }
3457
Cristina Stancu476493b2013-08-07 17:20:14 +01003458 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003459 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003460 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003461
Winson Chungc51db6a2011-10-05 11:44:49 -07003462 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003463 final SearchManager searchManager =
3464 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3465 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3466
3467 ComponentName activityName = null;
3468 if (globalSearchActivity != null) {
3469 // Check if the global search activity handles voice search
3470 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3471 intent.setPackage(globalSearchActivity.getPackageName());
3472 activityName = intent.resolveActivity(getPackageManager());
3473 }
3474
3475 if (activityName == null) {
3476 // Fallback: check if an activity other than the global search activity
3477 // resolves this
3478 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3479 activityName = intent.resolveActivity(getPackageManager());
3480 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003481 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003482 int coi = getCurrentOrientationIndexForGlobalIcons();
3483 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003484 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3485 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3486 if (sVoiceSearchIcon[coi] == null) {
3487 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3488 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3489 TOOLBAR_ICON_METADATA_NAME);
3490 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003491 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003492 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003493 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003494 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003495 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003496 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003497 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003498 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003499 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003500 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003501 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003502 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003503
Cristina Stancu476493b2013-08-07 17:20:14 +01003504 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003505 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3506 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003507 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003508 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003509 }
3510
Winson Chung5841efa2013-09-30 18:06:44 -07003511 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003512 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3513 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003514 boolean visible = !forceDisableVoiceButtonProxy &&
3515 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003516 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003517 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003518 }
3519 }
Winson Chung5841efa2013-09-30 18:06:44 -07003520
3521 /**
3522 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3523 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3524 */
3525 public void disableVoiceButtonProxy(boolean disabled) {
3526 updateVoiceButtonProxyVisible(disabled);
3527 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003528 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003529 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003530 */
3531 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003532 if (!DISABLE_MARKET_BUTTON) {
3533 final View marketButton = findViewById(R.id.market_button);
3534 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3535 // Find the app market activity by resolving an intent.
3536 // (If multiple app markets are installed, it will return the ResolverActivity.)
3537 ComponentName activityName = intent.resolveActivity(getPackageManager());
3538 if (activityName != null) {
3539 int coi = getCurrentOrientationIndexForGlobalIcons();
3540 mAppMarketIntent = intent;
3541 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3542 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3543 TOOLBAR_ICON_METADATA_NAME);
3544 marketButton.setVisibility(View.VISIBLE);
3545 } else {
3546 // We should hide and disable the view so that we don't try and restore the visibility
3547 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3548 marketButton.setVisibility(View.GONE);
3549 marketButton.setEnabled(false);
3550 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003551 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003552 }
3553
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003554 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003555 if (!DISABLE_MARKET_BUTTON) {
3556 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3557 Resources r = getResources();
3558 Drawable marketIconDrawable = d.newDrawable(r);
3559 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3560 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3561 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003562
Winson Chungd64a6662013-09-30 11:06:59 -07003563 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3564 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003565 }
3566
Michael Jurkad7c28052012-04-27 15:43:36 -07003567 @Override
3568 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003569 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003570 final List<CharSequence> text = event.getText();
3571 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003572 // Populate event with a fake title based on the current state.
3573 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003574 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003575 } else {
3576 text.add(getString(R.string.all_apps_home_button_label));
3577 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003578 return result;
3579 }
3580
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003581 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003582 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003583 */
3584 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3585 @Override
3586 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003587 closeSystemDialogs();
3588 }
3589 }
3590
3591 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003592 * Receives notifications whenever the appwidgets are reset.
3593 */
3594 private class AppWidgetResetObserver extends ContentObserver {
3595 public AppWidgetResetObserver() {
3596 super(new Handler());
3597 }
3598
3599 @Override
3600 public void onChange(boolean selfChange) {
3601 onAppWidgetReset();
3602 }
3603 }
3604
3605 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003606 * If the activity is currently paused, signal that we need to run the passed Runnable
3607 * in onResume.
3608 *
3609 * This needs to be called from incoming places where resources might have been loaded
3610 * while we are paused. That is becaues the Configuration might be wrong
3611 * when we're not running, and if it comes back to what it was when we
3612 * were paused, we are not restarted.
3613 *
3614 * Implementation of the method from LauncherModel.Callbacks.
3615 *
3616 * @return true if we are currently paused. The caller might be able to
3617 * skip some work in that case since we will come back again.
3618 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003619 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003620 if (mPaused) {
3621 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003622 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003623 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003624 }
3625 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003626 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003627 return true;
3628 } else {
3629 return false;
3630 }
3631 }
3632
Michael Jurkac402cd92013-05-20 15:49:32 +02003633 private boolean waitUntilResume(Runnable run) {
3634 return waitUntilResume(run, false);
3635 }
3636
Michael Jurka1e2f4652013-07-08 18:03:46 -07003637 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003638 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003639 }
3640
Michael Jurka7607c2f2013-04-03 14:33:19 -07003641 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003642 * If the activity is currently paused, signal that we need to re-run the loader
3643 * in onResume.
3644 *
3645 * This needs to be called from incoming places where resources might have been loaded
3646 * while we are paused. That is becaues the Configuration might be wrong
3647 * when we're not running, and if it comes back to what it was when we
3648 * were paused, we are not restarted.
3649 *
3650 * Implementation of the method from LauncherModel.Callbacks.
3651 *
3652 * @return true if we are currently paused. The caller might be able to
3653 * skip some work in that case since we will come back again.
3654 */
3655 public boolean setLoadOnResume() {
3656 if (mPaused) {
3657 Log.i(TAG, "setLoadOnResume");
3658 mOnResumeNeedsLoad = true;
3659 return true;
3660 } else {
3661 return false;
3662 }
3663 }
3664
3665 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003666 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003667 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003668 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003669 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003670 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003671 } else {
3672 return SCREEN_COUNT / 2;
3673 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003674 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003675
Joe Onorato9c1289c2009-08-17 11:03:03 -04003676 /**
3677 * Refreshes the shortcuts shown on the workspace.
3678 *
3679 * Implementation of the method from LauncherModel.Callbacks.
3680 */
3681 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003682 // If we're starting binding all over again, clear any bind calls we'd postponed in
3683 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3684 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003685 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003686
Winson Chungd64d1762013-08-20 14:37:16 -07003687 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003688 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003689 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003690
Michael Jurka05bf644e2011-11-30 20:28:53 -08003691 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003692 if (mHotseat != null) {
3693 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003694 }
3695 }
3696
Adam Cohendcd297f2013-06-18 13:13:40 -07003697 @Override
3698 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003699 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003700
Adam Cohen5084cba2013-09-03 12:01:16 -07003701 // If there are no screens, we need to have an empty screen
3702 if (orderedScreenIds.size() == 0) {
3703 mWorkspace.addExtraEmptyScreen();
3704 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003705
3706 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003707 // setCurrentPage() so ensure that all pages are added before calling this).
3708 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003709 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3710 mWorkspace.createCustomContentPage();
3711 }
Winson Chung64359a52013-07-08 17:17:08 -07003712 }
3713
3714 @Override
3715 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003716 // Log to disk
3717 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3718 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3719 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003720 int count = orderedScreenIds.size();
3721 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003722 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003723 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003724 }
3725
Adam Cohen39a06042013-07-19 14:30:12 -07003726 private boolean shouldShowWeightWatcher() {
3727 String spKey = LauncherAppState.getSharedPreferencesKey();
3728 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003729 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003730
3731 return show;
3732 }
3733
3734 private void toggleShowWeightWatcher() {
3735 String spKey = LauncherAppState.getSharedPreferencesKey();
3736 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3737 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3738
3739 show = !show;
3740
3741 SharedPreferences.Editor editor = sp.edit();
3742 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3743 editor.commit();
3744
3745 if (mWeightWatcher != null) {
3746 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3747 }
3748 }
3749
Winson Chungd64d1762013-08-20 14:37:16 -07003750 public void bindAppsAdded(final ArrayList<Long> newScreens,
3751 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003752 final ArrayList<ItemInfo> addAnimated,
3753 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003754 Runnable r = new Runnable() {
3755 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003756 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003757 }
3758 };
3759 if (waitUntilResume(r)) {
3760 return;
3761 }
3762
Winson Chungd64d1762013-08-20 14:37:16 -07003763 // Add the new screens
3764 bindAddScreens(newScreens);
3765
3766 // We add the items without animation on non-visible pages, and with
3767 // animations on the new page (which we will try and snap to).
3768 if (!addNotAnimated.isEmpty()) {
3769 bindItems(addNotAnimated, 0,
3770 addNotAnimated.size(), false);
3771 }
3772 if (!addAnimated.isEmpty()) {
3773 bindItems(addAnimated, 0,
3774 addAnimated.size(), true);
3775 }
Winson Chungc58497e2013-09-03 17:48:37 -07003776
Winson Chung87412982013-10-03 18:34:14 -07003777 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003778 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003779
Winson Chungc58497e2013-09-03 17:48:37 -07003780 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3781 addedApps != null && mAppsCustomizeContent != null) {
3782 mAppsCustomizeContent.addApps(addedApps);
3783 }
Winson Chungd64d1762013-08-20 14:37:16 -07003784 }
3785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003786 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787 * Bind the items start-end from the list.
3788 *
3789 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003790 */
Winson Chung64359a52013-07-08 17:17:08 -07003791 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3792 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003793 Runnable r = new Runnable() {
3794 public void run() {
3795 bindItems(shortcuts, start, end, forceAnimateIcons);
3796 }
3797 };
3798 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003799 return;
3800 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003801
Winson Chungf0c6ae02012-03-21 16:10:31 -07003802 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003803 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3804 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003805 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003806 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003807 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003808 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003809 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003810
3811 // Short circuit if we are loading dock items for a configuration which has no dock
3812 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3813 mHotseat == null) {
3814 continue;
3815 }
3816
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 switch (item.itemType) {
3818 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3819 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003820 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003821 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003822
Winson Chung64359a52013-07-08 17:17:08 -07003823 /*
3824 * TODO: FIX collision case
3825 */
Winson Chungce376632013-07-11 16:12:41 -07003826 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3827 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3828 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3829 throw new RuntimeException("OCCUPIED");
3830 }
Winson Chung64359a52013-07-08 17:17:08 -07003831 }
3832
Adam Cohendcd297f2013-06-18 13:13:40 -07003833 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3834 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003835 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003836 // Animate all the applications up now
3837 shortcut.setAlpha(0f);
3838 shortcut.setScaleX(0f);
3839 shortcut.setScaleY(0f);
3840 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003841 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003842 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003843 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003844 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003845 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003846 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003847 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003848 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3849 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003851 default:
3852 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003854 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003855
Winson Chung997a9232013-07-24 15:33:46 -07003856 if (animateIcons) {
3857 // Animate to the correct page
3858 if (newShortcutsScreenId > -1) {
3859 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003860 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003861 final Runnable startBounceAnimRunnable = new Runnable() {
3862 public void run() {
3863 anim.playTogether(bounceAnims);
3864 anim.start();
3865 }
3866 };
Winson Chung997a9232013-07-24 15:33:46 -07003867 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003868 // We post the animation slightly delayed to prevent slowdowns
3869 // when we are loading right after we return to launcher.
3870 mWorkspace.postDelayed(new Runnable() {
3871 public void run() {
3872 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003873 mWorkspace.postDelayed(startBounceAnimRunnable,
3874 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003875 }
3876 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003877 } else {
3878 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003879 }
3880 }
Winson Chung64359a52013-07-08 17:17:08 -07003881 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003882 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003883 }
3884
Joe Onorato9c1289c2009-08-17 11:03:03 -04003885 /**
3886 * Implementation of the method from LauncherModel.Callbacks.
3887 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003888 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003889 Runnable r = new Runnable() {
3890 public void run() {
3891 bindFolders(folders);
3892 }
3893 };
3894 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003895 return;
3896 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003897 sFolders.clear();
3898 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900
3901 /**
3902 * Add the views for a widget to the workspace.
3903 *
3904 * Implementation of the method from LauncherModel.Callbacks.
3905 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003906 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003907 Runnable r = new Runnable() {
3908 public void run() {
3909 bindAppWidget(item);
3910 }
3911 };
3912 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003913 return;
3914 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003915
Daniel Sandler843e8602010-06-07 14:59:01 -04003916 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3917 if (DEBUG_WIDGETS) {
3918 Log.d(TAG, "bindAppWidget: " + item);
3919 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003920 final Workspace workspace = mWorkspace;
3921
3922 final int appWidgetId = item.appWidgetId;
3923 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003924 if (DEBUG_WIDGETS) {
3925 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3926 }
3927
Joe Onorato9c1289c2009-08-17 11:03:03 -04003928 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3929
Joe Onorato9c1289c2009-08-17 11:03:03 -04003930 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003931 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003932
Adam Cohendcd297f2013-06-18 13:13:40 -07003933 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003934 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003935 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3936
Joe Onorato9c1289c2009-08-17 11:03:03 -04003937 workspace.requestLayout();
3938
Daniel Sandler843e8602010-06-07 14:59:01 -04003939 if (DEBUG_WIDGETS) {
3940 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3941 + (SystemClock.uptimeMillis()-start) + "ms");
3942 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003943 }
3944
Adam Cohen1462de32012-07-24 22:34:36 -07003945 public void onPageBoundSynchronously(int page) {
3946 mSynchronouslyBoundPages.add(page);
3947 }
3948
Joe Onorato9c1289c2009-08-17 11:03:03 -04003949 /**
3950 * Callback saying that there aren't any more items to bind.
3951 *
3952 * Implementation of the method from LauncherModel.Callbacks.
3953 */
Adam Cohene25af792013-06-06 23:08:25 -07003954 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003955 Runnable r = new Runnable() {
3956 public void run() {
3957 finishBindingItems(upgradePath);
3958 }
3959 };
3960 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003961 return;
3962 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003963 if (mSavedState != null) {
3964 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003965 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003966 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003967 mSavedState = null;
3968 }
3969
Adam Cohen1462de32012-07-24 22:34:36 -07003970 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003971
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003972 // If we received the result of any pending adds while the loader was running (e.g. the
3973 // widget configuration forced an orientation change), process them now.
3974 for (int i = 0; i < sPendingAddList.size(); i++) {
3975 completeAdd(sPendingAddList.get(i));
3976 }
3977 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003978
Winson Chungc51db6a2011-10-05 11:44:49 -07003979 // Update the market app icon as necessary (the other icons will be managed in response to
3980 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003981 if (!DISABLE_MARKET_BUTTON) {
3982 updateAppMarketIcon();
3983 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003984
Winson Chungf0c6ae02012-03-21 16:10:31 -07003985 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003986 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003987 mWorkspace.getUniqueComponents(true, null);
3988 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003989 }
Adam Cohen99894d92013-06-14 11:22:59 -07003990
Adam Cohen66a01fd2013-06-11 12:48:00 -07003991 mWorkspace.post(new Runnable() {
3992 @Override
3993 public void run() {
3994 onFinishBindingItems();
3995 }
3996 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003997 }
3998
Winson Chunga0b7e862013-09-05 16:03:15 -07003999 public boolean isAllAppsButtonRank(int rank) {
4000 if (mHotseat != null) {
4001 return mHotseat.isAllAppsButtonRank(rank);
4002 }
4003 return false;
4004 }
4005
Winson Chunga2413752012-04-03 14:22:34 -07004006 private boolean canRunNewAppsAnimation() {
4007 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4008 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4009 }
4010
Winson Chungc9168342013-06-26 14:54:55 -07004011 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4012 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4013 PropertyValuesHolder.ofFloat("alpha", 1f),
4014 PropertyValuesHolder.ofFloat("scaleX", 1f),
4015 PropertyValuesHolder.ofFloat("scaleY", 1f));
4016 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4017 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4018 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4019 return bounceAnim;
4020 }
4021
Adam Cohen27772732013-11-11 14:00:56 +00004022 public boolean useVerticalBarLayout() {
4023 return LauncherAppState.getInstance().getDynamicGrid().
4024 getDeviceProfile().isVerticalBarLayout();
4025 }
4026
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004027 @Override
4028 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004029 boolean searchVisible = updateGlobalSearchIcon();
4030 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004031 if (mSearchDropTargetBar != null) {
4032 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4033 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004034 }
4035
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004036 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004037 * Add the icons for all apps.
4038 *
4039 * Implementation of the method from LauncherModel.Callbacks.
4040 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004041 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07004042 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
4043 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4044 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4045 getHotseat().addAllAppsFolder(mIconCache, apps,
4046 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4047 }
4048 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004049 }
Winson Chungc58497e2013-09-03 17:48:37 -07004050 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004051 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004052 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004053 mAppsCustomizeContent.onPackagesUpdated(
4054 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004055 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004056 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004057 }
4058
4059 /**
4060 * A package was updated.
4061 *
4062 * Implementation of the method from LauncherModel.Callbacks.
4063 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004064 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004065 Runnable r = new Runnable() {
4066 public void run() {
4067 bindAppsUpdated(apps);
4068 }
4069 };
4070 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004071 return;
4072 }
4073
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004074 if (mWorkspace != null) {
4075 mWorkspace.updateShortcuts(apps);
4076 }
Winson Chungc58497e2013-09-03 17:48:37 -07004077
4078 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4079 mAppsCustomizeContent != null) {
4080 mAppsCustomizeContent.updateApps(apps);
4081 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004082 }
4083
4084 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004085 * A package was uninstalled. We take both the super set of packageNames
4086 * in addition to specific applications to remove, the reason being that
4087 * this can be called when a package is updated as well. In that scenario,
4088 * we only remove specific components from the workspace, where as
4089 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004090 *
4091 * Implementation of the method from LauncherModel.Callbacks.
4092 */
Winson Chung83892cc2013-05-01 16:53:33 -07004093 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004094 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004095 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004096 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004097 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004098 }
Winson Chungd64d1762013-08-20 14:37:16 -07004099 };
4100 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004101 return;
4102 }
4103
Winson Chungdf95eb12013-10-16 14:57:07 -07004104 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004105 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004106 }
4107 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004108 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004109 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004110
Winson Chunga1820962011-10-03 16:31:06 -07004111 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004112 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004113
Winson Chungdf95eb12013-10-16 14:57:07 -07004114 // Update AllApps
Winson Chungc58497e2013-09-03 17:48:37 -07004115 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4116 mAppsCustomizeContent != null) {
4117 mAppsCustomizeContent.removeApps(appInfos);
4118 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119 }
Joe Onoratobe386092009-11-17 17:32:16 -08004120
4121 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004122 * A number of packages were updated.
4123 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004124 private ArrayList<Object> mWidgetsAndShortcuts;
4125 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004126 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004127 bindPackagesUpdated(mWidgetsAndShortcuts);
4128 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004129 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004130 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004131 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4132 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4133 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004134 return;
4135 }
4136
Winson Chung64359a52013-07-08 17:17:08 -07004137 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004138 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4139 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004140 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004141 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004142 }
4143
Winson Chung400438b2011-01-16 17:53:48 -08004144 private int mapConfigurationOriActivityInfoOri(int configOri) {
4145 final Display d = getWindowManager().getDefaultDisplay();
4146 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4147 switch (d.getRotation()) {
4148 case Surface.ROTATION_0:
4149 case Surface.ROTATION_180:
4150 // We are currently in the same basic orientation as the natural orientation
4151 naturalOri = configOri;
4152 break;
4153 case Surface.ROTATION_90:
4154 case Surface.ROTATION_270:
4155 // We are currently in the other basic orientation to the natural orientation
4156 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4157 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4158 break;
4159 }
4160
4161 int[] oriMap = {
4162 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4163 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4164 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4165 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4166 };
4167 // Since the map starts at portrait, we need to offset if this device's natural orientation
4168 // is landscape.
4169 int indexOffset = 0;
4170 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4171 indexOffset = 1;
4172 }
4173 return oriMap[(d.getRotation() + indexOffset) % 4];
4174 }
Adam Cohen446e9402011-09-15 18:21:21 -07004175
Winson Chung4b919f82012-05-01 10:44:08 -07004176 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004177 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004178 getResources().getBoolean(R.bool.allow_rotation);
4179 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004180 }
Winson Chung4b919f82012-05-01 10:44:08 -07004181 public void lockScreenOrientation() {
4182 if (isRotationEnabled()) {
4183 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4184 .getConfiguration().orientation));
4185 }
4186 }
4187 public void unlockScreenOrientation(boolean immediate) {
4188 if (isRotationEnabled()) {
4189 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004190 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004191 } else {
4192 mHandler.postDelayed(new Runnable() {
4193 public void run() {
4194 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4195 }
4196 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004197 }
Winson Chung4b919f82012-05-01 10:44:08 -07004198 }
Winson Chung400438b2011-01-16 17:53:48 -08004199 }
4200
Winson Chung82f55532011-08-09 14:14:23 -07004201 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004202 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004203 if (DISABLE_CLINGS) {
4204 return false;
4205 }
4206
Winson Chung3a6e7f32013-10-09 15:50:52 -07004207 // For now, limit only to phones
4208 LauncherAppState app = LauncherAppState.getInstance();
4209 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4210 if (grid.isTablet()) {
4211 return false;
4212 }
Winson Chung13dbfe12013-10-31 11:42:39 -07004213 if (grid.isLandscape) {
4214 return false;
4215 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004216
Brett Chabot2a9e2282011-08-23 15:03:13 -07004217 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004218 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004219
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004220 // Disable clings for accessibility when explore by touch is enabled
4221 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4222 ACCESSIBILITY_SERVICE);
4223 if (a11yManager.isTouchExplorationEnabled()) {
4224 return false;
4225 }
4226
Michael Jurkae233a8b2013-03-19 13:49:20 +01004227 // Restricted secondary users (child mode) will potentially have very few apps
4228 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004229// boolean supportsLimitedUsers =
4230// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4231// Account[] accounts = AccountManager.get(this).getAccounts();
4232// if (supportsLimitedUsers && accounts.length == 0) {
4233// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4234// Bundle restrictions = um.getUserRestrictions();
4235// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4236// return false;
4237// }
4238// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004239 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004240 }
Michael Jurka22143132012-10-04 17:42:38 +02004241
Winson Chungfa545132013-09-26 17:45:32 -07004242 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004243 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004244 Cling cling = (Cling) findViewById(clingId);
4245 View scrim = null;
4246 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004247 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004248 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004249 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004250 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004251 cling.show(animate, SHOW_CLING_DURATION);
4252
4253 if (dimNavBarVisibilty) {
4254 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4255 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004256 }
4257 }
4258 return cling;
4259 }
Michael Jurka22143132012-10-04 17:42:38 +02004260
Winson Chungaf40f202013-09-18 18:26:31 -07004261 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4262 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004263 // To catch cases where siblings of top-level views are made invisible, just check whether
4264 // the cling is directly set to GONE before dismissing it.
4265 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004266 final Runnable cleanUpClingCb = new Runnable() {
4267 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004268 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004269 // We should update the shared preferences on a background thread
4270 new Thread("dismissClingThread") {
4271 public void run() {
4272 SharedPreferences.Editor editor = mSharedPrefs.edit();
4273 editor.putBoolean(flag, true);
4274 editor.commit();
4275 }
4276 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004277 if (postAnimationCb != null) {
4278 postAnimationCb.run();
4279 }
4280 }
4281 };
4282 if (duration <= 0) {
4283 cleanUpClingCb.run();
4284 } else {
4285 cling.hide(duration, cleanUpClingCb);
4286 }
Michael Jurka22143132012-10-04 17:42:38 +02004287 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004288
4289 if (restoreNavBarVisibilty) {
4290 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4291 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4292 }
Winson Chung82f55532011-08-09 14:14:23 -07004293 }
4294 }
Michael Jurka22143132012-10-04 17:42:38 +02004295
Winson Chung9d9d74f2011-09-19 11:49:12 -07004296 private void removeCling(int id) {
4297 final View cling = findViewById(id);
4298 if (cling != null) {
4299 final ViewGroup parent = (ViewGroup) cling.getParent();
4300 parent.post(new Runnable() {
4301 @Override
4302 public void run() {
4303 parent.removeView(cling);
4304 }
4305 });
Michael Jurka22143132012-10-04 17:42:38 +02004306 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004307 }
4308 }
Michael Jurka974c3862012-05-22 22:00:31 -07004309
4310 private boolean skipCustomClingIfNoAccounts() {
4311 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4312 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4313 if (customCling) {
4314 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004315 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004316 Account[] accounts = am.getAccountsByType("com.google");
4317 return accounts.length == 0;
4318 }
4319 return false;
4320 }
4321
Winson Chung82e5c982013-10-09 12:04:50 -07004322 public void updateCustomContentHintVisibility() {
4323 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4324 String ccHintStr = getFirstRunCustomContentHint();
4325
4326 if (mWorkspace.hasCustomContent()) {
4327 // Show the custom content hint if ccHintStr is not empty
4328 if (cling != null) {
4329 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4330 }
4331 } else {
4332 // Hide the custom content hint
4333 if (cling != null) {
4334 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4335 }
4336 }
4337 }
4338
4339 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4340 boolean animate) {
4341 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4342 if (ccHint != null) {
4343 if (visible && !ccHintStr.isEmpty()) {
4344 ccHint.setText(ccHintStr);
4345 ccHint.setVisibility(View.VISIBLE);
4346 if (animate) {
4347 ccHint.setAlpha(0f);
4348 ccHint.animate().alpha(1f)
4349 .setDuration(SHOW_CLING_DURATION)
4350 .start();
4351 } else {
4352 ccHint.setAlpha(1f);
4353 }
4354 } else {
4355 if (animate) {
4356 ccHint.animate().alpha(0f)
4357 .setDuration(SHOW_CLING_DURATION)
4358 .setListener(new AnimatorListenerAdapter() {
4359 @Override
4360 public void onAnimationEnd(Animator animation) {
4361 ccHint.setVisibility(View.GONE);
4362 }
4363 })
4364 .start();
4365 } else {
4366 ccHint.setAlpha(0f);
4367 ccHint.setVisibility(View.GONE);
4368 }
4369 }
4370 }
4371 }
4372
Winson Chungaf40f202013-09-18 18:26:31 -07004373 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004374 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004375 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004376 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004377 // If we're not using the default workspace layout, replace workspace cling
4378 // with a custom workspace cling (usually specified in an overlay)
4379 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004380 if (!DISABLE_CUSTOM_CLINGS) {
4381 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4382 getResources().getBoolean(R.bool.config_useCustomClings)) {
4383 // Use a custom cling
4384 View cling = findViewById(R.id.workspace_cling);
4385 ViewGroup clingParent = (ViewGroup) cling.getParent();
4386 int clingIndex = clingParent.indexOfChild(cling);
4387 clingParent.removeViewAt(clingIndex);
4388 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4389 clingParent.addView(customCling, clingIndex);
4390 customCling.setId(R.id.workspace_cling);
4391 }
4392 }
4393 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4394 if (cling != null) {
4395 String sbHintStr = getFirstRunClingSearchBarHint();
4396 String ccHintStr = getFirstRunCustomContentHint();
4397 if (!sbHintStr.isEmpty()) {
4398 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4399 sbHint.setText(sbHintStr);
4400 sbHint.setVisibility(View.VISIBLE);
4401 }
Winson Chung82e5c982013-10-09 12:04:50 -07004402 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004403 }
Winson Chungfa545132013-09-26 17:45:32 -07004404 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004405 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004406 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004407 }
4408 }
Winson Chungaf40f202013-09-18 18:26:31 -07004409
Winson Chunge6eabff2013-09-24 11:01:23 -07004410 protected String getFirstRunClingSearchBarHint() {
4411 return "";
4412 }
4413 protected String getFirstRunCustomContentHint() {
4414 return "";
4415 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004416 protected int getFirstRunFocusedHotseatAppDrawableId() {
4417 return -1;
4418 }
4419 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4420 return null;
4421 }
4422 protected int getFirstRunFocusedHotseatAppRank() {
4423 return -1;
4424 }
4425 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4426 return "";
4427 }
4428 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4429 return "";
4430 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004431
Winson Chungaf40f202013-09-18 18:26:31 -07004432 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004433 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004434 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004435 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004436 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4437
4438 // Set the focused hotseat app if there is one
4439 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4440 getFirstRunFocusedHotseatAppRank(),
4441 getFirstRunFocusedHotseatAppComponentName(),
4442 getFirstRunFocusedHotseatAppBubbleTitle(),
4443 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004444 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004445 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004446 }
4447 }
4448 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004449 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004450 if (isClingsEnabled() &&
4451 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004452 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4453 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004454 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004455 } else {
4456 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004457 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004458 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004459 }
Michael Jurka104c4562013-07-08 18:03:46 -07004460 protected SharedPreferences getSharedPrefs() {
4461 return mSharedPrefs;
4462 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004463 public boolean isFolderClingVisible() {
4464 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004465 if (cling != null) {
4466 return cling.getVisibility() == View.VISIBLE;
4467 }
4468 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004469 }
Winson Chungaf40f202013-09-18 18:26:31 -07004470 public void dismissFirstRunCling(View v) {
4471 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4472 Runnable cb = new Runnable() {
4473 public void run() {
4474 // Show the workspace cling next
4475 showFirstRunWorkspaceCling();
4476 }
4477 };
4478 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4479 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004480
4481 // Fade out the search bar for the workspace cling coming up
4482 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004483 }
Winson Chung82f55532011-08-09 14:14:23 -07004484 public void dismissWorkspaceCling(View v) {
4485 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004486 Runnable cb = null;
4487 if (v == null) {
4488 cb = new Runnable() {
4489 public void run() {
4490 mWorkspace.enterOverviewMode();
4491 }
4492 };
4493 }
4494 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4495 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004496
4497 // Fade in the search bar
4498 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004499 }
4500 public void dismissFolderCling(View v) {
4501 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004502 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4503 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004504 }
4505
Mathew Inwood72fbec12013-11-19 15:45:07 +00004506 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4507 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4508 if (ri == null) {
4509 return null;
4510 }
4511 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4512 }
4513
4514 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4515 Bitmap icon) {
4516 return new ShortcutInfo(shortcutIntent, caption, icon);
4517 }
4518
4519 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4520 dragView.setTag(dragInfo);
4521 mWorkspace.onDragStartedWithItem(dragView);
4522 mWorkspace.beginDragShared(dragView, source);
4523 }
4524
Winson Chung80baf5a2010-08-09 16:03:15 -07004525 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004526 * Prints out out state for debugging.
4527 */
4528 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004529 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004530 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004531 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4532 Log.d(TAG, "mRestoring=" + mRestoring);
4533 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4534 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004535 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004536 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004537
Winson Chung785d2eb2011-04-14 16:08:02 -07004538 if (mAppsCustomizeContent != null) {
4539 mAppsCustomizeContent.dumpState();
4540 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004541 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004542 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004543
4544 @Override
4545 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4546 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004547 synchronized (sDumpLogs) {
4548 writer.println(" ");
4549 writer.println("Debug logs: ");
4550 for (int i = 0; i < sDumpLogs.size(); i++) {
4551 writer.println(" " + sDumpLogs.get(i));
4552 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004553 }
4554 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004555
4556 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004557 if (DEBUG_DUMP_LOG) {
4558 synchronized (sDumpLogs) {
4559 Log.d(TAG, "");
4560 Log.d(TAG, "*********************");
4561 Log.d(TAG, "Launcher debug logs: ");
4562 for (int i = 0; i < sDumpLogs.size(); i++) {
4563 Log.d(TAG, " " + sDumpLogs.get(i));
4564 }
4565 Log.d(TAG, "*********************");
4566 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004567 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004568 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004569 }
4570
4571 public static void addDumpLog(String tag, String log, boolean debugLog) {
4572 if (debugLog) {
4573 Log.d(tag, log);
4574 }
Winson Chungede41292013-09-19 16:27:36 -07004575 if (DEBUG_DUMP_LOG) {
4576 sDateStamp.setTime(System.currentTimeMillis());
4577 synchronized (sDumpLogs) {
4578 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004579 }
Winson Chungede41292013-09-19 16:27:36 -07004580 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004581 }
4582
Winson Chungede41292013-09-19 16:27:36 -07004583 public void dumpLogsToLocalData() {
4584 if (DEBUG_DUMP_LOG) {
4585 new Thread("DumpLogsToLocalData") {
4586 @Override
4587 public void run() {
4588 boolean success = false;
4589 sDateStamp.setTime(sRunStart);
4590 String FILENAME = sDateStamp.getMonth() + "-"
4591 + sDateStamp.getDay() + "_"
4592 + sDateStamp.getHours() + "-"
4593 + sDateStamp.getMinutes() + "_"
4594 + sDateStamp.getSeconds() + ".txt";
4595
4596 FileOutputStream fos = null;
4597 File outFile = null;
4598 try {
4599 outFile = new File(getFilesDir(), FILENAME);
4600 outFile.createNewFile();
4601 fos = new FileOutputStream(outFile);
4602 } catch (Exception e) {
4603 e.printStackTrace();
4604 }
4605 if (fos != null) {
4606 PrintWriter writer = new PrintWriter(fos);
4607
4608 writer.println(" ");
4609 writer.println("Debug logs: ");
4610 synchronized (sDumpLogs) {
4611 for (int i = 0; i < sDumpLogs.size(); i++) {
4612 writer.println(" " + sDumpLogs.get(i));
4613 }
4614 }
4615 writer.close();
4616 }
4617 try {
4618 if (fos != null) {
4619 fos.close();
4620 success = true;
4621 }
4622 } catch (IOException e) {
4623 e.printStackTrace();
4624 }
4625 }
4626 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004627 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004629}
Michael Jurka2763be32011-02-24 11:19:57 -08004630
Michael Jurkaabded662011-03-04 12:06:57 -08004631interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004632 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004633 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004634 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004635 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004636 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004637}