blob: 336cf9e7a849dd51cebfe3e4b930d0514d09141a [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 Sandler6053b8002013-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;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700251 private View mOverviewPanel;
252
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 Chung07e045c62013-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
Adam Cohen30bacb22013-08-29 14:25:25 -07001064 protected void startSettings() {
1065 }
1066
Adam Cohenbffe7452013-07-22 18:21:45 -07001067 public interface QSBScroller {
1068 public void setScrollY(int scrollY);
1069 }
1070
Winson Chung98ca0f72013-07-29 12:58:51 -07001071 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001072 CustomContentCallbacks callbacks, String description) {
1073 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001074 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001075 }
1076
Adam Cohenedb40762013-07-18 16:45:45 -07001077 // The custom content needs to offset its content to account for the QSB
1078 public int getTopOffsetForCustomContent() {
1079 return mWorkspace.getPaddingTop();
1080 }
1081
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001082 @Override
1083 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 // Flag the loader to stop early before switching
1085 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001086 if (mAppsCustomizeContent != null) {
1087 mAppsCustomizeContent.surrender();
1088 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001089 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001090 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001092 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001093 @Override
1094 public void onWindowFocusChanged(boolean hasFocus) {
1095 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001096 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001097 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 private boolean acceptFilter() {
1100 final InputMethodManager inputManager = (InputMethodManager)
1101 getSystemService(Context.INPUT_METHOD_SERVICE);
1102 return !inputManager.isFullscreenMode();
1103 }
1104
1105 @Override
1106 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001107 final int uniChar = event.getUnicodeChar();
1108 final boolean handled = super.onKeyDown(keyCode, event);
1109 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1110 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1112 keyCode, event);
1113 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001114 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001115 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001116 // showSearchDialog()
1117 // If there are multiple keystrokes before the search dialog takes focus,
1118 // onSearchRequested() will be called for every keystroke,
1119 // but it is idempotent, so it's fine.
1120 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
1122 }
1123
Joe Onorato8a9625e2010-01-28 15:55:35 -08001124 // Eat the long press event so the keyboard doesn't come up.
1125 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1126 return true;
1127 }
1128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 return handled;
1130 }
1131
Karl Rosaen138a0412009-04-23 19:00:21 -07001132 private String getTypedText() {
1133 return mDefaultKeySsb.toString();
1134 }
1135
1136 private void clearTypedText() {
1137 mDefaultKeySsb.clear();
1138 mDefaultKeySsb.clearSpans();
1139 Selection.setSelection(mDefaultKeySsb, 0);
1140 }
1141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001143 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1144 * State
1145 */
1146 private static State intToState(int stateOrdinal) {
1147 State state = State.WORKSPACE;
1148 final State[] stateValues = State.values();
1149 for (int i = 0; i < stateValues.length; i++) {
1150 if (stateValues[i].ordinal() == stateOrdinal) {
1151 state = stateValues[i];
1152 break;
1153 }
1154 }
1155 return state;
1156 }
1157
1158 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 * Restores the previous state, if it exists.
1160 *
1161 * @param savedState The previous state.
1162 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001163 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 private void restoreState(Bundle savedState) {
1165 if (savedState == null) {
1166 return;
1167 }
1168
Michael Jurka883f55b2010-10-21 15:47:14 -07001169 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001170 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001171 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001172 }
1173
Adam Cohen21cd0022013-10-09 18:57:02 -07001174 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1175 PagedView.INVALID_RESTORE_PAGE);
1176 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001177 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 }
1179
Winson Chung3d503fb2011-07-13 17:25:49 -07001180 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001181 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001182
Winson Chung3d503fb2011-07-13 17:25:49 -07001183 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1184 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001185 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001186 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1187 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001188 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1189 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1190 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001191 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001192 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 mRestoring = true;
1194 }
1195
1196 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1197 if (renameFolder) {
1198 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001199 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 mRestoring = true;
1201 }
Winson Chunga12a2502010-12-20 14:41:35 -08001202
Winson Chung785d2eb2011-04-14 16:08:02 -07001203 // Restore the AppsCustomize tab
1204 if (mAppsCustomizeTabHost != null) {
1205 String curTab = savedState.getString("apps_customize_currentTab");
1206 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001207 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001208 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001209 mAppsCustomizeContent.loadAssociatedPages(
1210 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001211 }
1212
Winson Chung5afbf7b2011-07-25 11:53:08 -07001213 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1214 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001216 mItemIdToViewId = (HashMap<Integer, Integer>)
1217 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 }
1219
1220 /**
1221 * Finds all the views we need and configure them properly.
1222 */
1223 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001224 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001225
Craig Mautner360310b2012-10-26 15:13:08 -07001226 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001227 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1228 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001229 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001230
John Spurlock77e1f472013-09-11 10:09:51 -04001231 mLauncherView.setSystemUiVisibility(
1232 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001233 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234
Winson Chung4c98d922011-05-31 16:50:48 -07001235 // Setup the drag layer
1236 mDragLayer.setup(this, dragController);
1237
Winson Chung3d503fb2011-07-13 17:25:49 -07001238 // Setup the hotseat
1239 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1240 if (mHotseat != null) {
1241 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001242 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001243 }
1244
Adam Cohenf358a4b2013-07-23 16:47:31 -07001245 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001246 View widgetButton = findViewById(R.id.widget_button);
1247 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001248 @Override
1249 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001250 if (!mWorkspace.isSwitchingState()) {
1251 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1252 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001253 }
1254 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001255 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1256
1257 View wallpaperButton = findViewById(R.id.wallpaper_button);
1258 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001259 @Override
1260 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001261 if (!mWorkspace.isSwitchingState()) {
1262 startWallpaper();
1263 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001264 }
1265 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001266 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1267
1268 View settingsButton = findViewById(R.id.settings_button);
1269 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001270 @Override
1271 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001272 if (!mWorkspace.isSwitchingState()) {
1273 startSettings();
1274 }
Adam Cohen30bacb22013-08-29 14:25:25 -07001275 }
1276 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001277 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001278 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001279
Winson Chung4c98d922011-05-31 16:50:48 -07001280 // Setup the workspace
1281 mWorkspace.setHapticFeedbackEnabled(false);
1282 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001283 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001284 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001285
Winson Chungf0ea4d32011-06-06 14:27:16 -07001286 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001287 mSearchDropTargetBar = (SearchDropTargetBar)
1288 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001289
Winson Chungf0ea4d32011-06-06 14:27:16 -07001290 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001291 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001292 mAppsCustomizeContent = (AppsCustomizePagedView)
1293 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1294 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001295
Winson Chung3d503fb2011-07-13 17:25:49 -07001296 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001297 dragController.setDragScoller(mWorkspace);
1298 dragController.setScrollView(mDragLayer);
1299 dragController.setMoveTarget(mWorkspace);
1300 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001301 if (mSearchDropTargetBar != null) {
1302 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001303 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001304
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001305 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001306 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001307 mWeightWatcher = new WeightWatcher(this);
1308 mWeightWatcher.setAlpha(0.5f);
1309 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001310 new FrameLayout.LayoutParams(
1311 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001312 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001313 Gravity.BOTTOM)
1314 );
Adam Cohen39a06042013-07-19 14:30:12 -07001315
1316 boolean show = shouldShowWeightWatcher();
1317 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
1321 /**
1322 * Creates a view representing a shortcut.
1323 *
1324 * @param info The data structure describing the shortcut.
1325 *
1326 * @return A View inflated from R.layout.application.
1327 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001328 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001330 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
1333 /**
1334 * Creates a view representing a shortcut inflated from the specified resource.
1335 *
1336 * @param layoutResId The id of the XML layout used to create the shortcut.
1337 * @param parent The group the shortcut belongs to.
1338 * @param info The data structure describing the shortcut.
1339 *
1340 * @return A View inflated from layoutResId.
1341 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001342 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001343 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1344 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 return favorite;
1347 }
1348
1349 /**
1350 * Add an application shortcut to the workspace.
1351 *
1352 * @param data The intent describing the application.
1353 * @param cellInfo The position on screen where to create the shortcut.
1354 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001355 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001356 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001357 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001358
Adam Cohenfbba09b2011-07-18 21:43:05 -07001359 // First we check if we already know the exact location where we want to add this item.
1360 if (cellX >= 0 && cellY >= 0) {
1361 cellXY[0] = cellX;
1362 cellXY[1] = cellY;
1363 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001364 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001365 return;
1366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001368 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001369
Romain Guy73b979d2009-06-09 12:57:21 -07001370 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001371 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001373 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001374 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001375 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001376 } else {
1377 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
1379 }
Romain Guycbb89e42009-06-08 15:52:54 -07001380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 /**
1382 * Add a shortcut to the workspace.
1383 *
1384 * @param data The intent describing the shortcut.
1385 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001387 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001388 int cellY) {
1389 int[] cellXY = mTmpAddItemCellCoordinates;
1390 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001391 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001392
Michael Jurkad3ef3062010-11-23 16:23:58 -08001393 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001394
Adam Cohen558baaf2011-08-15 15:22:57 -07001395 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001396 if (info == null) {
1397 return;
1398 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001399 final View view = createShortcut(info);
1400
Adam Cohenfbba09b2011-07-18 21:43:05 -07001401 // First we check if we already know the exact location where we want to add this item.
1402 if (cellX >= 0 && cellY >= 0) {
1403 cellXY[0] = cellX;
1404 cellXY[1] = cellY;
1405 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001406
1407 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001408 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001409 true, null,null)) {
1410 return;
1411 }
1412 DragObject dragObject = new DragObject();
1413 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001414 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1415 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001416 return;
1417 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001418 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001419 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001420 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001421 foundCellSpan = (result != null);
1422 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001423 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001424 }
1425
1426 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001427 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001428 return;
1429 }
1430
Adam Cohendcd297f2013-06-18 13:13:40 -07001431 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432
1433 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001434 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001435 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
1437 }
1438
Adam Cohen2f093b62012-04-30 18:59:53 -07001439 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1440 int minHeight) {
1441 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001442 // We want to account for the extra amount of padding that we are adding to the widget
1443 // to ensure that it gets the full amount of space that it has requested
1444 int requiredWidth = minWidth + padding.left + padding.right;
1445 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001446 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001447 }
1448
Adam Cohen2f093b62012-04-30 18:59:53 -07001449 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1450 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001451 }
1452
Adam Cohen2f093b62012-04-30 18:59:53 -07001453 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1454 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001455 }
1456
Adam Cohen2f093b62012-04-30 18:59:53 -07001457 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1458 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001459 }
1460
Adam Cohen2f093b62012-04-30 18:59:53 -07001461 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1462 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001463 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001464 }
1465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001467 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001469 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001470 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001472 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001473 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1474 if (appWidgetInfo == null) {
1475 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1476 }
Romain Guycbb89e42009-06-08 15:52:54 -07001477
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001478 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001479 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001480
Adam Cohen2f093b62012-04-30 18:59:53 -07001481 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1482 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001485 // We have saved the position to which the widget was dragged-- this really only matters
1486 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001487 int[] cellXY = mTmpAddItemCellCoordinates;
1488 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001489 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001490 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001491 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1492 cellXY[0] = mPendingAddInfo.cellX;
1493 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001494 spanXY[0] = mPendingAddInfo.spanX;
1495 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001496 foundCellSpan = true;
1497 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001498 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001500 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1501 spanXY[1], cellXY, finalSpan);
1502 spanXY[0] = finalSpan[0];
1503 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001504 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001505 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001506 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001507 }
1508
Michael Jurka0280c3b2010-09-17 15:00:07 -07001509 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001510 if (appWidgetId != -1) {
1511 // Deleting an app widget ID is a void call but writes to disk before returning
1512 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001513 new AsyncTask<Void, Void, Void>() {
1514 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001515 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001516 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001517 }
Michael Jurka43467462013-11-14 12:03:58 +01001518 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001519 }
Winson Chung93eef082012-03-23 15:59:27 -07001520 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001521 return;
1522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001524 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001525 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1526 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001527 launcherInfo.spanX = spanXY[0];
1528 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001529 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1530 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001531
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001533 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534
1535 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001536 if (hostView == null) {
1537 // Perform actual inflation because we're live
1538 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1539 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1540 } else {
1541 // The AppWidgetHostView has already been inflated and instantiated
1542 launcherInfo.hostView = hostView;
1543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001546 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001547 launcherInfo.notifyWidgetSizeChanged(this);
1548
Adam Cohendcd297f2013-06-18 13:13:40 -07001549 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001550 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001551
1552 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001554 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
Romain Guycbb89e42009-06-08 15:52:54 -07001556
Adam Cohended9f8d2010-11-03 13:25:16 -07001557 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1558 @Override
1559 public void onReceive(Context context, Intent intent) {
1560 final String action = intent.getAction();
1561 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1562 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001563 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001564 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001565
Winson Chungc100e8e2011-08-09 16:02:43 -07001566 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001567 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001568 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001569 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001570 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001571 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1572 mUserPresent = true;
1573 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001574 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1575 mModel.resetLoadedState(false, true);
1576 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1577 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1578 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1579 mModel.resetLoadedState(false, true);
1580 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1581 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1582 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001583 }
1584 }
1585 };
1586
1587 @Override
1588 public void onAttachedToWindow() {
1589 super.onAttachedToWindow();
1590
1591 // Listen for broadcasts related to user-presence
1592 final IntentFilter filter = new IntentFilter();
1593 filter.addAction(Intent.ACTION_SCREEN_OFF);
1594 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001595 if (ENABLE_DEBUG_INTENTS) {
1596 filter.addAction(DebugIntents.DELETE_DATABASE);
1597 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1598 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001599 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001600 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001601 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001602 mVisible = true;
1603 }
1604
1605 @Override
1606 public void onDetachedFromWindow() {
1607 super.onDetachedFromWindow();
1608 mVisible = false;
1609
Adam Cohend113e0c2010-11-11 10:48:05 -08001610 if (mAttached) {
1611 unregisterReceiver(mReceiver);
1612 mAttached = false;
1613 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 updateRunning();
1615 }
1616
1617 public void onWindowVisibilityChanged(int visibility) {
1618 mVisible = visibility == View.VISIBLE;
1619 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001620 // The following code used to be in onResume, but it turns out onResume is called when
1621 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1622 // is a more appropriate event to handle
1623 if (mVisible) {
1624 mAppsCustomizeTabHost.onWindowVisible();
1625 if (!mWorkspaceLoading) {
1626 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001627 // We want to let Launcher draw itself at least once before we force it to build
1628 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001629 // apps is nice and speedy.
1630 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001631 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001632 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001633 if (mStarted) return;
1634 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001635 // We delay the layer building a bit in order to give
1636 // other message processing a time to run. In particular
1637 // this avoids a delay in hiding the IME if it was
1638 // currently shown, because doing that may involve
1639 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001640 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001641 final ViewTreeObserver.OnDrawListener listener = this;
1642 mWorkspace.post(new Runnable() {
1643 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001644 if (mWorkspace != null &&
1645 mWorkspace.getViewTreeObserver() != null) {
1646 mWorkspace.getViewTreeObserver().
1647 removeOnDrawListener(listener);
1648 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001649 }
1650 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001651 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001652 }
1653 });
1654 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001655 // When Launcher comes back to foreground, a different Activity might be responsible for
1656 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001657 if (!DISABLE_MARKET_BUTTON) {
1658 updateAppMarketIcon();
1659 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001660 clearTypedText();
1661 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001662 }
1663
1664 private void sendAdvanceMessage(long delay) {
1665 mHandler.removeMessages(ADVANCE_MSG);
1666 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1667 mHandler.sendMessageDelayed(msg, delay);
1668 mAutoAdvanceSentTime = System.currentTimeMillis();
1669 }
1670
1671 private void updateRunning() {
1672 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1673 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1674 mAutoAdvanceRunning = autoAdvanceRunning;
1675 if (autoAdvanceRunning) {
1676 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1677 sendAdvanceMessage(delay);
1678 } else {
1679 if (!mWidgetsToAdvance.isEmpty()) {
1680 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1681 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1682 }
1683 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001684 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001685 }
1686 }
1687 }
1688
1689 private final Handler mHandler = new Handler() {
1690 @Override
1691 public void handleMessage(Message msg) {
1692 if (msg.what == ADVANCE_MSG) {
1693 int i = 0;
1694 for (View key: mWidgetsToAdvance.keySet()) {
1695 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1696 final int delay = mAdvanceStagger * i;
1697 if (v instanceof Advanceable) {
1698 postDelayed(new Runnable() {
1699 public void run() {
1700 ((Advanceable) v).advance();
1701 }
1702 }, delay);
1703 }
1704 i++;
1705 }
1706 sendAdvanceMessage(mAdvanceInterval);
1707 }
1708 }
1709 };
1710
1711 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001712 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1714 if (v instanceof Advanceable) {
1715 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001716 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 updateRunning();
1718 }
1719 }
1720
1721 void removeWidgetToAutoAdvance(View hostView) {
1722 if (mWidgetsToAdvance.containsKey(hostView)) {
1723 mWidgetsToAdvance.remove(hostView);
1724 updateRunning();
1725 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001726 }
1727
Joe Onorato9c1289c2009-08-17 11:03:03 -04001728 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001730 launcherInfo.hostView = null;
1731 }
1732
Winson Chung93eef082012-03-23 15:59:27 -07001733 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1734 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1735 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 }
1737
Winson Chunga6945242014-01-08 14:04:34 -08001738 public DragLayer getDragLayer() {
1739 return mDragLayer;
1740 }
1741
1742 public Workspace getWorkspace() {
1743 return mWorkspace;
1744 }
1745
1746 public Hotseat getHotseat() {
1747 return mHotseat;
1748 }
1749
1750 public View getOverviewPanel() {
1751 return mOverviewPanel;
1752 }
1753
1754 public SearchDropTargetBar getSearchBar() {
1755 return mSearchDropTargetBar;
1756 }
1757
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001758 public LauncherAppWidgetHost getAppWidgetHost() {
1759 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 }
Romain Guycbb89e42009-06-08 15:52:54 -07001761
Winson Chunga9abd0e2010-10-27 17:18:37 -07001762 public LauncherModel getModel() {
1763 return mModel;
1764 }
1765
Winson Chunga6945242014-01-08 14:04:34 -08001766 public LauncherClings getLauncherClings() {
1767 return mLauncherClings;
1768 }
1769
1770 protected SharedPreferences getSharedPrefs() {
1771 return mSharedPrefs;
1772 }
1773
Bjorn Bringertc459e522013-06-07 19:36:01 +01001774 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001775 getWindow().closeAllPanels();
1776
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001777 // Whatever we were doing is hereby canceled.
1778 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001779 }
1780
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 @Override
1782 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001783 long startTime = 0;
1784 if (DEBUG_RESUME_TIME) {
1785 startTime = System.currentTimeMillis();
1786 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 super.onNewIntent(intent);
1788
1789 // Close the menu
1790 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001791 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001792 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001793
Adam Cohened307df2013-10-02 09:37:31 -07001794 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1795 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1796 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001797
Adam Cohen6fecd412013-10-02 17:41:50 -07001798 if (mWorkspace == null) {
1799 // Can be cases where mWorkspace is null, this prevents a NPE
1800 return;
1801 }
1802 Folder openFolder = mWorkspace.getOpenFolder();
1803 // In all these cases, only animate if we're already on home
1804 mWorkspace.exitWidgetResizeMode();
1805 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001806 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001807 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001808 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001809
Adam Cohen6fecd412013-10-02 17:41:50 -07001810 closeFolder();
1811 exitSpringLoadedDragMode();
1812
1813 // If we are already on home, then just animate back to the workspace,
1814 // otherwise, just wait until onResume to set the state back to Workspace
1815 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001816 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001817 } else {
1818 mOnResumeState = State.WORKSPACE;
1819 }
1820
1821 final View v = getWindow().peekDecorView();
1822 if (v != null && v.getWindowToken() != null) {
1823 InputMethodManager imm = (InputMethodManager)getSystemService(
1824 INPUT_METHOD_SERVICE);
1825 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1826 }
1827
1828 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001829 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001830 mAppsCustomizeTabHost.reset();
1831 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001832
1833 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 }
Adam Cohened307df2013-10-02 09:37:31 -07001835
Michael Jurka447bf842013-05-15 14:52:15 +02001836 if (DEBUG_RESUME_TIME) {
1837 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 }
1840
Adam Coppa120b8e2013-11-12 16:26:04 +00001841 /**
1842 * Override point for subclasses to prevent movement to the default screen when the home
1843 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1844 */
1845 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1846 return true;
1847 }
1848
1849 /**
1850 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1851 */
1852 protected void onHomeIntent() {
1853 // Do nothing
1854 }
1855
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001857 public void onRestoreInstanceState(Bundle state) {
1858 super.onRestoreInstanceState(state);
1859 for (int page: mSynchronouslyBoundPages) {
1860 mWorkspace.restoreInstanceStateForChild(page);
1861 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 }
1863
1864 @Override
1865 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001866 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001867 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1868 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001869 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001870 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871
Michael Jurka883f55b2010-10-21 15:47:14 -07001872 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001873 // We close any open folder since it will not be re-opened, and we need to make sure
1874 // this state is reflected.
1875 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876
Adam Cohendcd297f2013-06-18 13:13:40 -07001877 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001878 mWaitingForResult) {
1879 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001880 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001881 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1882 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001883 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1884 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1885 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001886 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
1888
1889 if (mFolderInfo != null && mWaitingForResult) {
1890 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1891 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1892 }
Michael Jurka946ad472010-07-09 18:05:18 -07001893
Winson Chung785d2eb2011-04-14 16:08:02 -07001894 // Save the current AppsCustomize tab
1895 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001896 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1897 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001898 if (currentTabTag != null) {
1899 outState.putString("apps_customize_currentTab", currentTabTag);
1900 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001901 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1902 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001903 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001904 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 }
1906
1907 @Override
1908 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001910
Winson Chunge7a03942011-08-05 15:05:12 -07001911 // Remove all pending runnables
1912 mHandler.removeMessages(ADVANCE_MSG);
1913 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001914 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001915
Winson Chungcd2b0142011-06-08 16:02:26 -07001916 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001917 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001918 mModel.stopLoader();
1919 app.setLauncher(null);
1920
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001922 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001924 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001926 mAppWidgetHost = null;
1927
1928 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929
1930 TextKeyListener.getInstance().release();
1931
Winson Chung81b52252012-08-27 15:34:29 -07001932 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1933 // to prevent leaking Launcher activities on orientation change.
1934 if (mModel != null) {
1935 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1936 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001937
1938 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001939 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001940
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001941 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001942 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001943 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001944 mWorkspace = null;
1945 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001946
Michael Jurka2ecf9952012-06-18 12:52:28 -07001947 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
Adam Cohena9cf38f2011-05-02 15:36:58 -07001950 public DragController getDragController() {
1951 return mDragController;
1952 }
1953
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 @Override
1955 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001956 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 super.startActivityForResult(intent, requestCode);
1958 }
1959
Winson Chung70d72102011-08-12 11:24:35 -07001960 /**
1961 * Indicates that we want global search for this activity by setting the globalSearch
1962 * argument for {@link #startSearch} to true.
1963 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001965 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001967
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001968 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001969
Karl Rosaen138a0412009-04-23 19:00:21 -07001970 if (initialQuery == null) {
1971 // Use any text typed in the launcher as the initial query
1972 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 if (appSearchData == null) {
1975 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001976 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
Winson Chungadf0c182012-08-23 14:59:07 -07001978 Rect sourceBounds = new Rect();
1979 if (mSearchDropTargetBar != null) {
1980 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1981 }
Romain Guycbb89e42009-06-08 15:52:54 -07001982
Bjorn Bringertc459e522013-06-07 19:36:01 +01001983 startSearch(initialQuery, selectInitialQuery,
1984 appSearchData, sourceBounds);
1985 }
1986
1987 public void startSearch(String initialQuery,
1988 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001989 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001990 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001991 }
1992
1993 /**
1994 * Starts the global search activity. This code is a copied from SearchManager
1995 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001996 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001997 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001998 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001999 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2000 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2001 if (globalSearchActivity == null) {
2002 Log.w(TAG, "No global search activity found.");
2003 return;
2004 }
2005 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2006 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2007 intent.setComponent(globalSearchActivity);
2008 // Make sure that we have a Bundle to put source in
2009 if (appSearchData == null) {
2010 appSearchData = new Bundle();
2011 } else {
2012 appSearchData = new Bundle(appSearchData);
2013 }
2014 // Set source to package name of app that starts global search, if not set already.
2015 if (!appSearchData.containsKey("source")) {
2016 appSearchData.putString("source", getPackageName());
2017 }
2018 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2019 if (!TextUtils.isEmpty(initialQuery)) {
2020 intent.putExtra(SearchManager.QUERY, initialQuery);
2021 }
2022 if (selectInitialQuery) {
2023 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2024 }
2025 intent.setSourceBounds(sourceBounds);
2026 try {
2027 startActivity(intent);
2028 } catch (ActivityNotFoundException ex) {
2029 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 }
2032
Winson Chung70d72102011-08-12 11:24:35 -07002033 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002034 public boolean onPrepareOptionsMenu(Menu menu) {
2035 super.onPrepareOptionsMenu(menu);
Dan Sandler14c6a3f2014-01-07 14:12:27 -05002036 showOverviewMode(true);
Michael Jurkab94f3f82013-09-11 18:08:54 +02002037 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002038 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002040 @Override
2041 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002042 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002043 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002044 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002045 }
2046
Joe Onorato9c1289c2009-08-17 11:03:03 -04002047 public boolean isWorkspaceLocked() {
2048 return mWorkspaceLoading || mWaitingForResult;
2049 }
2050
Michael Jurka0280c3b2010-09-17 15:00:07 -07002051 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002052 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002053 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002054 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2055 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002056 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002057 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002058 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002059
Adam Cohenad4e15c2013-10-17 16:21:35 -07002060 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2061 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2062 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2063 }
2064
2065 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2066 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2067 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002068 if (appWidgetInfo.configure != null) {
2069 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002070 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002071
Bjorn Bringert7984c942009-12-09 15:38:25 +00002072 // Launch over to configure widget, if needed
2073 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002074 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002075 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002076 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002078 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002079 Runnable onComplete = new Runnable() {
2080 @Override
2081 public void run() {
2082 // Exit spring loaded mode if necessary after adding the widget
2083 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2084 null);
2085 }
2086 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002087 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002088 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002089 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 }
2091 }
Romain Guycbb89e42009-06-08 15:52:54 -07002092
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002093 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002094 // Close any folders that may be open.
2095 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002096 mWorkspace.moveToCustomContentScreen(animate);
2097 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002098 /**
2099 * Process a shortcut drop.
2100 *
2101 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002102 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002103 * @param cell The cell it should be added to, optional
2104 * @param position The location on the screen where it was dropped, optional
2105 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002106 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002107 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002108 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002109 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002110 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002111 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002112
2113 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002114 mPendingAddInfo.cellX = cell[0];
2115 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002116 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002117
2118 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2119 createShortcutIntent.setComponent(componentName);
2120 processShortcut(createShortcutIntent);
2121 }
2122
Adam Cohenfbba09b2011-07-18 21:43:05 -07002123 /**
2124 * Process a widget drop.
2125 *
2126 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002127 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002128 * @param cell The cell it should be added to, optional
2129 * @param position The location on the screen where it was dropped, optional
2130 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002131 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002132 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002133 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002134 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002135 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002136 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002137 mPendingAddInfo.minSpanX = info.minSpanX;
2138 mPendingAddInfo.minSpanY = info.minSpanY;
2139
Adam Cohenfbba09b2011-07-18 21:43:05 -07002140 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002141 mPendingAddInfo.cellX = cell[0];
2142 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002143 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002144 if (span != null) {
2145 mPendingAddInfo.spanX = span[0];
2146 mPendingAddInfo.spanY = span[1];
2147 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002148
Adam Cohened66b2b2012-01-23 17:28:51 -08002149 AppWidgetHostView hostView = info.boundWidget;
2150 int appWidgetId;
2151 if (hostView != null) {
2152 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002153 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002154 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002155 // In this case, we either need to start an activity to get permission to bind
2156 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002157 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002158 Bundle options = info.bindOptions;
2159
2160 boolean success = false;
2161 if (options != null) {
2162 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2163 info.componentName, options);
2164 } else {
2165 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2166 info.componentName);
2167 }
2168 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002169 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002170 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002171 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002172 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2173 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2174 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002175 // TODO: we need to make sure that this accounts for the options bundle.
2176 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002177 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2178 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002179 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002180 }
2181
Joe Onoratodeb98af2010-02-19 14:59:39 -08002182 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002183 // Handle case where user selected "Applications"
2184 String applicationName = getResources().getString(R.string.group_applications);
2185 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002186
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002187 if (applicationName != null && applicationName.equals(shortcutName)) {
2188 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2189 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002190
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002191 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2192 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002193 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002194 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002195 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002196 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002197 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 }
2199
Winson Chung24ab2f12010-09-16 14:10:47 -07002200 void processWallpaper(Intent intent) {
2201 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2202 }
2203
Adam Cohendcd297f2013-06-18 13:13:40 -07002204 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002205 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002206 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 folderInfo.title = getText(R.string.folder_name);
2208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002210 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002211 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002212 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213
2214 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002215 FolderIcon newFolder =
2216 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002217 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002218 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002219 // Force measure the new folder icon
2220 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2221 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002222 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 }
Romain Guycbb89e42009-06-08 15:52:54 -07002224
Joe Onorato9c1289c2009-08-17 11:03:03 -04002225 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002226 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002227 }
2228
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002229 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002231 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002232 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 }
2234
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002235 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002236 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002237 }
2238
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002239 /**
2240 * Registers various content observers. The current implementation registers
2241 * only a favorites observer to keep track of the favorites applications.
2242 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002243 private void registerContentObservers() {
2244 ContentResolver resolver = getContentResolver();
2245 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2246 true, mWidgetObserver);
2247 }
2248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 @Override
2250 public boolean dispatchKeyEvent(KeyEvent event) {
2251 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2252 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002254 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002255 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002256 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002257 dumpState();
2258 return true;
2259 }
2260 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002261 }
2262 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2263 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002264 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 return true;
2266 }
2267 }
2268
2269 return super.dispatchKeyEvent(event);
2270 }
2271
Joe Onorato88ec0992009-11-19 13:16:06 -08002272 @Override
2273 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002274 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002275 if (mAppsCustomizeContent.getContentType() ==
2276 AppsCustomizePagedView.ContentType.Applications) {
2277 showWorkspace(true);
2278 } else {
2279 showOverviewMode(true);
2280 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002281 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002282 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002283 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002284 Folder openFolder = mWorkspace.getOpenFolder();
2285 if (openFolder.isEditingName()) {
2286 openFolder.dismissEditingName();
2287 } else {
2288 closeFolder();
2289 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002290 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002291 mWorkspace.exitWidgetResizeMode();
2292
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002293 // Back button is a no-op here, but give at least some feedback for the button press
2294 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002295 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002296 }
2297
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002299 * Re-listen when widgets are reset.
2300 */
2301 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002302 if (mAppWidgetHost != null) {
2303 mAppWidgetHost.startListening();
2304 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002305 }
2306
2307 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 * Launches the intent referred by the clicked shortcut.
2309 *
2310 * @param v The view representing the clicked shortcut.
2311 */
2312 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002313 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2314 // view has detached (it's possible for this to happen if the view is removed mid touch).
2315 if (v.getWindowToken() == null) {
2316 return;
2317 }
2318
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002319 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002320 return;
2321 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002322
Adam Cohen1697b792013-09-17 19:08:21 -07002323 if (v instanceof Workspace) {
2324 if (mWorkspace.isInOverviewMode()) {
2325 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002326 }
2327 return;
2328 }
2329
2330 if (v instanceof CellLayout) {
2331 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002332 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002333 }
2334 }
2335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002337 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002339 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2340 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002341
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002342 // Check for special shortcuts
2343 if (intent.getComponent() != null) {
2344 final String shortcutClass = intent.getComponent().getClassName();
2345
2346 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002347 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002348 return;
2349 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2350 MemoryDumpActivity.startDump(this);
2351 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002352 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2353 toggleShowWeightWatcher();
2354 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002355 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002356 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002357
2358 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002359 int[] pos = new int[2];
2360 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002361 intent.setSourceBounds(new Rect(pos[0], pos[1],
2362 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002363
2364 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002365
Daniel Sandlerff02d492013-08-05 02:12:05 -04002366 mStats.recordLaunch(intent, shortcut);
2367
Michael Jurkaddd62e92011-02-16 17:49:14 -08002368 if (success && v instanceof BubbleTextView) {
2369 mWaitingForResume = (BubbleTextView) v;
2370 mWaitingForResume.setStayPressed(true);
2371 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002373 if (v instanceof FolderIcon) {
2374 FolderIcon fi = (FolderIcon) v;
2375 handleFolderClick(fi);
2376 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002377 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002378 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002379 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002380 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002381 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 }
2384 }
2385
Michael Jurka0e260592010-06-30 17:07:39 -07002386 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002387 return false;
2388 }
2389
Michael Jurkaaf442092010-06-10 17:01:57 -07002390 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002391 * Event handler for the search button
2392 *
2393 * @param v The view that was clicked.
2394 */
2395 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002396 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2397
Amith Yamasania135ba82011-08-09 17:42:01 -07002398 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002399 }
2400
2401 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002402 * Event handler for the voice button
2403 *
2404 * @param v The view that was clicked.
2405 */
2406 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002407 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002408
Bjorn Bringertc459e522013-06-07 19:36:01 +01002409 startVoice();
2410 }
2411
2412 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002413 try {
2414 final SearchManager searchManager =
2415 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2416 ComponentName activityName = searchManager.getGlobalSearchActivity();
2417 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002418 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002419 if (activityName != null) {
2420 intent.setPackage(activityName.getPackageName());
2421 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002422 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002423 } catch (ActivityNotFoundException e) {
2424 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002425 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002426 startActivitySafely(null, intent, "onClickVoiceButton");
2427 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002428 }
2429
2430 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002431 * Event handler for the "grid" button that appears on the home screen, which
2432 * enters all apps mode.
2433 *
2434 * @param v The view that was clicked.
2435 */
2436 public void onClickAllAppsButton(View v) {
Winson Chunge7e97e62013-12-02 17:02:50 -08002437 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
Michael Jurka5130e402011-10-13 04:55:35 -07002438 }
2439
2440 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002441 // Provide the same haptic feedback that the system offers for virtual keys.
2442 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002443 }
2444
Adam Cohen61f560d2013-09-30 15:58:20 -07002445 public void performHapticFeedbackOnTouchDown(View v) {
2446 // Provide the same haptic feedback that the system offers for virtual keys.
2447 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2448 }
2449
2450 public View.OnTouchListener getHapticFeedbackTouchListener() {
2451 if (mHapticFeedbackTouchListener == null) {
2452 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2453 @Override
2454 public boolean onTouch(View v, MotionEvent event) {
2455 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2456 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2457 }
2458 return false;
2459 }
2460 };
2461 }
2462 return mHapticFeedbackTouchListener;
2463 }
2464
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002465 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002466 if (!DISABLE_MARKET_BUTTON) {
2467 if (mAppMarketIntent != null) {
2468 startActivitySafely(v, mAppMarketIntent, "app market");
2469 } else {
2470 Log.e(TAG, "Invalid app market intent.");
2471 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002472 }
2473 }
2474
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002475 /**
2476 * Called when the user stops interacting with the launcher.
2477 * This implies that the user is now on the homescreen and is not doing housekeeping.
2478 */
2479 protected void onInteractionEnd() {}
2480
2481 /**
2482 * Called when the user starts interacting with the launcher.
2483 * The possible interactions are:
2484 * - open all apps
2485 * - reorder an app shortcut, or a widget
2486 * - open the overview mode.
2487 * This is a good time to stop doing things that only make sense
2488 * when the user is on the homescreen and not doing housekeeping.
2489 */
2490 protected void onInteractionBegin() {}
2491
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002492 void startApplicationDetailsActivity(ComponentName componentName) {
2493 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002494 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2495 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002496 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002497 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002498 }
2499
Michael Jurka1e2f4652013-07-08 18:03:46 -07002500 // returns true if the activity was started
2501 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002502 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002503 // System applications cannot be installed. For now, show a toast explaining that.
2504 // We may give them the option of disabling apps this way.
2505 int messageId = R.string.uninstall_system_app_text;
2506 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002507 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002508 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002509 String packageName = componentName.getPackageName();
2510 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002511 Intent intent = new Intent(
2512 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002513 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2514 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002515 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002516 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002517 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002518 }
2519
Michael Jurka86a720e2012-05-09 11:23:23 -07002520 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002524 // Only launch using the new animation if the shortcut has not opted out (this is a
2525 // private contract between launcher and may be ignored in the future).
2526 boolean useLaunchAnimation = (v != null) &&
2527 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2528 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002529 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2530 v.getMeasuredWidth(), v.getMeasuredHeight());
2531
2532 startActivity(intent, opts.toBundle());
2533 } else {
2534 startActivity(intent);
2535 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002536 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 } catch (SecurityException e) {
2538 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002539 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002541 "or use the exported attribute for this activity. "
2542 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002544 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002546
Michael Jurka86a720e2012-05-09 11:23:23 -07002547 boolean startActivitySafely(View v, Intent intent, Object tag) {
2548 boolean success = false;
2549 try {
2550 success = startActivity(v, intent, tag);
2551 } catch (ActivityNotFoundException e) {
2552 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2553 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2554 }
2555 return success;
2556 }
2557
Adam Cohena9cf38f2011-05-02 15:36:58 -07002558 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002559 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002560 Folder openFolder = mWorkspace.getFolderForTag(info);
2561
2562 // If the folder info reports that the associated folder is open, then verify that
2563 // it is actually opened. There have been a few instances where this gets out of sync.
2564 if (info.opened && openFolder == null) {
2565 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002566 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002567 info.opened = false;
2568 }
2569
Adam Cohenfb91f302012-06-11 15:45:18 -07002570 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571 // Close any open folder
2572 closeFolder();
2573 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002574 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 } else {
2576 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 int folderScreen;
2578 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002579 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 // .. and close it
2581 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002582 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583 // Close any folder open on the current screen
2584 closeFolder();
2585 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002586 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 }
2588 }
2589 }
2590 }
2591
Adam Cohen268c4752012-06-06 17:47:33 -07002592 /**
2593 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2594 * in the DragLayer in the exact absolute location of the original FolderIcon.
2595 */
2596 private void copyFolderIconToImage(FolderIcon fi) {
2597 final int width = fi.getMeasuredWidth();
2598 final int height = fi.getMeasuredHeight();
2599
2600 // Lazy load ImageView, Bitmap and Canvas
2601 if (mFolderIconImageView == null) {
2602 mFolderIconImageView = new ImageView(this);
2603 }
2604 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2605 mFolderIconBitmap.getHeight() != height) {
2606 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2607 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2608 }
2609
2610 DragLayer.LayoutParams lp;
2611 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2612 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2613 } else {
2614 lp = new DragLayer.LayoutParams(width, height);
2615 }
2616
Adam Cohen307fe232012-08-16 17:55:58 -07002617 // The layout from which the folder is being opened may be scaled, adjust the starting
2618 // view size by this scale factor.
2619 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002620 lp.customPosition = true;
2621 lp.x = mRectForFolderAnimation.left;
2622 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002623 lp.width = (int) (scale * width);
2624 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002625
2626 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2627 fi.draw(mFolderIconCanvas);
2628 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002629 if (fi.getFolder() != null) {
2630 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2631 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002632 }
Adam Cohen268c4752012-06-06 17:47:33 -07002633 // Just in case this image view is still in the drag layer from a previous animation,
2634 // we remove it and re-add it.
2635 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2636 mDragLayer.removeView(mFolderIconImageView);
2637 }
2638 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002639 if (fi.getFolder() != null) {
2640 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002641 }
Adam Cohen268c4752012-06-06 17:47:33 -07002642 }
2643
Adam Cohen2801caf2011-05-13 20:57:39 -07002644 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002645 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002646 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2647 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2648 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2649
Adam Cohenc51934b2011-07-26 21:07:43 -07002650 FolderInfo info = (FolderInfo) fi.getTag();
2651 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2652 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002653 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2654 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002655 }
2656
Adam Cohen268c4752012-06-06 17:47:33 -07002657 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2658 copyFolderIconToImage(fi);
2659 fi.setVisibility(View.INVISIBLE);
2660
Michael Jurka2ecf9952012-06-18 12:52:28 -07002661 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002662 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002663 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2664 oa.start();
2665 }
2666
Adam Cohen268c4752012-06-06 17:47:33 -07002667 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002668 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002669 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2670 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2671 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2672
Adam Cohen268c4752012-06-06 17:47:33 -07002673 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002674
Adam Cohen268c4752012-06-06 17:47:33 -07002675 // We remove and re-draw the FolderIcon in-case it has changed
2676 mDragLayer.removeView(mFolderIconImageView);
2677 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002678 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002679 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002680 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002681 oa.addListener(new AnimatorListenerAdapter() {
2682 @Override
2683 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002684 if (cl != null) {
2685 cl.clearFolderLeaveBehind();
2686 // Remove the ImageView copy of the FolderIcon and make the original visible.
2687 mDragLayer.removeView(mFolderIconImageView);
2688 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002689 }
2690 }
2691 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002692 oa.start();
2693 }
2694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002696 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 * is animated relative to the specified View. If the View is null, no animation
2698 * is played.
2699 *
2700 * @param folderInfo The FolderInfo describing the folder to open.
2701 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002702 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002703 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002704 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002705
Adam Cohena9cf38f2011-05-02 15:36:58 -07002706 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707
Adam Cohen4554ee12011-08-03 16:13:21 -07002708 // Just verify that the folder hasn't already been added to the DragLayer.
2709 // There was a one-off crash where the folder had a parent already.
2710 if (folder.getParent() == null) {
2711 mDragLayer.addView(folder);
2712 mDragController.addDropTarget((DropTarget) folder);
2713 } else {
2714 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2715 folder.getParent() + ").");
2716 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002717 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002718 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002719
2720 // Notify the accessibility manager that this folder "window" has appeared and occluded
2721 // the workspace items
2722 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2723 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002724 }
2725
2726 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002727 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002728 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002729 if (folder.isEditingName()) {
2730 folder.dismissEditingName();
2731 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002732 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002733
2734 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002735 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002736 }
2737 }
2738
2739 void closeFolder(Folder folder) {
2740 folder.getInfo().opened = false;
2741
2742 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2743 if (parent != null) {
2744 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2745 shrinkAndFadeInFolderIcon(fi);
2746 }
2747 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002748
2749 // Notify the accessibility manager that this folder "window" has disappeard and no
2750 // longer occludeds the workspace items
2751 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002752 }
2753
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002754 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002755 if (!isDraggingEnabled()) return false;
2756 if (isWorkspaceLocked()) return false;
2757 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002758
Adam Cohen1697b792013-09-17 19:08:21 -07002759 if (v instanceof Workspace) {
2760 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002761 if (mWorkspace.enterOverviewMode()) {
2762 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2763 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2764 return true;
2765 } else {
2766 return false;
2767 }
Adam Cohen1697b792013-09-17 19:08:21 -07002768 }
Adam Cohen1697b792013-09-17 19:08:21 -07002769 }
2770
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002772 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002773 }
2774
Michael Jurka0280c3b2010-09-17 15:00:07 -07002775 resetAddInfo();
2776 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002777 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002778 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779 return true;
2780 }
2781
Winson Chung3d503fb2011-07-13 17:25:49 -07002782 // The hotseat touch handling does not go through Workspace, and we always allow long press
2783 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002784 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002785 final boolean inHotseat = isHotseatLayout(v);
2786 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002787 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002788 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002789 // User long pressed on empty space
2790 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2791 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2792 // Disabling reordering until we sort out some issues.
2793 if (mWorkspace.isInOverviewMode()) {
2794 mWorkspace.startReordering(v);
2795 } else {
2796 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002798 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002799 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2800 mHotseat.getOrderInHotseat(
2801 longClickCellInfo.cellX,
2802 longClickCellInfo.cellY));
2803 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002804 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002805 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002806 }
2807 }
2808 }
2809 return true;
2810 }
2811
Winson Chung3d503fb2011-07-13 17:25:49 -07002812 boolean isHotseatLayout(View layout) {
2813 return mHotseat != null && layout != null &&
2814 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2815 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002816
Winson Chung3d503fb2011-07-13 17:25:49 -07002817 /**
2818 * Returns the CellLayout of the specified container at the specified screen.
2819 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002820 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002821 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2822 if (mHotseat != null) {
2823 return mHotseat.getLayout();
2824 } else {
2825 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002826 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002827 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002828 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002829 }
2830 }
2831
Daniel Sandler843e8602010-06-07 14:59:01 -04002832 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002833 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002834 }
2835
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002836 /**
2837 * Helper method for the cameraZoomIn/cameraZoomOut animations
2838 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002839 * @param scaleFactor The scale factor used for the zoom
2840 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002841 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002842 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002843 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002844 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002845
Craig Mautner360310b2012-10-26 15:13:08 -07002846 private void setWorkspaceBackground(boolean workspace) {
2847 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002848 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002849 }
2850
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002851 void updateWallpaperVisibility(boolean visible) {
2852 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2853 int curflags = getWindow().getAttributes().flags
2854 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2855 if (wpflags != curflags) {
2856 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2857 }
Craig Mautner360310b2012-10-26 15:13:08 -07002858 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002859 }
2860
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002861 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2862 if (v instanceof LauncherTransitionable) {
2863 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2864 }
2865 }
2866
Michael Jurkabed61d22012-02-14 22:51:29 -08002867 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2868 if (v instanceof LauncherTransitionable) {
2869 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2870 }
Winson Chung70442722012-02-10 15:43:22 -08002871
2872 // Update the workspace transition step as well
2873 dispatchOnLauncherTransitionStep(v, 0f);
2874 }
2875
2876 private void dispatchOnLauncherTransitionStep(View v, float t) {
2877 if (v instanceof LauncherTransitionable) {
2878 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2879 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002880 }
2881
2882 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2883 if (v instanceof LauncherTransitionable) {
2884 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2885 }
Winson Chung70442722012-02-10 15:43:22 -08002886
2887 // Update the workspace transition step as well
2888 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002889 }
2890
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002891 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002892 * Things to test when changing the following seven functions.
2893 * - Home from workspace
2894 * - from center screen
2895 * - from other screens
2896 * - Home from all apps
2897 * - from center screen
2898 * - from other screens
2899 * - Back from all apps
2900 * - from center screen
2901 * - from other screens
2902 * - Launch app from workspace and quit
2903 * - with back
2904 * - with home
2905 * - Launch app from all apps and quit
2906 * - with back
2907 * - with home
2908 * - Go to a screen that's not the default, then all
2909 * apps, and launch and app, and go back
2910 * - with back
2911 * -with home
2912 * - On workspace, long press power and go back
2913 * - with back
2914 * - with home
2915 * - On all apps, long press power and go back
2916 * - with back
2917 * - with home
2918 * - On workspace, power off
2919 * - On all apps, power off
2920 * - Launch an app and turn off the screen while in that app
2921 * - Go back with home key
2922 * - Go back with back key TODO: make this not go to workspace
2923 * - From all apps
2924 * - From workspace
2925 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2926 * - From all apps
2927 * - From the center workspace
2928 * - From another workspace
2929 */
2930
2931 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002932 * Zoom the camera out from the workspace to reveal 'toView'.
2933 * Assumes that the view to show is anchored at either the very top or very bottom
2934 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002935 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002936 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002937 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2938 showAppsCustomizeHelper(animated, springLoaded, contentType);
2939 }
2940 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2941 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002942 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002943 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002944 mStateAnimation.cancel();
2945 mStateAnimation = null;
2946 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002947 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002948
Winson Chungf0ea4d32011-06-06 14:27:16 -07002949 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2950 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2951 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002952 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002953 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002954 final int startDelay =
2955 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002956
Michael Jurkab3e22d92011-10-31 15:58:33 -07002957 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002958
Michael Jurkad74c9842011-07-10 12:44:21 -07002959 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002960 Animator workspaceAnim =
2961 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08002962 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08002963 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
2964 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07002965 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2966 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002967
2968 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002969 toView.setScaleX(scale);
2970 toView.setScaleY(scale);
2971 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2972 scaleAnim.
2973 scaleX(1f).scaleY(1f).
2974 setDuration(duration).
2975 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002976
Winson Chungf0ea4d32011-06-06 14:27:16 -07002977 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002978 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002979 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002980 .ofFloat(toView, "alpha", 0f, 1f)
2981 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002982 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002983 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2984 @Override
2985 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002986 if (animation == null) {
2987 throw new RuntimeException("animation is null");
2988 }
Winson Chung70442722012-02-10 15:43:22 -08002989 float t = (Float) animation.getAnimatedValue();
2990 dispatchOnLauncherTransitionStep(fromView, t);
2991 dispatchOnLauncherTransitionStep(toView, t);
2992 }
2993 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002994
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002995 // toView should appear right at the end of the workspace shrink
2996 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002997 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002998 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002999 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003000
3001 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003002 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003003 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003004 // Prepare the position
3005 toView.setTranslationX(0.0f);
3006 toView.setTranslationY(0.0f);
3007 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003008 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003009 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003010 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003011 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003012 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3013 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003014
Winson Chungc7d2b602012-05-16 17:02:20 -07003015 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003016 if (mSearchDropTargetBar != null) {
3017 mSearchDropTargetBar.hideSearchBar(false);
3018 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003019 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003020 });
3021
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003022 if (workspaceAnim != null) {
3023 mStateAnimation.play(workspaceAnim);
3024 }
Michael Jurka1899a362011-11-03 13:50:45 -07003025
3026 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003027
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003028 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3029 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003030
3031 // If any of the objects being animated haven't been measured/laid out
3032 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003033 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003034 (mWorkspace.getMeasuredWidth() == 0) ||
3035 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003036 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003037 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003038
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003039 final AnimatorSet stateAnimation = mStateAnimation;
3040 final Runnable startAnimRunnable = new Runnable() {
3041 public void run() {
3042 // Check that mStateAnimation hasn't changed while
3043 // we waited for a layout/draw pass
3044 if (mStateAnimation != stateAnimation)
3045 return;
3046 setPivotsForZoom(toView, scale);
3047 dispatchOnLauncherTransitionStart(fromView, animated, false);
3048 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003049 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003050 }
3051 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003052 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003053 final ViewTreeObserver observer = toView.getViewTreeObserver();
3054 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3055 public void onGlobalLayout() {
3056 startAnimRunnable.run();
3057 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3058 }
3059 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003060 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003061 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003062 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003063 } else {
3064 toView.setTranslationX(0.0f);
3065 toView.setTranslationY(0.0f);
3066 toView.setScaleX(1.0f);
3067 toView.setScaleY(1.0f);
3068 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003069 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003070
Daniel Sandlere4f98912013-06-25 15:13:26 -04003071 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003072 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003073 if (mSearchDropTargetBar != null) {
3074 mSearchDropTargetBar.hideSearchBar(false);
3075 }
Michael Jurkaabded662011-03-04 12:06:57 -08003076 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003077 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003078 dispatchOnLauncherTransitionStart(fromView, animated, false);
3079 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003080 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003081 dispatchOnLauncherTransitionStart(toView, animated, false);
3082 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003083 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003084 }
3085
3086 /**
3087 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003088 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003089 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003090 */
Adam Cohened307df2013-10-02 09:37:31 -07003091 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003092 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003093
Michael Jurkab3e22d92011-10-31 15:58:33 -07003094 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003095 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003096 mStateAnimation.cancel();
3097 mStateAnimation = null;
3098 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003099 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003100
Winson Chungf0ea4d32011-06-06 14:27:16 -07003101 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003102 final int fadeOutDuration =
3103 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003104 final float scaleFactor = (float)
3105 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3106 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003107 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003108 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003109 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003110 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3111 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003112 toState, animated, stagger, -1);
3113 } else if (toState == Workspace.State.SPRING_LOADED ||
3114 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003115 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003116 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003117 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003118
Michael Jurkab3e22d92011-10-31 15:58:33 -07003119 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003120 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003121 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003122 final LauncherViewPropertyAnimator scaleAnim =
3123 new LauncherViewPropertyAnimator(fromView);
3124 scaleAnim.
3125 scaleX(scaleFactor).scaleY(scaleFactor).
3126 setDuration(duration).
3127 setInterpolator(new Workspace.ZoomInInterpolator());
3128
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003129 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003130 .ofFloat(fromView, "alpha", 1f, 0f)
3131 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003132 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003133 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3134 @Override
3135 public void onAnimationUpdate(ValueAnimator animation) {
3136 float t = 1f - (Float) animation.getAnimatedValue();
3137 dispatchOnLauncherTransitionStep(fromView, t);
3138 dispatchOnLauncherTransitionStep(toView, t);
3139 }
3140 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003141
Michael Jurka2ecf9952012-06-18 12:52:28 -07003142 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003143
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003144 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3145 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003146 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003147
3148 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003149 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003150 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003151 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003152 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3153 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003154 if (onCompleteRunnable != null) {
3155 onCompleteRunnable.run();
3156 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003157 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003158 }
3159 });
3160
Winson Chungf0ea4d32011-06-06 14:27:16 -07003161 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003162 if (workspaceAnim != null) {
3163 mStateAnimation.play(workspaceAnim);
3164 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003165 dispatchOnLauncherTransitionStart(fromView, animated, true);
3166 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003167 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003168 } else {
3169 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003170 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003171 dispatchOnLauncherTransitionStart(fromView, animated, true);
3172 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003173 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003174 dispatchOnLauncherTransitionStart(toView, animated, true);
3175 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003176 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003177 }
3178
Michael Jurkae326f182011-11-21 14:05:46 -08003179 @Override
3180 public void onTrimMemory(int level) {
3181 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003182 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003183 mAppsCustomizeTabHost.onTrimMemory();
3184 }
3185 }
3186
Adam Cohened307df2013-10-02 09:37:31 -07003187 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003188 showWorkspace(animated, null);
3189 }
3190
Adam Cohened307df2013-10-02 09:37:31 -07003191 protected void showWorkspace() {
3192 showWorkspace(true);
3193 }
3194
Adam Cohened66b2b2012-01-23 17:28:51 -08003195 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003196 if (mWorkspace.isInOverviewMode()) {
3197 mWorkspace.exitOverviewMode(animated);
3198 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003199 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003200 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003201 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003202 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003203
Winson Chungc7d2b602012-05-16 17:02:20 -07003204 // Show the search bar (only animate if we were showing the drop target bar in spring
3205 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003206 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003207 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003208 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003209
Michael Jurkab3e22d92011-10-31 15:58:33 -07003210 // Set focus to the AppsCustomize button
3211 if (mAllAppsButton != null) {
3212 mAllAppsButton.requestFocus();
3213 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003214 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003215
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003216 // Change the state *after* we've called all the transition code
3217 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003218
Winson Chung5fb63472011-02-02 17:03:37 -08003219 // Resume the auto-advance of widgets
3220 mUserPresent = true;
3221 updateRunning();
3222
alanv1d4fde62012-10-17 13:15:47 -07003223 // Send an accessibility event to announce the context change
3224 getWindow().getDecorView()
3225 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003226
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003227 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003228 }
3229
Adam Cohened307df2013-10-02 09:37:31 -07003230 void showOverviewMode(boolean animated) {
3231 mWorkspace.setVisibility(View.VISIBLE);
3232 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3233 mState = State.WORKSPACE;
3234 onWorkspaceShown(animated);
3235 }
3236
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003237 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003238 }
3239
Winson Chung82963d52013-10-09 11:20:57 -07003240 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3241 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003242 if (mState != State.WORKSPACE) return;
3243
Winson Chung82963d52013-10-09 11:20:57 -07003244 if (resetPageToZero) {
3245 mAppsCustomizeTabHost.reset();
3246 }
Winson Chungc58497e2013-09-03 17:48:37 -07003247 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003248 mAppsCustomizeTabHost.requestFocus();
3249
Michael Jurkab3e22d92011-10-31 15:58:33 -07003250 // Change the state *after* we've called all the transition code
3251 mState = State.APPS_CUSTOMIZE;
3252
3253 // Pause the auto-advance of widgets until we are out of AllApps
3254 mUserPresent = false;
3255 updateRunning();
3256 closeFolder();
3257
3258 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003259 getWindow().getDecorView()
3260 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003261 }
3262
Adam Cohen7777d962011-08-18 18:58:38 -07003263 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003264 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003265 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003266 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003267 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003268 }
Adam Cohen7777d962011-08-18 18:58:38 -07003269
Adam Cohenad4e15c2013-10-17 16:21:35 -07003270 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003271 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003272 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3273
Winson Chunge7a03942011-08-05 15:05:12 -07003274 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003275 @Override
3276 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003277 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003278 // Before we show workspace, hide all apps again because
3279 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3280 // clean up our state transition functions
3281 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003282 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003283 } else {
3284 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003285 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003286 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003287 }, delay);
3288
Winson Chung557d6ed2011-07-08 15:34:52 -07003289 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003290
Michael Jurkad3ef3062010-11-23 16:23:58 -08003291 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003292 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003293 final boolean animated = true;
3294 final boolean springLoaded = true;
3295 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003296 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003297 }
3298 // Otherwise, we are not in spring loaded mode, so don't do anything.
3299 }
3300
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301 void lockAllApps() {
3302 // TODO
3303 }
3304
3305 void unlockAllApps() {
3306 // TODO
3307 }
3308
Winson Chungf0ea4d32011-06-06 14:27:16 -07003309 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003310 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003311 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003312 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003313 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003314 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003315 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003316 int duration = 0;
3317 if (mSearchDropTargetBar != null) {
3318 duration = mSearchDropTargetBar.getTransitionInDuration();
3319 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003320 mHotseat.animate().alpha(1f).setDuration(duration);
3321 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003322 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003323 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003324 }
3325 }
3326 }
3327
3328 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003329 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003330 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003331 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003332 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003333 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003334 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003335 int duration = 0;
3336 if (mSearchDropTargetBar != null) {
3337 duration = mSearchDropTargetBar.getTransitionOutDuration();
3338 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003339 mHotseat.animate().alpha(0f).setDuration(duration);
3340 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003341 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003342 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003343 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003344 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003345 }
3346
Patrick Dubroy5f445422011-02-18 14:35:21 -08003347 /**
3348 * Add an item from all apps or customize onto the given workspace screen.
3349 * If layout is null, add to the current screen.
3350 */
3351 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003352 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003353 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003354 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003355 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003356
Winson Chungdff8ebb2011-09-08 17:25:31 -07003357 /** Maps the current orientation to an index for referencing orientation correct global icons */
3358 private int getCurrentOrientationIndexForGlobalIcons() {
3359 // default - 0, landscape - 1
3360 switch (getResources().getConfiguration().orientation) {
3361 case Configuration.ORIENTATION_LANDSCAPE:
3362 return 1;
3363 default:
3364 return 0;
3365 }
3366 }
3367
Michael Jurkaae65ee32012-04-30 11:45:54 -07003368 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003369 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003370 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003371 // Look for the toolbar icon specified in the activity meta-data
3372 Bundle metaData = packageManager.getActivityInfo(
3373 activityName, PackageManager.GET_META_DATA).metaData;
3374 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003375 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003376 if (iconResId != 0) {
3377 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003378 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003379 }
3380 }
3381 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003382 // This can happen if the activity defines an invalid drawable
3383 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3384 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003385 } catch (Resources.NotFoundException nfe) {
3386 // This can happen if the activity defines an invalid drawable
3387 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3388 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003389 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003390 return null;
3391 }
3392
3393 // if successful in getting icon, return it; otherwise, set button to use default drawable
3394 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003395 int buttonId, ComponentName activityName, int fallbackDrawableId,
3396 String toolbarResourceName) {
3397 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003398 Resources r = getResources();
3399 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3400 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003401
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003402 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003403 // If we were unable to find the icon via the meta-data, use a generic one
3404 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003405 toolbarIcon = r.getDrawable(fallbackDrawableId);
3406 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003407 if (button != null) {
3408 button.setCompoundDrawables(toolbarIcon, null, null, null);
3409 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003410 return null;
3411 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003412 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003413 if (button != null) {
3414 button.setCompoundDrawables(toolbarIcon, null, null, null);
3415 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003416 return toolbarIcon.getConstantState();
3417 }
3418 }
3419
3420 // if successful in getting icon, return it; otherwise, set button to use default drawable
3421 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003422 int buttonId, ComponentName activityName, int fallbackDrawableId,
3423 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003424 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003425 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003426
Michael Jurka19e0fc52011-07-22 18:00:21 -07003427 if (button != null) {
3428 // If we were unable to find the icon via the meta-data, use a
3429 // generic one
3430 if (toolbarIcon == null) {
3431 button.setImageResource(fallbackDrawableId);
3432 } else {
3433 button.setImageDrawable(toolbarIcon);
3434 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003435 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003436
3437 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3438
Michael Jurka0423dcf2010-10-05 14:56:18 -07003439 }
3440
Winson Chung1b884092012-06-08 17:13:02 -07003441 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003442 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003443 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003444 }
3445
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003446 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003447 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003448 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003449 }
3450
Winson Chungbb185bd2011-11-21 12:31:42 -08003451 private void invalidatePressedFocusedStates(View container, View button) {
3452 if (container instanceof HolographicLinearLayout) {
3453 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3454 layout.invalidatePressedFocusedStates();
3455 } else if (button instanceof HolographicImageView) {
3456 HolographicImageView view = (HolographicImageView) button;
3457 view.invalidatePressedFocusedStates();
3458 }
3459 }
3460
Cristina Stancu476493b2013-08-07 17:20:14 +01003461 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003462 if (mQsb == null) {
3463 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3464 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003465 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003466 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003467 }
3468
3469 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003470 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003471 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003472 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003473 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003474
Winson Chung1cad91e2011-05-25 17:41:01 -07003475 final SearchManager searchManager =
3476 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3477 ComponentName activityName = searchManager.getGlobalSearchActivity();
3478 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003479 int coi = getCurrentOrientationIndexForGlobalIcons();
3480 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003481 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3482 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3483 if (sGlobalSearchIcon[coi] == null) {
3484 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3485 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3486 TOOLBAR_ICON_METADATA_NAME);
3487 }
3488
Winson Chungc51db6a2011-10-05 11:44:49 -07003489 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3490 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003491 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003492 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003493 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003494 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003495 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3496 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003497 if (searchButton != null) searchButton.setVisibility(View.GONE);
3498 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003499 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003500 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003501 }
3502 }
3503
Cristina Stancu476493b2013-08-07 17:20:14 +01003504 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003505 final View searchButtonContainer = findViewById(R.id.search_button_container);
3506 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003507 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003508 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003509 }
3510
Cristina Stancu476493b2013-08-07 17:20:14 +01003511 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003512 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003513 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003514
Winson Chungc51db6a2011-10-05 11:44:49 -07003515 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003516 final SearchManager searchManager =
3517 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3518 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3519
3520 ComponentName activityName = null;
3521 if (globalSearchActivity != null) {
3522 // Check if the global search activity handles voice search
3523 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3524 intent.setPackage(globalSearchActivity.getPackageName());
3525 activityName = intent.resolveActivity(getPackageManager());
3526 }
3527
3528 if (activityName == null) {
3529 // Fallback: check if an activity other than the global search activity
3530 // resolves this
3531 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3532 activityName = intent.resolveActivity(getPackageManager());
3533 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003534 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003535 int coi = getCurrentOrientationIndexForGlobalIcons();
3536 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003537 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3538 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3539 if (sVoiceSearchIcon[coi] == null) {
3540 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3541 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3542 TOOLBAR_ICON_METADATA_NAME);
3543 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003544 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003545 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003546 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003547 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003548 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003549 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003550 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003551 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003552 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003553 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003554 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003555 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003556
Cristina Stancu476493b2013-08-07 17:20:14 +01003557 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003558 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3559 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003560 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003561 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003562 }
3563
Winson Chung5841efa2013-09-30 18:06:44 -07003564 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003565 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3566 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003567 boolean visible = !forceDisableVoiceButtonProxy &&
3568 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003569 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003570 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003571 }
3572 }
Winson Chung5841efa2013-09-30 18:06:44 -07003573
3574 /**
3575 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3576 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3577 */
3578 public void disableVoiceButtonProxy(boolean disabled) {
3579 updateVoiceButtonProxyVisible(disabled);
3580 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003581 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003582 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003583 */
3584 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003585 if (!DISABLE_MARKET_BUTTON) {
3586 final View marketButton = findViewById(R.id.market_button);
3587 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3588 // Find the app market activity by resolving an intent.
3589 // (If multiple app markets are installed, it will return the ResolverActivity.)
3590 ComponentName activityName = intent.resolveActivity(getPackageManager());
3591 if (activityName != null) {
3592 int coi = getCurrentOrientationIndexForGlobalIcons();
3593 mAppMarketIntent = intent;
3594 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3595 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3596 TOOLBAR_ICON_METADATA_NAME);
3597 marketButton.setVisibility(View.VISIBLE);
3598 } else {
3599 // We should hide and disable the view so that we don't try and restore the visibility
3600 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3601 marketButton.setVisibility(View.GONE);
3602 marketButton.setEnabled(false);
3603 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003604 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003605 }
3606
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003607 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003608 if (!DISABLE_MARKET_BUTTON) {
3609 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3610 Resources r = getResources();
3611 Drawable marketIconDrawable = d.newDrawable(r);
3612 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3613 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3614 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003615
Winson Chungd64a6662013-09-30 11:06:59 -07003616 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3617 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003618 }
3619
Michael Jurkad7c28052012-04-27 15:43:36 -07003620 @Override
3621 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003622 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003623 final List<CharSequence> text = event.getText();
3624 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003625 // Populate event with a fake title based on the current state.
3626 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003627 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003628 } else {
3629 text.add(getString(R.string.all_apps_home_button_label));
3630 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003631 return result;
3632 }
3633
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003634 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003635 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003636 */
3637 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3638 @Override
3639 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003640 closeSystemDialogs();
3641 }
3642 }
3643
3644 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003645 * Receives notifications whenever the appwidgets are reset.
3646 */
3647 private class AppWidgetResetObserver extends ContentObserver {
3648 public AppWidgetResetObserver() {
3649 super(new Handler());
3650 }
3651
3652 @Override
3653 public void onChange(boolean selfChange) {
3654 onAppWidgetReset();
3655 }
3656 }
3657
3658 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003659 * If the activity is currently paused, signal that we need to run the passed Runnable
3660 * in onResume.
3661 *
3662 * This needs to be called from incoming places where resources might have been loaded
3663 * while we are paused. That is becaues the Configuration might be wrong
3664 * when we're not running, and if it comes back to what it was when we
3665 * were paused, we are not restarted.
3666 *
3667 * Implementation of the method from LauncherModel.Callbacks.
3668 *
3669 * @return true if we are currently paused. The caller might be able to
3670 * skip some work in that case since we will come back again.
3671 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003672 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003673 if (mPaused) {
3674 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003675 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003676 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003677 }
3678 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003679 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003680 return true;
3681 } else {
3682 return false;
3683 }
3684 }
3685
Michael Jurkac402cd92013-05-20 15:49:32 +02003686 private boolean waitUntilResume(Runnable run) {
3687 return waitUntilResume(run, false);
3688 }
3689
Michael Jurka1e2f4652013-07-08 18:03:46 -07003690 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003691 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003692 }
3693
Michael Jurka7607c2f2013-04-03 14:33:19 -07003694 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003695 * If the activity is currently paused, signal that we need to re-run the loader
3696 * in onResume.
3697 *
3698 * This needs to be called from incoming places where resources might have been loaded
3699 * while we are paused. That is becaues the Configuration might be wrong
3700 * when we're not running, and if it comes back to what it was when we
3701 * were paused, we are not restarted.
3702 *
3703 * Implementation of the method from LauncherModel.Callbacks.
3704 *
3705 * @return true if we are currently paused. The caller might be able to
3706 * skip some work in that case since we will come back again.
3707 */
3708 public boolean setLoadOnResume() {
3709 if (mPaused) {
3710 Log.i(TAG, "setLoadOnResume");
3711 mOnResumeNeedsLoad = true;
3712 return true;
3713 } else {
3714 return false;
3715 }
3716 }
3717
3718 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003720 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003721 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003722 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003723 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003724 } else {
3725 return SCREEN_COUNT / 2;
3726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003728
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729 /**
3730 * Refreshes the shortcuts shown on the workspace.
3731 *
3732 * Implementation of the method from LauncherModel.Callbacks.
3733 */
3734 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003735 // If we're starting binding all over again, clear any bind calls we'd postponed in
3736 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3737 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003738 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003739
Winson Chungd64d1762013-08-20 14:37:16 -07003740 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003741 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003742 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003743
Michael Jurka05bf644e2011-11-30 20:28:53 -08003744 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003745 if (mHotseat != null) {
3746 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003747 }
3748 }
3749
Adam Cohendcd297f2013-06-18 13:13:40 -07003750 @Override
3751 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003752 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003753
Adam Cohen5084cba2013-09-03 12:01:16 -07003754 // If there are no screens, we need to have an empty screen
3755 if (orderedScreenIds.size() == 0) {
3756 mWorkspace.addExtraEmptyScreen();
3757 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003758
3759 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003760 // setCurrentPage() so ensure that all pages are added before calling this).
3761 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003762 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3763 mWorkspace.createCustomContentPage();
3764 }
Winson Chung64359a52013-07-08 17:17:08 -07003765 }
3766
3767 @Override
3768 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003769 // Log to disk
3770 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3771 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3772 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003773 int count = orderedScreenIds.size();
3774 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003775 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003776 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003777 }
3778
Adam Cohen39a06042013-07-19 14:30:12 -07003779 private boolean shouldShowWeightWatcher() {
3780 String spKey = LauncherAppState.getSharedPreferencesKey();
3781 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003782 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003783
3784 return show;
3785 }
3786
3787 private void toggleShowWeightWatcher() {
3788 String spKey = LauncherAppState.getSharedPreferencesKey();
3789 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3790 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3791
3792 show = !show;
3793
3794 SharedPreferences.Editor editor = sp.edit();
3795 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3796 editor.commit();
3797
3798 if (mWeightWatcher != null) {
3799 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3800 }
3801 }
3802
Winson Chungd64d1762013-08-20 14:37:16 -07003803 public void bindAppsAdded(final ArrayList<Long> newScreens,
3804 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003805 final ArrayList<ItemInfo> addAnimated,
3806 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003807 Runnable r = new Runnable() {
3808 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003809 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003810 }
3811 };
3812 if (waitUntilResume(r)) {
3813 return;
3814 }
3815
Winson Chungd64d1762013-08-20 14:37:16 -07003816 // Add the new screens
3817 bindAddScreens(newScreens);
3818
3819 // We add the items without animation on non-visible pages, and with
3820 // animations on the new page (which we will try and snap to).
3821 if (!addNotAnimated.isEmpty()) {
3822 bindItems(addNotAnimated, 0,
3823 addNotAnimated.size(), false);
3824 }
3825 if (!addAnimated.isEmpty()) {
3826 bindItems(addAnimated, 0,
3827 addAnimated.size(), true);
3828 }
Winson Chungc58497e2013-09-03 17:48:37 -07003829
Winson Chung87412982013-10-03 18:34:14 -07003830 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003831 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003832
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003833 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003834 addedApps != null && mAppsCustomizeContent != null) {
3835 mAppsCustomizeContent.addApps(addedApps);
3836 }
Winson Chungd64d1762013-08-20 14:37:16 -07003837 }
3838
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003839 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003840 * Bind the items start-end from the list.
3841 *
3842 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003843 */
Winson Chung64359a52013-07-08 17:17:08 -07003844 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3845 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003846 Runnable r = new Runnable() {
3847 public void run() {
3848 bindItems(shortcuts, start, end, forceAnimateIcons);
3849 }
3850 };
3851 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003852 return;
3853 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003854
Winson Chungf0c6ae02012-03-21 16:10:31 -07003855 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003856 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3857 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003858 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003859 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003860 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003861 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003862 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003863
3864 // Short circuit if we are loading dock items for a configuration which has no dock
3865 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3866 mHotseat == null) {
3867 continue;
3868 }
3869
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870 switch (item.itemType) {
3871 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3872 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003873 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003874 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003875
Winson Chung64359a52013-07-08 17:17:08 -07003876 /*
3877 * TODO: FIX collision case
3878 */
Winson Chungce376632013-07-11 16:12:41 -07003879 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3880 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3881 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3882 throw new RuntimeException("OCCUPIED");
3883 }
Winson Chung64359a52013-07-08 17:17:08 -07003884 }
3885
Adam Cohendcd297f2013-06-18 13:13:40 -07003886 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3887 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003888 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003889 // Animate all the applications up now
3890 shortcut.setAlpha(0f);
3891 shortcut.setScaleX(0f);
3892 shortcut.setScaleY(0f);
3893 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003894 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003897 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003898 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003899 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003900 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003901 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3902 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003903 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003904 default:
3905 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003907 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003908
Winson Chung997a9232013-07-24 15:33:46 -07003909 if (animateIcons) {
3910 // Animate to the correct page
3911 if (newShortcutsScreenId > -1) {
3912 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003913 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003914 final Runnable startBounceAnimRunnable = new Runnable() {
3915 public void run() {
3916 anim.playTogether(bounceAnims);
3917 anim.start();
3918 }
3919 };
Winson Chung997a9232013-07-24 15:33:46 -07003920 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003921 // We post the animation slightly delayed to prevent slowdowns
3922 // when we are loading right after we return to launcher.
3923 mWorkspace.postDelayed(new Runnable() {
3924 public void run() {
3925 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003926 mWorkspace.postDelayed(startBounceAnimRunnable,
3927 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003928 }
3929 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003930 } else {
3931 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003932 }
3933 }
Winson Chung64359a52013-07-08 17:17:08 -07003934 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003936 }
3937
Joe Onorato9c1289c2009-08-17 11:03:03 -04003938 /**
3939 * Implementation of the method from LauncherModel.Callbacks.
3940 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003941 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003942 Runnable r = new Runnable() {
3943 public void run() {
3944 bindFolders(folders);
3945 }
3946 };
3947 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003948 return;
3949 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003950 sFolders.clear();
3951 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003953
3954 /**
3955 * Add the views for a widget to the workspace.
3956 *
3957 * Implementation of the method from LauncherModel.Callbacks.
3958 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003959 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003960 Runnable r = new Runnable() {
3961 public void run() {
3962 bindAppWidget(item);
3963 }
3964 };
3965 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003966 return;
3967 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003968
Daniel Sandler843e8602010-06-07 14:59:01 -04003969 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3970 if (DEBUG_WIDGETS) {
3971 Log.d(TAG, "bindAppWidget: " + item);
3972 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973 final Workspace workspace = mWorkspace;
3974
3975 final int appWidgetId = item.appWidgetId;
3976 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003977 if (DEBUG_WIDGETS) {
3978 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3979 }
3980
Joe Onorato9c1289c2009-08-17 11:03:03 -04003981 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3982
Joe Onorato9c1289c2009-08-17 11:03:03 -04003983 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003984 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003985
Adam Cohendcd297f2013-06-18 13:13:40 -07003986 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003988 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3989
Joe Onorato9c1289c2009-08-17 11:03:03 -04003990 workspace.requestLayout();
3991
Daniel Sandler843e8602010-06-07 14:59:01 -04003992 if (DEBUG_WIDGETS) {
3993 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3994 + (SystemClock.uptimeMillis()-start) + "ms");
3995 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003996 }
3997
Adam Cohen1462de32012-07-24 22:34:36 -07003998 public void onPageBoundSynchronously(int page) {
3999 mSynchronouslyBoundPages.add(page);
4000 }
4001
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002 /**
4003 * Callback saying that there aren't any more items to bind.
4004 *
4005 * Implementation of the method from LauncherModel.Callbacks.
4006 */
Adam Cohene25af792013-06-06 23:08:25 -07004007 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004008 Runnable r = new Runnable() {
4009 public void run() {
4010 finishBindingItems(upgradePath);
4011 }
4012 };
4013 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004014 return;
4015 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004016 if (mSavedState != null) {
4017 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004018 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004019 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004020 mSavedState = null;
4021 }
4022
Adam Cohen1462de32012-07-24 22:34:36 -07004023 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004024
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004025 // If we received the result of any pending adds while the loader was running (e.g. the
4026 // widget configuration forced an orientation change), process them now.
4027 for (int i = 0; i < sPendingAddList.size(); i++) {
4028 completeAdd(sPendingAddList.get(i));
4029 }
4030 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004031
Winson Chungc51db6a2011-10-05 11:44:49 -07004032 // Update the market app icon as necessary (the other icons will be managed in response to
4033 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004034 if (!DISABLE_MARKET_BUTTON) {
4035 updateAppMarketIcon();
4036 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004037
Winson Chungf0c6ae02012-03-21 16:10:31 -07004038 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004039 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004040 mWorkspace.getUniqueComponents(true, null);
4041 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004042 }
Adam Cohen99894d92013-06-14 11:22:59 -07004043
Adam Cohen66a01fd2013-06-11 12:48:00 -07004044 mWorkspace.post(new Runnable() {
4045 @Override
4046 public void run() {
4047 onFinishBindingItems();
4048 }
4049 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07004050 }
4051
Winson Chunga0b7e862013-09-05 16:03:15 -07004052 public boolean isAllAppsButtonRank(int rank) {
4053 if (mHotseat != null) {
4054 return mHotseat.isAllAppsButtonRank(rank);
4055 }
4056 return false;
4057 }
4058
Winson Chunga2413752012-04-03 14:22:34 -07004059 private boolean canRunNewAppsAnimation() {
4060 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4061 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4062 }
4063
Winson Chungc9168342013-06-26 14:54:55 -07004064 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4065 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4066 PropertyValuesHolder.ofFloat("alpha", 1f),
4067 PropertyValuesHolder.ofFloat("scaleX", 1f),
4068 PropertyValuesHolder.ofFloat("scaleY", 1f));
4069 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4070 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4071 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4072 return bounceAnim;
4073 }
4074
Adam Cohen27772732013-11-11 14:00:56 +00004075 public boolean useVerticalBarLayout() {
4076 return LauncherAppState.getInstance().getDynamicGrid().
4077 getDeviceProfile().isVerticalBarLayout();
4078 }
4079
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004080 protected Rect getSearchBarBounds() {
4081 return LauncherAppState.getInstance().getDynamicGrid().
4082 getDeviceProfile().getSearchBarBounds();
4083 }
4084
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004085 @Override
4086 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004087 boolean searchVisible = updateGlobalSearchIcon();
4088 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004089 if (mSearchDropTargetBar != null) {
4090 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4091 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004092 }
4093
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004094 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004095 * Add the icons for all apps.
4096 *
4097 * Implementation of the method from LauncherModel.Callbacks.
4098 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004099 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004100 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004101 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4102 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4103 getHotseat().addAllAppsFolder(mIconCache, apps,
4104 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4105 }
4106 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004107 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004108 if (mAppsCustomizeContent != null) {
4109 mAppsCustomizeContent.onPackagesUpdated(
4110 LauncherModel.getSortedWidgetsAndShortcuts(this));
4111 }
Winson Chungc58497e2013-09-03 17:48:37 -07004112 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004113 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004114 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004115 mAppsCustomizeContent.onPackagesUpdated(
4116 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004117 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004118 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119 }
4120
4121 /**
4122 * A package was updated.
4123 *
4124 * Implementation of the method from LauncherModel.Callbacks.
4125 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004126 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004127 Runnable r = new Runnable() {
4128 public void run() {
4129 bindAppsUpdated(apps);
4130 }
4131 };
4132 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004133 return;
4134 }
4135
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004136 if (mWorkspace != null) {
4137 mWorkspace.updateShortcuts(apps);
4138 }
Winson Chungc58497e2013-09-03 17:48:37 -07004139
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004140 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004141 mAppsCustomizeContent != null) {
4142 mAppsCustomizeContent.updateApps(apps);
4143 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004144 }
4145
4146 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004147 * A package was uninstalled. We take both the super set of packageNames
4148 * in addition to specific applications to remove, the reason being that
4149 * this can be called when a package is updated as well. In that scenario,
4150 * we only remove specific components from the workspace, where as
4151 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004152 *
4153 * Implementation of the method from LauncherModel.Callbacks.
4154 */
Winson Chung83892cc2013-05-01 16:53:33 -07004155 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004156 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004157 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004158 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004159 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004160 }
Winson Chungd64d1762013-08-20 14:37:16 -07004161 };
4162 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004163 return;
4164 }
4165
Winson Chungdf95eb12013-10-16 14:57:07 -07004166 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004167 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004168 }
4169 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004170 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004171 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004172
Winson Chunga1820962011-10-03 16:31:06 -07004173 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004174 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004175
Winson Chungdf95eb12013-10-16 14:57:07 -07004176 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004177 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004178 mAppsCustomizeContent != null) {
4179 mAppsCustomizeContent.removeApps(appInfos);
4180 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004181 }
Joe Onoratobe386092009-11-17 17:32:16 -08004182
4183 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004184 * A number of packages were updated.
4185 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004186 private ArrayList<Object> mWidgetsAndShortcuts;
4187 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004188 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004189 bindPackagesUpdated(mWidgetsAndShortcuts);
4190 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004191 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004192 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004193 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4194 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4195 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004196 return;
4197 }
4198
Winson Chung64359a52013-07-08 17:17:08 -07004199 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004200 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004201 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004202 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004203 }
4204
Winson Chung400438b2011-01-16 17:53:48 -08004205 private int mapConfigurationOriActivityInfoOri(int configOri) {
4206 final Display d = getWindowManager().getDefaultDisplay();
4207 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4208 switch (d.getRotation()) {
4209 case Surface.ROTATION_0:
4210 case Surface.ROTATION_180:
4211 // We are currently in the same basic orientation as the natural orientation
4212 naturalOri = configOri;
4213 break;
4214 case Surface.ROTATION_90:
4215 case Surface.ROTATION_270:
4216 // We are currently in the other basic orientation to the natural orientation
4217 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4218 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4219 break;
4220 }
4221
4222 int[] oriMap = {
4223 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4224 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4225 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4226 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4227 };
4228 // Since the map starts at portrait, we need to offset if this device's natural orientation
4229 // is landscape.
4230 int indexOffset = 0;
4231 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4232 indexOffset = 1;
4233 }
4234 return oriMap[(d.getRotation() + indexOffset) % 4];
4235 }
Adam Cohen446e9402011-09-15 18:21:21 -07004236
Winson Chung4b919f82012-05-01 10:44:08 -07004237 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004238 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004239 getResources().getBoolean(R.bool.allow_rotation);
4240 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004241 }
Winson Chung4b919f82012-05-01 10:44:08 -07004242 public void lockScreenOrientation() {
4243 if (isRotationEnabled()) {
4244 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4245 .getConfiguration().orientation));
4246 }
4247 }
4248 public void unlockScreenOrientation(boolean immediate) {
4249 if (isRotationEnabled()) {
4250 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004251 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004252 } else {
4253 mHandler.postDelayed(new Runnable() {
4254 public void run() {
4255 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4256 }
4257 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004258 }
Winson Chung4b919f82012-05-01 10:44:08 -07004259 }
Winson Chung400438b2011-01-16 17:53:48 -08004260 }
4261
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004262 /**
4263 * Called when the SearchBar hint should be changed.
4264 *
4265 * @param hint the hint to be displayed in the search bar.
4266 */
4267 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004268 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004269 }
4270
Winson Chunge6eabff2013-09-24 11:01:23 -07004271 protected String getFirstRunClingSearchBarHint() {
4272 return "";
4273 }
4274 protected String getFirstRunCustomContentHint() {
4275 return "";
4276 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004277 protected int getFirstRunFocusedHotseatAppDrawableId() {
4278 return -1;
4279 }
4280 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4281 return null;
4282 }
4283 protected int getFirstRunFocusedHotseatAppRank() {
4284 return -1;
4285 }
4286 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4287 return "";
4288 }
4289 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4290 return "";
4291 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004292
Winson Chungaf40f202013-09-18 18:26:31 -07004293 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004294 mLauncherClings.dismissFirstRunCling(v);
4295 }
4296 public void dismissMigrationClingCopyApps(View v) {
4297 mLauncherClings.dismissMigrationClingCopyApps(v);
4298 }
4299 public void dismissMigrationClingUseDefault(View v) {
4300 mLauncherClings.dismissMigrationClingUseDefault(v);
4301 }
4302 public void dismissMigrationWorkspaceCling(View v) {
4303 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004304 }
Winson Chung82f55532011-08-09 14:14:23 -07004305 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004306 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004307 }
4308 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004309 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004310 }
4311
Winson Chunga6945242014-01-08 14:04:34 -08004312 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004313 return !ActivityManager.isRunningInTestHarness() &&
4314 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004315 }
4316
4317 public void showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004318 if (shouldRunFirstRunActivity() &&
4319 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004320 Intent firstRunIntent = getFirstRunActivity();
4321 if (firstRunIntent != null) {
4322 startActivity(firstRunIntent);
4323 markFirstRunActivityShown();
4324 }
4325 }
4326 }
4327
4328 private void markFirstRunActivityShown() {
4329 SharedPreferences.Editor editor = mSharedPrefs.edit();
4330 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4331 editor.apply();
4332 }
4333
4334 void showWorkspaceSearchAndHotseat() {
4335 mWorkspace.setAlpha(1f);
4336 mHotseat.setAlpha(1f);
4337 mPageIndicators.setAlpha(1f);
4338 mSearchDropTargetBar.showSearchBar(false);
4339 }
4340
4341 void hideWorkspaceSearchAndHotseat() {
4342 mWorkspace.setAlpha(0f);
4343 mHotseat.setAlpha(0f);
4344 mPageIndicators.setAlpha(0f);
4345 mSearchDropTargetBar.hideSearchBar(false);
4346 }
4347
4348
Mathew Inwood72fbec12013-11-19 15:45:07 +00004349 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4350 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4351 if (ri == null) {
4352 return null;
4353 }
4354 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4355 }
4356
4357 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4358 Bitmap icon) {
4359 return new ShortcutInfo(shortcutIntent, caption, icon);
4360 }
4361
4362 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4363 dragView.setTag(dragInfo);
4364 mWorkspace.onDragStartedWithItem(dragView);
4365 mWorkspace.beginDragShared(dragView, source);
4366 }
4367
Winson Chung80baf5a2010-08-09 16:03:15 -07004368 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004369 * Prints out out state for debugging.
4370 */
4371 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004372 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004373 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004374 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4375 Log.d(TAG, "mRestoring=" + mRestoring);
4376 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4377 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004378 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004379 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004380
Winson Chung785d2eb2011-04-14 16:08:02 -07004381 if (mAppsCustomizeContent != null) {
4382 mAppsCustomizeContent.dumpState();
4383 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004384 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004385 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004386
4387 @Override
4388 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4389 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004390 synchronized (sDumpLogs) {
4391 writer.println(" ");
4392 writer.println("Debug logs: ");
4393 for (int i = 0; i < sDumpLogs.size(); i++) {
4394 writer.println(" " + sDumpLogs.get(i));
4395 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004396 }
4397 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004398
4399 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004400 if (DEBUG_DUMP_LOG) {
4401 synchronized (sDumpLogs) {
4402 Log.d(TAG, "");
4403 Log.d(TAG, "*********************");
4404 Log.d(TAG, "Launcher debug logs: ");
4405 for (int i = 0; i < sDumpLogs.size(); i++) {
4406 Log.d(TAG, " " + sDumpLogs.get(i));
4407 }
4408 Log.d(TAG, "*********************");
4409 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004410 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004411 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004412 }
4413
4414 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004415 addDumpLog(tag, log, null, debugLog);
4416 }
4417
4418 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004419 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004420 if (e != null) {
4421 Log.d(tag, log, e);
4422 } else {
4423 Log.d(tag, log);
4424 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004425 }
Winson Chungede41292013-09-19 16:27:36 -07004426 if (DEBUG_DUMP_LOG) {
4427 sDateStamp.setTime(System.currentTimeMillis());
4428 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004429 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4430 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004431 }
Winson Chungede41292013-09-19 16:27:36 -07004432 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004433 }
4434
Winson Chungede41292013-09-19 16:27:36 -07004435 public void dumpLogsToLocalData() {
4436 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004437 new AsyncTask<Void, Void, Void>() {
4438 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004439 boolean success = false;
4440 sDateStamp.setTime(sRunStart);
4441 String FILENAME = sDateStamp.getMonth() + "-"
4442 + sDateStamp.getDay() + "_"
4443 + sDateStamp.getHours() + "-"
4444 + sDateStamp.getMinutes() + "_"
4445 + sDateStamp.getSeconds() + ".txt";
4446
4447 FileOutputStream fos = null;
4448 File outFile = null;
4449 try {
4450 outFile = new File(getFilesDir(), FILENAME);
4451 outFile.createNewFile();
4452 fos = new FileOutputStream(outFile);
4453 } catch (Exception e) {
4454 e.printStackTrace();
4455 }
4456 if (fos != null) {
4457 PrintWriter writer = new PrintWriter(fos);
4458
4459 writer.println(" ");
4460 writer.println("Debug logs: ");
4461 synchronized (sDumpLogs) {
4462 for (int i = 0; i < sDumpLogs.size(); i++) {
4463 writer.println(" " + sDumpLogs.get(i));
4464 }
4465 }
4466 writer.close();
4467 }
4468 try {
4469 if (fos != null) {
4470 fos.close();
4471 success = true;
4472 }
4473 } catch (IOException e) {
4474 e.printStackTrace();
4475 }
Michael Jurka43467462013-11-14 12:03:58 +01004476 return null;
Winson Chungede41292013-09-19 16:27:36 -07004477 }
Michael Jurka43467462013-11-14 12:03:58 +01004478 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004479 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004481}
Michael Jurka2763be32011-02-24 11:19:57 -08004482
Michael Jurkaabded662011-03-04 12:06:57 -08004483interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004484 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004485 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004486 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004487 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004488 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004489}
Dan Sandlerd5024042014-01-09 15:01:33 -05004490
4491interface DebugIntents {
4492 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4493 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004494}