blob: 6e3032f286aa08b262f6b76fd12b975ea14a4070 [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
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070045import android.content.pm.PackageManager.NameNotFoundException;
Mathew Inwood72fbec12013-11-19 15:45:07 +000046import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070050import android.graphics.Bitmap;
51import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070052import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070057import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070062import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070068import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070070import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080089import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
94import android.widget.TextView;
95import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070096
Daniel Sandler325dc232013-06-05 22:57:57 -040097import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700101import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000113import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
Winson Chunga6945242014-01-08 14:04:34 -0800115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100119public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700121 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Daniel Sandlerf061f822013-06-27 13:59:36 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700127 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400128 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700129 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700130
Dan Sandlerd5024042014-01-09 15:01:33 -0500131 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
132
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";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800158 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Winson Chung2672ff92012-05-04 16:22:30 -0700160 // The Intent extra that defines whether to ignore the launch animation
161 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400162 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: int
165 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700166 // Type: int
167 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700169 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
170 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
172 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700173 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700175 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: boolean
177 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
178 // Type: long
179 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700180 // Type: int
181 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
182 // Type: int
183 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
184 // Type: parcelable
185 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000186 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800187 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000188 // Type: int[]
189 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
Adam Cohenb54a5982014-01-08 15:21:04 -0800191
192 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
193
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100194 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700195 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100196 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700197 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100198 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700199
Adam Cohen39a06042013-07-19 14:30:12 -0700200 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700201 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700202
Winson Chunga6945242014-01-08 14:04:34 -0800203 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
204
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700206 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700207 private State mState = State.WORKSPACE;
208 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700211 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
212 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800215 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000217 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
218 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
219
Winson Chunga2413752012-04-03 14:22:34 -0700220 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700221 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
222 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700223 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700224
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800225 private final BroadcastReceiver mCloseSystemDialogsReceiver
226 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800227 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private LayoutInflater mInflater;
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700232 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800233 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800234 private DragLayer mDragLayer;
235 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700236 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800237 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700239 private AppWidgetManager mAppWidgetManager;
240 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Michael Jurkac9d95c52011-08-29 14:03:34 -0700242 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700243 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800244 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700245
Michael Jurka0280c3b2010-09-17 15:00:07 -0700246 private int[] mTmpAddItemCellCoordinates = new int[2];
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private FolderInfo mFolderInfo;
249
Winson Chung3d503fb2011-07-13 17:25:49 -0700250 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800251 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700252
Michael Jurka838a4ca2011-02-07 13:33:06 -0800253 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700254
Winson Chungc51db6a2011-10-05 11:44:49 -0700255 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700256 private AppsCustomizeTabHost mAppsCustomizeTabHost;
257 private AppsCustomizePagedView mAppsCustomizeContent;
258 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800259 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700262 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
263 // scroll issues (because the workspace may not have been measured yet) and extra work.
264 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
265 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266
267 private SpannableStringBuilder mDefaultKeySsb = null;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private boolean mWorkspaceLoading = true;
270
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800271 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 private boolean mRestoring;
273 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700274 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
Michael Jurka1e2f4652013-07-08 18:03:46 -0700276 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700277 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
278
Winson Chung4a2afa32012-07-19 14:53:05 -0700279 // Keep track of whether the user has left launcher
280 private static boolean sPausedFromUserAction = false;
281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private Bundle mSavedInstanceState;
283
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800286 private boolean mUserPresent = true;
287 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700288 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700291 private static LocaleConfiguration sLocaleConfiguration = null;
292
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700293 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700294
Adam Cohen61f560d2013-09-30 15:58:20 -0700295 private View.OnTouchListener mHapticFeedbackTouchListener;
296
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 // Related to the auto-advancing of widgets
298 private final int ADVANCE_MSG = 1;
299 private final int mAdvanceInterval = 20000;
300 private final int mAdvanceStagger = 250;
301 private long mAutoAdvanceSentTime;
302 private long mAutoAdvanceTimeLeft = -1;
303 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
304 new HashMap<View, AppWidgetProviderInfo>();
305
Winson Chung400438b2011-01-16 17:53:48 -0800306 // Determines how long to wait after a rotation before restoring the screen orientation to
307 // match the sensor state.
308 private final int mRestoreScreenOrientationDelay = 500;
309
Michael Jurka4ef207b2010-11-29 17:05:45 -0800310 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700311 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
312 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
313 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800314
Winson Chungd64a6662013-09-30 11:06:59 -0700315 private Intent mAppMarketIntent = null;
316 private static final boolean DISABLE_MARKET_BUTTON = true;
317
Craig Mautner360310b2012-10-26 15:13:08 -0700318 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700319
Adam Cohen1462de32012-07-24 22:34:36 -0700320 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700321 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700322
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700323 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700324 static Date sDateStamp = new Date();
325 static DateFormat sDateFormat =
326 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
327 static long sRunStart = System.currentTimeMillis();
328 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700329
Winson Chung46353de2012-02-16 14:05:10 -0800330 // We only want to get the SharedPreferences once since it does an FS stat each time we get
331 // it from the context.
332 private SharedPreferences mSharedPrefs;
333
Adam Cohene25af792013-06-06 23:08:25 -0700334 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
335
Winson Chungf0c6ae02012-03-21 16:10:31 -0700336 // Holds the page that we need to animate to, and the icon views that we need to animate up
337 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700338 private ImageView mFolderIconImageView;
339 private Bitmap mFolderIconBitmap;
340 private Canvas mFolderIconCanvas;
341 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700342
Michael Jurkaddd62e92011-02-16 17:49:14 -0800343 private BubbleTextView mWaitingForResume;
344
Michael Jurkac1f5d262011-09-30 19:32:27 -0700345 private Runnable mBuildLayersRunnable = new Runnable() {
346 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700347 if (mWorkspace != null) {
348 mWorkspace.buildPageHardwareLayers();
349 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700350 }
351 };
352
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353 private static ArrayList<PendingAddArguments> sPendingAddList
354 = new ArrayList<PendingAddArguments>();
355
Michael Jurka7267fa52013-09-26 15:29:57 -0700356 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800357
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358 private static class PendingAddArguments {
359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
365 }
366
Daniel Sandlerff02d492013-08-05 02:12:05 -0400367 private Stats mStats;
368
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800369 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800370 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700371 }
372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 @Override
374 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700375 if (DEBUG_STRICT_MODE) {
376 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
377 .detectDiskReads()
378 .detectDiskWrites()
379 .detectNetwork() // or .detectAll() for all detectable problems
380 .penaltyLog()
381 .build());
382 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
383 .detectLeakedSqlLiteObjects()
384 .detectLeakedClosableObjects()
385 .penaltyLog()
386 .penaltyDeath()
387 .build());
388 }
389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400391
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400392 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400393 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700394
395 // Determine the dynamic grid properties
396 Point smallestSize = new Point();
397 Point largestSize = new Point();
398 Point realSize = new Point();
399 Display display = getWindowManager().getDefaultDisplay();
400 display.getCurrentSizeRange(smallestSize, largestSize);
401 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700402 DisplayMetrics dm = new DisplayMetrics();
403 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700404
Winson Chung5f8afe62013-08-12 16:19:28 -0700405 // Lazy-initialize the dynamic grid
406 DeviceProfile grid = app.initDynamicGrid(this,
407 Math.min(smallestSize.x, smallestSize.y),
408 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700409 realSize.x, realSize.y,
410 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700411
412 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700414 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800415 mModel = app.setLauncher(this);
416 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700417 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400418 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800419 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Daniel Sandlerff02d492013-08-05 02:12:05 -0400422 mStats = new Stats(this);
423
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700424 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700425
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700426 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
427 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700428
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700429 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
430 // this also ensures that any synchronous binding below doesn't re-trigger another
431 // LauncherModel load.
432 mPaused = false;
433
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200435 android.os.Debug.startMethodTracing(
436 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 }
438
Adam Cohen53805212013-10-01 10:39:23 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100444 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800446 registerContentObservers();
447
Joe Onorato7c312c12009-08-13 21:36:53 -0700448 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 mSavedState = savedInstanceState;
451 restoreState(mSavedState);
452
453 if (PROFILE_STARTUP) {
454 android.os.Debug.stopMethodTracing();
455 }
456
457 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700458 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 // If the user leaves launcher, then we should just load items asynchronously when
460 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500461 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 } else {
463 // We only load the page synchronously if the user rotates (or triggers a
464 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800465 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468
469 // For handling default keys
470 mDefaultKeySsb = new SpannableStringBuilder();
471 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800472
473 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
474 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800475
Adam Cohenf9426d52012-06-04 17:26:21 -0700476 updateGlobalIcons();
477
478 // On large interfaces, we want the screen to auto-rotate based on the current orientation
479 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700480
Adam Cohenb54a5982014-01-08 15:21:04 -0800481 showFirstRunActivity();
Winson Chunga6945242014-01-08 14:04:34 -0800482 if (mModel.canMigrateFromOldLauncherDb()) {
483 mLauncherClings.showMigrationCling();
484 } else {
485 mLauncherClings.showFirstRunCling();
486 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700487 }
488
Winson Chung4a2afa32012-07-19 14:53:05 -0700489 protected void onUserLeaveHint() {
490 super.onUserLeaveHint();
491 sPausedFromUserAction = true;
492 }
493
Winson Chung98ca0f72013-07-29 12:58:51 -0700494 /** To be overriden by subclasses to hint to Launcher that we have custom content */
495 protected boolean hasCustomContentToLeft() {
496 return false;
497 }
498
Dave Hawkeya8881582013-09-17 15:55:33 -0600499 /**
500 * To be overridden by subclasses to create the custom content and call
501 * {@link #addToCustomContentPage}. This will only be invoked if
502 * {@link #hasCustomContentToLeft()} is {@code true}.
503 */
504 protected void addCustomContentToLeft() {
505 }
506
507 /**
Adam Cohenb54a5982014-01-08 15:21:04 -0800508 * To be overridden by subclasses to indicate that there is an activity to launch
509 * before showing the standard launcher experience.
510 */
511 protected boolean hasFirstRunActivity() {
512 return false;
513 }
514
515 /**
516 * To be overridden by subclasses to launch any first run activity
517 */
518 protected Intent getFirstRunActivity() {
519 return null;
520 }
521
522 /**
Dave Hawkeya8881582013-09-17 15:55:33 -0600523 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
524 * ensure the custom content page is added or removed if necessary.
525 */
526 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600527 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600528 // Not bound yet, wait for bindScreens to be called.
529 return;
530 }
531
532 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
533 // Create the custom content page and call the subclass to populate it.
534 mWorkspace.createCustomContentPage();
535 addCustomContentToLeft();
536 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
537 mWorkspace.removeCustomContentPage();
538 }
539 }
540
Adam Cohenf9426d52012-06-04 17:26:21 -0700541 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700542 boolean searchVisible = false;
543 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800544 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700545 int coi = getCurrentOrientationIndexForGlobalIcons();
546 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
547 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700548 if (!DISABLE_MARKET_BUTTON) {
549 updateAppMarketIcon();
550 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700551 searchVisible = updateGlobalSearchIcon();
552 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700553 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700554 if (sGlobalSearchIcon[coi] != null) {
555 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700556 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700557 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700558 if (sVoiceSearchIcon[coi] != null) {
559 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700560 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700561 }
Winson Chungd64a6662013-09-30 11:06:59 -0700562 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700563 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800564 }
Winson Chungadf0c182012-08-23 14:59:07 -0700565 if (mSearchDropTargetBar != null) {
566 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
Romain Guycbb89e42009-06-08 15:52:54 -0700569
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700571 if (sLocaleConfiguration == null) {
572 new AsyncTask<Void, Void, LocaleConfiguration>() {
573 @Override
574 protected LocaleConfiguration doInBackground(Void... unused) {
575 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
576 readConfiguration(Launcher.this, localeConfiguration);
577 return localeConfiguration;
578 }
579
580 @Override
581 protected void onPostExecute(LocaleConfiguration result) {
582 sLocaleConfiguration = result;
583 checkForLocaleChange(); // recursive, but now with a locale configuration
584 }
585 }.execute();
586 return;
587 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700588
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 final Configuration configuration = getResources().getConfiguration();
590
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700591 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 final String locale = configuration.locale.toString();
593
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700594 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 final int mcc = configuration.mcc;
596
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700597 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 final int mnc = configuration.mnc;
599
Romain Guy84f296c2009-11-04 15:00:44 -0800600 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601
Romain Guy84f296c2009-11-04 15:00:44 -0800602 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700603 sLocaleConfiguration.locale = locale;
604 sLocaleConfiguration.mcc = mcc;
605 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700606
Joe Onorato0589f0f2010-02-08 13:44:00 -0800607 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700608
609 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100610 new AsyncTask<Void, Void, Void>() {
611 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700612 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100613 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700614 }
Michael Jurka43467462013-11-14 12:03:58 +0100615 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700616 }
617 }
618
619 private static class LocaleConfiguration {
620 public String locale;
621 public int mcc = -1;
622 public int mnc = -1;
623 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700624
Romain Guy98d01652009-06-30 16:21:04 -0700625 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
626 DataInputStream in = null;
627 try {
628 in = new DataInputStream(context.openFileInput(PREFERENCES));
629 configuration.locale = in.readUTF();
630 configuration.mcc = in.readInt();
631 configuration.mnc = in.readInt();
632 } catch (FileNotFoundException e) {
633 // Ignore
634 } catch (IOException e) {
635 // Ignore
636 } finally {
637 if (in != null) {
638 try {
639 in.close();
640 } catch (IOException e) {
641 // Ignore
642 }
643 }
644 }
645 }
646
647 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
648 DataOutputStream out = null;
649 try {
650 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
651 out.writeUTF(configuration.locale);
652 out.writeInt(configuration.mcc);
653 out.writeInt(configuration.mnc);
654 out.flush();
655 } catch (FileNotFoundException e) {
656 // Ignore
657 } catch (IOException e) {
658 //noinspection ResultOfMethodCallIgnored
659 context.getFileStreamPath(PREFERENCES).delete();
660 } finally {
661 if (out != null) {
662 try {
663 out.close();
664 } catch (IOException e) {
665 // Ignore
666 }
667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669 }
670
Anton Hanssona2f665f2013-09-26 12:18:32 +0100671 public Stats getStats() {
672 return mStats;
673 }
674
Bjorn Bringertc459e522013-06-07 19:36:01 +0100675 public LayoutInflater getInflater() {
676 return mInflater;
677 }
678
Winson Chung36a62fe2012-05-06 18:04:42 -0700679 boolean isDraggingEnabled() {
680 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
681 // that is subsequently removed from the workspace in startBinding().
682 return !mModel.isLoadingWorkspace();
683 }
684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 static int getScreen() {
686 synchronized (sLock) {
687 return sScreen;
688 }
689 }
690
691 static void setScreen(int screen) {
692 synchronized (sLock) {
693 sScreen = screen;
694 }
695 }
696
Winson Chung557d6ed2011-07-08 15:34:52 -0700697 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000698 * Copied from View -- the View version of the method isn't called
699 * anywhere else in our process and only exists for API level 17+,
700 * so it's ok to keep our own version with no API requirement.
701 */
702 public static int generateViewId() {
703 for (;;) {
704 final int result = sNextGeneratedId.get();
705 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
706 int newValue = result + 1;
707 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
708 if (sNextGeneratedId.compareAndSet(result, newValue)) {
709 return result;
710 }
711 }
712 }
713
714 public int getViewIdForItem(ItemInfo info) {
715 // This cast is safe given the > 2B range for int.
716 int itemId = (int) info.id;
717 if (mItemIdToViewId.containsKey(itemId)) {
718 return mItemIdToViewId.get(itemId);
719 }
720 int viewId = generateViewId();
721 mItemIdToViewId.put(itemId, viewId);
722 return viewId;
723 }
724
725 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700726 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
727 * a configuration step, this allows the proper animations to run after other transitions.
728 */
729 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700730 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 switch (args.requestCode) {
732 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700733 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800735 break;
736 case REQUEST_PICK_SHORTCUT:
737 processShortcut(args.intent);
738 break;
739 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700740 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700741 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700742 result = true;
743 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 case REQUEST_CREATE_APPWIDGET:
745 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700746 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700747 result = true;
748 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800749 }
Michael Jurka27614d22012-04-02 06:40:22 -0700750 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
751 // if you turned the screen off and then back while in All Apps, Launcher would not
752 // return to the workspace. Clearing mAddInfo.container here fixes this issue
753 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700754 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800755 }
756
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 protected void onActivityResult(
759 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700760 // Reset the startActivity waiting flag
761 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800762 int pendingAddWidgetId = mPendingAddWidgetId;
763 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700764
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 Runnable exitSpringLoaded = new Runnable() {
766 @Override
767 public void run() {
768 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
769 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
770 }
771 };
772
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
776 if (resultCode == RESULT_CANCELED) {
777 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
779 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700780 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
782 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 }
784 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200785 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
786 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
787 mWorkspace.exitOverviewMode(false);
788 }
789 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700790 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200791
Adam Cohened66b2b2012-01-23 17:28:51 -0800792 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
793 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700794
Adam Cohened66b2b2012-01-23 17:28:51 -0800795 // We have special handling for widgets
796 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800797 final int appWidgetId;
798 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
799 : -1;
800 if (widgetId < 0) {
801 appWidgetId = pendingAddWidgetId;
802 } else {
803 appWidgetId = widgetId;
804 }
805
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 final int result;
807 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800808 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700809 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
810 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700811 result = RESULT_CANCELED;
812 completeTwoStageWidgetDrop(result, appWidgetId);
813 onComplete = new Runnable() {
814 @Override
815 public void run() {
816 exitSpringLoadedDragModeDelayed(false, 0, null);
817 }
818 };
Winson Chung5aaab772012-04-27 15:24:02 -0700819 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700820 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700821 final CellLayout dropLayout =
822 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
823 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700824 onComplete = new Runnable() {
825 @Override
826 public void run() {
827 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700828 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700829 }
830 };
Winson Chung5aaab772012-04-27 15:24:02 -0700831 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700832 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
833 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800834 return;
835 }
836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 // The pattern used here is that a user PICKs a specific application,
838 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
841 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700842 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800843 final PendingAddArguments args = new PendingAddArguments();
844 args.requestCode = requestCode;
845 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 args.cellX = mPendingAddInfo.cellX;
849 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800850 if (isWorkspaceLocked()) {
851 sPendingAddList.add(args);
852 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700853 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700855 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
856 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700857 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700858 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
859 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800861 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800862 }
863
864 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700865 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700866 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800867 Runnable onCompleteRunnable = null;
868 int animationType = 0;
869
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700870 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 if (resultCode == RESULT_OK) {
872 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
873 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700874 mPendingAddWidgetInfo);
875 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 onCompleteRunnable = new Runnable() {
877 @Override
878 public void run() {
879 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700880 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700881 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
882 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800883 }
884 };
885 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800886 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800887 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800888 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700889 if (mDragLayer.getAnimatedView() != null) {
890 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
891 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
892 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700893 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700894 // The animated view may be null in the case of a rotation during widget configuration
895 onCompleteRunnable.run();
896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
898
899 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700900 protected void onStop() {
901 super.onStop();
902 FirstFrameAnimatorHelper.setIsVisible(false);
903 }
904
905 @Override
906 protected void onStart() {
907 super.onStart();
908 FirstFrameAnimatorHelper.setIsVisible(true);
909 }
910
911 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200913 long startTime = 0;
914 if (DEBUG_RESUME_TIME) {
915 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400916 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700919
Winson Chung4a2afa32012-07-19 14:53:05 -0700920 // Restore the previous launcher state
921 if (mOnResumeState == State.WORKSPACE) {
922 showWorkspace(false);
923 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800924 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700925 }
926 mOnResumeState = State.NONE;
927
Craig Mautner360310b2012-10-26 15:13:08 -0700928 // Background was set to gradient in onPause(), restore to black if in all apps.
929 setWorkspaceBackground(mState == State.WORKSPACE);
930
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800931 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700932 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700933 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500935 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700937 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700939 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200940 // We might have postponed some bind calls until onResume (see waitUntilResume) --
941 // execute them here
942 long startTimeCallbacks = 0;
943 if (DEBUG_RESUME_TIME) {
944 startTimeCallbacks = System.currentTimeMillis();
945 }
946
947 if (mAppsCustomizeContent != null) {
948 mAppsCustomizeContent.setBulkBind(true);
949 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700950 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
951 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200952 }
953 if (mAppsCustomizeContent != null) {
954 mAppsCustomizeContent.setBulkBind(false);
955 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700956 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200957 if (DEBUG_RESUME_TIME) {
958 Log.d(TAG, "Time spent processing callbacks in onResume: " +
959 (System.currentTimeMillis() - startTimeCallbacks));
960 }
Michael Jurka447bf842013-05-15 14:52:15 +0200961 }
Michael Jurka54554252013-08-01 12:52:23 +0200962 if (mOnResumeCallbacks.size() > 0) {
963 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
964 mOnResumeCallbacks.get(i).run();
965 }
966 mOnResumeCallbacks.clear();
967 }
Winson Chunge4e50662012-01-23 14:45:13 -0800968
969 // Reset the pressed state of icons that were locked in the press state while activities
970 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800971 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800972 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800973 mWaitingForResume.setStayPressed(false);
974 }
Winson Chunge4e50662012-01-23 14:45:13 -0800975 if (mAppsCustomizeContent != null) {
976 // Resets the previous all apps icon press state
977 mAppsCustomizeContent.resetDrawableState();
978 }
Winson Chung82963d52013-10-09 11:20:57 -0700979
Adam Cohen06dff352012-06-01 17:17:08 -0700980 // It is possible that widgets can receive updates while launcher is not in the foreground.
981 // Consequently, the widgets will be inflated in the orientation of the foreground activity
982 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
983 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700984 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700985
Winson Chung780fe592013-09-26 14:48:44 -0700986 // Process any items that were added while Launcher was away.
987 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
988
Winson Chung5841efa2013-09-30 18:06:44 -0700989 // Update the voice search button proxy
990 updateVoiceButtonProxyVisible(false);
991
Adam Cohenf9426d52012-06-04 17:26:21 -0700992 // Again, as with the above scenario, it's possible that one or more of the global icons
993 // were updated in the wrong orientation.
994 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200995 if (DEBUG_RESUME_TIME) {
996 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
997 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700998
999 if (mWorkspace.getCustomContentCallbacks() != null) {
1000 // If we are resuming and the custom content is the current page, we call onShow().
1001 // It is also poassible that onShow will instead be called slightly after first layout
1002 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1003 if (mWorkspace.isOnOrMovingToCustomContent()) {
1004 mWorkspace.getCustomContentCallbacks().onShow();
1005 }
1006 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001007 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001008 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001009 }
1010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001012 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001013 // Ensure that items added to Launcher are queued until Launcher returns
1014 InstallShortcutReceiver.enableInstallQueue();
1015
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001016 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001017 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001018 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001019 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001020
1021 // We call onHide() aggressively. The custom content callbacks should be able to
1022 // debounce excess onHide calls.
1023 if (mWorkspace.getCustomContentCallbacks() != null) {
1024 mWorkspace.getCustomContentCallbacks().onHide();
1025 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 }
Romain Guycbb89e42009-06-08 15:52:54 -07001027
Adam Cohen66a01fd2013-06-11 12:48:00 -07001028 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -07001029 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -06001030 addCustomContentToLeft();
1031 }
Adam Cohen66a01fd2013-06-11 12:48:00 -07001032 }
1033
Adam Cohenbffe7452013-07-22 18:21:45 -07001034 QSBScroller mQsbScroller = new QSBScroller() {
1035 int scrollY = 0;
1036
1037 @Override
1038 public void setScrollY(int scroll) {
1039 scrollY = scroll;
1040
1041 if (mWorkspace.isOnOrMovingToCustomContent()) {
1042 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001043 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001044 }
1045 }
1046 };
1047
1048 public void resetQSBScroll() {
1049 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001050 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001051 }
1052
1053 public interface CustomContentCallbacks {
1054 // Custom content is completely shown
1055 public void onShow();
1056
1057 // Custom content is completely hidden
1058 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001059
1060 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1061 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001062 }
1063
Jorim Jaggid017f882014-01-14 17:08:48 -08001064 protected boolean hasSettings() {
1065 return false;
1066 }
1067
Adam Cohen30bacb22013-08-29 14:25:25 -07001068 protected void startSettings() {
1069 }
1070
Adam Cohenbffe7452013-07-22 18:21:45 -07001071 public interface QSBScroller {
1072 public void setScrollY(int scrollY);
1073 }
1074
Winson Chung98ca0f72013-07-29 12:58:51 -07001075 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001076 CustomContentCallbacks callbacks, String description) {
1077 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001078 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001079 }
1080
Adam Cohenedb40762013-07-18 16:45:45 -07001081 // The custom content needs to offset its content to account for the QSB
1082 public int getTopOffsetForCustomContent() {
1083 return mWorkspace.getPaddingTop();
1084 }
1085
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001086 @Override
1087 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 // Flag the loader to stop early before switching
1089 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001090 if (mAppsCustomizeContent != null) {
1091 mAppsCustomizeContent.surrender();
1092 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001093 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001094 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001096 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001097 @Override
1098 public void onWindowFocusChanged(boolean hasFocus) {
1099 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001100 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001101 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 private boolean acceptFilter() {
1104 final InputMethodManager inputManager = (InputMethodManager)
1105 getSystemService(Context.INPUT_METHOD_SERVICE);
1106 return !inputManager.isFullscreenMode();
1107 }
1108
1109 @Override
1110 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001111 final int uniChar = event.getUnicodeChar();
1112 final boolean handled = super.onKeyDown(keyCode, event);
1113 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1114 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1116 keyCode, event);
1117 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001118 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001119 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001120 // showSearchDialog()
1121 // If there are multiple keystrokes before the search dialog takes focus,
1122 // onSearchRequested() will be called for every keystroke,
1123 // but it is idempotent, so it's fine.
1124 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126 }
1127
Joe Onorato8a9625e2010-01-28 15:55:35 -08001128 // Eat the long press event so the keyboard doesn't come up.
1129 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1130 return true;
1131 }
1132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 return handled;
1134 }
1135
Karl Rosaen138a0412009-04-23 19:00:21 -07001136 private String getTypedText() {
1137 return mDefaultKeySsb.toString();
1138 }
1139
1140 private void clearTypedText() {
1141 mDefaultKeySsb.clear();
1142 mDefaultKeySsb.clearSpans();
1143 Selection.setSelection(mDefaultKeySsb, 0);
1144 }
1145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001147 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1148 * State
1149 */
1150 private static State intToState(int stateOrdinal) {
1151 State state = State.WORKSPACE;
1152 final State[] stateValues = State.values();
1153 for (int i = 0; i < stateValues.length; i++) {
1154 if (stateValues[i].ordinal() == stateOrdinal) {
1155 state = stateValues[i];
1156 break;
1157 }
1158 }
1159 return state;
1160 }
1161
1162 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 * Restores the previous state, if it exists.
1164 *
1165 * @param savedState The previous state.
1166 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001167 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 private void restoreState(Bundle savedState) {
1169 if (savedState == null) {
1170 return;
1171 }
1172
Michael Jurka883f55b2010-10-21 15:47:14 -07001173 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001174 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001175 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001176 }
1177
Adam Cohen21cd0022013-10-09 18:57:02 -07001178 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1179 PagedView.INVALID_RESTORE_PAGE);
1180 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001181 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
1183
Winson Chung3d503fb2011-07-13 17:25:49 -07001184 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001185 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001186
Winson Chung3d503fb2011-07-13 17:25:49 -07001187 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1188 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001189 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001190 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1191 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001192 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1193 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1194 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001195 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001196 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 mRestoring = true;
1198 }
1199
1200 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1201 if (renameFolder) {
1202 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001203 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 mRestoring = true;
1205 }
Winson Chunga12a2502010-12-20 14:41:35 -08001206
Winson Chung785d2eb2011-04-14 16:08:02 -07001207 // Restore the AppsCustomize tab
1208 if (mAppsCustomizeTabHost != null) {
1209 String curTab = savedState.getString("apps_customize_currentTab");
1210 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001211 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001212 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001213 mAppsCustomizeContent.loadAssociatedPages(
1214 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 }
1216
Winson Chung5afbf7b2011-07-25 11:53:08 -07001217 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1218 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001219 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001220 mItemIdToViewId = (HashMap<Integer, Integer>)
1221 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
1223
1224 /**
1225 * Finds all the views we need and configure them properly.
1226 */
1227 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001228 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001229
Craig Mautner360310b2012-10-26 15:13:08 -07001230 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001231 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1232 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001233 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001234
John Spurlock77e1f472013-09-11 10:09:51 -04001235 mLauncherView.setSystemUiVisibility(
1236 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001237 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238
Winson Chung4c98d922011-05-31 16:50:48 -07001239 // Setup the drag layer
1240 mDragLayer.setup(this, dragController);
1241
Winson Chung3d503fb2011-07-13 17:25:49 -07001242 // Setup the hotseat
1243 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1244 if (mHotseat != null) {
1245 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001246 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001247 }
1248
Jorim Jaggid017f882014-01-14 17:08:48 -08001249 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001250 View widgetButton = findViewById(R.id.widget_button);
1251 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001252 @Override
1253 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001254 if (!mWorkspace.isSwitchingState()) {
1255 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1256 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001257 }
1258 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001259 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1260
1261 View wallpaperButton = findViewById(R.id.wallpaper_button);
1262 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001263 @Override
1264 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001265 if (!mWorkspace.isSwitchingState()) {
1266 startWallpaper();
1267 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001268 }
1269 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001270 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1271
1272 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001273 if (hasSettings()) {
1274 settingsButton.setOnClickListener(new OnClickListener() {
1275 @Override
1276 public void onClick(View arg0) {
1277 if (!mWorkspace.isSwitchingState()) {
1278 startSettings();
1279 }
1280 }
1281 });
1282 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1283 } else {
1284 settingsButton.setVisibility(View.GONE);
1285 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1286 lp.gravity = Gravity.END | Gravity.TOP;
1287 widgetButton.requestLayout();
1288 }
1289
Adam Cohendbdff6b2013-09-18 19:09:15 -07001290 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001291
Winson Chung4c98d922011-05-31 16:50:48 -07001292 // Setup the workspace
1293 mWorkspace.setHapticFeedbackEnabled(false);
1294 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001295 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001296 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001297
Winson Chungf0ea4d32011-06-06 14:27:16 -07001298 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001299 mSearchDropTargetBar = (SearchDropTargetBar)
1300 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001301
Winson Chungf0ea4d32011-06-06 14:27:16 -07001302 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001303 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001304 mAppsCustomizeContent = (AppsCustomizePagedView)
1305 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1306 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001307
Winson Chung3d503fb2011-07-13 17:25:49 -07001308 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001309 dragController.setDragScoller(mWorkspace);
1310 dragController.setScrollView(mDragLayer);
1311 dragController.setMoveTarget(mWorkspace);
1312 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001313 if (mSearchDropTargetBar != null) {
1314 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001315 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001316
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001317 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001318 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001319 mWeightWatcher = new WeightWatcher(this);
1320 mWeightWatcher.setAlpha(0.5f);
1321 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001322 new FrameLayout.LayoutParams(
1323 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001324 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001325 Gravity.BOTTOM)
1326 );
Adam Cohen39a06042013-07-19 14:30:12 -07001327
1328 boolean show = shouldShowWeightWatcher();
1329 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
1333 /**
1334 * Creates a view representing a shortcut.
1335 *
1336 * @param info The data structure describing the shortcut.
1337 *
1338 * @return A View inflated from R.layout.application.
1339 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001340 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001342 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344
1345 /**
1346 * Creates a view representing a shortcut inflated from the specified resource.
1347 *
1348 * @param layoutResId The id of the XML layout used to create the shortcut.
1349 * @param parent The group the shortcut belongs to.
1350 * @param info The data structure describing the shortcut.
1351 *
1352 * @return A View inflated from layoutResId.
1353 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001354 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001355 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1356 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 return favorite;
1359 }
1360
1361 /**
1362 * Add an application shortcut to the workspace.
1363 *
1364 * @param data The intent describing the application.
1365 * @param cellInfo The position on screen where to create the shortcut.
1366 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001367 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001369 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001370
Adam Cohenfbba09b2011-07-18 21:43:05 -07001371 // First we check if we already know the exact location where we want to add this item.
1372 if (cellX >= 0 && cellY >= 0) {
1373 cellXY[0] = cellX;
1374 cellXY[1] = cellY;
1375 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001376 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001377 return;
1378 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001380 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001381
Romain Guy73b979d2009-06-09 12:57:21 -07001382 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001383 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001385 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001386 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001387 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001388 } else {
1389 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
1391 }
Romain Guycbb89e42009-06-08 15:52:54 -07001392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 /**
1394 * Add a shortcut to the workspace.
1395 *
1396 * @param data The intent describing the shortcut.
1397 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001399 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001400 int cellY) {
1401 int[] cellXY = mTmpAddItemCellCoordinates;
1402 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001403 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001404
Michael Jurkad3ef3062010-11-23 16:23:58 -08001405 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001406
Adam Cohen558baaf2011-08-15 15:22:57 -07001407 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001408 if (info == null) {
1409 return;
1410 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001411 final View view = createShortcut(info);
1412
Adam Cohenfbba09b2011-07-18 21:43:05 -07001413 // First we check if we already know the exact location where we want to add this item.
1414 if (cellX >= 0 && cellY >= 0) {
1415 cellXY[0] = cellX;
1416 cellXY[1] = cellY;
1417 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001418
1419 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001420 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001421 true, null,null)) {
1422 return;
1423 }
1424 DragObject dragObject = new DragObject();
1425 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001426 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1427 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001428 return;
1429 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001430 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001431 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001432 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001433 foundCellSpan = (result != null);
1434 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001435 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001436 }
1437
1438 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001439 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001440 return;
1441 }
1442
Adam Cohendcd297f2013-06-18 13:13:40 -07001443 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444
1445 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001446 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001447 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 }
1449 }
1450
Adam Cohen2f093b62012-04-30 18:59:53 -07001451 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1452 int minHeight) {
1453 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001454 // We want to account for the extra amount of padding that we are adding to the widget
1455 // to ensure that it gets the full amount of space that it has requested
1456 int requiredWidth = minWidth + padding.left + padding.right;
1457 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001458 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001459 }
1460
Adam Cohen2f093b62012-04-30 18:59:53 -07001461 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1462 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001463 }
1464
Adam Cohen2f093b62012-04-30 18:59:53 -07001465 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1466 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001467 }
1468
Adam Cohen2f093b62012-04-30 18:59:53 -07001469 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1470 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001471 }
1472
Adam Cohen2f093b62012-04-30 18:59:53 -07001473 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1474 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001475 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001476 }
1477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001479 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001481 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001482 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001484 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001485 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1486 if (appWidgetInfo == null) {
1487 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1488 }
Romain Guycbb89e42009-06-08 15:52:54 -07001489
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001490 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001491 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001492
Adam Cohen2f093b62012-04-30 18:59:53 -07001493 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1494 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001497 // We have saved the position to which the widget was dragged-- this really only matters
1498 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 int[] cellXY = mTmpAddItemCellCoordinates;
1500 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001501 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001502 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001503 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1504 cellXY[0] = mPendingAddInfo.cellX;
1505 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001506 spanXY[0] = mPendingAddInfo.spanX;
1507 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508 foundCellSpan = true;
1509 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001510 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001511 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001512 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1513 spanXY[1], cellXY, finalSpan);
1514 spanXY[0] = finalSpan[0];
1515 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001516 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001517 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001518 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001519 }
1520
Michael Jurka0280c3b2010-09-17 15:00:07 -07001521 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001522 if (appWidgetId != -1) {
1523 // Deleting an app widget ID is a void call but writes to disk before returning
1524 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001525 new AsyncTask<Void, Void, Void>() {
1526 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001527 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001528 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001529 }
Michael Jurka43467462013-11-14 12:03:58 +01001530 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001531 }
Winson Chung93eef082012-03-23 15:59:27 -07001532 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001533 return;
1534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001536 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001537 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1538 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001539 launcherInfo.spanX = spanXY[0];
1540 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001541 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1542 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546
1547 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001548 if (hostView == null) {
1549 // Perform actual inflation because we're live
1550 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1551 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1552 } else {
1553 // The AppWidgetHostView has already been inflated and instantiated
1554 launcherInfo.hostView = hostView;
1555 }
Romain Guycbb89e42009-06-08 15:52:54 -07001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001558 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001559 launcherInfo.notifyWidgetSizeChanged(this);
1560
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001562 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001563
1564 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001566 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
Romain Guycbb89e42009-06-08 15:52:54 -07001568
Adam Cohended9f8d2010-11-03 13:25:16 -07001569 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1570 @Override
1571 public void onReceive(Context context, Intent intent) {
1572 final String action = intent.getAction();
1573 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1574 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001575 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001576 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001577
Winson Chungc100e8e2011-08-09 16:02:43 -07001578 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001579 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001580 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001581 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001582 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001583 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1584 mUserPresent = true;
1585 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001586 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1587 mModel.resetLoadedState(false, true);
1588 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1589 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1590 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1591 mModel.resetLoadedState(false, true);
1592 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1593 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1594 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001595 }
1596 }
1597 };
1598
1599 @Override
1600 public void onAttachedToWindow() {
1601 super.onAttachedToWindow();
1602
1603 // Listen for broadcasts related to user-presence
1604 final IntentFilter filter = new IntentFilter();
1605 filter.addAction(Intent.ACTION_SCREEN_OFF);
1606 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001607 if (ENABLE_DEBUG_INTENTS) {
1608 filter.addAction(DebugIntents.DELETE_DATABASE);
1609 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1610 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001611 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001612 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001613 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 mVisible = true;
1615 }
1616
1617 @Override
1618 public void onDetachedFromWindow() {
1619 super.onDetachedFromWindow();
1620 mVisible = false;
1621
Adam Cohend113e0c2010-11-11 10:48:05 -08001622 if (mAttached) {
1623 unregisterReceiver(mReceiver);
1624 mAttached = false;
1625 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 updateRunning();
1627 }
1628
1629 public void onWindowVisibilityChanged(int visibility) {
1630 mVisible = visibility == View.VISIBLE;
1631 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001632 // The following code used to be in onResume, but it turns out onResume is called when
1633 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1634 // is a more appropriate event to handle
1635 if (mVisible) {
1636 mAppsCustomizeTabHost.onWindowVisible();
1637 if (!mWorkspaceLoading) {
1638 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001639 // We want to let Launcher draw itself at least once before we force it to build
1640 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001641 // apps is nice and speedy.
1642 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001643 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001644 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001645 if (mStarted) return;
1646 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001647 // We delay the layer building a bit in order to give
1648 // other message processing a time to run. In particular
1649 // this avoids a delay in hiding the IME if it was
1650 // currently shown, because doing that may involve
1651 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001652 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001653 final ViewTreeObserver.OnDrawListener listener = this;
1654 mWorkspace.post(new Runnable() {
1655 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001656 if (mWorkspace != null &&
1657 mWorkspace.getViewTreeObserver() != null) {
1658 mWorkspace.getViewTreeObserver().
1659 removeOnDrawListener(listener);
1660 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001661 }
1662 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001663 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001664 }
1665 });
1666 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001667 // When Launcher comes back to foreground, a different Activity might be responsible for
1668 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001669 if (!DISABLE_MARKET_BUTTON) {
1670 updateAppMarketIcon();
1671 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001672 clearTypedText();
1673 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001674 }
1675
1676 private void sendAdvanceMessage(long delay) {
1677 mHandler.removeMessages(ADVANCE_MSG);
1678 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1679 mHandler.sendMessageDelayed(msg, delay);
1680 mAutoAdvanceSentTime = System.currentTimeMillis();
1681 }
1682
1683 private void updateRunning() {
1684 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1685 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1686 mAutoAdvanceRunning = autoAdvanceRunning;
1687 if (autoAdvanceRunning) {
1688 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1689 sendAdvanceMessage(delay);
1690 } else {
1691 if (!mWidgetsToAdvance.isEmpty()) {
1692 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1693 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1694 }
1695 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001696 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001697 }
1698 }
1699 }
1700
1701 private final Handler mHandler = new Handler() {
1702 @Override
1703 public void handleMessage(Message msg) {
1704 if (msg.what == ADVANCE_MSG) {
1705 int i = 0;
1706 for (View key: mWidgetsToAdvance.keySet()) {
1707 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1708 final int delay = mAdvanceStagger * i;
1709 if (v instanceof Advanceable) {
1710 postDelayed(new Runnable() {
1711 public void run() {
1712 ((Advanceable) v).advance();
1713 }
1714 }, delay);
1715 }
1716 i++;
1717 }
1718 sendAdvanceMessage(mAdvanceInterval);
1719 }
1720 }
1721 };
1722
1723 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001724 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1726 if (v instanceof Advanceable) {
1727 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001728 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 updateRunning();
1730 }
1731 }
1732
1733 void removeWidgetToAutoAdvance(View hostView) {
1734 if (mWidgetsToAdvance.containsKey(hostView)) {
1735 mWidgetsToAdvance.remove(hostView);
1736 updateRunning();
1737 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001738 }
1739
Joe Onorato9c1289c2009-08-17 11:03:03 -04001740 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742 launcherInfo.hostView = null;
1743 }
1744
Winson Chung93eef082012-03-23 15:59:27 -07001745 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1746 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1747 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 }
1749
Winson Chunga6945242014-01-08 14:04:34 -08001750 public DragLayer getDragLayer() {
1751 return mDragLayer;
1752 }
1753
1754 public Workspace getWorkspace() {
1755 return mWorkspace;
1756 }
1757
1758 public Hotseat getHotseat() {
1759 return mHotseat;
1760 }
1761
Jorim Jaggid017f882014-01-14 17:08:48 -08001762 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001763 return mOverviewPanel;
1764 }
1765
1766 public SearchDropTargetBar getSearchBar() {
1767 return mSearchDropTargetBar;
1768 }
1769
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001770 public LauncherAppWidgetHost getAppWidgetHost() {
1771 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 }
Romain Guycbb89e42009-06-08 15:52:54 -07001773
Winson Chunga9abd0e2010-10-27 17:18:37 -07001774 public LauncherModel getModel() {
1775 return mModel;
1776 }
1777
Winson Chunga6945242014-01-08 14:04:34 -08001778 public LauncherClings getLauncherClings() {
1779 return mLauncherClings;
1780 }
1781
1782 protected SharedPreferences getSharedPrefs() {
1783 return mSharedPrefs;
1784 }
1785
Bjorn Bringertc459e522013-06-07 19:36:01 +01001786 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001787 getWindow().closeAllPanels();
1788
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001789 // Whatever we were doing is hereby canceled.
1790 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001791 }
1792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 @Override
1794 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001795 long startTime = 0;
1796 if (DEBUG_RESUME_TIME) {
1797 startTime = System.currentTimeMillis();
1798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 super.onNewIntent(intent);
1800
1801 // Close the menu
1802 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001803 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001804 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001805
Adam Cohened307df2013-10-02 09:37:31 -07001806 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1807 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1808 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001809
Adam Cohen6fecd412013-10-02 17:41:50 -07001810 if (mWorkspace == null) {
1811 // Can be cases where mWorkspace is null, this prevents a NPE
1812 return;
1813 }
1814 Folder openFolder = mWorkspace.getOpenFolder();
1815 // In all these cases, only animate if we're already on home
1816 mWorkspace.exitWidgetResizeMode();
1817 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001818 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001819 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001820 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001821
Adam Cohen6fecd412013-10-02 17:41:50 -07001822 closeFolder();
1823 exitSpringLoadedDragMode();
1824
1825 // If we are already on home, then just animate back to the workspace,
1826 // otherwise, just wait until onResume to set the state back to Workspace
1827 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001828 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001829 } else {
1830 mOnResumeState = State.WORKSPACE;
1831 }
1832
1833 final View v = getWindow().peekDecorView();
1834 if (v != null && v.getWindowToken() != null) {
1835 InputMethodManager imm = (InputMethodManager)getSystemService(
1836 INPUT_METHOD_SERVICE);
1837 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1838 }
1839
1840 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001841 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001842 mAppsCustomizeTabHost.reset();
1843 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001844
1845 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 }
Adam Cohened307df2013-10-02 09:37:31 -07001847
Michael Jurka447bf842013-05-15 14:52:15 +02001848 if (DEBUG_RESUME_TIME) {
1849 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 }
1852
Adam Coppa120b8e2013-11-12 16:26:04 +00001853 /**
1854 * Override point for subclasses to prevent movement to the default screen when the home
1855 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1856 */
1857 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1858 return true;
1859 }
1860
1861 /**
1862 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1863 */
1864 protected void onHomeIntent() {
1865 // Do nothing
1866 }
1867
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001869 public void onRestoreInstanceState(Bundle state) {
1870 super.onRestoreInstanceState(state);
1871 for (int page: mSynchronouslyBoundPages) {
1872 mWorkspace.restoreInstanceStateForChild(page);
1873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
1875
1876 @Override
1877 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001878 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001879 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1880 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001881 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001882 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883
Michael Jurka883f55b2010-10-21 15:47:14 -07001884 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001885 // We close any open folder since it will not be re-opened, and we need to make sure
1886 // this state is reflected.
1887 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888
Adam Cohendcd297f2013-06-18 13:13:40 -07001889 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001890 mWaitingForResult) {
1891 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001892 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001893 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1894 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001895 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1896 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1897 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001898 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
1900
1901 if (mFolderInfo != null && mWaitingForResult) {
1902 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1903 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1904 }
Michael Jurka946ad472010-07-09 18:05:18 -07001905
Winson Chung785d2eb2011-04-14 16:08:02 -07001906 // Save the current AppsCustomize tab
1907 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001908 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1909 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001910 if (currentTabTag != null) {
1911 outState.putString("apps_customize_currentTab", currentTabTag);
1912 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001913 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1914 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001915 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001916 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 }
1918
1919 @Override
1920 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001922
Winson Chunge7a03942011-08-05 15:05:12 -07001923 // Remove all pending runnables
1924 mHandler.removeMessages(ADVANCE_MSG);
1925 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001926 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001927
Winson Chungcd2b0142011-06-08 16:02:26 -07001928 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001929 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001930 mModel.stopLoader();
1931 app.setLauncher(null);
1932
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001934 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001936 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001938 mAppWidgetHost = null;
1939
1940 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941
1942 TextKeyListener.getInstance().release();
1943
Winson Chung81b52252012-08-27 15:34:29 -07001944 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1945 // to prevent leaking Launcher activities on orientation change.
1946 if (mModel != null) {
1947 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1948 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001949
1950 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001951 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001952
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001953 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001954 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001955 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001956 mWorkspace = null;
1957 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001958
Michael Jurka2ecf9952012-06-18 12:52:28 -07001959 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961
Adam Cohena9cf38f2011-05-02 15:36:58 -07001962 public DragController getDragController() {
1963 return mDragController;
1964 }
1965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 @Override
1967 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001968 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 super.startActivityForResult(intent, requestCode);
1970 }
1971
Winson Chung70d72102011-08-12 11:24:35 -07001972 /**
1973 * Indicates that we want global search for this activity by setting the globalSearch
1974 * argument for {@link #startSearch} to true.
1975 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001977 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001979
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001980 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001981
Karl Rosaen138a0412009-04-23 19:00:21 -07001982 if (initialQuery == null) {
1983 // Use any text typed in the launcher as the initial query
1984 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001985 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 if (appSearchData == null) {
1987 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001988 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
Winson Chungadf0c182012-08-23 14:59:07 -07001990 Rect sourceBounds = new Rect();
1991 if (mSearchDropTargetBar != null) {
1992 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1993 }
Romain Guycbb89e42009-06-08 15:52:54 -07001994
Bjorn Bringertc459e522013-06-07 19:36:01 +01001995 startSearch(initialQuery, selectInitialQuery,
1996 appSearchData, sourceBounds);
1997 }
1998
1999 public void startSearch(String initialQuery,
2000 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002001 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002002 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002003 }
2004
2005 /**
2006 * Starts the global search activity. This code is a copied from SearchManager
2007 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002008 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002009 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002010 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002011 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2012 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2013 if (globalSearchActivity == null) {
2014 Log.w(TAG, "No global search activity found.");
2015 return;
2016 }
2017 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2018 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2019 intent.setComponent(globalSearchActivity);
2020 // Make sure that we have a Bundle to put source in
2021 if (appSearchData == null) {
2022 appSearchData = new Bundle();
2023 } else {
2024 appSearchData = new Bundle(appSearchData);
2025 }
2026 // Set source to package name of app that starts global search, if not set already.
2027 if (!appSearchData.containsKey("source")) {
2028 appSearchData.putString("source", getPackageName());
2029 }
2030 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2031 if (!TextUtils.isEmpty(initialQuery)) {
2032 intent.putExtra(SearchManager.QUERY, initialQuery);
2033 }
2034 if (selectInitialQuery) {
2035 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2036 }
2037 intent.setSourceBounds(sourceBounds);
2038 try {
2039 startActivity(intent);
2040 } catch (ActivityNotFoundException ex) {
2041 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2042 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 }
2044
Winson Chung70d72102011-08-12 11:24:35 -07002045 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002046 public boolean onPrepareOptionsMenu(Menu menu) {
2047 super.onPrepareOptionsMenu(menu);
Dan Sandler14c6a3f2014-01-07 14:12:27 -05002048 showOverviewMode(true);
Michael Jurkab94f3f82013-09-11 18:08:54 +02002049 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002052 @Override
2053 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002054 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002055 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002056 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002057 }
2058
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059 public boolean isWorkspaceLocked() {
2060 return mWorkspaceLoading || mWaitingForResult;
2061 }
2062
Michael Jurka0280c3b2010-09-17 15:00:07 -07002063 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002064 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002065 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002066 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2067 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002068 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002069 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002070 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002071
Adam Cohenad4e15c2013-10-17 16:21:35 -07002072 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2073 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2074 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2075 }
2076
2077 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2078 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2079 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002080 if (appWidgetInfo.configure != null) {
2081 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002082 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002083
Bjorn Bringert7984c942009-12-09 15:38:25 +00002084 // Launch over to configure widget, if needed
2085 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002086 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002087 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002088 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002090 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002091 Runnable onComplete = new Runnable() {
2092 @Override
2093 public void run() {
2094 // Exit spring loaded mode if necessary after adding the widget
2095 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2096 null);
2097 }
2098 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002099 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002100 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002101 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 }
2103 }
Romain Guycbb89e42009-06-08 15:52:54 -07002104
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002105 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002106 // Close any folders that may be open.
2107 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002108 mWorkspace.moveToCustomContentScreen(animate);
2109 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002110 /**
2111 * Process a shortcut drop.
2112 *
2113 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002114 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002115 * @param cell The cell it should be added to, optional
2116 * @param position The location on the screen where it was dropped, optional
2117 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002118 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002119 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002120 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002121 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002122 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002123 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002124
2125 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002126 mPendingAddInfo.cellX = cell[0];
2127 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002128 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002129
2130 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2131 createShortcutIntent.setComponent(componentName);
2132 processShortcut(createShortcutIntent);
2133 }
2134
Adam Cohenfbba09b2011-07-18 21:43:05 -07002135 /**
2136 * Process a widget drop.
2137 *
2138 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002139 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002140 * @param cell The cell it should be added to, optional
2141 * @param position The location on the screen where it was dropped, optional
2142 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002143 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002144 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002145 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002146 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002147 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002148 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002149 mPendingAddInfo.minSpanX = info.minSpanX;
2150 mPendingAddInfo.minSpanY = info.minSpanY;
2151
Adam Cohenfbba09b2011-07-18 21:43:05 -07002152 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002153 mPendingAddInfo.cellX = cell[0];
2154 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002155 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002156 if (span != null) {
2157 mPendingAddInfo.spanX = span[0];
2158 mPendingAddInfo.spanY = span[1];
2159 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002160
Adam Cohened66b2b2012-01-23 17:28:51 -08002161 AppWidgetHostView hostView = info.boundWidget;
2162 int appWidgetId;
2163 if (hostView != null) {
2164 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002165 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002166 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002167 // In this case, we either need to start an activity to get permission to bind
2168 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002169 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002170 Bundle options = info.bindOptions;
2171
2172 boolean success = false;
2173 if (options != null) {
2174 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2175 info.componentName, options);
2176 } else {
2177 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2178 info.componentName);
2179 }
2180 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002181 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002182 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002183 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002184 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2185 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2186 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002187 // TODO: we need to make sure that this accounts for the options bundle.
2188 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002189 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2190 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002191 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002192 }
2193
Joe Onoratodeb98af2010-02-19 14:59:39 -08002194 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002195 // Handle case where user selected "Applications"
2196 String applicationName = getResources().getString(R.string.group_applications);
2197 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002198
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002199 if (applicationName != null && applicationName.equals(shortcutName)) {
2200 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2201 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002202
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002203 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2204 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002205 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002206 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002207 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002208 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 }
2211
Winson Chung24ab2f12010-09-16 14:10:47 -07002212 void processWallpaper(Intent intent) {
2213 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2214 }
2215
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002217 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002218 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 folderInfo.title = getText(R.string.folder_name);
2220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002222 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002223 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002224 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225
2226 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002227 FolderIcon newFolder =
2228 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002229 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002230 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002231 // Force measure the new folder icon
2232 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2233 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002234 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 }
Romain Guycbb89e42009-06-08 15:52:54 -07002236
Joe Onorato9c1289c2009-08-17 11:03:03 -04002237 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002238 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002239 }
2240
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002241 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002243 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002244 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 }
2246
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002247 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002248 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002249 }
2250
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002251 /**
2252 * Registers various content observers. The current implementation registers
2253 * only a favorites observer to keep track of the favorites applications.
2254 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002255 private void registerContentObservers() {
2256 ContentResolver resolver = getContentResolver();
2257 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2258 true, mWidgetObserver);
2259 }
2260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 @Override
2262 public boolean dispatchKeyEvent(KeyEvent event) {
2263 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2264 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002266 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002267 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002268 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002269 dumpState();
2270 return true;
2271 }
2272 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002273 }
2274 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2275 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002276 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 return true;
2278 }
2279 }
2280
2281 return super.dispatchKeyEvent(event);
2282 }
2283
Joe Onorato88ec0992009-11-19 13:16:06 -08002284 @Override
2285 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002286 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002287 if (mAppsCustomizeContent.getContentType() ==
2288 AppsCustomizePagedView.ContentType.Applications) {
2289 showWorkspace(true);
2290 } else {
2291 showOverviewMode(true);
2292 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002293 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002294 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002295 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002296 Folder openFolder = mWorkspace.getOpenFolder();
2297 if (openFolder.isEditingName()) {
2298 openFolder.dismissEditingName();
2299 } else {
2300 closeFolder();
2301 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002302 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002303 mWorkspace.exitWidgetResizeMode();
2304
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002305 // Back button is a no-op here, but give at least some feedback for the button press
2306 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002307 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002308 }
2309
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002310 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002311 * Re-listen when widgets are reset.
2312 */
2313 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002314 if (mAppWidgetHost != null) {
2315 mAppWidgetHost.startListening();
2316 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002317 }
2318
2319 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 * Launches the intent referred by the clicked shortcut.
2321 *
2322 * @param v The view representing the clicked shortcut.
2323 */
2324 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002325 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2326 // view has detached (it's possible for this to happen if the view is removed mid touch).
2327 if (v.getWindowToken() == null) {
2328 return;
2329 }
2330
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002331 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002332 return;
2333 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002334
Adam Cohen1697b792013-09-17 19:08:21 -07002335 if (v instanceof Workspace) {
2336 if (mWorkspace.isInOverviewMode()) {
2337 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002338 }
2339 return;
2340 }
2341
2342 if (v instanceof CellLayout) {
2343 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002344 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002345 }
2346 }
2347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002349 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002351 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2352 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002353
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002354 // Check for special shortcuts
2355 if (intent.getComponent() != null) {
2356 final String shortcutClass = intent.getComponent().getClassName();
2357
2358 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002359 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002360 return;
2361 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2362 MemoryDumpActivity.startDump(this);
2363 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002364 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2365 toggleShowWeightWatcher();
2366 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002367 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002368 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002369
2370 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002371 int[] pos = new int[2];
2372 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002373 intent.setSourceBounds(new Rect(pos[0], pos[1],
2374 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002375
2376 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002377
Daniel Sandlerff02d492013-08-05 02:12:05 -04002378 mStats.recordLaunch(intent, shortcut);
2379
Michael Jurkaddd62e92011-02-16 17:49:14 -08002380 if (success && v instanceof BubbleTextView) {
2381 mWaitingForResume = (BubbleTextView) v;
2382 mWaitingForResume.setStayPressed(true);
2383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002385 if (v instanceof FolderIcon) {
2386 FolderIcon fi = (FolderIcon) v;
2387 handleFolderClick(fi);
2388 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002389 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002390 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002391 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002392 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002393 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 }
2396 }
2397
Michael Jurka0e260592010-06-30 17:07:39 -07002398 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002399 return false;
2400 }
2401
Michael Jurkaaf442092010-06-10 17:01:57 -07002402 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002403 * Event handler for the search button
2404 *
2405 * @param v The view that was clicked.
2406 */
2407 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002408 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2409
Amith Yamasania135ba82011-08-09 17:42:01 -07002410 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002411 }
2412
2413 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002414 * Event handler for the voice button
2415 *
2416 * @param v The view that was clicked.
2417 */
2418 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002419 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002420
Bjorn Bringertc459e522013-06-07 19:36:01 +01002421 startVoice();
2422 }
2423
2424 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002425 try {
2426 final SearchManager searchManager =
2427 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2428 ComponentName activityName = searchManager.getGlobalSearchActivity();
2429 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002430 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002431 if (activityName != null) {
2432 intent.setPackage(activityName.getPackageName());
2433 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002434 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002435 } catch (ActivityNotFoundException e) {
2436 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002437 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002438 startActivitySafely(null, intent, "onClickVoiceButton");
2439 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002440 }
2441
2442 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002443 * Event handler for the "grid" button that appears on the home screen, which
2444 * enters all apps mode.
2445 *
2446 * @param v The view that was clicked.
2447 */
2448 public void onClickAllAppsButton(View v) {
Winson Chunge7e97e62013-12-02 17:02:50 -08002449 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
Michael Jurka5130e402011-10-13 04:55:35 -07002450 }
2451
2452 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002453 // Provide the same haptic feedback that the system offers for virtual keys.
2454 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002455 }
2456
Adam Cohen61f560d2013-09-30 15:58:20 -07002457 public void performHapticFeedbackOnTouchDown(View v) {
2458 // Provide the same haptic feedback that the system offers for virtual keys.
2459 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2460 }
2461
2462 public View.OnTouchListener getHapticFeedbackTouchListener() {
2463 if (mHapticFeedbackTouchListener == null) {
2464 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2465 @Override
2466 public boolean onTouch(View v, MotionEvent event) {
2467 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2468 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2469 }
2470 return false;
2471 }
2472 };
2473 }
2474 return mHapticFeedbackTouchListener;
2475 }
2476
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002477 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002478 if (!DISABLE_MARKET_BUTTON) {
2479 if (mAppMarketIntent != null) {
2480 startActivitySafely(v, mAppMarketIntent, "app market");
2481 } else {
2482 Log.e(TAG, "Invalid app market intent.");
2483 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002484 }
2485 }
2486
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002487 /**
2488 * Called when the user stops interacting with the launcher.
2489 * This implies that the user is now on the homescreen and is not doing housekeeping.
2490 */
2491 protected void onInteractionEnd() {}
2492
2493 /**
2494 * Called when the user starts interacting with the launcher.
2495 * The possible interactions are:
2496 * - open all apps
2497 * - reorder an app shortcut, or a widget
2498 * - open the overview mode.
2499 * This is a good time to stop doing things that only make sense
2500 * when the user is on the homescreen and not doing housekeeping.
2501 */
2502 protected void onInteractionBegin() {}
2503
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002504 void startApplicationDetailsActivity(ComponentName componentName) {
2505 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002506 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2507 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002508 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002509 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002510 }
2511
Michael Jurka1e2f4652013-07-08 18:03:46 -07002512 // returns true if the activity was started
2513 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002514 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002515 // System applications cannot be installed. For now, show a toast explaining that.
2516 // We may give them the option of disabling apps this way.
2517 int messageId = R.string.uninstall_system_app_text;
2518 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002519 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002520 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002521 String packageName = componentName.getPackageName();
2522 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002523 Intent intent = new Intent(
2524 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002525 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2526 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002527 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002528 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002529 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002530 }
2531
Michael Jurka86a720e2012-05-09 11:23:23 -07002532 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002536 // Only launch using the new animation if the shortcut has not opted out (this is a
2537 // private contract between launcher and may be ignored in the future).
2538 boolean useLaunchAnimation = (v != null) &&
2539 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2540 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002541 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2542 v.getMeasuredWidth(), v.getMeasuredHeight());
2543
2544 startActivity(intent, opts.toBundle());
2545 } else {
2546 startActivity(intent);
2547 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002548 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 } catch (SecurityException e) {
2550 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002551 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002553 "or use the exported attribute for this activity. "
2554 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002556 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002558
Michael Jurka86a720e2012-05-09 11:23:23 -07002559 boolean startActivitySafely(View v, Intent intent, Object tag) {
2560 boolean success = false;
2561 try {
2562 success = startActivity(v, intent, tag);
2563 } catch (ActivityNotFoundException e) {
2564 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2565 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2566 }
2567 return success;
2568 }
2569
Adam Cohena9cf38f2011-05-02 15:36:58 -07002570 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002571 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002572 Folder openFolder = mWorkspace.getFolderForTag(info);
2573
2574 // If the folder info reports that the associated folder is open, then verify that
2575 // it is actually opened. There have been a few instances where this gets out of sync.
2576 if (info.opened && openFolder == null) {
2577 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002578 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002579 info.opened = false;
2580 }
2581
Adam Cohenfb91f302012-06-11 15:45:18 -07002582 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583 // Close any open folder
2584 closeFolder();
2585 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002586 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 } else {
2588 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 int folderScreen;
2590 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002591 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 // .. and close it
2593 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002594 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 // Close any folder open on the current screen
2596 closeFolder();
2597 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002598 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 }
2600 }
2601 }
2602 }
2603
Adam Cohen268c4752012-06-06 17:47:33 -07002604 /**
2605 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2606 * in the DragLayer in the exact absolute location of the original FolderIcon.
2607 */
2608 private void copyFolderIconToImage(FolderIcon fi) {
2609 final int width = fi.getMeasuredWidth();
2610 final int height = fi.getMeasuredHeight();
2611
2612 // Lazy load ImageView, Bitmap and Canvas
2613 if (mFolderIconImageView == null) {
2614 mFolderIconImageView = new ImageView(this);
2615 }
2616 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2617 mFolderIconBitmap.getHeight() != height) {
2618 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2619 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2620 }
2621
2622 DragLayer.LayoutParams lp;
2623 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2624 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2625 } else {
2626 lp = new DragLayer.LayoutParams(width, height);
2627 }
2628
Adam Cohen307fe232012-08-16 17:55:58 -07002629 // The layout from which the folder is being opened may be scaled, adjust the starting
2630 // view size by this scale factor.
2631 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002632 lp.customPosition = true;
2633 lp.x = mRectForFolderAnimation.left;
2634 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002635 lp.width = (int) (scale * width);
2636 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002637
2638 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2639 fi.draw(mFolderIconCanvas);
2640 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002641 if (fi.getFolder() != null) {
2642 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2643 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002644 }
Adam Cohen268c4752012-06-06 17:47:33 -07002645 // Just in case this image view is still in the drag layer from a previous animation,
2646 // we remove it and re-add it.
2647 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2648 mDragLayer.removeView(mFolderIconImageView);
2649 }
2650 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002651 if (fi.getFolder() != null) {
2652 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002653 }
Adam Cohen268c4752012-06-06 17:47:33 -07002654 }
2655
Adam Cohen2801caf2011-05-13 20:57:39 -07002656 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002657 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002658 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2659 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2660 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2661
Adam Cohenc51934b2011-07-26 21:07:43 -07002662 FolderInfo info = (FolderInfo) fi.getTag();
2663 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2664 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002665 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2666 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002667 }
2668
Adam Cohen268c4752012-06-06 17:47:33 -07002669 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2670 copyFolderIconToImage(fi);
2671 fi.setVisibility(View.INVISIBLE);
2672
Michael Jurka2ecf9952012-06-18 12:52:28 -07002673 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002674 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002675 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2676 oa.start();
2677 }
2678
Adam Cohen268c4752012-06-06 17:47:33 -07002679 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002680 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002681 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2682 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2683 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2684
Adam Cohen268c4752012-06-06 17:47:33 -07002685 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002686
Adam Cohen268c4752012-06-06 17:47:33 -07002687 // We remove and re-draw the FolderIcon in-case it has changed
2688 mDragLayer.removeView(mFolderIconImageView);
2689 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002690 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002691 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002692 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002693 oa.addListener(new AnimatorListenerAdapter() {
2694 @Override
2695 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002696 if (cl != null) {
2697 cl.clearFolderLeaveBehind();
2698 // Remove the ImageView copy of the FolderIcon and make the original visible.
2699 mDragLayer.removeView(mFolderIconImageView);
2700 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002701 }
2702 }
2703 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002704 oa.start();
2705 }
2706
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002708 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 * is animated relative to the specified View. If the View is null, no animation
2710 * is played.
2711 *
2712 * @param folderInfo The FolderInfo describing the folder to open.
2713 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002714 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002715 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002716 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717
Adam Cohena9cf38f2011-05-02 15:36:58 -07002718 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002719
Adam Cohen4554ee12011-08-03 16:13:21 -07002720 // Just verify that the folder hasn't already been added to the DragLayer.
2721 // There was a one-off crash where the folder had a parent already.
2722 if (folder.getParent() == null) {
2723 mDragLayer.addView(folder);
2724 mDragController.addDropTarget((DropTarget) folder);
2725 } else {
2726 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2727 folder.getParent() + ").");
2728 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002729 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002730 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002731
2732 // Notify the accessibility manager that this folder "window" has appeared and occluded
2733 // the workspace items
2734 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2735 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002736 }
2737
2738 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002739 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002740 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002741 if (folder.isEditingName()) {
2742 folder.dismissEditingName();
2743 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002744 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002745
2746 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002747 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002748 }
2749 }
2750
2751 void closeFolder(Folder folder) {
2752 folder.getInfo().opened = false;
2753
2754 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2755 if (parent != null) {
2756 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2757 shrinkAndFadeInFolderIcon(fi);
2758 }
2759 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002760
2761 // Notify the accessibility manager that this folder "window" has disappeard and no
2762 // longer occludeds the workspace items
2763 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002764 }
2765
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002767 if (!isDraggingEnabled()) return false;
2768 if (isWorkspaceLocked()) return false;
2769 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002770
Adam Cohen1697b792013-09-17 19:08:21 -07002771 if (v instanceof Workspace) {
2772 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002773 if (mWorkspace.enterOverviewMode()) {
2774 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2775 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2776 return true;
2777 } else {
2778 return false;
2779 }
Adam Cohen1697b792013-09-17 19:08:21 -07002780 }
Adam Cohen1697b792013-09-17 19:08:21 -07002781 }
2782
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002783 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002784 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785 }
2786
Michael Jurka0280c3b2010-09-17 15:00:07 -07002787 resetAddInfo();
2788 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002790 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002791 return true;
2792 }
2793
Winson Chung3d503fb2011-07-13 17:25:49 -07002794 // The hotseat touch handling does not go through Workspace, and we always allow long press
2795 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002796 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002797 final boolean inHotseat = isHotseatLayout(v);
2798 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002799 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002800 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002801 // User long pressed on empty space
2802 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2803 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2804 // Disabling reordering until we sort out some issues.
2805 if (mWorkspace.isInOverviewMode()) {
2806 mWorkspace.startReordering(v);
2807 } else {
2808 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002811 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2812 mHotseat.getOrderInHotseat(
2813 longClickCellInfo.cellX,
2814 longClickCellInfo.cellY));
2815 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002816 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002817 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002818 }
2819 }
2820 }
2821 return true;
2822 }
2823
Winson Chung3d503fb2011-07-13 17:25:49 -07002824 boolean isHotseatLayout(View layout) {
2825 return mHotseat != null && layout != null &&
2826 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2827 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002828
Winson Chung3d503fb2011-07-13 17:25:49 -07002829 /**
2830 * Returns the CellLayout of the specified container at the specified screen.
2831 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002832 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002833 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2834 if (mHotseat != null) {
2835 return mHotseat.getLayout();
2836 } else {
2837 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002838 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002839 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002840 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002841 }
2842 }
2843
Daniel Sandler843e8602010-06-07 14:59:01 -04002844 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002845 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002846 }
2847
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002848 /**
2849 * Helper method for the cameraZoomIn/cameraZoomOut animations
2850 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002851 * @param scaleFactor The scale factor used for the zoom
2852 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002853 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002854 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002855 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002856 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002857
Craig Mautner360310b2012-10-26 15:13:08 -07002858 private void setWorkspaceBackground(boolean workspace) {
2859 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002860 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002861 }
2862
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002863 void updateWallpaperVisibility(boolean visible) {
2864 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2865 int curflags = getWindow().getAttributes().flags
2866 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2867 if (wpflags != curflags) {
2868 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2869 }
Craig Mautner360310b2012-10-26 15:13:08 -07002870 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002871 }
2872
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002873 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2874 if (v instanceof LauncherTransitionable) {
2875 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2876 }
2877 }
2878
Michael Jurkabed61d22012-02-14 22:51:29 -08002879 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2880 if (v instanceof LauncherTransitionable) {
2881 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2882 }
Winson Chung70442722012-02-10 15:43:22 -08002883
2884 // Update the workspace transition step as well
2885 dispatchOnLauncherTransitionStep(v, 0f);
2886 }
2887
2888 private void dispatchOnLauncherTransitionStep(View v, float t) {
2889 if (v instanceof LauncherTransitionable) {
2890 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2891 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002892 }
2893
2894 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2895 if (v instanceof LauncherTransitionable) {
2896 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2897 }
Winson Chung70442722012-02-10 15:43:22 -08002898
2899 // Update the workspace transition step as well
2900 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002901 }
2902
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002903 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002904 * Things to test when changing the following seven functions.
2905 * - Home from workspace
2906 * - from center screen
2907 * - from other screens
2908 * - Home from all apps
2909 * - from center screen
2910 * - from other screens
2911 * - Back from all apps
2912 * - from center screen
2913 * - from other screens
2914 * - Launch app from workspace and quit
2915 * - with back
2916 * - with home
2917 * - Launch app from all apps and quit
2918 * - with back
2919 * - with home
2920 * - Go to a screen that's not the default, then all
2921 * apps, and launch and app, and go back
2922 * - with back
2923 * -with home
2924 * - On workspace, long press power and go back
2925 * - with back
2926 * - with home
2927 * - On all apps, long press power and go back
2928 * - with back
2929 * - with home
2930 * - On workspace, power off
2931 * - On all apps, power off
2932 * - Launch an app and turn off the screen while in that app
2933 * - Go back with home key
2934 * - Go back with back key TODO: make this not go to workspace
2935 * - From all apps
2936 * - From workspace
2937 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2938 * - From all apps
2939 * - From the center workspace
2940 * - From another workspace
2941 */
2942
2943 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002944 * Zoom the camera out from the workspace to reveal 'toView'.
2945 * Assumes that the view to show is anchored at either the very top or very bottom
2946 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002947 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002948 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002949 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2950 showAppsCustomizeHelper(animated, springLoaded, contentType);
2951 }
2952 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2953 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002954 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002955 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002956 mStateAnimation.cancel();
2957 mStateAnimation = null;
2958 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002959 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002960
Winson Chungf0ea4d32011-06-06 14:27:16 -07002961 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2962 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2963 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002964 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002965 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002966 final int startDelay =
2967 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002968
Michael Jurkab3e22d92011-10-31 15:58:33 -07002969 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002970
Michael Jurkad74c9842011-07-10 12:44:21 -07002971 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002972 Animator workspaceAnim =
2973 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08002974 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08002975 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
2976 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07002977 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2978 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002979
2980 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002981 toView.setScaleX(scale);
2982 toView.setScaleY(scale);
2983 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2984 scaleAnim.
2985 scaleX(1f).scaleY(1f).
2986 setDuration(duration).
2987 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002988
Winson Chungf0ea4d32011-06-06 14:27:16 -07002989 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002990 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002991 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002992 .ofFloat(toView, "alpha", 0f, 1f)
2993 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002994 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002995 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2996 @Override
2997 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002998 if (animation == null) {
2999 throw new RuntimeException("animation is null");
3000 }
Winson Chung70442722012-02-10 15:43:22 -08003001 float t = (Float) animation.getAnimatedValue();
3002 dispatchOnLauncherTransitionStep(fromView, t);
3003 dispatchOnLauncherTransitionStep(toView, t);
3004 }
3005 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003006
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003007 // toView should appear right at the end of the workspace shrink
3008 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003009 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003010 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003011 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003012
3013 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003014 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003015 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003016 // Prepare the position
3017 toView.setTranslationX(0.0f);
3018 toView.setTranslationY(0.0f);
3019 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003020 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003021 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003022 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003023 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003024 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3025 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003026
Winson Chungc7d2b602012-05-16 17:02:20 -07003027 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003028 if (mSearchDropTargetBar != null) {
3029 mSearchDropTargetBar.hideSearchBar(false);
3030 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003031 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003032 });
3033
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003034 if (workspaceAnim != null) {
3035 mStateAnimation.play(workspaceAnim);
3036 }
Michael Jurka1899a362011-11-03 13:50:45 -07003037
3038 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003039
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003040 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3041 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003042
3043 // If any of the objects being animated haven't been measured/laid out
3044 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003045 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003046 (mWorkspace.getMeasuredWidth() == 0) ||
3047 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003048 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003049 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003050
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003051 final AnimatorSet stateAnimation = mStateAnimation;
3052 final Runnable startAnimRunnable = new Runnable() {
3053 public void run() {
3054 // Check that mStateAnimation hasn't changed while
3055 // we waited for a layout/draw pass
3056 if (mStateAnimation != stateAnimation)
3057 return;
3058 setPivotsForZoom(toView, scale);
3059 dispatchOnLauncherTransitionStart(fromView, animated, false);
3060 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003061 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003062 }
3063 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003064 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003065 final ViewTreeObserver observer = toView.getViewTreeObserver();
3066 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3067 public void onGlobalLayout() {
3068 startAnimRunnable.run();
3069 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3070 }
3071 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003072 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003073 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003074 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003075 } else {
3076 toView.setTranslationX(0.0f);
3077 toView.setTranslationY(0.0f);
3078 toView.setScaleX(1.0f);
3079 toView.setScaleY(1.0f);
3080 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003081 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003082
Daniel Sandlere4f98912013-06-25 15:13:26 -04003083 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003084 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003085 if (mSearchDropTargetBar != null) {
3086 mSearchDropTargetBar.hideSearchBar(false);
3087 }
Michael Jurkaabded662011-03-04 12:06:57 -08003088 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003089 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003090 dispatchOnLauncherTransitionStart(fromView, animated, false);
3091 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003092 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003093 dispatchOnLauncherTransitionStart(toView, animated, false);
3094 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003095 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003096 }
3097
3098 /**
3099 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003100 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003101 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003102 */
Adam Cohened307df2013-10-02 09:37:31 -07003103 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003104 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003105
Michael Jurkab3e22d92011-10-31 15:58:33 -07003106 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003107 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003108 mStateAnimation.cancel();
3109 mStateAnimation = null;
3110 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003111 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003112
Winson Chungf0ea4d32011-06-06 14:27:16 -07003113 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003114 final int fadeOutDuration =
3115 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003116 final float scaleFactor = (float)
3117 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3118 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003119 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003120 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003121 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003122 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3123 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003124 toState, animated, stagger, -1);
3125 } else if (toState == Workspace.State.SPRING_LOADED ||
3126 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003127 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003128 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003129 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003130
Michael Jurkab3e22d92011-10-31 15:58:33 -07003131 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003132 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003133 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003134 final LauncherViewPropertyAnimator scaleAnim =
3135 new LauncherViewPropertyAnimator(fromView);
3136 scaleAnim.
3137 scaleX(scaleFactor).scaleY(scaleFactor).
3138 setDuration(duration).
3139 setInterpolator(new Workspace.ZoomInInterpolator());
3140
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003141 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003142 .ofFloat(fromView, "alpha", 1f, 0f)
3143 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003144 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003145 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3146 @Override
3147 public void onAnimationUpdate(ValueAnimator animation) {
3148 float t = 1f - (Float) animation.getAnimatedValue();
3149 dispatchOnLauncherTransitionStep(fromView, t);
3150 dispatchOnLauncherTransitionStep(toView, t);
3151 }
3152 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003153
Michael Jurka2ecf9952012-06-18 12:52:28 -07003154 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003155
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003156 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3157 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003158 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003159
3160 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003161 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003162 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003163 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003164 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3165 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003166 if (onCompleteRunnable != null) {
3167 onCompleteRunnable.run();
3168 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003169 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003170 }
3171 });
3172
Winson Chungf0ea4d32011-06-06 14:27:16 -07003173 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003174 if (workspaceAnim != null) {
3175 mStateAnimation.play(workspaceAnim);
3176 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003177 dispatchOnLauncherTransitionStart(fromView, animated, true);
3178 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003179 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003180 } else {
3181 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003182 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003183 dispatchOnLauncherTransitionStart(fromView, animated, true);
3184 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003185 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003186 dispatchOnLauncherTransitionStart(toView, animated, true);
3187 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003188 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003189 }
3190
Michael Jurkae326f182011-11-21 14:05:46 -08003191 @Override
3192 public void onTrimMemory(int level) {
3193 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003194 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003195 mAppsCustomizeTabHost.onTrimMemory();
3196 }
3197 }
3198
Adam Cohened307df2013-10-02 09:37:31 -07003199 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003200 showWorkspace(animated, null);
3201 }
3202
Adam Cohened307df2013-10-02 09:37:31 -07003203 protected void showWorkspace() {
3204 showWorkspace(true);
3205 }
3206
Adam Cohened66b2b2012-01-23 17:28:51 -08003207 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003208 if (mWorkspace.isInOverviewMode()) {
3209 mWorkspace.exitOverviewMode(animated);
3210 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003211 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003212 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003213 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003214 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003215
Winson Chungc7d2b602012-05-16 17:02:20 -07003216 // Show the search bar (only animate if we were showing the drop target bar in spring
3217 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003218 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003219 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003220 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003221
Michael Jurkab3e22d92011-10-31 15:58:33 -07003222 // Set focus to the AppsCustomize button
3223 if (mAllAppsButton != null) {
3224 mAllAppsButton.requestFocus();
3225 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003226 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003227
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003228 // Change the state *after* we've called all the transition code
3229 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003230
Winson Chung5fb63472011-02-02 17:03:37 -08003231 // Resume the auto-advance of widgets
3232 mUserPresent = true;
3233 updateRunning();
3234
alanv1d4fde62012-10-17 13:15:47 -07003235 // Send an accessibility event to announce the context change
3236 getWindow().getDecorView()
3237 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003238
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003239 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003240 }
3241
Adam Cohened307df2013-10-02 09:37:31 -07003242 void showOverviewMode(boolean animated) {
3243 mWorkspace.setVisibility(View.VISIBLE);
3244 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3245 mState = State.WORKSPACE;
3246 onWorkspaceShown(animated);
3247 }
3248
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003249 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003250 }
3251
Winson Chung82963d52013-10-09 11:20:57 -07003252 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3253 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003254 if (mState != State.WORKSPACE) return;
3255
Winson Chung82963d52013-10-09 11:20:57 -07003256 if (resetPageToZero) {
3257 mAppsCustomizeTabHost.reset();
3258 }
Winson Chungc58497e2013-09-03 17:48:37 -07003259 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003260 mAppsCustomizeTabHost.requestFocus();
3261
Michael Jurkab3e22d92011-10-31 15:58:33 -07003262 // Change the state *after* we've called all the transition code
3263 mState = State.APPS_CUSTOMIZE;
3264
3265 // Pause the auto-advance of widgets until we are out of AllApps
3266 mUserPresent = false;
3267 updateRunning();
3268 closeFolder();
3269
3270 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003271 getWindow().getDecorView()
3272 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003273 }
3274
Adam Cohen7777d962011-08-18 18:58:38 -07003275 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003276 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003277 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003278 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003279 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003280 }
Adam Cohen7777d962011-08-18 18:58:38 -07003281
Adam Cohenad4e15c2013-10-17 16:21:35 -07003282 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003283 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003284 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3285
Winson Chunge7a03942011-08-05 15:05:12 -07003286 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003287 @Override
3288 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003289 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003290 // Before we show workspace, hide all apps again because
3291 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3292 // clean up our state transition functions
3293 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003294 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003295 } else {
3296 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003297 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003298 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003299 }, delay);
3300
Winson Chung557d6ed2011-07-08 15:34:52 -07003301 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003302
Michael Jurkad3ef3062010-11-23 16:23:58 -08003303 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003304 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003305 final boolean animated = true;
3306 final boolean springLoaded = true;
3307 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003308 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003309 }
3310 // Otherwise, we are not in spring loaded mode, so don't do anything.
3311 }
3312
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313 void lockAllApps() {
3314 // TODO
3315 }
3316
3317 void unlockAllApps() {
3318 // TODO
3319 }
3320
Winson Chungf0ea4d32011-06-06 14:27:16 -07003321 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003322 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003323 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003324 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003325 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003326 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003327 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003328 int duration = 0;
3329 if (mSearchDropTargetBar != null) {
3330 duration = mSearchDropTargetBar.getTransitionInDuration();
3331 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003332 mHotseat.animate().alpha(1f).setDuration(duration);
3333 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003334 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003335 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003336 }
3337 }
3338 }
3339
3340 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003341 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003342 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003343 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003344 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003345 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003346 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003347 int duration = 0;
3348 if (mSearchDropTargetBar != null) {
3349 duration = mSearchDropTargetBar.getTransitionOutDuration();
3350 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003351 mHotseat.animate().alpha(0f).setDuration(duration);
3352 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003353 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003354 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003355 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003356 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003357 }
3358
Patrick Dubroy5f445422011-02-18 14:35:21 -08003359 /**
3360 * Add an item from all apps or customize onto the given workspace screen.
3361 * If layout is null, add to the current screen.
3362 */
3363 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003364 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003365 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003366 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003367 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003368
Winson Chungdff8ebb2011-09-08 17:25:31 -07003369 /** Maps the current orientation to an index for referencing orientation correct global icons */
3370 private int getCurrentOrientationIndexForGlobalIcons() {
3371 // default - 0, landscape - 1
3372 switch (getResources().getConfiguration().orientation) {
3373 case Configuration.ORIENTATION_LANDSCAPE:
3374 return 1;
3375 default:
3376 return 0;
3377 }
3378 }
3379
Michael Jurkaae65ee32012-04-30 11:45:54 -07003380 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003381 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003382 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003383 // Look for the toolbar icon specified in the activity meta-data
3384 Bundle metaData = packageManager.getActivityInfo(
3385 activityName, PackageManager.GET_META_DATA).metaData;
3386 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003387 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003388 if (iconResId != 0) {
3389 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003390 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003391 }
3392 }
3393 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003394 // This can happen if the activity defines an invalid drawable
3395 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3396 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003397 } catch (Resources.NotFoundException nfe) {
3398 // This can happen if the activity defines an invalid drawable
3399 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3400 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003401 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003402 return null;
3403 }
3404
3405 // if successful in getting icon, return it; otherwise, set button to use default drawable
3406 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003407 int buttonId, ComponentName activityName, int fallbackDrawableId,
3408 String toolbarResourceName) {
3409 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003410 Resources r = getResources();
3411 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3412 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003413
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003414 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003415 // If we were unable to find the icon via the meta-data, use a generic one
3416 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003417 toolbarIcon = r.getDrawable(fallbackDrawableId);
3418 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003419 if (button != null) {
3420 button.setCompoundDrawables(toolbarIcon, null, null, null);
3421 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003422 return null;
3423 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003424 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003425 if (button != null) {
3426 button.setCompoundDrawables(toolbarIcon, null, null, null);
3427 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003428 return toolbarIcon.getConstantState();
3429 }
3430 }
3431
3432 // if successful in getting icon, return it; otherwise, set button to use default drawable
3433 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003434 int buttonId, ComponentName activityName, int fallbackDrawableId,
3435 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003436 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003437 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003438
Michael Jurka19e0fc52011-07-22 18:00:21 -07003439 if (button != null) {
3440 // If we were unable to find the icon via the meta-data, use a
3441 // generic one
3442 if (toolbarIcon == null) {
3443 button.setImageResource(fallbackDrawableId);
3444 } else {
3445 button.setImageDrawable(toolbarIcon);
3446 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003447 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003448
3449 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3450
Michael Jurka0423dcf2010-10-05 14:56:18 -07003451 }
3452
Winson Chung1b884092012-06-08 17:13:02 -07003453 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003454 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003455 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003456 }
3457
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003458 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003459 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003460 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003461 }
3462
Winson Chungbb185bd2011-11-21 12:31:42 -08003463 private void invalidatePressedFocusedStates(View container, View button) {
3464 if (container instanceof HolographicLinearLayout) {
3465 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3466 layout.invalidatePressedFocusedStates();
3467 } else if (button instanceof HolographicImageView) {
3468 HolographicImageView view = (HolographicImageView) button;
3469 view.invalidatePressedFocusedStates();
3470 }
3471 }
3472
Cristina Stancu476493b2013-08-07 17:20:14 +01003473 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003474 if (mQsb == null) {
3475 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3476 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003477 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003478 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003479 }
3480
3481 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003482 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003483 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003484 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003485 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003486
Winson Chung1cad91e2011-05-25 17:41:01 -07003487 final SearchManager searchManager =
3488 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3489 ComponentName activityName = searchManager.getGlobalSearchActivity();
3490 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003491 int coi = getCurrentOrientationIndexForGlobalIcons();
3492 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003493 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3494 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3495 if (sGlobalSearchIcon[coi] == null) {
3496 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3497 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3498 TOOLBAR_ICON_METADATA_NAME);
3499 }
3500
Winson Chungc51db6a2011-10-05 11:44:49 -07003501 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3502 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003503 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003504 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003505 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003506 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003507 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3508 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003509 if (searchButton != null) searchButton.setVisibility(View.GONE);
3510 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003511 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003512 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003513 }
3514 }
3515
Cristina Stancu476493b2013-08-07 17:20:14 +01003516 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003517 final View searchButtonContainer = findViewById(R.id.search_button_container);
3518 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003519 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003520 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003521 }
3522
Cristina Stancu476493b2013-08-07 17:20:14 +01003523 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003524 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003525 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003526
Winson Chungc51db6a2011-10-05 11:44:49 -07003527 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003528 final SearchManager searchManager =
3529 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3530 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3531
3532 ComponentName activityName = null;
3533 if (globalSearchActivity != null) {
3534 // Check if the global search activity handles voice search
3535 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3536 intent.setPackage(globalSearchActivity.getPackageName());
3537 activityName = intent.resolveActivity(getPackageManager());
3538 }
3539
3540 if (activityName == null) {
3541 // Fallback: check if an activity other than the global search activity
3542 // resolves this
3543 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3544 activityName = intent.resolveActivity(getPackageManager());
3545 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003546 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003547 int coi = getCurrentOrientationIndexForGlobalIcons();
3548 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003549 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3550 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3551 if (sVoiceSearchIcon[coi] == null) {
3552 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3553 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3554 TOOLBAR_ICON_METADATA_NAME);
3555 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003556 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003557 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003558 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003559 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003560 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003561 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003562 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003563 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003564 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003565 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003566 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003567 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003568
Cristina Stancu476493b2013-08-07 17:20:14 +01003569 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003570 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3571 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003572 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003573 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003574 }
3575
Winson Chung5841efa2013-09-30 18:06:44 -07003576 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003577 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3578 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003579 boolean visible = !forceDisableVoiceButtonProxy &&
3580 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003581 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003582 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003583 }
3584 }
Winson Chung5841efa2013-09-30 18:06:44 -07003585
3586 /**
3587 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3588 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3589 */
3590 public void disableVoiceButtonProxy(boolean disabled) {
3591 updateVoiceButtonProxyVisible(disabled);
3592 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003593 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003594 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003595 */
3596 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003597 if (!DISABLE_MARKET_BUTTON) {
3598 final View marketButton = findViewById(R.id.market_button);
3599 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3600 // Find the app market activity by resolving an intent.
3601 // (If multiple app markets are installed, it will return the ResolverActivity.)
3602 ComponentName activityName = intent.resolveActivity(getPackageManager());
3603 if (activityName != null) {
3604 int coi = getCurrentOrientationIndexForGlobalIcons();
3605 mAppMarketIntent = intent;
3606 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3607 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3608 TOOLBAR_ICON_METADATA_NAME);
3609 marketButton.setVisibility(View.VISIBLE);
3610 } else {
3611 // We should hide and disable the view so that we don't try and restore the visibility
3612 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3613 marketButton.setVisibility(View.GONE);
3614 marketButton.setEnabled(false);
3615 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003616 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003617 }
3618
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003619 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003620 if (!DISABLE_MARKET_BUTTON) {
3621 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3622 Resources r = getResources();
3623 Drawable marketIconDrawable = d.newDrawable(r);
3624 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3625 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3626 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003627
Winson Chungd64a6662013-09-30 11:06:59 -07003628 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3629 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003630 }
3631
Michael Jurkad7c28052012-04-27 15:43:36 -07003632 @Override
3633 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003634 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003635 final List<CharSequence> text = event.getText();
3636 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003637 // Populate event with a fake title based on the current state.
3638 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003639 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003640 } else {
3641 text.add(getString(R.string.all_apps_home_button_label));
3642 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003643 return result;
3644 }
3645
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003646 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003647 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003648 */
3649 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3650 @Override
3651 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003652 closeSystemDialogs();
3653 }
3654 }
3655
3656 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003657 * Receives notifications whenever the appwidgets are reset.
3658 */
3659 private class AppWidgetResetObserver extends ContentObserver {
3660 public AppWidgetResetObserver() {
3661 super(new Handler());
3662 }
3663
3664 @Override
3665 public void onChange(boolean selfChange) {
3666 onAppWidgetReset();
3667 }
3668 }
3669
3670 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003671 * If the activity is currently paused, signal that we need to run the passed Runnable
3672 * in onResume.
3673 *
3674 * This needs to be called from incoming places where resources might have been loaded
3675 * while we are paused. That is becaues the Configuration might be wrong
3676 * when we're not running, and if it comes back to what it was when we
3677 * were paused, we are not restarted.
3678 *
3679 * Implementation of the method from LauncherModel.Callbacks.
3680 *
3681 * @return true if we are currently paused. The caller might be able to
3682 * skip some work in that case since we will come back again.
3683 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003684 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003685 if (mPaused) {
3686 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003687 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003688 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003689 }
3690 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003691 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003692 return true;
3693 } else {
3694 return false;
3695 }
3696 }
3697
Michael Jurkac402cd92013-05-20 15:49:32 +02003698 private boolean waitUntilResume(Runnable run) {
3699 return waitUntilResume(run, false);
3700 }
3701
Michael Jurka1e2f4652013-07-08 18:03:46 -07003702 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003703 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003704 }
3705
Michael Jurka7607c2f2013-04-03 14:33:19 -07003706 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003707 * If the activity is currently paused, signal that we need to re-run the loader
3708 * in onResume.
3709 *
3710 * This needs to be called from incoming places where resources might have been loaded
3711 * while we are paused. That is becaues the Configuration might be wrong
3712 * when we're not running, and if it comes back to what it was when we
3713 * were paused, we are not restarted.
3714 *
3715 * Implementation of the method from LauncherModel.Callbacks.
3716 *
3717 * @return true if we are currently paused. The caller might be able to
3718 * skip some work in that case since we will come back again.
3719 */
3720 public boolean setLoadOnResume() {
3721 if (mPaused) {
3722 Log.i(TAG, "setLoadOnResume");
3723 mOnResumeNeedsLoad = true;
3724 return true;
3725 } else {
3726 return false;
3727 }
3728 }
3729
3730 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003732 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003734 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003735 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003736 } else {
3737 return SCREEN_COUNT / 2;
3738 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003740
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741 /**
3742 * Refreshes the shortcuts shown on the workspace.
3743 *
3744 * Implementation of the method from LauncherModel.Callbacks.
3745 */
3746 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003747 // If we're starting binding all over again, clear any bind calls we'd postponed in
3748 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3749 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003750 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003751
Winson Chungd64d1762013-08-20 14:37:16 -07003752 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003753 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003754 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003755
Michael Jurka05bf6442011-11-30 20:28:53 -08003756 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003757 if (mHotseat != null) {
3758 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003759 }
3760 }
3761
Adam Cohendcd297f2013-06-18 13:13:40 -07003762 @Override
3763 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003764 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003765
Adam Cohen5084cba2013-09-03 12:01:16 -07003766 // If there are no screens, we need to have an empty screen
3767 if (orderedScreenIds.size() == 0) {
3768 mWorkspace.addExtraEmptyScreen();
3769 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003770
3771 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003772 // setCurrentPage() so ensure that all pages are added before calling this).
3773 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003774 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3775 mWorkspace.createCustomContentPage();
3776 }
Winson Chung64359a52013-07-08 17:17:08 -07003777 }
3778
3779 @Override
3780 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003781 // Log to disk
3782 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3783 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3784 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003785 int count = orderedScreenIds.size();
3786 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003787 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003788 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003789 }
3790
Adam Cohen39a06042013-07-19 14:30:12 -07003791 private boolean shouldShowWeightWatcher() {
3792 String spKey = LauncherAppState.getSharedPreferencesKey();
3793 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003794 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003795
3796 return show;
3797 }
3798
3799 private void toggleShowWeightWatcher() {
3800 String spKey = LauncherAppState.getSharedPreferencesKey();
3801 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3802 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3803
3804 show = !show;
3805
3806 SharedPreferences.Editor editor = sp.edit();
3807 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3808 editor.commit();
3809
3810 if (mWeightWatcher != null) {
3811 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3812 }
3813 }
3814
Winson Chungd64d1762013-08-20 14:37:16 -07003815 public void bindAppsAdded(final ArrayList<Long> newScreens,
3816 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003817 final ArrayList<ItemInfo> addAnimated,
3818 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003819 Runnable r = new Runnable() {
3820 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003821 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003822 }
3823 };
3824 if (waitUntilResume(r)) {
3825 return;
3826 }
3827
Winson Chungd64d1762013-08-20 14:37:16 -07003828 // Add the new screens
3829 bindAddScreens(newScreens);
3830
3831 // We add the items without animation on non-visible pages, and with
3832 // animations on the new page (which we will try and snap to).
3833 if (!addNotAnimated.isEmpty()) {
3834 bindItems(addNotAnimated, 0,
3835 addNotAnimated.size(), false);
3836 }
3837 if (!addAnimated.isEmpty()) {
3838 bindItems(addAnimated, 0,
3839 addAnimated.size(), true);
3840 }
Winson Chungc58497e2013-09-03 17:48:37 -07003841
Winson Chung87412982013-10-03 18:34:14 -07003842 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003843 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003844
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003845 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003846 addedApps != null && mAppsCustomizeContent != null) {
3847 mAppsCustomizeContent.addApps(addedApps);
3848 }
Winson Chungd64d1762013-08-20 14:37:16 -07003849 }
3850
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003851 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 * Bind the items start-end from the list.
3853 *
3854 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003855 */
Winson Chung64359a52013-07-08 17:17:08 -07003856 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3857 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003858 Runnable r = new Runnable() {
3859 public void run() {
3860 bindItems(shortcuts, start, end, forceAnimateIcons);
3861 }
3862 };
3863 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003864 return;
3865 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003866
Winson Chungf0c6ae02012-03-21 16:10:31 -07003867 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003868 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3869 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003870 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003871 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003872 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003873 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003874 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003875
3876 // Short circuit if we are loading dock items for a configuration which has no dock
3877 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3878 mHotseat == null) {
3879 continue;
3880 }
3881
Joe Onorato9c1289c2009-08-17 11:03:03 -04003882 switch (item.itemType) {
3883 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3884 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003885 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003886 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003887
Winson Chung64359a52013-07-08 17:17:08 -07003888 /*
3889 * TODO: FIX collision case
3890 */
Winson Chungce376632013-07-11 16:12:41 -07003891 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3892 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3893 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3894 throw new RuntimeException("OCCUPIED");
3895 }
Winson Chung64359a52013-07-08 17:17:08 -07003896 }
3897
Adam Cohendcd297f2013-06-18 13:13:40 -07003898 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3899 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003900 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003901 // Animate all the applications up now
3902 shortcut.setAlpha(0f);
3903 shortcut.setScaleX(0f);
3904 shortcut.setScaleY(0f);
3905 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003906 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003909 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003910 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003911 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003912 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003913 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3914 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003915 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003916 default:
3917 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003919 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003920
Winson Chung997a9232013-07-24 15:33:46 -07003921 if (animateIcons) {
3922 // Animate to the correct page
3923 if (newShortcutsScreenId > -1) {
3924 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003925 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003926 final Runnable startBounceAnimRunnable = new Runnable() {
3927 public void run() {
3928 anim.playTogether(bounceAnims);
3929 anim.start();
3930 }
3931 };
Winson Chung997a9232013-07-24 15:33:46 -07003932 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003933 // We post the animation slightly delayed to prevent slowdowns
3934 // when we are loading right after we return to launcher.
3935 mWorkspace.postDelayed(new Runnable() {
3936 public void run() {
3937 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003938 mWorkspace.postDelayed(startBounceAnimRunnable,
3939 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003940 }
3941 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003942 } else {
3943 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003944 }
3945 }
Winson Chung64359a52013-07-08 17:17:08 -07003946 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003947 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003948 }
3949
Joe Onorato9c1289c2009-08-17 11:03:03 -04003950 /**
3951 * Implementation of the method from LauncherModel.Callbacks.
3952 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003953 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003954 Runnable r = new Runnable() {
3955 public void run() {
3956 bindFolders(folders);
3957 }
3958 };
3959 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003960 return;
3961 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003962 sFolders.clear();
3963 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965
3966 /**
3967 * Add the views for a widget to the workspace.
3968 *
3969 * Implementation of the method from LauncherModel.Callbacks.
3970 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003971 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003972 Runnable r = new Runnable() {
3973 public void run() {
3974 bindAppWidget(item);
3975 }
3976 };
3977 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003978 return;
3979 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003980
Daniel Sandler843e8602010-06-07 14:59:01 -04003981 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3982 if (DEBUG_WIDGETS) {
3983 Log.d(TAG, "bindAppWidget: " + item);
3984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003985 final Workspace workspace = mWorkspace;
3986
3987 final int appWidgetId = item.appWidgetId;
3988 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003989 if (DEBUG_WIDGETS) {
3990 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3991 }
3992
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3994
Joe Onorato9c1289c2009-08-17 11:03:03 -04003995 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003996 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003997
Adam Cohendcd297f2013-06-18 13:13:40 -07003998 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003999 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004000 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4001
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002 workspace.requestLayout();
4003
Daniel Sandler843e8602010-06-07 14:59:01 -04004004 if (DEBUG_WIDGETS) {
4005 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4006 + (SystemClock.uptimeMillis()-start) + "ms");
4007 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004008 }
4009
Adam Cohen1462de32012-07-24 22:34:36 -07004010 public void onPageBoundSynchronously(int page) {
4011 mSynchronouslyBoundPages.add(page);
4012 }
4013
Joe Onorato9c1289c2009-08-17 11:03:03 -04004014 /**
4015 * Callback saying that there aren't any more items to bind.
4016 *
4017 * Implementation of the method from LauncherModel.Callbacks.
4018 */
Adam Cohene25af792013-06-06 23:08:25 -07004019 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004020 Runnable r = new Runnable() {
4021 public void run() {
4022 finishBindingItems(upgradePath);
4023 }
4024 };
4025 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004026 return;
4027 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004028 if (mSavedState != null) {
4029 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004030 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004031 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004032 mSavedState = null;
4033 }
4034
Adam Cohen1462de32012-07-24 22:34:36 -07004035 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004037 // If we received the result of any pending adds while the loader was running (e.g. the
4038 // widget configuration forced an orientation change), process them now.
4039 for (int i = 0; i < sPendingAddList.size(); i++) {
4040 completeAdd(sPendingAddList.get(i));
4041 }
4042 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004043
Winson Chungc51db6a2011-10-05 11:44:49 -07004044 // Update the market app icon as necessary (the other icons will be managed in response to
4045 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004046 if (!DISABLE_MARKET_BUTTON) {
4047 updateAppMarketIcon();
4048 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004049
Winson Chungf0c6ae02012-03-21 16:10:31 -07004050 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004051 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004052 mWorkspace.getUniqueComponents(true, null);
4053 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004054 }
Adam Cohen99894d92013-06-14 11:22:59 -07004055
Adam Cohen66a01fd2013-06-11 12:48:00 -07004056 mWorkspace.post(new Runnable() {
4057 @Override
4058 public void run() {
4059 onFinishBindingItems();
4060 }
4061 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07004062 }
4063
Winson Chunga0b7e862013-09-05 16:03:15 -07004064 public boolean isAllAppsButtonRank(int rank) {
4065 if (mHotseat != null) {
4066 return mHotseat.isAllAppsButtonRank(rank);
4067 }
4068 return false;
4069 }
4070
Winson Chunga2413752012-04-03 14:22:34 -07004071 private boolean canRunNewAppsAnimation() {
4072 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4073 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4074 }
4075
Winson Chungc9168342013-06-26 14:54:55 -07004076 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4077 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4078 PropertyValuesHolder.ofFloat("alpha", 1f),
4079 PropertyValuesHolder.ofFloat("scaleX", 1f),
4080 PropertyValuesHolder.ofFloat("scaleY", 1f));
4081 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4082 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4083 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4084 return bounceAnim;
4085 }
4086
Adam Cohen27772732013-11-11 14:00:56 +00004087 public boolean useVerticalBarLayout() {
4088 return LauncherAppState.getInstance().getDynamicGrid().
4089 getDeviceProfile().isVerticalBarLayout();
4090 }
4091
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004092 protected Rect getSearchBarBounds() {
4093 return LauncherAppState.getInstance().getDynamicGrid().
4094 getDeviceProfile().getSearchBarBounds();
4095 }
4096
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004097 @Override
4098 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004099 boolean searchVisible = updateGlobalSearchIcon();
4100 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004101 if (mSearchDropTargetBar != null) {
4102 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4103 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004104 }
4105
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004106 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107 * Add the icons for all apps.
4108 *
4109 * Implementation of the method from LauncherModel.Callbacks.
4110 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004111 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004112 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004113 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4114 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4115 getHotseat().addAllAppsFolder(mIconCache, apps,
4116 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4117 }
4118 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004119 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004120 if (mAppsCustomizeContent != null) {
4121 mAppsCustomizeContent.onPackagesUpdated(
4122 LauncherModel.getSortedWidgetsAndShortcuts(this));
4123 }
Winson Chungc58497e2013-09-03 17:48:37 -07004124 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004125 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004126 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004127 mAppsCustomizeContent.onPackagesUpdated(
4128 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004129 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004130 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004131 }
4132
4133 /**
4134 * A package was updated.
4135 *
4136 * Implementation of the method from LauncherModel.Callbacks.
4137 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004138 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004139 Runnable r = new Runnable() {
4140 public void run() {
4141 bindAppsUpdated(apps);
4142 }
4143 };
4144 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004145 return;
4146 }
4147
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004148 if (mWorkspace != null) {
4149 mWorkspace.updateShortcuts(apps);
4150 }
Winson Chungc58497e2013-09-03 17:48:37 -07004151
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004152 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004153 mAppsCustomizeContent != null) {
4154 mAppsCustomizeContent.updateApps(apps);
4155 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004156 }
4157
4158 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004159 * A package was uninstalled. We take both the super set of packageNames
4160 * in addition to specific applications to remove, the reason being that
4161 * this can be called when a package is updated as well. In that scenario,
4162 * we only remove specific components from the workspace, where as
4163 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004164 *
4165 * Implementation of the method from LauncherModel.Callbacks.
4166 */
Winson Chung83892cc2013-05-01 16:53:33 -07004167 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004168 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004169 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004170 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004171 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004172 }
Winson Chungd64d1762013-08-20 14:37:16 -07004173 };
4174 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004175 return;
4176 }
4177
Winson Chungdf95eb12013-10-16 14:57:07 -07004178 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004179 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004180 }
4181 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004182 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004183 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004184
Winson Chunga1820962011-10-03 16:31:06 -07004185 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004186 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004187
Winson Chungdf95eb12013-10-16 14:57:07 -07004188 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004189 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004190 mAppsCustomizeContent != null) {
4191 mAppsCustomizeContent.removeApps(appInfos);
4192 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004193 }
Joe Onoratobe386092009-11-17 17:32:16 -08004194
4195 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004196 * A number of packages were updated.
4197 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004198 private ArrayList<Object> mWidgetsAndShortcuts;
4199 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004200 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004201 bindPackagesUpdated(mWidgetsAndShortcuts);
4202 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004203 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004204 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004205 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4206 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4207 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004208 return;
4209 }
4210
Winson Chung64359a52013-07-08 17:17:08 -07004211 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004212 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004213 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004214 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004215 }
4216
Winson Chung400438b2011-01-16 17:53:48 -08004217 private int mapConfigurationOriActivityInfoOri(int configOri) {
4218 final Display d = getWindowManager().getDefaultDisplay();
4219 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4220 switch (d.getRotation()) {
4221 case Surface.ROTATION_0:
4222 case Surface.ROTATION_180:
4223 // We are currently in the same basic orientation as the natural orientation
4224 naturalOri = configOri;
4225 break;
4226 case Surface.ROTATION_90:
4227 case Surface.ROTATION_270:
4228 // We are currently in the other basic orientation to the natural orientation
4229 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4230 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4231 break;
4232 }
4233
4234 int[] oriMap = {
4235 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4236 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4237 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4238 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4239 };
4240 // Since the map starts at portrait, we need to offset if this device's natural orientation
4241 // is landscape.
4242 int indexOffset = 0;
4243 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4244 indexOffset = 1;
4245 }
4246 return oriMap[(d.getRotation() + indexOffset) % 4];
4247 }
Adam Cohen446e9402011-09-15 18:21:21 -07004248
Winson Chung4b919f82012-05-01 10:44:08 -07004249 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004250 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004251 getResources().getBoolean(R.bool.allow_rotation);
4252 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004253 }
Winson Chung4b919f82012-05-01 10:44:08 -07004254 public void lockScreenOrientation() {
4255 if (isRotationEnabled()) {
4256 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4257 .getConfiguration().orientation));
4258 }
4259 }
4260 public void unlockScreenOrientation(boolean immediate) {
4261 if (isRotationEnabled()) {
4262 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004263 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004264 } else {
4265 mHandler.postDelayed(new Runnable() {
4266 public void run() {
4267 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4268 }
4269 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004270 }
Winson Chung4b919f82012-05-01 10:44:08 -07004271 }
Winson Chung400438b2011-01-16 17:53:48 -08004272 }
4273
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004274 /**
4275 * Called when the SearchBar hint should be changed.
4276 *
4277 * @param hint the hint to be displayed in the search bar.
4278 */
4279 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004280 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004281 }
4282
Winson Chunge6eabff2013-09-24 11:01:23 -07004283 protected String getFirstRunClingSearchBarHint() {
4284 return "";
4285 }
4286 protected String getFirstRunCustomContentHint() {
4287 return "";
4288 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004289 protected int getFirstRunFocusedHotseatAppDrawableId() {
4290 return -1;
4291 }
4292 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4293 return null;
4294 }
4295 protected int getFirstRunFocusedHotseatAppRank() {
4296 return -1;
4297 }
4298 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4299 return "";
4300 }
4301 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4302 return "";
4303 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004304
Winson Chungaf40f202013-09-18 18:26:31 -07004305 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004306 mLauncherClings.dismissFirstRunCling(v);
4307 }
4308 public void dismissMigrationClingCopyApps(View v) {
4309 mLauncherClings.dismissMigrationClingCopyApps(v);
4310 }
4311 public void dismissMigrationClingUseDefault(View v) {
4312 mLauncherClings.dismissMigrationClingUseDefault(v);
4313 }
4314 public void dismissMigrationWorkspaceCling(View v) {
4315 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004316 }
Winson Chung82f55532011-08-09 14:14:23 -07004317 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004318 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004319 }
4320 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004321 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004322 }
4323
Winson Chunga6945242014-01-08 14:04:34 -08004324 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004325 return !ActivityManager.isRunningInTestHarness() &&
4326 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004327 }
4328
4329 public void showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004330 if (shouldRunFirstRunActivity() &&
4331 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004332 Intent firstRunIntent = getFirstRunActivity();
4333 if (firstRunIntent != null) {
4334 startActivity(firstRunIntent);
4335 markFirstRunActivityShown();
4336 }
4337 }
4338 }
4339
4340 private void markFirstRunActivityShown() {
4341 SharedPreferences.Editor editor = mSharedPrefs.edit();
4342 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4343 editor.apply();
4344 }
4345
4346 void showWorkspaceSearchAndHotseat() {
4347 mWorkspace.setAlpha(1f);
4348 mHotseat.setAlpha(1f);
4349 mPageIndicators.setAlpha(1f);
4350 mSearchDropTargetBar.showSearchBar(false);
4351 }
4352
4353 void hideWorkspaceSearchAndHotseat() {
4354 mWorkspace.setAlpha(0f);
4355 mHotseat.setAlpha(0f);
4356 mPageIndicators.setAlpha(0f);
4357 mSearchDropTargetBar.hideSearchBar(false);
4358 }
4359
4360
Mathew Inwood72fbec12013-11-19 15:45:07 +00004361 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4362 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4363 if (ri == null) {
4364 return null;
4365 }
4366 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4367 }
4368
4369 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4370 Bitmap icon) {
4371 return new ShortcutInfo(shortcutIntent, caption, icon);
4372 }
4373
4374 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4375 dragView.setTag(dragInfo);
4376 mWorkspace.onDragStartedWithItem(dragView);
4377 mWorkspace.beginDragShared(dragView, source);
4378 }
4379
Winson Chung80baf5a2010-08-09 16:03:15 -07004380 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004381 * Prints out out state for debugging.
4382 */
4383 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004384 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004385 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004386 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4387 Log.d(TAG, "mRestoring=" + mRestoring);
4388 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4389 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004390 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004391 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004392
Winson Chung785d2eb2011-04-14 16:08:02 -07004393 if (mAppsCustomizeContent != null) {
4394 mAppsCustomizeContent.dumpState();
4395 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004396 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004397 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004398
4399 @Override
4400 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4401 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004402 synchronized (sDumpLogs) {
4403 writer.println(" ");
4404 writer.println("Debug logs: ");
4405 for (int i = 0; i < sDumpLogs.size(); i++) {
4406 writer.println(" " + sDumpLogs.get(i));
4407 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004408 }
4409 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004410
4411 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004412 if (DEBUG_DUMP_LOG) {
4413 synchronized (sDumpLogs) {
4414 Log.d(TAG, "");
4415 Log.d(TAG, "*********************");
4416 Log.d(TAG, "Launcher debug logs: ");
4417 for (int i = 0; i < sDumpLogs.size(); i++) {
4418 Log.d(TAG, " " + sDumpLogs.get(i));
4419 }
4420 Log.d(TAG, "*********************");
4421 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004422 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004423 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004424 }
4425
4426 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004427 addDumpLog(tag, log, null, debugLog);
4428 }
4429
4430 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004431 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004432 if (e != null) {
4433 Log.d(tag, log, e);
4434 } else {
4435 Log.d(tag, log);
4436 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004437 }
Winson Chungede41292013-09-19 16:27:36 -07004438 if (DEBUG_DUMP_LOG) {
4439 sDateStamp.setTime(System.currentTimeMillis());
4440 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004441 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4442 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004443 }
Winson Chungede41292013-09-19 16:27:36 -07004444 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004445 }
4446
Winson Chungede41292013-09-19 16:27:36 -07004447 public void dumpLogsToLocalData() {
4448 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004449 new AsyncTask<Void, Void, Void>() {
4450 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004451 boolean success = false;
4452 sDateStamp.setTime(sRunStart);
4453 String FILENAME = sDateStamp.getMonth() + "-"
4454 + sDateStamp.getDay() + "_"
4455 + sDateStamp.getHours() + "-"
4456 + sDateStamp.getMinutes() + "_"
4457 + sDateStamp.getSeconds() + ".txt";
4458
4459 FileOutputStream fos = null;
4460 File outFile = null;
4461 try {
4462 outFile = new File(getFilesDir(), FILENAME);
4463 outFile.createNewFile();
4464 fos = new FileOutputStream(outFile);
4465 } catch (Exception e) {
4466 e.printStackTrace();
4467 }
4468 if (fos != null) {
4469 PrintWriter writer = new PrintWriter(fos);
4470
4471 writer.println(" ");
4472 writer.println("Debug logs: ");
4473 synchronized (sDumpLogs) {
4474 for (int i = 0; i < sDumpLogs.size(); i++) {
4475 writer.println(" " + sDumpLogs.get(i));
4476 }
4477 }
4478 writer.close();
4479 }
4480 try {
4481 if (fos != null) {
4482 fos.close();
4483 success = true;
4484 }
4485 } catch (IOException e) {
4486 e.printStackTrace();
4487 }
Michael Jurka43467462013-11-14 12:03:58 +01004488 return null;
Winson Chungede41292013-09-19 16:27:36 -07004489 }
Michael Jurka43467462013-11-14 12:03:58 +01004490 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004491 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004493}
Michael Jurka2763be32011-02-24 11:19:57 -08004494
Michael Jurkaabded662011-03-04 12:06:57 -08004495interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004496 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004497 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004498 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004499 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004500 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004501}
Dan Sandlerd5024042014-01-09 15:01:33 -05004502
4503interface DebugIntents {
4504 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4505 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004506}