blob: aadcd8799bee95ce91b4b4d5e0da2db7d7ef1454 [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;
Cristina Stancu476493b2013-08-07 17:20:14 +0100259 private View mQsbBar;
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
Winson Chungc51db6a2011-10-05 11:44:49 -07001287 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001288
Winson Chungf0ea4d32011-06-06 14:27:16 -07001289 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001290 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001291 mAppsCustomizeContent = (AppsCustomizePagedView)
1292 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1293 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001294
Winson Chung3d503fb2011-07-13 17:25:49 -07001295 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001296 dragController.setDragScoller(mWorkspace);
1297 dragController.setScrollView(mDragLayer);
1298 dragController.setMoveTarget(mWorkspace);
1299 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001300 if (mSearchDropTargetBar != null) {
1301 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001302 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001303
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001304 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001305 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001306 mWeightWatcher = new WeightWatcher(this);
1307 mWeightWatcher.setAlpha(0.5f);
1308 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001309 new FrameLayout.LayoutParams(
1310 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001311 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001312 Gravity.BOTTOM)
1313 );
Adam Cohen39a06042013-07-19 14:30:12 -07001314
1315 boolean show = shouldShowWeightWatcher();
1316 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319
1320 /**
1321 * Creates a view representing a shortcut.
1322 *
1323 * @param info The data structure describing the shortcut.
1324 *
1325 * @return A View inflated from R.layout.application.
1326 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001327 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001329 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
1331
1332 /**
1333 * Creates a view representing a shortcut inflated from the specified resource.
1334 *
1335 * @param layoutResId The id of the XML layout used to create the shortcut.
1336 * @param parent The group the shortcut belongs to.
1337 * @param info The data structure describing the shortcut.
1338 *
1339 * @return A View inflated from layoutResId.
1340 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001341 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001342 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1343 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 return favorite;
1346 }
1347
1348 /**
1349 * Add an application shortcut to the workspace.
1350 *
1351 * @param data The intent describing the application.
1352 * @param cellInfo The position on screen where to create the shortcut.
1353 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001354 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001355 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001356 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001357
Adam Cohenfbba09b2011-07-18 21:43:05 -07001358 // First we check if we already know the exact location where we want to add this item.
1359 if (cellX >= 0 && cellY >= 0) {
1360 cellXY[0] = cellX;
1361 cellXY[1] = cellY;
1362 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001363 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001364 return;
1365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001367 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001368
Romain Guy73b979d2009-06-09 12:57:21 -07001369 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001370 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001372 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001373 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001374 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001375 } else {
1376 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
1378 }
Romain Guycbb89e42009-06-08 15:52:54 -07001379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 /**
1381 * Add a shortcut to the workspace.
1382 *
1383 * @param data The intent describing the shortcut.
1384 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001386 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001387 int cellY) {
1388 int[] cellXY = mTmpAddItemCellCoordinates;
1389 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001390 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001391
Michael Jurkad3ef3062010-11-23 16:23:58 -08001392 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001393
Adam Cohen558baaf2011-08-15 15:22:57 -07001394 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001395 if (info == null) {
1396 return;
1397 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001398 final View view = createShortcut(info);
1399
Adam Cohenfbba09b2011-07-18 21:43:05 -07001400 // First we check if we already know the exact location where we want to add this item.
1401 if (cellX >= 0 && cellY >= 0) {
1402 cellXY[0] = cellX;
1403 cellXY[1] = cellY;
1404 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001405
1406 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001407 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001408 true, null,null)) {
1409 return;
1410 }
1411 DragObject dragObject = new DragObject();
1412 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001413 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1414 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001415 return;
1416 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001417 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001418 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001419 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001420 foundCellSpan = (result != null);
1421 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001422 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001423 }
1424
1425 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001426 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001427 return;
1428 }
1429
Adam Cohendcd297f2013-06-18 13:13:40 -07001430 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431
1432 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001433 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001434 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436 }
1437
Adam Cohen2f093b62012-04-30 18:59:53 -07001438 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1439 int minHeight) {
1440 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001441 // We want to account for the extra amount of padding that we are adding to the widget
1442 // to ensure that it gets the full amount of space that it has requested
1443 int requiredWidth = minWidth + padding.left + padding.right;
1444 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001445 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001446 }
1447
Adam Cohen2f093b62012-04-30 18:59:53 -07001448 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1449 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001450 }
1451
Adam Cohen2f093b62012-04-30 18:59:53 -07001452 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1453 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001454 }
1455
Adam Cohen2f093b62012-04-30 18:59:53 -07001456 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1457 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001458 }
1459
Adam Cohen2f093b62012-04-30 18:59:53 -07001460 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1461 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001462 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001463 }
1464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001466 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001468 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001469 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001471 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001472 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1473 if (appWidgetInfo == null) {
1474 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1475 }
Romain Guycbb89e42009-06-08 15:52:54 -07001476
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001477 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001478 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001479
Adam Cohen2f093b62012-04-30 18:59:53 -07001480 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1481 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001484 // We have saved the position to which the widget was dragged-- this really only matters
1485 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001486 int[] cellXY = mTmpAddItemCellCoordinates;
1487 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001488 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001489 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001490 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1491 cellXY[0] = mPendingAddInfo.cellX;
1492 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001493 spanXY[0] = mPendingAddInfo.spanX;
1494 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001495 foundCellSpan = true;
1496 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001497 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001499 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1500 spanXY[1], cellXY, finalSpan);
1501 spanXY[0] = finalSpan[0];
1502 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001503 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001504 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001505 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001506 }
1507
Michael Jurka0280c3b2010-09-17 15:00:07 -07001508 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001509 if (appWidgetId != -1) {
1510 // Deleting an app widget ID is a void call but writes to disk before returning
1511 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001512 new AsyncTask<Void, Void, Void>() {
1513 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001514 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001515 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001516 }
Michael Jurka43467462013-11-14 12:03:58 +01001517 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001518 }
Winson Chung93eef082012-03-23 15:59:27 -07001519 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001520 return;
1521 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001523 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001524 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1525 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001526 launcherInfo.spanX = spanXY[0];
1527 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001528 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1529 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001530
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001532 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533
1534 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001535 if (hostView == null) {
1536 // Perform actual inflation because we're live
1537 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1538 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1539 } else {
1540 // The AppWidgetHostView has already been inflated and instantiated
1541 launcherInfo.hostView = hostView;
1542 }
Romain Guycbb89e42009-06-08 15:52:54 -07001543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001545 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001546 launcherInfo.notifyWidgetSizeChanged(this);
1547
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001549 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001550
1551 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001553 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Adam Cohended9f8d2010-11-03 13:25:16 -07001556 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1557 @Override
1558 public void onReceive(Context context, Intent intent) {
1559 final String action = intent.getAction();
1560 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1561 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001562 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001563 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001564
Winson Chungc100e8e2011-08-09 16:02:43 -07001565 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001566 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001567 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001568 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001569 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001570 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1571 mUserPresent = true;
1572 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001573 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1574 mModel.resetLoadedState(false, true);
1575 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1576 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1577 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1578 mModel.resetLoadedState(false, true);
1579 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1580 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1581 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001582 }
1583 }
1584 };
1585
1586 @Override
1587 public void onAttachedToWindow() {
1588 super.onAttachedToWindow();
1589
1590 // Listen for broadcasts related to user-presence
1591 final IntentFilter filter = new IntentFilter();
1592 filter.addAction(Intent.ACTION_SCREEN_OFF);
1593 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001594 if (ENABLE_DEBUG_INTENTS) {
1595 filter.addAction(DebugIntents.DELETE_DATABASE);
1596 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1597 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001598 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001599 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001600 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001601 mVisible = true;
1602 }
1603
1604 @Override
1605 public void onDetachedFromWindow() {
1606 super.onDetachedFromWindow();
1607 mVisible = false;
1608
Adam Cohend113e0c2010-11-11 10:48:05 -08001609 if (mAttached) {
1610 unregisterReceiver(mReceiver);
1611 mAttached = false;
1612 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001613 updateRunning();
1614 }
1615
1616 public void onWindowVisibilityChanged(int visibility) {
1617 mVisible = visibility == View.VISIBLE;
1618 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001619 // The following code used to be in onResume, but it turns out onResume is called when
1620 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1621 // is a more appropriate event to handle
1622 if (mVisible) {
1623 mAppsCustomizeTabHost.onWindowVisible();
1624 if (!mWorkspaceLoading) {
1625 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001626 // We want to let Launcher draw itself at least once before we force it to build
1627 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001628 // apps is nice and speedy.
1629 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001630 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001631 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001632 if (mStarted) return;
1633 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001634 // We delay the layer building a bit in order to give
1635 // other message processing a time to run. In particular
1636 // this avoids a delay in hiding the IME if it was
1637 // currently shown, because doing that may involve
1638 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001639 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001640 final ViewTreeObserver.OnDrawListener listener = this;
1641 mWorkspace.post(new Runnable() {
1642 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001643 if (mWorkspace != null &&
1644 mWorkspace.getViewTreeObserver() != null) {
1645 mWorkspace.getViewTreeObserver().
1646 removeOnDrawListener(listener);
1647 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001648 }
1649 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001650 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001651 }
1652 });
1653 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001654 // When Launcher comes back to foreground, a different Activity might be responsible for
1655 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001656 if (!DISABLE_MARKET_BUTTON) {
1657 updateAppMarketIcon();
1658 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001659 clearTypedText();
1660 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001661 }
1662
1663 private void sendAdvanceMessage(long delay) {
1664 mHandler.removeMessages(ADVANCE_MSG);
1665 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1666 mHandler.sendMessageDelayed(msg, delay);
1667 mAutoAdvanceSentTime = System.currentTimeMillis();
1668 }
1669
1670 private void updateRunning() {
1671 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1672 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1673 mAutoAdvanceRunning = autoAdvanceRunning;
1674 if (autoAdvanceRunning) {
1675 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1676 sendAdvanceMessage(delay);
1677 } else {
1678 if (!mWidgetsToAdvance.isEmpty()) {
1679 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1680 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1681 }
1682 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001683 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001684 }
1685 }
1686 }
1687
1688 private final Handler mHandler = new Handler() {
1689 @Override
1690 public void handleMessage(Message msg) {
1691 if (msg.what == ADVANCE_MSG) {
1692 int i = 0;
1693 for (View key: mWidgetsToAdvance.keySet()) {
1694 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1695 final int delay = mAdvanceStagger * i;
1696 if (v instanceof Advanceable) {
1697 postDelayed(new Runnable() {
1698 public void run() {
1699 ((Advanceable) v).advance();
1700 }
1701 }, delay);
1702 }
1703 i++;
1704 }
1705 sendAdvanceMessage(mAdvanceInterval);
1706 }
1707 }
1708 };
1709
1710 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001711 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1713 if (v instanceof Advanceable) {
1714 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001715 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 updateRunning();
1717 }
1718 }
1719
1720 void removeWidgetToAutoAdvance(View hostView) {
1721 if (mWidgetsToAdvance.containsKey(hostView)) {
1722 mWidgetsToAdvance.remove(hostView);
1723 updateRunning();
1724 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001725 }
1726
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001729 launcherInfo.hostView = null;
1730 }
1731
Winson Chung93eef082012-03-23 15:59:27 -07001732 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1733 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1734 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 }
1736
Winson Chunga6945242014-01-08 14:04:34 -08001737 public DragLayer getDragLayer() {
1738 return mDragLayer;
1739 }
1740
1741 public Workspace getWorkspace() {
1742 return mWorkspace;
1743 }
1744
1745 public Hotseat getHotseat() {
1746 return mHotseat;
1747 }
1748
1749 public View getOverviewPanel() {
1750 return mOverviewPanel;
1751 }
1752
1753 public SearchDropTargetBar getSearchBar() {
1754 return mSearchDropTargetBar;
1755 }
1756
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001757 public LauncherAppWidgetHost getAppWidgetHost() {
1758 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 }
Romain Guycbb89e42009-06-08 15:52:54 -07001760
Winson Chunga9abd0e2010-10-27 17:18:37 -07001761 public LauncherModel getModel() {
1762 return mModel;
1763 }
1764
Winson Chunga6945242014-01-08 14:04:34 -08001765 public LauncherClings getLauncherClings() {
1766 return mLauncherClings;
1767 }
1768
1769 protected SharedPreferences getSharedPrefs() {
1770 return mSharedPrefs;
1771 }
1772
Bjorn Bringertc459e522013-06-07 19:36:01 +01001773 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001774 getWindow().closeAllPanels();
1775
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001776 // Whatever we were doing is hereby canceled.
1777 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001778 }
1779
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 @Override
1781 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001782 long startTime = 0;
1783 if (DEBUG_RESUME_TIME) {
1784 startTime = System.currentTimeMillis();
1785 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 super.onNewIntent(intent);
1787
1788 // Close the menu
1789 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001790 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001791 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001792
Adam Cohened307df2013-10-02 09:37:31 -07001793 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1794 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1795 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001796
Adam Cohen6fecd412013-10-02 17:41:50 -07001797 if (mWorkspace == null) {
1798 // Can be cases where mWorkspace is null, this prevents a NPE
1799 return;
1800 }
1801 Folder openFolder = mWorkspace.getOpenFolder();
1802 // In all these cases, only animate if we're already on home
1803 mWorkspace.exitWidgetResizeMode();
1804 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001805 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001806 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001807 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001808
Adam Cohen6fecd412013-10-02 17:41:50 -07001809 closeFolder();
1810 exitSpringLoadedDragMode();
1811
1812 // If we are already on home, then just animate back to the workspace,
1813 // otherwise, just wait until onResume to set the state back to Workspace
1814 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001815 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001816 } else {
1817 mOnResumeState = State.WORKSPACE;
1818 }
1819
1820 final View v = getWindow().peekDecorView();
1821 if (v != null && v.getWindowToken() != null) {
1822 InputMethodManager imm = (InputMethodManager)getSystemService(
1823 INPUT_METHOD_SERVICE);
1824 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1825 }
1826
1827 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001828 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001829 mAppsCustomizeTabHost.reset();
1830 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001831
1832 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 }
Adam Cohened307df2013-10-02 09:37:31 -07001834
Michael Jurka447bf842013-05-15 14:52:15 +02001835 if (DEBUG_RESUME_TIME) {
1836 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1837 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 }
1839
Adam Coppa120b8e2013-11-12 16:26:04 +00001840 /**
1841 * Override point for subclasses to prevent movement to the default screen when the home
1842 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1843 */
1844 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1845 return true;
1846 }
1847
1848 /**
1849 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1850 */
1851 protected void onHomeIntent() {
1852 // Do nothing
1853 }
1854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001856 public void onRestoreInstanceState(Bundle state) {
1857 super.onRestoreInstanceState(state);
1858 for (int page: mSynchronouslyBoundPages) {
1859 mWorkspace.restoreInstanceStateForChild(page);
1860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 }
1862
1863 @Override
1864 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001865 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001866 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1867 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001868 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001869 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870
Michael Jurka883f55b2010-10-21 15:47:14 -07001871 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001872 // We close any open folder since it will not be re-opened, and we need to make sure
1873 // this state is reflected.
1874 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875
Adam Cohendcd297f2013-06-18 13:13:40 -07001876 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001877 mWaitingForResult) {
1878 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001879 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001880 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1881 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001882 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1883 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1884 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001885 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 }
1887
1888 if (mFolderInfo != null && mWaitingForResult) {
1889 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1890 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1891 }
Michael Jurka946ad472010-07-09 18:05:18 -07001892
Winson Chung785d2eb2011-04-14 16:08:02 -07001893 // Save the current AppsCustomize tab
1894 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001895 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1896 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001897 if (currentTabTag != null) {
1898 outState.putString("apps_customize_currentTab", currentTabTag);
1899 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001900 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1901 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001902 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001903 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 }
1905
1906 @Override
1907 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001909
Winson Chunge7a03942011-08-05 15:05:12 -07001910 // Remove all pending runnables
1911 mHandler.removeMessages(ADVANCE_MSG);
1912 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001913 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001914
Winson Chungcd2b0142011-06-08 16:02:26 -07001915 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001916 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001917 mModel.stopLoader();
1918 app.setLauncher(null);
1919
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001921 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001923 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001925 mAppWidgetHost = null;
1926
1927 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928
1929 TextKeyListener.getInstance().release();
1930
Winson Chung81b52252012-08-27 15:34:29 -07001931 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1932 // to prevent leaking Launcher activities on orientation change.
1933 if (mModel != null) {
1934 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1935 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001936
1937 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001938 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001939
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001940 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001941 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001942 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001943 mWorkspace = null;
1944 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001945
Michael Jurka2ecf9952012-06-18 12:52:28 -07001946 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948
Adam Cohena9cf38f2011-05-02 15:36:58 -07001949 public DragController getDragController() {
1950 return mDragController;
1951 }
1952
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 @Override
1954 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001955 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 super.startActivityForResult(intent, requestCode);
1957 }
1958
Winson Chung70d72102011-08-12 11:24:35 -07001959 /**
1960 * Indicates that we want global search for this activity by setting the globalSearch
1961 * argument for {@link #startSearch} to true.
1962 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001964 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001966
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001967 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001968
Karl Rosaen138a0412009-04-23 19:00:21 -07001969 if (initialQuery == null) {
1970 // Use any text typed in the launcher as the initial query
1971 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 if (appSearchData == null) {
1974 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001975 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
Winson Chungadf0c182012-08-23 14:59:07 -07001977 Rect sourceBounds = new Rect();
1978 if (mSearchDropTargetBar != null) {
1979 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1980 }
Romain Guycbb89e42009-06-08 15:52:54 -07001981
Bjorn Bringertc459e522013-06-07 19:36:01 +01001982 startSearch(initialQuery, selectInitialQuery,
1983 appSearchData, sourceBounds);
1984 }
1985
1986 public void startSearch(String initialQuery,
1987 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001988 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001989 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001990 }
1991
1992 /**
1993 * Starts the global search activity. This code is a copied from SearchManager
1994 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001995 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001996 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001997 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001998 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1999 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2000 if (globalSearchActivity == null) {
2001 Log.w(TAG, "No global search activity found.");
2002 return;
2003 }
2004 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2005 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2006 intent.setComponent(globalSearchActivity);
2007 // Make sure that we have a Bundle to put source in
2008 if (appSearchData == null) {
2009 appSearchData = new Bundle();
2010 } else {
2011 appSearchData = new Bundle(appSearchData);
2012 }
2013 // Set source to package name of app that starts global search, if not set already.
2014 if (!appSearchData.containsKey("source")) {
2015 appSearchData.putString("source", getPackageName());
2016 }
2017 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2018 if (!TextUtils.isEmpty(initialQuery)) {
2019 intent.putExtra(SearchManager.QUERY, initialQuery);
2020 }
2021 if (selectInitialQuery) {
2022 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2023 }
2024 intent.setSourceBounds(sourceBounds);
2025 try {
2026 startActivity(intent);
2027 } catch (ActivityNotFoundException ex) {
2028 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2029 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 }
2031
Winson Chung70d72102011-08-12 11:24:35 -07002032 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002033 public boolean onPrepareOptionsMenu(Menu menu) {
2034 super.onPrepareOptionsMenu(menu);
Dan Sandler14c6a3f2014-01-07 14:12:27 -05002035 showOverviewMode(true);
Michael Jurkab94f3f82013-09-11 18:08:54 +02002036 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002039 @Override
2040 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002041 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002042 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002043 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002044 }
2045
Joe Onorato9c1289c2009-08-17 11:03:03 -04002046 public boolean isWorkspaceLocked() {
2047 return mWorkspaceLoading || mWaitingForResult;
2048 }
2049
Michael Jurka0280c3b2010-09-17 15:00:07 -07002050 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002051 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002052 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002053 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2054 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002055 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002056 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002057 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002058
Adam Cohenad4e15c2013-10-17 16:21:35 -07002059 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2060 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2061 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2062 }
2063
2064 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2065 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2066 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002067 if (appWidgetInfo.configure != null) {
2068 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002069 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002070
Bjorn Bringert7984c942009-12-09 15:38:25 +00002071 // Launch over to configure widget, if needed
2072 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002073 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002074 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002075 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002077 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002078 Runnable onComplete = new Runnable() {
2079 @Override
2080 public void run() {
2081 // Exit spring loaded mode if necessary after adding the widget
2082 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2083 null);
2084 }
2085 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002086 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002087 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002088 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 }
2090 }
Romain Guycbb89e42009-06-08 15:52:54 -07002091
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002092 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002093 // Close any folders that may be open.
2094 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002095 mWorkspace.moveToCustomContentScreen(animate);
2096 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002097 /**
2098 * Process a shortcut drop.
2099 *
2100 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002101 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002102 * @param cell The cell it should be added to, optional
2103 * @param position The location on the screen where it was dropped, optional
2104 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002105 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002106 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002107 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002108 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002109 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002110 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002111
2112 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002113 mPendingAddInfo.cellX = cell[0];
2114 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002115 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002116
2117 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2118 createShortcutIntent.setComponent(componentName);
2119 processShortcut(createShortcutIntent);
2120 }
2121
Adam Cohenfbba09b2011-07-18 21:43:05 -07002122 /**
2123 * Process a widget drop.
2124 *
2125 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002126 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002127 * @param cell The cell it should be added to, optional
2128 * @param position The location on the screen where it was dropped, optional
2129 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002130 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002131 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002132 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002133 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002134 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002135 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002136 mPendingAddInfo.minSpanX = info.minSpanX;
2137 mPendingAddInfo.minSpanY = info.minSpanY;
2138
Adam Cohenfbba09b2011-07-18 21:43:05 -07002139 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002140 mPendingAddInfo.cellX = cell[0];
2141 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002142 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002143 if (span != null) {
2144 mPendingAddInfo.spanX = span[0];
2145 mPendingAddInfo.spanY = span[1];
2146 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002147
Adam Cohened66b2b2012-01-23 17:28:51 -08002148 AppWidgetHostView hostView = info.boundWidget;
2149 int appWidgetId;
2150 if (hostView != null) {
2151 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002152 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002153 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002154 // In this case, we either need to start an activity to get permission to bind
2155 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002156 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002157 Bundle options = info.bindOptions;
2158
2159 boolean success = false;
2160 if (options != null) {
2161 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2162 info.componentName, options);
2163 } else {
2164 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2165 info.componentName);
2166 }
2167 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002168 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002169 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002170 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002171 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2172 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2173 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002174 // TODO: we need to make sure that this accounts for the options bundle.
2175 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002176 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2177 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002178 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002179 }
2180
Joe Onoratodeb98af2010-02-19 14:59:39 -08002181 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002182 // Handle case where user selected "Applications"
2183 String applicationName = getResources().getString(R.string.group_applications);
2184 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002185
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002186 if (applicationName != null && applicationName.equals(shortcutName)) {
2187 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2188 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002189
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002190 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2191 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002192 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002193 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002194 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002195 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
2198
Winson Chung24ab2f12010-09-16 14:10:47 -07002199 void processWallpaper(Intent intent) {
2200 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2201 }
2202
Adam Cohendcd297f2013-06-18 13:13:40 -07002203 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002204 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002205 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 folderInfo.title = getText(R.string.folder_name);
2207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002209 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002210 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002211 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212
2213 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002214 FolderIcon newFolder =
2215 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002217 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002218 // Force measure the new folder icon
2219 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2220 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002221 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 }
Romain Guycbb89e42009-06-08 15:52:54 -07002223
Joe Onorato9c1289c2009-08-17 11:03:03 -04002224 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002225 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002226 }
2227
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002228 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002230 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002231 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
2233
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002234 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002235 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002236 }
2237
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002238 /**
2239 * Registers various content observers. The current implementation registers
2240 * only a favorites observer to keep track of the favorites applications.
2241 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002242 private void registerContentObservers() {
2243 ContentResolver resolver = getContentResolver();
2244 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2245 true, mWidgetObserver);
2246 }
2247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 @Override
2249 public boolean dispatchKeyEvent(KeyEvent event) {
2250 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2251 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002253 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002254 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002255 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002256 dumpState();
2257 return true;
2258 }
2259 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002260 }
2261 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2262 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002263 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 return true;
2265 }
2266 }
2267
2268 return super.dispatchKeyEvent(event);
2269 }
2270
Joe Onorato88ec0992009-11-19 13:16:06 -08002271 @Override
2272 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002273 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002274 if (mAppsCustomizeContent.getContentType() ==
2275 AppsCustomizePagedView.ContentType.Applications) {
2276 showWorkspace(true);
2277 } else {
2278 showOverviewMode(true);
2279 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002280 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002281 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002282 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002283 Folder openFolder = mWorkspace.getOpenFolder();
2284 if (openFolder.isEditingName()) {
2285 openFolder.dismissEditingName();
2286 } else {
2287 closeFolder();
2288 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002289 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002290 mWorkspace.exitWidgetResizeMode();
2291
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002292 // Back button is a no-op here, but give at least some feedback for the button press
2293 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002294 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002295 }
2296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002298 * Re-listen when widgets are reset.
2299 */
2300 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002301 if (mAppWidgetHost != null) {
2302 mAppWidgetHost.startListening();
2303 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002304 }
2305
2306 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 * Launches the intent referred by the clicked shortcut.
2308 *
2309 * @param v The view representing the clicked shortcut.
2310 */
2311 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002312 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2313 // view has detached (it's possible for this to happen if the view is removed mid touch).
2314 if (v.getWindowToken() == null) {
2315 return;
2316 }
2317
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002318 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002319 return;
2320 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002321
Adam Cohen1697b792013-09-17 19:08:21 -07002322 if (v instanceof Workspace) {
2323 if (mWorkspace.isInOverviewMode()) {
2324 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002325 }
2326 return;
2327 }
2328
2329 if (v instanceof CellLayout) {
2330 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002331 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002332 }
2333 }
2334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002336 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002338 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2339 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002340
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002341 // Check for special shortcuts
2342 if (intent.getComponent() != null) {
2343 final String shortcutClass = intent.getComponent().getClassName();
2344
2345 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002346 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002347 return;
2348 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2349 MemoryDumpActivity.startDump(this);
2350 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002351 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2352 toggleShowWeightWatcher();
2353 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002354 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002355 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002356
2357 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002358 int[] pos = new int[2];
2359 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002360 intent.setSourceBounds(new Rect(pos[0], pos[1],
2361 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002362
2363 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002364
Daniel Sandlerff02d492013-08-05 02:12:05 -04002365 mStats.recordLaunch(intent, shortcut);
2366
Michael Jurkaddd62e92011-02-16 17:49:14 -08002367 if (success && v instanceof BubbleTextView) {
2368 mWaitingForResume = (BubbleTextView) v;
2369 mWaitingForResume.setStayPressed(true);
2370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002372 if (v instanceof FolderIcon) {
2373 FolderIcon fi = (FolderIcon) v;
2374 handleFolderClick(fi);
2375 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002376 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002377 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002378 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002379 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002380 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 }
2383 }
2384
Michael Jurka0e260592010-06-30 17:07:39 -07002385 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002386 return false;
2387 }
2388
Michael Jurkaaf442092010-06-10 17:01:57 -07002389 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002390 * Event handler for the search button
2391 *
2392 * @param v The view that was clicked.
2393 */
2394 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002395 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2396
Amith Yamasania135ba82011-08-09 17:42:01 -07002397 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002398 }
2399
2400 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002401 * Event handler for the voice button
2402 *
2403 * @param v The view that was clicked.
2404 */
2405 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002406 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002407
Bjorn Bringertc459e522013-06-07 19:36:01 +01002408 startVoice();
2409 }
2410
2411 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002412 try {
2413 final SearchManager searchManager =
2414 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2415 ComponentName activityName = searchManager.getGlobalSearchActivity();
2416 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002417 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002418 if (activityName != null) {
2419 intent.setPackage(activityName.getPackageName());
2420 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002421 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002422 } catch (ActivityNotFoundException e) {
2423 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002424 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002425 startActivitySafely(null, intent, "onClickVoiceButton");
2426 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002427 }
2428
2429 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002430 * Event handler for the "grid" button that appears on the home screen, which
2431 * enters all apps mode.
2432 *
2433 * @param v The view that was clicked.
2434 */
2435 public void onClickAllAppsButton(View v) {
Winson Chunge7e97e62013-12-02 17:02:50 -08002436 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
Michael Jurka5130e402011-10-13 04:55:35 -07002437 }
2438
2439 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002440 // Provide the same haptic feedback that the system offers for virtual keys.
2441 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002442 }
2443
Adam Cohen61f560d2013-09-30 15:58:20 -07002444 public void performHapticFeedbackOnTouchDown(View v) {
2445 // Provide the same haptic feedback that the system offers for virtual keys.
2446 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2447 }
2448
2449 public View.OnTouchListener getHapticFeedbackTouchListener() {
2450 if (mHapticFeedbackTouchListener == null) {
2451 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2452 @Override
2453 public boolean onTouch(View v, MotionEvent event) {
2454 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2455 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2456 }
2457 return false;
2458 }
2459 };
2460 }
2461 return mHapticFeedbackTouchListener;
2462 }
2463
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002464 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002465 if (!DISABLE_MARKET_BUTTON) {
2466 if (mAppMarketIntent != null) {
2467 startActivitySafely(v, mAppMarketIntent, "app market");
2468 } else {
2469 Log.e(TAG, "Invalid app market intent.");
2470 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002471 }
2472 }
2473
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002474 /**
2475 * Called when the user stops interacting with the launcher.
2476 * This implies that the user is now on the homescreen and is not doing housekeeping.
2477 */
2478 protected void onInteractionEnd() {}
2479
2480 /**
2481 * Called when the user starts interacting with the launcher.
2482 * The possible interactions are:
2483 * - open all apps
2484 * - reorder an app shortcut, or a widget
2485 * - open the overview mode.
2486 * This is a good time to stop doing things that only make sense
2487 * when the user is on the homescreen and not doing housekeeping.
2488 */
2489 protected void onInteractionBegin() {}
2490
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002491 void startApplicationDetailsActivity(ComponentName componentName) {
2492 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002493 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2494 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002495 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002496 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002497 }
2498
Michael Jurka1e2f4652013-07-08 18:03:46 -07002499 // returns true if the activity was started
2500 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002501 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002502 // System applications cannot be installed. For now, show a toast explaining that.
2503 // We may give them the option of disabling apps this way.
2504 int messageId = R.string.uninstall_system_app_text;
2505 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002506 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002507 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002508 String packageName = componentName.getPackageName();
2509 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002510 Intent intent = new Intent(
2511 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002512 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2513 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002514 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002515 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002516 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002517 }
2518
Michael Jurka86a720e2012-05-09 11:23:23 -07002519 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002523 // Only launch using the new animation if the shortcut has not opted out (this is a
2524 // private contract between launcher and may be ignored in the future).
2525 boolean useLaunchAnimation = (v != null) &&
2526 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2527 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002528 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2529 v.getMeasuredWidth(), v.getMeasuredHeight());
2530
2531 startActivity(intent, opts.toBundle());
2532 } else {
2533 startActivity(intent);
2534 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002535 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 } catch (SecurityException e) {
2537 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002538 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002540 "or use the exported attribute for this activity. "
2541 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002543 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002545
Michael Jurka86a720e2012-05-09 11:23:23 -07002546 boolean startActivitySafely(View v, Intent intent, Object tag) {
2547 boolean success = false;
2548 try {
2549 success = startActivity(v, intent, tag);
2550 } catch (ActivityNotFoundException e) {
2551 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2552 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2553 }
2554 return success;
2555 }
2556
Adam Cohena9cf38f2011-05-02 15:36:58 -07002557 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002558 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002559 Folder openFolder = mWorkspace.getFolderForTag(info);
2560
2561 // If the folder info reports that the associated folder is open, then verify that
2562 // it is actually opened. There have been a few instances where this gets out of sync.
2563 if (info.opened && openFolder == null) {
2564 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002565 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002566 info.opened = false;
2567 }
2568
Adam Cohenfb91f302012-06-11 15:45:18 -07002569 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 // Close any open folder
2571 closeFolder();
2572 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002573 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 } else {
2575 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 int folderScreen;
2577 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002578 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 // .. and close it
2580 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002581 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582 // Close any folder open on the current screen
2583 closeFolder();
2584 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002585 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586 }
2587 }
2588 }
2589 }
2590
Adam Cohen268c4752012-06-06 17:47:33 -07002591 /**
2592 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2593 * in the DragLayer in the exact absolute location of the original FolderIcon.
2594 */
2595 private void copyFolderIconToImage(FolderIcon fi) {
2596 final int width = fi.getMeasuredWidth();
2597 final int height = fi.getMeasuredHeight();
2598
2599 // Lazy load ImageView, Bitmap and Canvas
2600 if (mFolderIconImageView == null) {
2601 mFolderIconImageView = new ImageView(this);
2602 }
2603 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2604 mFolderIconBitmap.getHeight() != height) {
2605 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2606 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2607 }
2608
2609 DragLayer.LayoutParams lp;
2610 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2611 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2612 } else {
2613 lp = new DragLayer.LayoutParams(width, height);
2614 }
2615
Adam Cohen307fe232012-08-16 17:55:58 -07002616 // The layout from which the folder is being opened may be scaled, adjust the starting
2617 // view size by this scale factor.
2618 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002619 lp.customPosition = true;
2620 lp.x = mRectForFolderAnimation.left;
2621 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002622 lp.width = (int) (scale * width);
2623 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002624
2625 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2626 fi.draw(mFolderIconCanvas);
2627 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002628 if (fi.getFolder() != null) {
2629 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2630 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002631 }
Adam Cohen268c4752012-06-06 17:47:33 -07002632 // Just in case this image view is still in the drag layer from a previous animation,
2633 // we remove it and re-add it.
2634 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2635 mDragLayer.removeView(mFolderIconImageView);
2636 }
2637 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002638 if (fi.getFolder() != null) {
2639 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002640 }
Adam Cohen268c4752012-06-06 17:47:33 -07002641 }
2642
Adam Cohen2801caf2011-05-13 20:57:39 -07002643 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002644 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002645 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2646 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2647 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2648
Adam Cohenc51934b2011-07-26 21:07:43 -07002649 FolderInfo info = (FolderInfo) fi.getTag();
2650 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2651 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002652 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2653 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002654 }
2655
Adam Cohen268c4752012-06-06 17:47:33 -07002656 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2657 copyFolderIconToImage(fi);
2658 fi.setVisibility(View.INVISIBLE);
2659
Michael Jurka2ecf9952012-06-18 12:52:28 -07002660 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002661 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002662 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2663 oa.start();
2664 }
2665
Adam Cohen268c4752012-06-06 17:47:33 -07002666 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002667 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002668 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2669 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2670 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2671
Adam Cohen268c4752012-06-06 17:47:33 -07002672 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002673
Adam Cohen268c4752012-06-06 17:47:33 -07002674 // We remove and re-draw the FolderIcon in-case it has changed
2675 mDragLayer.removeView(mFolderIconImageView);
2676 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002677 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002678 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002679 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002680 oa.addListener(new AnimatorListenerAdapter() {
2681 @Override
2682 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002683 if (cl != null) {
2684 cl.clearFolderLeaveBehind();
2685 // Remove the ImageView copy of the FolderIcon and make the original visible.
2686 mDragLayer.removeView(mFolderIconImageView);
2687 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002688 }
2689 }
2690 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002691 oa.start();
2692 }
2693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002695 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002696 * is animated relative to the specified View. If the View is null, no animation
2697 * is played.
2698 *
2699 * @param folderInfo The FolderInfo describing the folder to open.
2700 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002701 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002702 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002703 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704
Adam Cohena9cf38f2011-05-02 15:36:58 -07002705 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002706
Adam Cohen4554ee12011-08-03 16:13:21 -07002707 // Just verify that the folder hasn't already been added to the DragLayer.
2708 // There was a one-off crash where the folder had a parent already.
2709 if (folder.getParent() == null) {
2710 mDragLayer.addView(folder);
2711 mDragController.addDropTarget((DropTarget) folder);
2712 } else {
2713 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2714 folder.getParent() + ").");
2715 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002716 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002717 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002718
2719 // Notify the accessibility manager that this folder "window" has appeared and occluded
2720 // the workspace items
2721 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2722 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002723 }
2724
2725 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002726 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002727 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002728 if (folder.isEditingName()) {
2729 folder.dismissEditingName();
2730 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002731 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002732
2733 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002734 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002735 }
2736 }
2737
2738 void closeFolder(Folder folder) {
2739 folder.getInfo().opened = false;
2740
2741 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2742 if (parent != null) {
2743 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2744 shrinkAndFadeInFolderIcon(fi);
2745 }
2746 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002747
2748 // Notify the accessibility manager that this folder "window" has disappeard and no
2749 // longer occludeds the workspace items
2750 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002751 }
2752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002753 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002754 if (!isDraggingEnabled()) return false;
2755 if (isWorkspaceLocked()) return false;
2756 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002757
Adam Cohen1697b792013-09-17 19:08:21 -07002758 if (v instanceof Workspace) {
2759 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002760 if (mWorkspace.enterOverviewMode()) {
2761 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2762 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2763 return true;
2764 } else {
2765 return false;
2766 }
Adam Cohen1697b792013-09-17 19:08:21 -07002767 }
Adam Cohen1697b792013-09-17 19:08:21 -07002768 }
2769
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002770 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002771 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002772 }
2773
Michael Jurka0280c3b2010-09-17 15:00:07 -07002774 resetAddInfo();
2775 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002776 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002777 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002778 return true;
2779 }
2780
Winson Chung3d503fb2011-07-13 17:25:49 -07002781 // The hotseat touch handling does not go through Workspace, and we always allow long press
2782 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002783 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002784 final boolean inHotseat = isHotseatLayout(v);
2785 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002786 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002787 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002788 // User long pressed on empty space
2789 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2790 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2791 // Disabling reordering until we sort out some issues.
2792 if (mWorkspace.isInOverviewMode()) {
2793 mWorkspace.startReordering(v);
2794 } else {
2795 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002797 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002798 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2799 mHotseat.getOrderInHotseat(
2800 longClickCellInfo.cellX,
2801 longClickCellInfo.cellY));
2802 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002803 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002804 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002805 }
2806 }
2807 }
2808 return true;
2809 }
2810
Winson Chung3d503fb2011-07-13 17:25:49 -07002811 boolean isHotseatLayout(View layout) {
2812 return mHotseat != null && layout != null &&
2813 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2814 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002815
Winson Chung3d503fb2011-07-13 17:25:49 -07002816 /**
2817 * Returns the CellLayout of the specified container at the specified screen.
2818 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002819 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002820 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2821 if (mHotseat != null) {
2822 return mHotseat.getLayout();
2823 } else {
2824 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002825 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002826 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002827 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002828 }
2829 }
2830
Daniel Sandler843e8602010-06-07 14:59:01 -04002831 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002832 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002833 }
2834
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002835 /**
2836 * Helper method for the cameraZoomIn/cameraZoomOut animations
2837 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002838 * @param scaleFactor The scale factor used for the zoom
2839 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002840 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002841 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002842 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002843 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002844
Craig Mautner360310b2012-10-26 15:13:08 -07002845 private void setWorkspaceBackground(boolean workspace) {
2846 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002847 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002848 }
2849
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002850 void updateWallpaperVisibility(boolean visible) {
2851 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2852 int curflags = getWindow().getAttributes().flags
2853 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2854 if (wpflags != curflags) {
2855 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2856 }
Craig Mautner360310b2012-10-26 15:13:08 -07002857 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002858 }
2859
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002860 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2861 if (v instanceof LauncherTransitionable) {
2862 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2863 }
2864 }
2865
Michael Jurkabed61d22012-02-14 22:51:29 -08002866 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2867 if (v instanceof LauncherTransitionable) {
2868 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2869 }
Winson Chung70442722012-02-10 15:43:22 -08002870
2871 // Update the workspace transition step as well
2872 dispatchOnLauncherTransitionStep(v, 0f);
2873 }
2874
2875 private void dispatchOnLauncherTransitionStep(View v, float t) {
2876 if (v instanceof LauncherTransitionable) {
2877 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2878 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002879 }
2880
2881 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2882 if (v instanceof LauncherTransitionable) {
2883 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2884 }
Winson Chung70442722012-02-10 15:43:22 -08002885
2886 // Update the workspace transition step as well
2887 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002888 }
2889
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002890 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002891 * Things to test when changing the following seven functions.
2892 * - Home from workspace
2893 * - from center screen
2894 * - from other screens
2895 * - Home from all apps
2896 * - from center screen
2897 * - from other screens
2898 * - Back from all apps
2899 * - from center screen
2900 * - from other screens
2901 * - Launch app from workspace and quit
2902 * - with back
2903 * - with home
2904 * - Launch app from all apps and quit
2905 * - with back
2906 * - with home
2907 * - Go to a screen that's not the default, then all
2908 * apps, and launch and app, and go back
2909 * - with back
2910 * -with home
2911 * - On workspace, long press power and go back
2912 * - with back
2913 * - with home
2914 * - On all apps, long press power and go back
2915 * - with back
2916 * - with home
2917 * - On workspace, power off
2918 * - On all apps, power off
2919 * - Launch an app and turn off the screen while in that app
2920 * - Go back with home key
2921 * - Go back with back key TODO: make this not go to workspace
2922 * - From all apps
2923 * - From workspace
2924 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2925 * - From all apps
2926 * - From the center workspace
2927 * - From another workspace
2928 */
2929
2930 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002931 * Zoom the camera out from the workspace to reveal 'toView'.
2932 * Assumes that the view to show is anchored at either the very top or very bottom
2933 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002934 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002935 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002936 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2937 showAppsCustomizeHelper(animated, springLoaded, contentType);
2938 }
2939 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2940 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002941 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002942 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002943 mStateAnimation.cancel();
2944 mStateAnimation = null;
2945 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002946 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002947
Winson Chungf0ea4d32011-06-06 14:27:16 -07002948 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2949 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2950 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002951 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002952 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002953 final int startDelay =
2954 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002955
Michael Jurkab3e22d92011-10-31 15:58:33 -07002956 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002957
Michael Jurkad74c9842011-07-10 12:44:21 -07002958 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002959 Animator workspaceAnim =
2960 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08002961 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08002962 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
2963 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07002964 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2965 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002966
2967 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002968 toView.setScaleX(scale);
2969 toView.setScaleY(scale);
2970 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2971 scaleAnim.
2972 scaleX(1f).scaleY(1f).
2973 setDuration(duration).
2974 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002975
Winson Chungf0ea4d32011-06-06 14:27:16 -07002976 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002977 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002978 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002979 .ofFloat(toView, "alpha", 0f, 1f)
2980 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002981 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002982 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2983 @Override
2984 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002985 if (animation == null) {
2986 throw new RuntimeException("animation is null");
2987 }
Winson Chung70442722012-02-10 15:43:22 -08002988 float t = (Float) animation.getAnimatedValue();
2989 dispatchOnLauncherTransitionStep(fromView, t);
2990 dispatchOnLauncherTransitionStep(toView, t);
2991 }
2992 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002993
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002994 // toView should appear right at the end of the workspace shrink
2995 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002996 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002997 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002998 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002999
3000 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003001 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003002 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003003 // Prepare the position
3004 toView.setTranslationX(0.0f);
3005 toView.setTranslationY(0.0f);
3006 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003007 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003008 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003009 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003010 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003011 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3012 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003013
Winson Chungc7d2b602012-05-16 17:02:20 -07003014 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003015 if (mSearchDropTargetBar != null) {
3016 mSearchDropTargetBar.hideSearchBar(false);
3017 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003018 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003019 });
3020
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003021 if (workspaceAnim != null) {
3022 mStateAnimation.play(workspaceAnim);
3023 }
Michael Jurka1899a362011-11-03 13:50:45 -07003024
3025 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003026
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003027 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3028 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003029
3030 // If any of the objects being animated haven't been measured/laid out
3031 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003032 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003033 (mWorkspace.getMeasuredWidth() == 0) ||
3034 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003035 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003036 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003037
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003038 final AnimatorSet stateAnimation = mStateAnimation;
3039 final Runnable startAnimRunnable = new Runnable() {
3040 public void run() {
3041 // Check that mStateAnimation hasn't changed while
3042 // we waited for a layout/draw pass
3043 if (mStateAnimation != stateAnimation)
3044 return;
3045 setPivotsForZoom(toView, scale);
3046 dispatchOnLauncherTransitionStart(fromView, animated, false);
3047 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003048 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003049 }
3050 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003051 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003052 final ViewTreeObserver observer = toView.getViewTreeObserver();
3053 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3054 public void onGlobalLayout() {
3055 startAnimRunnable.run();
3056 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3057 }
3058 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003059 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003060 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003061 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003062 } else {
3063 toView.setTranslationX(0.0f);
3064 toView.setTranslationY(0.0f);
3065 toView.setScaleX(1.0f);
3066 toView.setScaleY(1.0f);
3067 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003068 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003069
Daniel Sandlere4f98912013-06-25 15:13:26 -04003070 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003071 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003072 if (mSearchDropTargetBar != null) {
3073 mSearchDropTargetBar.hideSearchBar(false);
3074 }
Michael Jurkaabded662011-03-04 12:06:57 -08003075 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003076 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003077 dispatchOnLauncherTransitionStart(fromView, animated, false);
3078 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003079 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003080 dispatchOnLauncherTransitionStart(toView, animated, false);
3081 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003082 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003083 }
3084
3085 /**
3086 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003087 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003088 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003089 */
Adam Cohened307df2013-10-02 09:37:31 -07003090 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003091 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003092
Michael Jurkab3e22d92011-10-31 15:58:33 -07003093 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003094 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003095 mStateAnimation.cancel();
3096 mStateAnimation = null;
3097 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003098 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003099
Winson Chungf0ea4d32011-06-06 14:27:16 -07003100 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003101 final int fadeOutDuration =
3102 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003103 final float scaleFactor = (float)
3104 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3105 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003106 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003107 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003108 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003109 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3110 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003111 toState, animated, stagger, -1);
3112 } else if (toState == Workspace.State.SPRING_LOADED ||
3113 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003114 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003115 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003116 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003117
Michael Jurkab3e22d92011-10-31 15:58:33 -07003118 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003119 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003120 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003121 final LauncherViewPropertyAnimator scaleAnim =
3122 new LauncherViewPropertyAnimator(fromView);
3123 scaleAnim.
3124 scaleX(scaleFactor).scaleY(scaleFactor).
3125 setDuration(duration).
3126 setInterpolator(new Workspace.ZoomInInterpolator());
3127
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003128 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003129 .ofFloat(fromView, "alpha", 1f, 0f)
3130 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003131 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003132 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3133 @Override
3134 public void onAnimationUpdate(ValueAnimator animation) {
3135 float t = 1f - (Float) animation.getAnimatedValue();
3136 dispatchOnLauncherTransitionStep(fromView, t);
3137 dispatchOnLauncherTransitionStep(toView, t);
3138 }
3139 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003140
Michael Jurka2ecf9952012-06-18 12:52:28 -07003141 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003142
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003143 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3144 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003145 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003146
3147 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003148 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003149 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003150 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003151 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3152 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003153 if (onCompleteRunnable != null) {
3154 onCompleteRunnable.run();
3155 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003156 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003157 }
3158 });
3159
Winson Chungf0ea4d32011-06-06 14:27:16 -07003160 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003161 if (workspaceAnim != null) {
3162 mStateAnimation.play(workspaceAnim);
3163 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003164 dispatchOnLauncherTransitionStart(fromView, animated, true);
3165 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003166 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003167 } else {
3168 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003169 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003170 dispatchOnLauncherTransitionStart(fromView, animated, true);
3171 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003172 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003173 dispatchOnLauncherTransitionStart(toView, animated, true);
3174 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003175 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003176 }
3177
Michael Jurkae326f182011-11-21 14:05:46 -08003178 @Override
3179 public void onTrimMemory(int level) {
3180 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003181 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003182 mAppsCustomizeTabHost.onTrimMemory();
3183 }
3184 }
3185
Adam Cohened307df2013-10-02 09:37:31 -07003186 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003187 showWorkspace(animated, null);
3188 }
3189
Adam Cohened307df2013-10-02 09:37:31 -07003190 protected void showWorkspace() {
3191 showWorkspace(true);
3192 }
3193
Adam Cohened66b2b2012-01-23 17:28:51 -08003194 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003195 if (mWorkspace.isInOverviewMode()) {
3196 mWorkspace.exitOverviewMode(animated);
3197 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003198 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003199 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003200 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003201 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003202
Winson Chungc7d2b602012-05-16 17:02:20 -07003203 // Show the search bar (only animate if we were showing the drop target bar in spring
3204 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003205 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003206 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003207 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003208
Michael Jurkab3e22d92011-10-31 15:58:33 -07003209 // Set focus to the AppsCustomize button
3210 if (mAllAppsButton != null) {
3211 mAllAppsButton.requestFocus();
3212 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003213 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003214
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003215 // Change the state *after* we've called all the transition code
3216 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003217
Winson Chung5fb63472011-02-02 17:03:37 -08003218 // Resume the auto-advance of widgets
3219 mUserPresent = true;
3220 updateRunning();
3221
alanv1d4fde62012-10-17 13:15:47 -07003222 // Send an accessibility event to announce the context change
3223 getWindow().getDecorView()
3224 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003225
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003226 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003227 }
3228
Adam Cohened307df2013-10-02 09:37:31 -07003229 void showOverviewMode(boolean animated) {
3230 mWorkspace.setVisibility(View.VISIBLE);
3231 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3232 mState = State.WORKSPACE;
3233 onWorkspaceShown(animated);
3234 }
3235
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003236 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003237 }
3238
Winson Chung82963d52013-10-09 11:20:57 -07003239 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3240 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003241 if (mState != State.WORKSPACE) return;
3242
Winson Chung82963d52013-10-09 11:20:57 -07003243 if (resetPageToZero) {
3244 mAppsCustomizeTabHost.reset();
3245 }
Winson Chungc58497e2013-09-03 17:48:37 -07003246 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003247 mAppsCustomizeTabHost.requestFocus();
3248
Michael Jurkab3e22d92011-10-31 15:58:33 -07003249 // Change the state *after* we've called all the transition code
3250 mState = State.APPS_CUSTOMIZE;
3251
3252 // Pause the auto-advance of widgets until we are out of AllApps
3253 mUserPresent = false;
3254 updateRunning();
3255 closeFolder();
3256
3257 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003258 getWindow().getDecorView()
3259 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003260 }
3261
Adam Cohen7777d962011-08-18 18:58:38 -07003262 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003263 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003264 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003265 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003266 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003267 }
Adam Cohen7777d962011-08-18 18:58:38 -07003268
Adam Cohenad4e15c2013-10-17 16:21:35 -07003269 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003270 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003271 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3272
Winson Chunge7a03942011-08-05 15:05:12 -07003273 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003274 @Override
3275 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003276 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003277 // Before we show workspace, hide all apps again because
3278 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3279 // clean up our state transition functions
3280 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003281 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003282 } else {
3283 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003284 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003285 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003286 }, delay);
3287
Winson Chung557d6ed2011-07-08 15:34:52 -07003288 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003289
Michael Jurkad3ef3062010-11-23 16:23:58 -08003290 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003291 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003292 final boolean animated = true;
3293 final boolean springLoaded = true;
3294 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003295 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003296 }
3297 // Otherwise, we are not in spring loaded mode, so don't do anything.
3298 }
3299
Michael Jurkab3e22d92011-10-31 15:58:33 -07003300 void lockAllApps() {
3301 // TODO
3302 }
3303
3304 void unlockAllApps() {
3305 // TODO
3306 }
3307
Winson Chungf0ea4d32011-06-06 14:27:16 -07003308 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003309 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003310 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003311 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003312 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003313 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003314 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003315 int duration = 0;
3316 if (mSearchDropTargetBar != null) {
3317 duration = mSearchDropTargetBar.getTransitionInDuration();
3318 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003319 mHotseat.animate().alpha(1f).setDuration(duration);
3320 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003321 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003322 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003323 }
3324 }
3325 }
3326
3327 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003328 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003329 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003330 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003331 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003332 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003333 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003334 int duration = 0;
3335 if (mSearchDropTargetBar != null) {
3336 duration = mSearchDropTargetBar.getTransitionOutDuration();
3337 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003338 mHotseat.animate().alpha(0f).setDuration(duration);
3339 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003340 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003341 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003342 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003343 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003344 }
3345
Patrick Dubroy5f445422011-02-18 14:35:21 -08003346 /**
3347 * Add an item from all apps or customize onto the given workspace screen.
3348 * If layout is null, add to the current screen.
3349 */
3350 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003351 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003352 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003353 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003354 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003355
Winson Chungdff8ebb2011-09-08 17:25:31 -07003356 /** Maps the current orientation to an index for referencing orientation correct global icons */
3357 private int getCurrentOrientationIndexForGlobalIcons() {
3358 // default - 0, landscape - 1
3359 switch (getResources().getConfiguration().orientation) {
3360 case Configuration.ORIENTATION_LANDSCAPE:
3361 return 1;
3362 default:
3363 return 0;
3364 }
3365 }
3366
Michael Jurkaae65ee32012-04-30 11:45:54 -07003367 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003368 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003369 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003370 // Look for the toolbar icon specified in the activity meta-data
3371 Bundle metaData = packageManager.getActivityInfo(
3372 activityName, PackageManager.GET_META_DATA).metaData;
3373 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003374 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003375 if (iconResId != 0) {
3376 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003377 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003378 }
3379 }
3380 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003381 // This can happen if the activity defines an invalid drawable
3382 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3383 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003384 } catch (Resources.NotFoundException nfe) {
3385 // This can happen if the activity defines an invalid drawable
3386 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3387 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003388 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003389 return null;
3390 }
3391
3392 // if successful in getting icon, return it; otherwise, set button to use default drawable
3393 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003394 int buttonId, ComponentName activityName, int fallbackDrawableId,
3395 String toolbarResourceName) {
3396 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003397 Resources r = getResources();
3398 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3399 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003400
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003401 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003402 // If we were unable to find the icon via the meta-data, use a generic one
3403 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003404 toolbarIcon = r.getDrawable(fallbackDrawableId);
3405 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003406 if (button != null) {
3407 button.setCompoundDrawables(toolbarIcon, null, null, null);
3408 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003409 return null;
3410 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003411 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003412 if (button != null) {
3413 button.setCompoundDrawables(toolbarIcon, null, null, null);
3414 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003415 return toolbarIcon.getConstantState();
3416 }
3417 }
3418
3419 // if successful in getting icon, return it; otherwise, set button to use default drawable
3420 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003421 int buttonId, ComponentName activityName, int fallbackDrawableId,
3422 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003423 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003424 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003425
Michael Jurka19e0fc52011-07-22 18:00:21 -07003426 if (button != null) {
3427 // If we were unable to find the icon via the meta-data, use a
3428 // generic one
3429 if (toolbarIcon == null) {
3430 button.setImageResource(fallbackDrawableId);
3431 } else {
3432 button.setImageDrawable(toolbarIcon);
3433 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003434 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003435
3436 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3437
Michael Jurka0423dcf2010-10-05 14:56:18 -07003438 }
3439
Winson Chung1b884092012-06-08 17:13:02 -07003440 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003441 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003442 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003443 }
3444
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003445 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003446 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003447 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003448 }
3449
Winson Chungbb185bd2011-11-21 12:31:42 -08003450 private void invalidatePressedFocusedStates(View container, View button) {
3451 if (container instanceof HolographicLinearLayout) {
3452 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3453 layout.invalidatePressedFocusedStates();
3454 } else if (button instanceof HolographicImageView) {
3455 HolographicImageView view = (HolographicImageView) button;
3456 view.invalidatePressedFocusedStates();
3457 }
3458 }
3459
Cristina Stancu476493b2013-08-07 17:20:14 +01003460 public View getQsbBar() {
3461 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003462 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3463 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003464 }
3465 return mQsbBar;
3466 }
3467
3468 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003469 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003470 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003471 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003472 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003473
Winson Chung1cad91e2011-05-25 17:41:01 -07003474 final SearchManager searchManager =
3475 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3476 ComponentName activityName = searchManager.getGlobalSearchActivity();
3477 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003478 int coi = getCurrentOrientationIndexForGlobalIcons();
3479 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003480 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3481 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3482 if (sGlobalSearchIcon[coi] == null) {
3483 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3484 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3485 TOOLBAR_ICON_METADATA_NAME);
3486 }
3487
Winson Chungc51db6a2011-10-05 11:44:49 -07003488 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3489 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003490 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003491 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003492 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003493 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003494 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3495 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003496 if (searchButton != null) searchButton.setVisibility(View.GONE);
3497 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003498 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003499 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003500 }
3501 }
3502
Cristina Stancu476493b2013-08-07 17:20:14 +01003503 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003504 final View searchButtonContainer = findViewById(R.id.search_button_container);
3505 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003506 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003507 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003508 }
3509
Cristina Stancu476493b2013-08-07 17:20:14 +01003510 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003511 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003512 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003513
Winson Chungc51db6a2011-10-05 11:44:49 -07003514 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003515 final SearchManager searchManager =
3516 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3517 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3518
3519 ComponentName activityName = null;
3520 if (globalSearchActivity != null) {
3521 // Check if the global search activity handles voice search
3522 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3523 intent.setPackage(globalSearchActivity.getPackageName());
3524 activityName = intent.resolveActivity(getPackageManager());
3525 }
3526
3527 if (activityName == null) {
3528 // Fallback: check if an activity other than the global search activity
3529 // resolves this
3530 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3531 activityName = intent.resolveActivity(getPackageManager());
3532 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003533 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003534 int coi = getCurrentOrientationIndexForGlobalIcons();
3535 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003536 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3537 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3538 if (sVoiceSearchIcon[coi] == null) {
3539 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3540 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3541 TOOLBAR_ICON_METADATA_NAME);
3542 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003543 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003544 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003545 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003546 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003547 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003548 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003549 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003550 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003551 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003552 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003553 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003554 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003555
Cristina Stancu476493b2013-08-07 17:20:14 +01003556 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003557 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3558 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003559 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003560 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003561 }
3562
Winson Chung5841efa2013-09-30 18:06:44 -07003563 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003564 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3565 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003566 boolean visible = !forceDisableVoiceButtonProxy &&
3567 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003568 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003569 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003570 }
3571 }
Winson Chung5841efa2013-09-30 18:06:44 -07003572
3573 /**
3574 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3575 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3576 */
3577 public void disableVoiceButtonProxy(boolean disabled) {
3578 updateVoiceButtonProxyVisible(disabled);
3579 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003580 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003581 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003582 */
3583 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003584 if (!DISABLE_MARKET_BUTTON) {
3585 final View marketButton = findViewById(R.id.market_button);
3586 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3587 // Find the app market activity by resolving an intent.
3588 // (If multiple app markets are installed, it will return the ResolverActivity.)
3589 ComponentName activityName = intent.resolveActivity(getPackageManager());
3590 if (activityName != null) {
3591 int coi = getCurrentOrientationIndexForGlobalIcons();
3592 mAppMarketIntent = intent;
3593 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3594 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3595 TOOLBAR_ICON_METADATA_NAME);
3596 marketButton.setVisibility(View.VISIBLE);
3597 } else {
3598 // We should hide and disable the view so that we don't try and restore the visibility
3599 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3600 marketButton.setVisibility(View.GONE);
3601 marketButton.setEnabled(false);
3602 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003603 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003604 }
3605
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003606 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003607 if (!DISABLE_MARKET_BUTTON) {
3608 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3609 Resources r = getResources();
3610 Drawable marketIconDrawable = d.newDrawable(r);
3611 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3612 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3613 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003614
Winson Chungd64a6662013-09-30 11:06:59 -07003615 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3616 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003617 }
3618
Michael Jurkad7c28052012-04-27 15:43:36 -07003619 @Override
3620 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003621 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003622 final List<CharSequence> text = event.getText();
3623 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003624 // Populate event with a fake title based on the current state.
3625 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003626 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003627 } else {
3628 text.add(getString(R.string.all_apps_home_button_label));
3629 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003630 return result;
3631 }
3632
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003633 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003634 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003635 */
3636 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3637 @Override
3638 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003639 closeSystemDialogs();
3640 }
3641 }
3642
3643 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003644 * Receives notifications whenever the appwidgets are reset.
3645 */
3646 private class AppWidgetResetObserver extends ContentObserver {
3647 public AppWidgetResetObserver() {
3648 super(new Handler());
3649 }
3650
3651 @Override
3652 public void onChange(boolean selfChange) {
3653 onAppWidgetReset();
3654 }
3655 }
3656
3657 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003658 * If the activity is currently paused, signal that we need to run the passed Runnable
3659 * in onResume.
3660 *
3661 * This needs to be called from incoming places where resources might have been loaded
3662 * while we are paused. That is becaues the Configuration might be wrong
3663 * when we're not running, and if it comes back to what it was when we
3664 * were paused, we are not restarted.
3665 *
3666 * Implementation of the method from LauncherModel.Callbacks.
3667 *
3668 * @return true if we are currently paused. The caller might be able to
3669 * skip some work in that case since we will come back again.
3670 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003671 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003672 if (mPaused) {
3673 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003674 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003675 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003676 }
3677 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003678 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003679 return true;
3680 } else {
3681 return false;
3682 }
3683 }
3684
Michael Jurkac402cd92013-05-20 15:49:32 +02003685 private boolean waitUntilResume(Runnable run) {
3686 return waitUntilResume(run, false);
3687 }
3688
Michael Jurka1e2f4652013-07-08 18:03:46 -07003689 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003690 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003691 }
3692
Michael Jurka7607c2f2013-04-03 14:33:19 -07003693 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003694 * If the activity is currently paused, signal that we need to re-run the loader
3695 * in onResume.
3696 *
3697 * This needs to be called from incoming places where resources might have been loaded
3698 * while we are paused. That is becaues the Configuration might be wrong
3699 * when we're not running, and if it comes back to what it was when we
3700 * were paused, we are not restarted.
3701 *
3702 * Implementation of the method from LauncherModel.Callbacks.
3703 *
3704 * @return true if we are currently paused. The caller might be able to
3705 * skip some work in that case since we will come back again.
3706 */
3707 public boolean setLoadOnResume() {
3708 if (mPaused) {
3709 Log.i(TAG, "setLoadOnResume");
3710 mOnResumeNeedsLoad = true;
3711 return true;
3712 } else {
3713 return false;
3714 }
3715 }
3716
3717 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003719 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003721 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003722 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003723 } else {
3724 return SCREEN_COUNT / 2;
3725 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003727
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 /**
3729 * Refreshes the shortcuts shown on the workspace.
3730 *
3731 * Implementation of the method from LauncherModel.Callbacks.
3732 */
3733 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003734 // If we're starting binding all over again, clear any bind calls we'd postponed in
3735 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3736 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003737 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003738
Winson Chungd64d1762013-08-20 14:37:16 -07003739 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003740 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003741 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003742
Michael Jurka05bf644e2011-11-30 20:28:53 -08003743 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003744 if (mHotseat != null) {
3745 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003746 }
3747 }
3748
Adam Cohendcd297f2013-06-18 13:13:40 -07003749 @Override
3750 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003751 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003752
Adam Cohen5084cba2013-09-03 12:01:16 -07003753 // If there are no screens, we need to have an empty screen
3754 if (orderedScreenIds.size() == 0) {
3755 mWorkspace.addExtraEmptyScreen();
3756 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003757
3758 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003759 // setCurrentPage() so ensure that all pages are added before calling this).
3760 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003761 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3762 mWorkspace.createCustomContentPage();
3763 }
Winson Chung64359a52013-07-08 17:17:08 -07003764 }
3765
3766 @Override
3767 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003768 // Log to disk
3769 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3770 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3771 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003772 int count = orderedScreenIds.size();
3773 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003774 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003775 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003776 }
3777
Adam Cohen39a06042013-07-19 14:30:12 -07003778 private boolean shouldShowWeightWatcher() {
3779 String spKey = LauncherAppState.getSharedPreferencesKey();
3780 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003781 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003782
3783 return show;
3784 }
3785
3786 private void toggleShowWeightWatcher() {
3787 String spKey = LauncherAppState.getSharedPreferencesKey();
3788 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3789 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3790
3791 show = !show;
3792
3793 SharedPreferences.Editor editor = sp.edit();
3794 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3795 editor.commit();
3796
3797 if (mWeightWatcher != null) {
3798 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3799 }
3800 }
3801
Winson Chungd64d1762013-08-20 14:37:16 -07003802 public void bindAppsAdded(final ArrayList<Long> newScreens,
3803 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003804 final ArrayList<ItemInfo> addAnimated,
3805 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003806 Runnable r = new Runnable() {
3807 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003808 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003809 }
3810 };
3811 if (waitUntilResume(r)) {
3812 return;
3813 }
3814
Winson Chungd64d1762013-08-20 14:37:16 -07003815 // Add the new screens
3816 bindAddScreens(newScreens);
3817
3818 // We add the items without animation on non-visible pages, and with
3819 // animations on the new page (which we will try and snap to).
3820 if (!addNotAnimated.isEmpty()) {
3821 bindItems(addNotAnimated, 0,
3822 addNotAnimated.size(), false);
3823 }
3824 if (!addAnimated.isEmpty()) {
3825 bindItems(addAnimated, 0,
3826 addAnimated.size(), true);
3827 }
Winson Chungc58497e2013-09-03 17:48:37 -07003828
Winson Chung87412982013-10-03 18:34:14 -07003829 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003830 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003831
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003832 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003833 addedApps != null && mAppsCustomizeContent != null) {
3834 mAppsCustomizeContent.addApps(addedApps);
3835 }
Winson Chungd64d1762013-08-20 14:37:16 -07003836 }
3837
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003838 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003839 * Bind the items start-end from the list.
3840 *
3841 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003842 */
Winson Chung64359a52013-07-08 17:17:08 -07003843 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3844 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003845 Runnable r = new Runnable() {
3846 public void run() {
3847 bindItems(shortcuts, start, end, forceAnimateIcons);
3848 }
3849 };
3850 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003851 return;
3852 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003853
Winson Chungf0c6ae02012-03-21 16:10:31 -07003854 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003855 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3856 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003857 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003858 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003859 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003860 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003861 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003862
3863 // Short circuit if we are loading dock items for a configuration which has no dock
3864 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3865 mHotseat == null) {
3866 continue;
3867 }
3868
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 switch (item.itemType) {
3870 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3871 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003872 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003873 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003874
Winson Chung64359a52013-07-08 17:17:08 -07003875 /*
3876 * TODO: FIX collision case
3877 */
Winson Chungce376632013-07-11 16:12:41 -07003878 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3879 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3880 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3881 throw new RuntimeException("OCCUPIED");
3882 }
Winson Chung64359a52013-07-08 17:17:08 -07003883 }
3884
Adam Cohendcd297f2013-06-18 13:13:40 -07003885 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3886 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003887 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003888 // Animate all the applications up now
3889 shortcut.setAlpha(0f);
3890 shortcut.setScaleX(0f);
3891 shortcut.setScaleY(0f);
3892 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003893 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003894 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003896 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003897 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003898 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003899 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003900 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3901 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003902 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003903 default:
3904 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003906 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003907
Winson Chung997a9232013-07-24 15:33:46 -07003908 if (animateIcons) {
3909 // Animate to the correct page
3910 if (newShortcutsScreenId > -1) {
3911 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003912 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003913 final Runnable startBounceAnimRunnable = new Runnable() {
3914 public void run() {
3915 anim.playTogether(bounceAnims);
3916 anim.start();
3917 }
3918 };
Winson Chung997a9232013-07-24 15:33:46 -07003919 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003920 // We post the animation slightly delayed to prevent slowdowns
3921 // when we are loading right after we return to launcher.
3922 mWorkspace.postDelayed(new Runnable() {
3923 public void run() {
3924 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003925 mWorkspace.postDelayed(startBounceAnimRunnable,
3926 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003927 }
3928 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003929 } else {
3930 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003931 }
3932 }
Winson Chung64359a52013-07-08 17:17:08 -07003933 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003934 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003935 }
3936
Joe Onorato9c1289c2009-08-17 11:03:03 -04003937 /**
3938 * Implementation of the method from LauncherModel.Callbacks.
3939 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003940 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003941 Runnable r = new Runnable() {
3942 public void run() {
3943 bindFolders(folders);
3944 }
3945 };
3946 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003947 return;
3948 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003949 sFolders.clear();
3950 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003951 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003952
3953 /**
3954 * Add the views for a widget to the workspace.
3955 *
3956 * Implementation of the method from LauncherModel.Callbacks.
3957 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003958 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003959 Runnable r = new Runnable() {
3960 public void run() {
3961 bindAppWidget(item);
3962 }
3963 };
3964 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003965 return;
3966 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003967
Daniel Sandler843e8602010-06-07 14:59:01 -04003968 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3969 if (DEBUG_WIDGETS) {
3970 Log.d(TAG, "bindAppWidget: " + item);
3971 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003972 final Workspace workspace = mWorkspace;
3973
3974 final int appWidgetId = item.appWidgetId;
3975 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003976 if (DEBUG_WIDGETS) {
3977 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3978 }
3979
Joe Onorato9c1289c2009-08-17 11:03:03 -04003980 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3981
Joe Onorato9c1289c2009-08-17 11:03:03 -04003982 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003983 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984
Adam Cohendcd297f2013-06-18 13:13:40 -07003985 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003986 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003987 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3988
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 workspace.requestLayout();
3990
Daniel Sandler843e8602010-06-07 14:59:01 -04003991 if (DEBUG_WIDGETS) {
3992 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3993 + (SystemClock.uptimeMillis()-start) + "ms");
3994 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003995 }
3996
Adam Cohen1462de32012-07-24 22:34:36 -07003997 public void onPageBoundSynchronously(int page) {
3998 mSynchronouslyBoundPages.add(page);
3999 }
4000
Joe Onorato9c1289c2009-08-17 11:03:03 -04004001 /**
4002 * Callback saying that there aren't any more items to bind.
4003 *
4004 * Implementation of the method from LauncherModel.Callbacks.
4005 */
Adam Cohene25af792013-06-06 23:08:25 -07004006 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004007 Runnable r = new Runnable() {
4008 public void run() {
4009 finishBindingItems(upgradePath);
4010 }
4011 };
4012 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004013 return;
4014 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004015 if (mSavedState != null) {
4016 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004017 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004018 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004019 mSavedState = null;
4020 }
4021
Adam Cohen1462de32012-07-24 22:34:36 -07004022 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004023
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004024 // If we received the result of any pending adds while the loader was running (e.g. the
4025 // widget configuration forced an orientation change), process them now.
4026 for (int i = 0; i < sPendingAddList.size(); i++) {
4027 completeAdd(sPendingAddList.get(i));
4028 }
4029 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030
Winson Chungc51db6a2011-10-05 11:44:49 -07004031 // Update the market app icon as necessary (the other icons will be managed in response to
4032 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004033 if (!DISABLE_MARKET_BUTTON) {
4034 updateAppMarketIcon();
4035 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004036
Winson Chungf0c6ae02012-03-21 16:10:31 -07004037 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004038 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004039 mWorkspace.getUniqueComponents(true, null);
4040 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004041 }
Adam Cohen99894d92013-06-14 11:22:59 -07004042
Adam Cohen66a01fd2013-06-11 12:48:00 -07004043 mWorkspace.post(new Runnable() {
4044 @Override
4045 public void run() {
4046 onFinishBindingItems();
4047 }
4048 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07004049 }
4050
Winson Chunga0b7e862013-09-05 16:03:15 -07004051 public boolean isAllAppsButtonRank(int rank) {
4052 if (mHotseat != null) {
4053 return mHotseat.isAllAppsButtonRank(rank);
4054 }
4055 return false;
4056 }
4057
Winson Chunga2413752012-04-03 14:22:34 -07004058 private boolean canRunNewAppsAnimation() {
4059 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4060 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4061 }
4062
Winson Chungc9168342013-06-26 14:54:55 -07004063 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4064 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4065 PropertyValuesHolder.ofFloat("alpha", 1f),
4066 PropertyValuesHolder.ofFloat("scaleX", 1f),
4067 PropertyValuesHolder.ofFloat("scaleY", 1f));
4068 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4069 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4070 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4071 return bounceAnim;
4072 }
4073
Adam Cohen27772732013-11-11 14:00:56 +00004074 public boolean useVerticalBarLayout() {
4075 return LauncherAppState.getInstance().getDynamicGrid().
4076 getDeviceProfile().isVerticalBarLayout();
4077 }
4078
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004079 protected Rect getSearchBarBounds() {
4080 return LauncherAppState.getInstance().getDynamicGrid().
4081 getDeviceProfile().getSearchBarBounds();
4082 }
4083
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004084 @Override
4085 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004086 boolean searchVisible = updateGlobalSearchIcon();
4087 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004088 if (mSearchDropTargetBar != null) {
4089 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4090 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004091 }
4092
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004093 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004094 * Add the icons for all apps.
4095 *
4096 * Implementation of the method from LauncherModel.Callbacks.
4097 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004098 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004099 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004100 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4101 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4102 getHotseat().addAllAppsFolder(mIconCache, apps,
4103 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4104 }
4105 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004106 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004107 if (mAppsCustomizeContent != null) {
4108 mAppsCustomizeContent.onPackagesUpdated(
4109 LauncherModel.getSortedWidgetsAndShortcuts(this));
4110 }
Winson Chungc58497e2013-09-03 17:48:37 -07004111 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004112 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004113 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004114 mAppsCustomizeContent.onPackagesUpdated(
4115 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004116 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004117 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004118 }
4119
4120 /**
4121 * A package was updated.
4122 *
4123 * Implementation of the method from LauncherModel.Callbacks.
4124 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004125 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004126 Runnable r = new Runnable() {
4127 public void run() {
4128 bindAppsUpdated(apps);
4129 }
4130 };
4131 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004132 return;
4133 }
4134
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004135 if (mWorkspace != null) {
4136 mWorkspace.updateShortcuts(apps);
4137 }
Winson Chungc58497e2013-09-03 17:48:37 -07004138
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004139 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004140 mAppsCustomizeContent != null) {
4141 mAppsCustomizeContent.updateApps(apps);
4142 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004143 }
4144
4145 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004146 * A package was uninstalled. We take both the super set of packageNames
4147 * in addition to specific applications to remove, the reason being that
4148 * this can be called when a package is updated as well. In that scenario,
4149 * we only remove specific components from the workspace, where as
4150 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004151 *
4152 * Implementation of the method from LauncherModel.Callbacks.
4153 */
Winson Chung83892cc2013-05-01 16:53:33 -07004154 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004155 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004156 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004157 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004158 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004159 }
Winson Chungd64d1762013-08-20 14:37:16 -07004160 };
4161 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004162 return;
4163 }
4164
Winson Chungdf95eb12013-10-16 14:57:07 -07004165 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004166 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004167 }
4168 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004169 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004170 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004171
Winson Chunga1820962011-10-03 16:31:06 -07004172 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004173 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004174
Winson Chungdf95eb12013-10-16 14:57:07 -07004175 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004176 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004177 mAppsCustomizeContent != null) {
4178 mAppsCustomizeContent.removeApps(appInfos);
4179 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004180 }
Joe Onoratobe386092009-11-17 17:32:16 -08004181
4182 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004183 * A number of packages were updated.
4184 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004185 private ArrayList<Object> mWidgetsAndShortcuts;
4186 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004187 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004188 bindPackagesUpdated(mWidgetsAndShortcuts);
4189 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004190 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004191 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004192 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4193 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4194 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004195 return;
4196 }
4197
Winson Chung64359a52013-07-08 17:17:08 -07004198 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004199 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004200 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004201 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004202 }
4203
Winson Chung400438b2011-01-16 17:53:48 -08004204 private int mapConfigurationOriActivityInfoOri(int configOri) {
4205 final Display d = getWindowManager().getDefaultDisplay();
4206 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4207 switch (d.getRotation()) {
4208 case Surface.ROTATION_0:
4209 case Surface.ROTATION_180:
4210 // We are currently in the same basic orientation as the natural orientation
4211 naturalOri = configOri;
4212 break;
4213 case Surface.ROTATION_90:
4214 case Surface.ROTATION_270:
4215 // We are currently in the other basic orientation to the natural orientation
4216 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4217 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4218 break;
4219 }
4220
4221 int[] oriMap = {
4222 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4223 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4224 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4225 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4226 };
4227 // Since the map starts at portrait, we need to offset if this device's natural orientation
4228 // is landscape.
4229 int indexOffset = 0;
4230 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4231 indexOffset = 1;
4232 }
4233 return oriMap[(d.getRotation() + indexOffset) % 4];
4234 }
Adam Cohen446e9402011-09-15 18:21:21 -07004235
Winson Chung4b919f82012-05-01 10:44:08 -07004236 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004237 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004238 getResources().getBoolean(R.bool.allow_rotation);
4239 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004240 }
Winson Chung4b919f82012-05-01 10:44:08 -07004241 public void lockScreenOrientation() {
4242 if (isRotationEnabled()) {
4243 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4244 .getConfiguration().orientation));
4245 }
4246 }
4247 public void unlockScreenOrientation(boolean immediate) {
4248 if (isRotationEnabled()) {
4249 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004250 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004251 } else {
4252 mHandler.postDelayed(new Runnable() {
4253 public void run() {
4254 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4255 }
4256 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004257 }
Winson Chung4b919f82012-05-01 10:44:08 -07004258 }
Winson Chung400438b2011-01-16 17:53:48 -08004259 }
4260
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004261 /**
4262 * Called when the SearchBar hint should be changed.
4263 *
4264 * @param hint the hint to be displayed in the search bar.
4265 */
4266 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004267 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004268 }
4269
Winson Chunge6eabff2013-09-24 11:01:23 -07004270 protected String getFirstRunClingSearchBarHint() {
4271 return "";
4272 }
4273 protected String getFirstRunCustomContentHint() {
4274 return "";
4275 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004276 protected int getFirstRunFocusedHotseatAppDrawableId() {
4277 return -1;
4278 }
4279 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4280 return null;
4281 }
4282 protected int getFirstRunFocusedHotseatAppRank() {
4283 return -1;
4284 }
4285 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4286 return "";
4287 }
4288 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4289 return "";
4290 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004291
Winson Chungaf40f202013-09-18 18:26:31 -07004292 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004293 mLauncherClings.dismissFirstRunCling(v);
4294 }
4295 public void dismissMigrationClingCopyApps(View v) {
4296 mLauncherClings.dismissMigrationClingCopyApps(v);
4297 }
4298 public void dismissMigrationClingUseDefault(View v) {
4299 mLauncherClings.dismissMigrationClingUseDefault(v);
4300 }
4301 public void dismissMigrationWorkspaceCling(View v) {
4302 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004303 }
Winson Chung82f55532011-08-09 14:14:23 -07004304 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004305 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004306 }
4307 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004308 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004309 }
4310
Winson Chunga6945242014-01-08 14:04:34 -08004311 private boolean shouldRunFirstRunActivity() {
4312 return !ActivityManager.isRunningInTestHarness();
4313 }
4314
4315 public void showFirstRunActivity() {
4316 if (shouldRunFirstRunActivity() && hasFirstRunActivity()
4317 && !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false)) {
4318 Intent firstRunIntent = getFirstRunActivity();
4319 if (firstRunIntent != null) {
4320 startActivity(firstRunIntent);
4321 markFirstRunActivityShown();
4322 }
4323 }
4324 }
4325
4326 private void markFirstRunActivityShown() {
4327 SharedPreferences.Editor editor = mSharedPrefs.edit();
4328 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4329 editor.apply();
4330 }
4331
4332 void showWorkspaceSearchAndHotseat() {
4333 mWorkspace.setAlpha(1f);
4334 mHotseat.setAlpha(1f);
4335 mPageIndicators.setAlpha(1f);
4336 mSearchDropTargetBar.showSearchBar(false);
4337 }
4338
4339 void hideWorkspaceSearchAndHotseat() {
4340 mWorkspace.setAlpha(0f);
4341 mHotseat.setAlpha(0f);
4342 mPageIndicators.setAlpha(0f);
4343 mSearchDropTargetBar.hideSearchBar(false);
4344 }
4345
4346
Mathew Inwood72fbec12013-11-19 15:45:07 +00004347 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4348 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4349 if (ri == null) {
4350 return null;
4351 }
4352 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4353 }
4354
4355 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4356 Bitmap icon) {
4357 return new ShortcutInfo(shortcutIntent, caption, icon);
4358 }
4359
4360 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4361 dragView.setTag(dragInfo);
4362 mWorkspace.onDragStartedWithItem(dragView);
4363 mWorkspace.beginDragShared(dragView, source);
4364 }
4365
Winson Chung80baf5a2010-08-09 16:03:15 -07004366 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004367 * Prints out out state for debugging.
4368 */
4369 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004370 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004371 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004372 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4373 Log.d(TAG, "mRestoring=" + mRestoring);
4374 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4375 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004376 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004377 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004378
Winson Chung785d2eb2011-04-14 16:08:02 -07004379 if (mAppsCustomizeContent != null) {
4380 mAppsCustomizeContent.dumpState();
4381 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004382 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004383 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004384
4385 @Override
4386 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4387 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004388 synchronized (sDumpLogs) {
4389 writer.println(" ");
4390 writer.println("Debug logs: ");
4391 for (int i = 0; i < sDumpLogs.size(); i++) {
4392 writer.println(" " + sDumpLogs.get(i));
4393 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004394 }
4395 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004396
4397 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004398 if (DEBUG_DUMP_LOG) {
4399 synchronized (sDumpLogs) {
4400 Log.d(TAG, "");
4401 Log.d(TAG, "*********************");
4402 Log.d(TAG, "Launcher debug logs: ");
4403 for (int i = 0; i < sDumpLogs.size(); i++) {
4404 Log.d(TAG, " " + sDumpLogs.get(i));
4405 }
4406 Log.d(TAG, "*********************");
4407 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004408 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004409 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004410 }
4411
4412 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004413 addDumpLog(tag, log, null, debugLog);
4414 }
4415
4416 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004417 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004418 if (e != null) {
4419 Log.d(tag, log, e);
4420 } else {
4421 Log.d(tag, log);
4422 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004423 }
Winson Chungede41292013-09-19 16:27:36 -07004424 if (DEBUG_DUMP_LOG) {
4425 sDateStamp.setTime(System.currentTimeMillis());
4426 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004427 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4428 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004429 }
Winson Chungede41292013-09-19 16:27:36 -07004430 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004431 }
4432
Winson Chungede41292013-09-19 16:27:36 -07004433 public void dumpLogsToLocalData() {
4434 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004435 new AsyncTask<Void, Void, Void>() {
4436 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004437 boolean success = false;
4438 sDateStamp.setTime(sRunStart);
4439 String FILENAME = sDateStamp.getMonth() + "-"
4440 + sDateStamp.getDay() + "_"
4441 + sDateStamp.getHours() + "-"
4442 + sDateStamp.getMinutes() + "_"
4443 + sDateStamp.getSeconds() + ".txt";
4444
4445 FileOutputStream fos = null;
4446 File outFile = null;
4447 try {
4448 outFile = new File(getFilesDir(), FILENAME);
4449 outFile.createNewFile();
4450 fos = new FileOutputStream(outFile);
4451 } catch (Exception e) {
4452 e.printStackTrace();
4453 }
4454 if (fos != null) {
4455 PrintWriter writer = new PrintWriter(fos);
4456
4457 writer.println(" ");
4458 writer.println("Debug logs: ");
4459 synchronized (sDumpLogs) {
4460 for (int i = 0; i < sDumpLogs.size(); i++) {
4461 writer.println(" " + sDumpLogs.get(i));
4462 }
4463 }
4464 writer.close();
4465 }
4466 try {
4467 if (fos != null) {
4468 fos.close();
4469 success = true;
4470 }
4471 } catch (IOException e) {
4472 e.printStackTrace();
4473 }
Michael Jurka43467462013-11-14 12:03:58 +01004474 return null;
Winson Chungede41292013-09-19 16:27:36 -07004475 }
Michael Jurka43467462013-11-14 12:03:58 +01004476 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004477 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004479}
Michael Jurka2763be32011-02-24 11:19:57 -08004480
Michael Jurkaabded662011-03-04 12:06:57 -08004481interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004482 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004483 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004484 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004485 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004486 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004487}
Dan Sandlerd5024042014-01-09 15:01:33 -05004488
4489interface DebugIntents {
4490 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4491 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004492}