blob: 2172194127fc4bfb6ac3ecb2f0d3b275c9add3aa [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;
Winson Chunge43a1e72014-01-15 10:33:02 -080044import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070046import android.content.pm.PackageManager.NameNotFoundException;
Mathew Inwood72fbec12013-11-19 15:45:07 +000047import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070053import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070071import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080086import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070089import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherActivityInfoCompat;
99import com.android.launcher3.compat.LauncherAppsCompat;
100import com.android.launcher3.compat.UserHandleCompat;
101import com.android.launcher3.compat.UserManagerCompat;
Daniel Sandler325dc232013-06-05 22:57:57 -0400102import com.android.launcher3.DropTarget.DragObject;
Anjali Koppalf5d29132014-02-28 13:33:27 -0800103import com.android.launcher3.PagedView.PageSwitchListener;
Romain Guyedcce092010-03-04 13:03:17 -0800104
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.DataInputStream;
106import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700108import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700115import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700118import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000119import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120
Winson Chunga6945242014-01-08 14:04:34 -0800121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122/**
123 * Default launcher application.
124 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100125public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700126 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700127 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800128 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700129 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
Daniel Sandlerf061f822013-06-27 13:59:36 -0400131 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400132 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700133 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400134 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700135 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700136
Dan Sandlerd5024042014-01-09 15:01:33 -0500137 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700142 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700143 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Michael Jurka8b805b12012-04-18 14:23:14 -0700145 private static final int REQUEST_BIND_APPWIDGET = 11;
146
Mathew Inwood876a8462013-06-14 14:12:41 +0100147 /**
148 * IntentStarter uses request codes starting with this. This must be greater than all activity
149 * request codes used internally.
150 */
151 protected static final int REQUEST_LAST = 100;
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
154
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800155 static final int SCREEN_COUNT = 5;
156 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Romain Guy98d01652009-06-30 16:21:04 -0700158 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800159 // To turn on these properties, type
160 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
161 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
162 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800163 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chung2672ff92012-05-04 16:22:30 -0700165 // The Intent extra that defines whether to ignore the launch animation
166 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400167 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700168
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
170 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700171 // Type: int
172 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
175 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
177 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700178 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: boolean
182 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
183 // Type: long
184 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700185 // Type: int
186 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
187 // Type: int
188 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000193 // Type: int[]
194 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Adam Cohenb54a5982014-01-08 15:21:04 -0800196
Adam Cohen432609a2014-03-13 17:03:22 -0700197 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
199
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100200 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700201 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100202 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700203 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100204 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700205
Adam Cohen39a06042013-07-19 14:30:12 -0700206 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700207 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700208
Winson Chunga6945242014-01-08 14:04:34 -0800209 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
210
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700212 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700213 private State mState = State.WORKSPACE;
214 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700217 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800218 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT_FOLDER_CLOSE = 400;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700219 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700220 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800223 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000225 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
226 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
227
Winson Chunga2413752012-04-03 14:22:34 -0700228 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700229 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
230 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700231 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700232
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800233 private final BroadcastReceiver mCloseSystemDialogsReceiver
234 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800235 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 private LayoutInflater mInflater;
238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700240 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800241 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800242 private DragLayer mDragLayer;
243 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700244 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800245 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700246
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700247 private AppWidgetManager mAppWidgetManager;
248 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700249
Michael Jurkac9d95c52011-08-29 14:03:34 -0700250 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700251 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800252 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700253
Michael Jurka0280c3b2010-09-17 15:00:07 -0700254 private int[] mTmpAddItemCellCoordinates = new int[2];
255
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 private FolderInfo mFolderInfo;
257
Winson Chung3d503fb2011-07-13 17:25:49 -0700258 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800259 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700260
Michael Jurka838a4ca2011-02-07 13:33:06 -0800261 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700262
Winson Chungc51db6a2011-10-05 11:44:49 -0700263 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700264 private AppsCustomizeTabHost mAppsCustomizeTabHost;
265 private AppsCustomizePagedView mAppsCustomizeContent;
266 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800267 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700270 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
271 // scroll issues (because the workspace may not have been measured yet) and extra work.
272 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
273 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
275 private SpannableStringBuilder mDefaultKeySsb = null;
276
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277 private boolean mWorkspaceLoading = true;
278
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800279 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 private boolean mRestoring;
281 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700282 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
Michael Jurka1e2f4652013-07-08 18:03:46 -0700284 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700285 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
286
Winson Chung4a2afa32012-07-19 14:53:05 -0700287 // Keep track of whether the user has left launcher
288 private static boolean sPausedFromUserAction = false;
289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private Bundle mSavedInstanceState;
291
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800293 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800294 private boolean mUserPresent = true;
295 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700296 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800297 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700299 private static LocaleConfiguration sLocaleConfiguration = null;
300
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700301 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700302
Adam Cohen61f560d2013-09-30 15:58:20 -0700303 private View.OnTouchListener mHapticFeedbackTouchListener;
304
Adam Cohended9f8d2010-11-03 13:25:16 -0700305 // Related to the auto-advancing of widgets
306 private final int ADVANCE_MSG = 1;
307 private final int mAdvanceInterval = 20000;
308 private final int mAdvanceStagger = 250;
309 private long mAutoAdvanceSentTime;
310 private long mAutoAdvanceTimeLeft = -1;
311 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
312 new HashMap<View, AppWidgetProviderInfo>();
313
Winson Chung400438b2011-01-16 17:53:48 -0800314 // Determines how long to wait after a rotation before restoring the screen orientation to
315 // match the sensor state.
316 private final int mRestoreScreenOrientationDelay = 500;
317
Michael Jurka4ef207b2010-11-29 17:05:45 -0800318 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700319 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
320 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
321 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800322
Winson Chungd64a6662013-09-30 11:06:59 -0700323 private Intent mAppMarketIntent = null;
324 private static final boolean DISABLE_MARKET_BUTTON = true;
325
Craig Mautner360310b2012-10-26 15:13:08 -0700326 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700327
Adam Cohen1462de32012-07-24 22:34:36 -0700328 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700329 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700330
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700331 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700332 static Date sDateStamp = new Date();
333 static DateFormat sDateFormat =
334 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
335 static long sRunStart = System.currentTimeMillis();
336 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700337
Winson Chung46353de2012-02-16 14:05:10 -0800338 // We only want to get the SharedPreferences once since it does an FS stat each time we get
339 // it from the context.
340 private SharedPreferences mSharedPrefs;
341
Adam Cohene25af792013-06-06 23:08:25 -0700342 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
343
Winson Chungf0c6ae02012-03-21 16:10:31 -0700344 // Holds the page that we need to animate to, and the icon views that we need to animate up
345 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700346 private ImageView mFolderIconImageView;
347 private Bitmap mFolderIconBitmap;
348 private Canvas mFolderIconCanvas;
349 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700350
Michael Jurkaddd62e92011-02-16 17:49:14 -0800351 private BubbleTextView mWaitingForResume;
352
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 private Runnable mBuildLayersRunnable = new Runnable() {
354 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700355 if (mWorkspace != null) {
356 mWorkspace.buildPageHardwareLayers();
357 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700358 }
359 };
360
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 private static ArrayList<PendingAddArguments> sPendingAddList
362 = new ArrayList<PendingAddArguments>();
363
Michael Jurka7267fa52013-09-26 15:29:57 -0700364 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800365
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 private static class PendingAddArguments {
367 int requestCode;
368 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700369 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700370 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800371 int cellX;
372 int cellY;
373 }
374
Daniel Sandlerff02d492013-08-05 02:12:05 -0400375 private Stats mStats;
376
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800377 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800378 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700379 }
380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 @Override
382 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700383 if (DEBUG_STRICT_MODE) {
384 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
385 .detectDiskReads()
386 .detectDiskWrites()
387 .detectNetwork() // or .detectAll() for all detectable problems
388 .penaltyLog()
389 .build());
390 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
391 .detectLeakedSqlLiteObjects()
392 .detectLeakedClosableObjects()
393 .penaltyLog()
394 .penaltyDeath()
395 .build());
396 }
397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400399
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400400 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400401 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700402 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700403 // Determine the dynamic grid properties
404 Point smallestSize = new Point();
405 Point largestSize = new Point();
406 Point realSize = new Point();
407 Display display = getWindowManager().getDefaultDisplay();
408 display.getCurrentSizeRange(smallestSize, largestSize);
409 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700410 DisplayMetrics dm = new DisplayMetrics();
411 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700412
Winson Chung5f8afe62013-08-12 16:19:28 -0700413 // Lazy-initialize the dynamic grid
414 DeviceProfile grid = app.initDynamicGrid(this,
415 Math.min(smallestSize.x, smallestSize.y),
416 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700417 realSize.x, realSize.y,
418 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700419
420 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400421 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700422 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 mModel = app.setLauncher(this);
424 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700425 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400426 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800427 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Daniel Sandlerff02d492013-08-05 02:12:05 -0400430 mStats = new Stats(this);
431
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700433
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
435 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700437 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
438 // this also ensures that any synchronous binding below doesn't re-trigger another
439 // LauncherModel load.
440 mPaused = false;
441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200443 android.os.Debug.startMethodTracing(
444 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 }
446
Adam Cohen53805212013-10-01 10:39:23 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100452 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800454 registerContentObservers();
455
Joe Onorato7c312c12009-08-13 21:36:53 -0700456 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700457
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 mSavedState = savedInstanceState;
459 restoreState(mSavedState);
460
461 if (PROFILE_STARTUP) {
462 android.os.Debug.stopMethodTracing();
463 }
464
465 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700466 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 // If the user leaves launcher, then we should just load items asynchronously when
468 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500469 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 } else {
471 // We only load the page synchronously if the user rotates (or triggers a
472 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800473 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700474 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 }
476
477 // For handling default keys
478 mDefaultKeySsb = new SpannableStringBuilder();
479 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800480
481 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
482 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800483
Adam Cohenf9426d52012-06-04 17:26:21 -0700484 updateGlobalIcons();
485
486 // On large interfaces, we want the screen to auto-rotate based on the current orientation
487 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700488
Adam Cohen432609a2014-03-13 17:03:22 -0700489 if (shouldShowIntroScreen()) {
490 showIntroScreen();
491 } else {
492 showFirstRunActivity();
493 }
494
Winson Chunge43a1e72014-01-15 10:33:02 -0800495 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
496 // on the device, then we always show the first run cling experience (or if there is no
497 // launcher2). Otherwise, we prompt the user upon started for migration
Winson Chunge43a1e72014-01-15 10:33:02 -0800498 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
499 if (mModel.canMigrateFromOldLauncherDb(this)) {
500 mLauncherClings.showMigrationCling();
501 } else {
502 mLauncherClings.showFirstRunCling();
503 }
Winson Chunga6945242014-01-08 14:04:34 -0800504 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800505 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800506 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700507 }
508
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700509 @Override
510 public void onLauncherProviderChange() { }
511
Winson Chung4a2afa32012-07-19 14:53:05 -0700512 protected void onUserLeaveHint() {
513 super.onUserLeaveHint();
514 sPausedFromUserAction = true;
515 }
516
Winson Chung98ca0f72013-07-29 12:58:51 -0700517 /** To be overriden by subclasses to hint to Launcher that we have custom content */
518 protected boolean hasCustomContentToLeft() {
519 return false;
520 }
521
Dave Hawkeya8881582013-09-17 15:55:33 -0600522 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500523 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600524 * {@link #addToCustomContentPage}. This will only be invoked if
525 * {@link #hasCustomContentToLeft()} is {@code true}.
526 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500527 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600528 }
529
530 /**
531 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
532 * ensure the custom content page is added or removed if necessary.
533 */
534 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600535 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600536 // Not bound yet, wait for bindScreens to be called.
537 return;
538 }
539
540 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
541 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500542 mWorkspace.createCustomContentContainer();
543 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600544 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
545 mWorkspace.removeCustomContentPage();
546 }
547 }
548
Adam Cohenf9426d52012-06-04 17:26:21 -0700549 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700550 boolean searchVisible = false;
551 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800552 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700553 int coi = getCurrentOrientationIndexForGlobalIcons();
554 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
555 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700556 if (!DISABLE_MARKET_BUTTON) {
557 updateAppMarketIcon();
558 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700559 searchVisible = updateGlobalSearchIcon();
560 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700561 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700562 if (sGlobalSearchIcon[coi] != null) {
563 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700564 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700565 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700566 if (sVoiceSearchIcon[coi] != null) {
567 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700568 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700569 }
Winson Chungd64a6662013-09-30 11:06:59 -0700570 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700571 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800572 }
Winson Chungadf0c182012-08-23 14:59:07 -0700573 if (mSearchDropTargetBar != null) {
574 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
575 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
Romain Guycbb89e42009-06-08 15:52:54 -0700577
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700579 if (sLocaleConfiguration == null) {
580 new AsyncTask<Void, Void, LocaleConfiguration>() {
581 @Override
582 protected LocaleConfiguration doInBackground(Void... unused) {
583 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
584 readConfiguration(Launcher.this, localeConfiguration);
585 return localeConfiguration;
586 }
587
588 @Override
589 protected void onPostExecute(LocaleConfiguration result) {
590 sLocaleConfiguration = result;
591 checkForLocaleChange(); // recursive, but now with a locale configuration
592 }
593 }.execute();
594 return;
595 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 final Configuration configuration = getResources().getConfiguration();
598
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700599 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final String locale = configuration.locale.toString();
601
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 final int mcc = configuration.mcc;
604
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 final int mnc = configuration.mnc;
607
Romain Guy84f296c2009-11-04 15:00:44 -0800608 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609
Romain Guy84f296c2009-11-04 15:00:44 -0800610 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700611 sLocaleConfiguration.locale = locale;
612 sLocaleConfiguration.mcc = mcc;
613 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700614
Joe Onorato0589f0f2010-02-08 13:44:00 -0800615 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616
617 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100618 new AsyncTask<Void, Void, Void>() {
619 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700620 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100621 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 }
Michael Jurka43467462013-11-14 12:03:58 +0100623 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700624 }
625 }
626
627 private static class LocaleConfiguration {
628 public String locale;
629 public int mcc = -1;
630 public int mnc = -1;
631 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700632
Romain Guy98d01652009-06-30 16:21:04 -0700633 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
634 DataInputStream in = null;
635 try {
636 in = new DataInputStream(context.openFileInput(PREFERENCES));
637 configuration.locale = in.readUTF();
638 configuration.mcc = in.readInt();
639 configuration.mnc = in.readInt();
640 } catch (FileNotFoundException e) {
641 // Ignore
642 } catch (IOException e) {
643 // Ignore
644 } finally {
645 if (in != null) {
646 try {
647 in.close();
648 } catch (IOException e) {
649 // Ignore
650 }
651 }
652 }
653 }
654
655 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
656 DataOutputStream out = null;
657 try {
658 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
659 out.writeUTF(configuration.locale);
660 out.writeInt(configuration.mcc);
661 out.writeInt(configuration.mnc);
662 out.flush();
663 } catch (FileNotFoundException e) {
664 // Ignore
665 } catch (IOException e) {
666 //noinspection ResultOfMethodCallIgnored
667 context.getFileStreamPath(PREFERENCES).delete();
668 } finally {
669 if (out != null) {
670 try {
671 out.close();
672 } catch (IOException e) {
673 // Ignore
674 }
675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 }
678
Anton Hanssona2f665f2013-09-26 12:18:32 +0100679 public Stats getStats() {
680 return mStats;
681 }
682
Bjorn Bringertc459e522013-06-07 19:36:01 +0100683 public LayoutInflater getInflater() {
684 return mInflater;
685 }
686
Winson Chung36a62fe2012-05-06 18:04:42 -0700687 boolean isDraggingEnabled() {
688 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
689 // that is subsequently removed from the workspace in startBinding().
690 return !mModel.isLoadingWorkspace();
691 }
692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 static int getScreen() {
694 synchronized (sLock) {
695 return sScreen;
696 }
697 }
698
699 static void setScreen(int screen) {
700 synchronized (sLock) {
701 sScreen = screen;
702 }
703 }
704
Winson Chung557d6ed2011-07-08 15:34:52 -0700705 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000706 * Copied from View -- the View version of the method isn't called
707 * anywhere else in our process and only exists for API level 17+,
708 * so it's ok to keep our own version with no API requirement.
709 */
710 public static int generateViewId() {
711 for (;;) {
712 final int result = sNextGeneratedId.get();
713 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
714 int newValue = result + 1;
715 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
716 if (sNextGeneratedId.compareAndSet(result, newValue)) {
717 return result;
718 }
719 }
720 }
721
722 public int getViewIdForItem(ItemInfo info) {
723 // This cast is safe given the > 2B range for int.
724 int itemId = (int) info.id;
725 if (mItemIdToViewId.containsKey(itemId)) {
726 return mItemIdToViewId.get(itemId);
727 }
728 int viewId = generateViewId();
729 mItemIdToViewId.put(itemId, viewId);
730 return viewId;
731 }
732
733 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700734 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
735 * a configuration step, this allows the proper animations to run after other transitions.
736 */
737 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700738 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 case REQUEST_PICK_SHORTCUT:
741 processShortcut(args.intent);
742 break;
743 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700744 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700745 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700746 result = true;
747 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800748 case REQUEST_CREATE_APPWIDGET:
749 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700751 result = true;
752 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800753 }
Michael Jurka27614d22012-04-02 06:40:22 -0700754 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
755 // if you turned the screen off and then back while in All Apps, Launcher would not
756 // return to the workspace. Clearing mAddInfo.container here fixes this issue
757 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700758 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800759 }
760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 protected void onActivityResult(
763 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700764 // Reset the startActivity waiting flag
765 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800766 int pendingAddWidgetId = mPendingAddWidgetId;
767 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700768
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 Runnable exitSpringLoaded = new Runnable() {
770 @Override
771 public void run() {
772 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
773 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
774 }
775 };
776
Michael Jurka8b805b12012-04-18 14:23:14 -0700777 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700779 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
780 if (resultCode == RESULT_CANCELED) {
781 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
783 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700784 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700785 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
786 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700787 }
788 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200789 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
790 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
791 mWorkspace.exitOverviewMode(false);
792 }
793 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700794 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200795
Adam Cohened66b2b2012-01-23 17:28:51 -0800796 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
797 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700798
Adam Cohened66b2b2012-01-23 17:28:51 -0800799 // We have special handling for widgets
800 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800801 final int appWidgetId;
802 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
803 : -1;
804 if (widgetId < 0) {
805 appWidgetId = pendingAddWidgetId;
806 } else {
807 appWidgetId = widgetId;
808 }
809
Adam Cohenad4e15c2013-10-17 16:21:35 -0700810 final int result;
811 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800812 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700813 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
814 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700815 result = RESULT_CANCELED;
816 completeTwoStageWidgetDrop(result, appWidgetId);
817 onComplete = new Runnable() {
818 @Override
819 public void run() {
820 exitSpringLoadedDragModeDelayed(false, 0, null);
821 }
822 };
Winson Chung5aaab772012-04-27 15:24:02 -0700823 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700824 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700825 final CellLayout dropLayout =
826 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
827 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700828 onComplete = new Runnable() {
829 @Override
830 public void run() {
831 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700832 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700833 }
834 };
Winson Chung5aaab772012-04-27 15:24:02 -0700835 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700836 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
837 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800838 return;
839 }
840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 // The pattern used here is that a user PICKs a specific application,
842 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
845 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700846 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800847 final PendingAddArguments args = new PendingAddArguments();
848 args.requestCode = requestCode;
849 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 args.cellX = mPendingAddInfo.cellX;
853 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800854 if (isWorkspaceLocked()) {
855 sPendingAddList.add(args);
856 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700857 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700859 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
860 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700861 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700862 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
863 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 }
867
868 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700869 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700870 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 Runnable onCompleteRunnable = null;
872 int animationType = 0;
873
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700874 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 if (resultCode == RESULT_OK) {
876 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
877 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700878 mPendingAddWidgetInfo);
879 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 onCompleteRunnable = new Runnable() {
881 @Override
882 public void run() {
883 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700885 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
886 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800887 }
888 };
889 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800890 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700893 if (mDragLayer.getAnimatedView() != null) {
894 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
895 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
896 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700897 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700898 // The animated view may be null in the case of a rotation during widget configuration
899 onCompleteRunnable.run();
900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
902
903 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700904 protected void onStop() {
905 super.onStop();
906 FirstFrameAnimatorHelper.setIsVisible(false);
907 }
908
909 @Override
910 protected void onStart() {
911 super.onStart();
912 FirstFrameAnimatorHelper.setIsVisible(true);
913 }
914
915 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200917 long startTime = 0;
918 if (DEBUG_RESUME_TIME) {
919 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400920 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700923
Winson Chung4a2afa32012-07-19 14:53:05 -0700924 // Restore the previous launcher state
925 if (mOnResumeState == State.WORKSPACE) {
926 showWorkspace(false);
927 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800928 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700929 }
930 mOnResumeState = State.NONE;
931
Craig Mautner360310b2012-10-26 15:13:08 -0700932 // Background was set to gradient in onPause(), restore to black if in all apps.
933 setWorkspaceBackground(mState == State.WORKSPACE);
934
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800935 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700936 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700937 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500939 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700941 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700943 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200944 // We might have postponed some bind calls until onResume (see waitUntilResume) --
945 // execute them here
946 long startTimeCallbacks = 0;
947 if (DEBUG_RESUME_TIME) {
948 startTimeCallbacks = System.currentTimeMillis();
949 }
950
951 if (mAppsCustomizeContent != null) {
952 mAppsCustomizeContent.setBulkBind(true);
953 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700954 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
955 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200956 }
957 if (mAppsCustomizeContent != null) {
958 mAppsCustomizeContent.setBulkBind(false);
959 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700960 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200961 if (DEBUG_RESUME_TIME) {
962 Log.d(TAG, "Time spent processing callbacks in onResume: " +
963 (System.currentTimeMillis() - startTimeCallbacks));
964 }
Michael Jurka447bf842013-05-15 14:52:15 +0200965 }
Michael Jurka54554252013-08-01 12:52:23 +0200966 if (mOnResumeCallbacks.size() > 0) {
967 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
968 mOnResumeCallbacks.get(i).run();
969 }
970 mOnResumeCallbacks.clear();
971 }
Winson Chunge4e50662012-01-23 14:45:13 -0800972
973 // Reset the pressed state of icons that were locked in the press state while activities
974 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800975 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800976 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800977 mWaitingForResume.setStayPressed(false);
978 }
Winson Chunge4e50662012-01-23 14:45:13 -0800979 if (mAppsCustomizeContent != null) {
980 // Resets the previous all apps icon press state
981 mAppsCustomizeContent.resetDrawableState();
982 }
Winson Chung82963d52013-10-09 11:20:57 -0700983
Adam Cohen06dff352012-06-01 17:17:08 -0700984 // It is possible that widgets can receive updates while launcher is not in the foreground.
985 // Consequently, the widgets will be inflated in the orientation of the foreground activity
986 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
987 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700988 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700989
Winson Chung780fe592013-09-26 14:48:44 -0700990 // Process any items that were added while Launcher was away.
991 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
992
Winson Chung5841efa2013-09-30 18:06:44 -0700993 // Update the voice search button proxy
994 updateVoiceButtonProxyVisible(false);
995
Adam Cohenf9426d52012-06-04 17:26:21 -0700996 // Again, as with the above scenario, it's possible that one or more of the global icons
997 // were updated in the wrong orientation.
998 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200999 if (DEBUG_RESUME_TIME) {
1000 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1001 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001002
1003 if (mWorkspace.getCustomContentCallbacks() != null) {
1004 // If we are resuming and the custom content is the current page, we call onShow().
1005 // It is also poassible that onShow will instead be called slightly after first layout
1006 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1007 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001008 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001009 }
1010 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001011 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001012 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001013 }
1014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001016 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001017 // Ensure that items added to Launcher are queued until Launcher returns
1018 InstallShortcutReceiver.enableInstallQueue();
1019
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001020 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001021 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001022 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001023 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001024
1025 // We call onHide() aggressively. The custom content callbacks should be able to
1026 // debounce excess onHide calls.
1027 if (mWorkspace.getCustomContentCallbacks() != null) {
1028 mWorkspace.getCustomContentCallbacks().onHide();
1029 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001030 }
Romain Guycbb89e42009-06-08 15:52:54 -07001031
Adam Cohenbffe7452013-07-22 18:21:45 -07001032 QSBScroller mQsbScroller = new QSBScroller() {
1033 int scrollY = 0;
1034
1035 @Override
1036 public void setScrollY(int scroll) {
1037 scrollY = scroll;
1038
1039 if (mWorkspace.isOnOrMovingToCustomContent()) {
1040 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001041 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001042 }
1043 }
1044 };
1045
1046 public void resetQSBScroll() {
1047 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001048 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001049 }
1050
1051 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001052 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1053 // by a onResume or by scrolling otherwise.
1054 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001055
1056 // Custom content is completely hidden
1057 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001058
1059 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1060 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001061 }
1062
Jorim Jaggid017f882014-01-14 17:08:48 -08001063 protected boolean hasSettings() {
1064 return false;
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);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001229 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001230 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001231
John Spurlock77e1f472013-09-11 10:09:51 -04001232 mLauncherView.setSystemUiVisibility(
1233 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001234 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235
Winson Chung4c98d922011-05-31 16:50:48 -07001236 // Setup the drag layer
1237 mDragLayer.setup(this, dragController);
1238
Winson Chung3d503fb2011-07-13 17:25:49 -07001239 // Setup the hotseat
1240 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1241 if (mHotseat != null) {
1242 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001243 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001244 }
1245
Jorim Jaggid017f882014-01-14 17:08:48 -08001246 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001247 View widgetButton = findViewById(R.id.widget_button);
1248 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001249 @Override
1250 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001251 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001252 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001253 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001254 }
1255 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001256 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1257
1258 View wallpaperButton = findViewById(R.id.wallpaper_button);
1259 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001260 @Override
1261 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001262 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001263 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001264 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001265 }
1266 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001267 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1268
1269 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001270 if (hasSettings()) {
1271 settingsButton.setOnClickListener(new OnClickListener() {
1272 @Override
1273 public void onClick(View arg0) {
1274 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001275 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001276 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001277 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001278 });
1279 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1280 } else {
1281 settingsButton.setVisibility(View.GONE);
1282 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1283 lp.gravity = Gravity.END | Gravity.TOP;
1284 widgetButton.requestLayout();
1285 }
1286
Adam Cohendbdff6b2013-09-18 19:09:15 -07001287 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001288
Winson Chung4c98d922011-05-31 16:50:48 -07001289 // Setup the workspace
1290 mWorkspace.setHapticFeedbackEnabled(false);
1291 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001292 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001293 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001294
Winson Chungf0ea4d32011-06-06 14:27:16 -07001295 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001296 mSearchDropTargetBar = (SearchDropTargetBar)
1297 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001298
Winson Chungf0ea4d32011-06-06 14:27:16 -07001299 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001300 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001301 mAppsCustomizeContent = (AppsCustomizePagedView)
1302 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1303 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001304
Winson Chung3d503fb2011-07-13 17:25:49 -07001305 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001306 dragController.setDragScoller(mWorkspace);
1307 dragController.setScrollView(mDragLayer);
1308 dragController.setMoveTarget(mWorkspace);
1309 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001310 if (mSearchDropTargetBar != null) {
1311 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001312 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001313
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001314 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001315 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001316 mWeightWatcher = new WeightWatcher(this);
1317 mWeightWatcher.setAlpha(0.5f);
1318 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001319 new FrameLayout.LayoutParams(
1320 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001321 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001322 Gravity.BOTTOM)
1323 );
Adam Cohen39a06042013-07-19 14:30:12 -07001324
1325 boolean show = shouldShowWeightWatcher();
1326 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
1329
1330 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001331 * Sets the all apps button. This method is called from {@link Hotseat}.
1332 */
1333 public void setAllAppsButton(View allAppsButton) {
1334 mAllAppsButton = allAppsButton;
1335 }
1336
1337 public View getAllAppsButton() {
1338 return mAllAppsButton;
1339 }
1340
1341 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 * Creates a view representing a shortcut.
1343 *
1344 * @param info The data structure describing the shortcut.
1345 *
1346 * @return A View inflated from R.layout.application.
1347 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001348 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001350 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
1353 /**
1354 * Creates a view representing a shortcut inflated from the specified resource.
1355 *
1356 * @param layoutResId The id of the XML layout used to create the shortcut.
1357 * @param parent The group the shortcut belongs to.
1358 * @param info The data structure describing the shortcut.
1359 *
1360 * @return A View inflated from layoutResId.
1361 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001362 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001363 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1364 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 return favorite;
1367 }
1368
1369 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 * Add a shortcut to the workspace.
1371 *
1372 * @param data The intent describing the shortcut.
1373 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001375 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001376 int cellY) {
1377 int[] cellXY = mTmpAddItemCellCoordinates;
1378 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001379 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001380
Michael Jurkad3ef3062010-11-23 16:23:58 -08001381 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001382
Adam Cohen558baaf2011-08-15 15:22:57 -07001383 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001384 if (info == null) {
1385 return;
1386 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001387 final View view = createShortcut(info);
1388
Adam Cohenfbba09b2011-07-18 21:43:05 -07001389 // First we check if we already know the exact location where we want to add this item.
1390 if (cellX >= 0 && cellY >= 0) {
1391 cellXY[0] = cellX;
1392 cellXY[1] = cellY;
1393 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001394
1395 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001396 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001397 true, null,null)) {
1398 return;
1399 }
1400 DragObject dragObject = new DragObject();
1401 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001402 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1403 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001404 return;
1405 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001406 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001407 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001408 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001409 foundCellSpan = (result != null);
1410 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001411 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001412 }
1413
1414 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001415 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001416 return;
1417 }
1418
Adam Cohendcd297f2013-06-18 13:13:40 -07001419 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420
1421 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001422 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001423 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 }
1425 }
1426
Adam Cohen2f093b62012-04-30 18:59:53 -07001427 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1428 int minHeight) {
1429 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001430 // We want to account for the extra amount of padding that we are adding to the widget
1431 // to ensure that it gets the full amount of space that it has requested
1432 int requiredWidth = minWidth + padding.left + padding.right;
1433 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001434 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001435 }
1436
Adam Cohen2f093b62012-04-30 18:59:53 -07001437 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1438 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001439 }
1440
Adam Cohen2f093b62012-04-30 18:59:53 -07001441 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1442 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001443 }
1444
Adam Cohen2f093b62012-04-30 18:59:53 -07001445 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1446 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001447 }
1448
Adam Cohen2f093b62012-04-30 18:59:53 -07001449 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1450 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001451 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001452 }
1453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001455 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001457 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001458 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001460 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001461 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1462 if (appWidgetInfo == null) {
1463 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1464 }
Romain Guycbb89e42009-06-08 15:52:54 -07001465
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001466 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001467 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001468
Adam Cohen2f093b62012-04-30 18:59:53 -07001469 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1470 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001471
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001473 // We have saved the position to which the widget was dragged-- this really only matters
1474 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001475 int[] cellXY = mTmpAddItemCellCoordinates;
1476 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001477 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001478 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001479 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1480 cellXY[0] = mPendingAddInfo.cellX;
1481 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001482 spanXY[0] = mPendingAddInfo.spanX;
1483 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001484 foundCellSpan = true;
1485 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001486 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001487 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001488 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1489 spanXY[1], cellXY, finalSpan);
1490 spanXY[0] = finalSpan[0];
1491 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001492 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001493 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001494 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001495 }
1496
Michael Jurka0280c3b2010-09-17 15:00:07 -07001497 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001498 if (appWidgetId != -1) {
1499 // Deleting an app widget ID is a void call but writes to disk before returning
1500 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001501 new AsyncTask<Void, Void, Void>() {
1502 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001503 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001504 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001505 }
Michael Jurka43467462013-11-14 12:03:58 +01001506 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001507 }
Winson Chung93eef082012-03-23 15:59:27 -07001508 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001509 return;
1510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001512 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001513 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1514 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001515 launcherInfo.spanX = spanXY[0];
1516 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001517 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1518 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001521 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522
1523 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001524 if (hostView == null) {
1525 // Perform actual inflation because we're live
1526 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1527 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1528 } else {
1529 // The AppWidgetHostView has already been inflated and instantiated
1530 launcherInfo.hostView = hostView;
1531 }
Romain Guycbb89e42009-06-08 15:52:54 -07001532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001534 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001535 launcherInfo.notifyWidgetSizeChanged(this);
1536
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001538 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001539
1540 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001542 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Adam Cohended9f8d2010-11-03 13:25:16 -07001545 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1546 @Override
1547 public void onReceive(Context context, Intent intent) {
1548 final String action = intent.getAction();
1549 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1550 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001551 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001552 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001553
Winson Chungc100e8e2011-08-09 16:02:43 -07001554 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001555 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001556 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001557 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001558 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001559 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1560 mUserPresent = true;
1561 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001562 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1563 mModel.resetLoadedState(false, true);
1564 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1565 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1566 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1567 mModel.resetLoadedState(false, true);
1568 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1569 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1570 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001571 }
1572 }
1573 };
1574
1575 @Override
1576 public void onAttachedToWindow() {
1577 super.onAttachedToWindow();
1578
1579 // Listen for broadcasts related to user-presence
1580 final IntentFilter filter = new IntentFilter();
1581 filter.addAction(Intent.ACTION_SCREEN_OFF);
1582 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001583 if (ENABLE_DEBUG_INTENTS) {
1584 filter.addAction(DebugIntents.DELETE_DATABASE);
1585 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1586 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001587 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001588 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001589 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 mVisible = true;
1591 }
1592
1593 @Override
1594 public void onDetachedFromWindow() {
1595 super.onDetachedFromWindow();
1596 mVisible = false;
1597
Adam Cohend113e0c2010-11-11 10:48:05 -08001598 if (mAttached) {
1599 unregisterReceiver(mReceiver);
1600 mAttached = false;
1601 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001602 updateRunning();
1603 }
1604
1605 public void onWindowVisibilityChanged(int visibility) {
1606 mVisible = visibility == View.VISIBLE;
1607 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001608 // The following code used to be in onResume, but it turns out onResume is called when
1609 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1610 // is a more appropriate event to handle
1611 if (mVisible) {
1612 mAppsCustomizeTabHost.onWindowVisible();
1613 if (!mWorkspaceLoading) {
1614 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001615 // We want to let Launcher draw itself at least once before we force it to build
1616 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001617 // apps is nice and speedy.
1618 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001619 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001620 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001621 if (mStarted) return;
1622 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001623 // We delay the layer building a bit in order to give
1624 // other message processing a time to run. In particular
1625 // this avoids a delay in hiding the IME if it was
1626 // currently shown, because doing that may involve
1627 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001628 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001629 final ViewTreeObserver.OnDrawListener listener = this;
1630 mWorkspace.post(new Runnable() {
1631 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001632 if (mWorkspace != null &&
1633 mWorkspace.getViewTreeObserver() != null) {
1634 mWorkspace.getViewTreeObserver().
1635 removeOnDrawListener(listener);
1636 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001637 }
1638 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001639 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001640 }
1641 });
1642 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001643 // When Launcher comes back to foreground, a different Activity might be responsible for
1644 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001645 if (!DISABLE_MARKET_BUTTON) {
1646 updateAppMarketIcon();
1647 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001648 clearTypedText();
1649 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001650 }
1651
1652 private void sendAdvanceMessage(long delay) {
1653 mHandler.removeMessages(ADVANCE_MSG);
1654 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1655 mHandler.sendMessageDelayed(msg, delay);
1656 mAutoAdvanceSentTime = System.currentTimeMillis();
1657 }
1658
1659 private void updateRunning() {
1660 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1661 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1662 mAutoAdvanceRunning = autoAdvanceRunning;
1663 if (autoAdvanceRunning) {
1664 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1665 sendAdvanceMessage(delay);
1666 } else {
1667 if (!mWidgetsToAdvance.isEmpty()) {
1668 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1669 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1670 }
1671 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001672 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 }
1674 }
1675 }
1676
1677 private final Handler mHandler = new Handler() {
1678 @Override
1679 public void handleMessage(Message msg) {
1680 if (msg.what == ADVANCE_MSG) {
1681 int i = 0;
1682 for (View key: mWidgetsToAdvance.keySet()) {
1683 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1684 final int delay = mAdvanceStagger * i;
1685 if (v instanceof Advanceable) {
1686 postDelayed(new Runnable() {
1687 public void run() {
1688 ((Advanceable) v).advance();
1689 }
1690 }, delay);
1691 }
1692 i++;
1693 }
1694 sendAdvanceMessage(mAdvanceInterval);
1695 }
1696 }
1697 };
1698
1699 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001700 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1702 if (v instanceof Advanceable) {
1703 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001704 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 updateRunning();
1706 }
1707 }
1708
1709 void removeWidgetToAutoAdvance(View hostView) {
1710 if (mWidgetsToAdvance.containsKey(hostView)) {
1711 mWidgetsToAdvance.remove(hostView);
1712 updateRunning();
1713 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001714 }
1715
Joe Onorato9c1289c2009-08-17 11:03:03 -04001716 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001718 launcherInfo.hostView = null;
1719 }
1720
Winson Chung93eef082012-03-23 15:59:27 -07001721 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1722 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1723 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 }
1725
Winson Chunga6945242014-01-08 14:04:34 -08001726 public DragLayer getDragLayer() {
1727 return mDragLayer;
1728 }
1729
1730 public Workspace getWorkspace() {
1731 return mWorkspace;
1732 }
1733
1734 public Hotseat getHotseat() {
1735 return mHotseat;
1736 }
1737
Jorim Jaggid017f882014-01-14 17:08:48 -08001738 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001739 return mOverviewPanel;
1740 }
1741
1742 public SearchDropTargetBar getSearchBar() {
1743 return mSearchDropTargetBar;
1744 }
1745
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001746 public LauncherAppWidgetHost getAppWidgetHost() {
1747 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
Romain Guycbb89e42009-06-08 15:52:54 -07001749
Winson Chunga9abd0e2010-10-27 17:18:37 -07001750 public LauncherModel getModel() {
1751 return mModel;
1752 }
1753
Winson Chunga6945242014-01-08 14:04:34 -08001754 public LauncherClings getLauncherClings() {
1755 return mLauncherClings;
1756 }
1757
1758 protected SharedPreferences getSharedPrefs() {
1759 return mSharedPrefs;
1760 }
1761
Bjorn Bringertc459e522013-06-07 19:36:01 +01001762 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001763 getWindow().closeAllPanels();
1764
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001765 // Whatever we were doing is hereby canceled.
1766 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001767 }
1768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 @Override
1770 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001771 long startTime = 0;
1772 if (DEBUG_RESUME_TIME) {
1773 startTime = System.currentTimeMillis();
1774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 super.onNewIntent(intent);
1776
1777 // Close the menu
1778 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001779 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001780 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001781
Adam Cohened307df2013-10-02 09:37:31 -07001782 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1783 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1784 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001785
Adam Cohen6fecd412013-10-02 17:41:50 -07001786 if (mWorkspace == null) {
1787 // Can be cases where mWorkspace is null, this prevents a NPE
1788 return;
1789 }
1790 Folder openFolder = mWorkspace.getOpenFolder();
1791 // In all these cases, only animate if we're already on home
1792 mWorkspace.exitWidgetResizeMode();
1793 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001794 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001795 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001796 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001797
Adam Cohen6fecd412013-10-02 17:41:50 -07001798 closeFolder();
1799 exitSpringLoadedDragMode();
1800
1801 // If we are already on home, then just animate back to the workspace,
1802 // otherwise, just wait until onResume to set the state back to Workspace
1803 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001804 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001805 } else {
1806 mOnResumeState = State.WORKSPACE;
1807 }
1808
1809 final View v = getWindow().peekDecorView();
1810 if (v != null && v.getWindowToken() != null) {
1811 InputMethodManager imm = (InputMethodManager)getSystemService(
1812 INPUT_METHOD_SERVICE);
1813 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1814 }
1815
1816 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001817 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001818 mAppsCustomizeTabHost.reset();
1819 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001820
1821 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 }
Adam Cohened307df2013-10-02 09:37:31 -07001823
Michael Jurka447bf842013-05-15 14:52:15 +02001824 if (DEBUG_RESUME_TIME) {
1825 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 }
1828
Adam Coppa120b8e2013-11-12 16:26:04 +00001829 /**
1830 * Override point for subclasses to prevent movement to the default screen when the home
1831 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1832 */
1833 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1834 return true;
1835 }
1836
1837 /**
1838 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1839 */
1840 protected void onHomeIntent() {
1841 // Do nothing
1842 }
1843
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001845 public void onRestoreInstanceState(Bundle state) {
1846 super.onRestoreInstanceState(state);
1847 for (int page: mSynchronouslyBoundPages) {
1848 mWorkspace.restoreInstanceStateForChild(page);
1849 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 }
1851
1852 @Override
1853 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001854 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001855 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1856 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001857 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001858 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859
Michael Jurka883f55b2010-10-21 15:47:14 -07001860 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001861 // We close any open folder since it will not be re-opened, and we need to make sure
1862 // this state is reflected.
1863 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864
Adam Cohendcd297f2013-06-18 13:13:40 -07001865 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001866 mWaitingForResult) {
1867 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001868 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001869 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1870 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001871 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1872 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1873 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001874 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 }
1876
1877 if (mFolderInfo != null && mWaitingForResult) {
1878 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1879 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1880 }
Michael Jurka946ad472010-07-09 18:05:18 -07001881
Winson Chung785d2eb2011-04-14 16:08:02 -07001882 // Save the current AppsCustomize tab
1883 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001884 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1885 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001886 if (currentTabTag != null) {
1887 outState.putString("apps_customize_currentTab", currentTabTag);
1888 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001889 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1890 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001891 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001892 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 }
1894
1895 @Override
1896 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001898
Winson Chunge7a03942011-08-05 15:05:12 -07001899 // Remove all pending runnables
1900 mHandler.removeMessages(ADVANCE_MSG);
1901 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001902 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001903
Winson Chungcd2b0142011-06-08 16:02:26 -07001904 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001905 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001906 mModel.stopLoader();
1907 app.setLauncher(null);
1908
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001910 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001912 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001914 mAppWidgetHost = null;
1915
1916 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917
1918 TextKeyListener.getInstance().release();
1919
Winson Chung81b52252012-08-27 15:34:29 -07001920 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1921 // to prevent leaking Launcher activities on orientation change.
1922 if (mModel != null) {
1923 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1924 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001925
1926 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001927 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001928
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001929 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001930 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001931 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001932 mWorkspace = null;
1933 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001934
Michael Jurka2ecf9952012-06-18 12:52:28 -07001935 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
Adam Cohena9cf38f2011-05-02 15:36:58 -07001938 public DragController getDragController() {
1939 return mDragController;
1940 }
1941
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 @Override
1943 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001944 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 super.startActivityForResult(intent, requestCode);
1946 }
1947
Winson Chung70d72102011-08-12 11:24:35 -07001948 /**
1949 * Indicates that we want global search for this activity by setting the globalSearch
1950 * argument for {@link #startSearch} to true.
1951 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001953 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001955
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001956 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001957
Karl Rosaen138a0412009-04-23 19:00:21 -07001958 if (initialQuery == null) {
1959 // Use any text typed in the launcher as the initial query
1960 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 if (appSearchData == null) {
1963 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001964 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
Winson Chungadf0c182012-08-23 14:59:07 -07001966 Rect sourceBounds = new Rect();
1967 if (mSearchDropTargetBar != null) {
1968 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1969 }
Romain Guycbb89e42009-06-08 15:52:54 -07001970
Bjorn Bringertc459e522013-06-07 19:36:01 +01001971 startSearch(initialQuery, selectInitialQuery,
1972 appSearchData, sourceBounds);
1973 }
1974
1975 public void startSearch(String initialQuery,
1976 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001977 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001978 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001979 }
1980
1981 /**
1982 * Starts the global search activity. This code is a copied from SearchManager
1983 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001984 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001985 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001986 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001987 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1988 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1989 if (globalSearchActivity == null) {
1990 Log.w(TAG, "No global search activity found.");
1991 return;
1992 }
1993 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1994 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1995 intent.setComponent(globalSearchActivity);
1996 // Make sure that we have a Bundle to put source in
1997 if (appSearchData == null) {
1998 appSearchData = new Bundle();
1999 } else {
2000 appSearchData = new Bundle(appSearchData);
2001 }
2002 // Set source to package name of app that starts global search, if not set already.
2003 if (!appSearchData.containsKey("source")) {
2004 appSearchData.putString("source", getPackageName());
2005 }
2006 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2007 if (!TextUtils.isEmpty(initialQuery)) {
2008 intent.putExtra(SearchManager.QUERY, initialQuery);
2009 }
2010 if (selectInitialQuery) {
2011 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2012 }
2013 intent.setSourceBounds(sourceBounds);
2014 try {
2015 startActivity(intent);
2016 } catch (ActivityNotFoundException ex) {
2017 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 }
2020
Yura4f93ec62014-02-04 14:15:21 +00002021 public boolean isOnCustomContent() {
2022 return mWorkspace.isOnOrMovingToCustomContent();
2023 }
2024
Winson Chung70d72102011-08-12 11:24:35 -07002025 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002026 public boolean onPrepareOptionsMenu(Menu menu) {
2027 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002028 if (!isOnCustomContent()) {
2029 // Close any open folders
2030 closeFolder();
2031 // Stop resizing any widgets
2032 mWorkspace.exitWidgetResizeMode();
2033 if (!mWorkspace.isInOverviewMode()) {
2034 // Show the overview mode
2035 showOverviewMode(true);
2036 } else {
2037 showWorkspace(true);
2038 }
Winson Chunge0298742014-01-17 12:03:00 -08002039 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002040 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002043 @Override
2044 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002045 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002046 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002047 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002048 }
2049
Joe Onorato9c1289c2009-08-17 11:03:03 -04002050 public boolean isWorkspaceLocked() {
2051 return mWorkspaceLoading || mWaitingForResult;
2052 }
2053
Adam Cohen517a7f52014-03-01 12:12:59 -08002054 public boolean isWorkspaceLoading() {
2055 return mWorkspaceLoading;
2056 }
2057
Michael Jurka0280c3b2010-09-17 15:00:07 -07002058 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002059 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002060 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002061 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2062 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002063 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002064 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002065 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002066
Adam Cohenad4e15c2013-10-17 16:21:35 -07002067 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2068 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2069 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2070 }
2071
2072 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2073 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2074 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002075 if (appWidgetInfo.configure != null) {
2076 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002077 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002078
Bjorn Bringert7984c942009-12-09 15:38:25 +00002079 // Launch over to configure widget, if needed
2080 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002081 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002082 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002083 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002085 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002086 Runnable onComplete = new Runnable() {
2087 @Override
2088 public void run() {
2089 // Exit spring loaded mode if necessary after adding the widget
2090 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2091 null);
2092 }
2093 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002094 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002095 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002096 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 }
2098 }
Romain Guycbb89e42009-06-08 15:52:54 -07002099
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002100 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002101 // Close any folders that may be open.
2102 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002103 mWorkspace.moveToCustomContentScreen(animate);
2104 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002105 /**
2106 * Process a shortcut drop.
2107 *
2108 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002109 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002110 * @param cell The cell it should be added to, optional
2111 * @param position The location on the screen where it was dropped, optional
2112 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002113 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002114 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002115 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002116 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002117 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002118 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002119
2120 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002121 mPendingAddInfo.cellX = cell[0];
2122 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002123 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002124
2125 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2126 createShortcutIntent.setComponent(componentName);
2127 processShortcut(createShortcutIntent);
2128 }
2129
Adam Cohenfbba09b2011-07-18 21:43:05 -07002130 /**
2131 * Process a widget drop.
2132 *
2133 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002134 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002135 * @param cell The cell it should be added to, optional
2136 * @param position The location on the screen where it was dropped, optional
2137 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002138 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002139 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002140 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002141 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002142 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002143 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002144 mPendingAddInfo.minSpanX = info.minSpanX;
2145 mPendingAddInfo.minSpanY = info.minSpanY;
2146
Adam Cohenfbba09b2011-07-18 21:43:05 -07002147 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002148 mPendingAddInfo.cellX = cell[0];
2149 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002150 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002151 if (span != null) {
2152 mPendingAddInfo.spanX = span[0];
2153 mPendingAddInfo.spanY = span[1];
2154 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002155
Adam Cohened66b2b2012-01-23 17:28:51 -08002156 AppWidgetHostView hostView = info.boundWidget;
2157 int appWidgetId;
2158 if (hostView != null) {
2159 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002160 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002161 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002162 // In this case, we either need to start an activity to get permission to bind
2163 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002164 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002165 Bundle options = info.bindOptions;
2166
2167 boolean success = false;
2168 if (options != null) {
2169 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2170 info.componentName, options);
2171 } else {
2172 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2173 info.componentName);
2174 }
2175 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002176 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002177 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002178 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002179 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2180 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2181 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002182 // TODO: we need to make sure that this accounts for the options bundle.
2183 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002184 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2185 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002186 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002187 }
2188
Joe Onoratodeb98af2010-02-19 14:59:39 -08002189 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002190 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
2192
Winson Chung24ab2f12010-09-16 14:10:47 -07002193 void processWallpaper(Intent intent) {
2194 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2195 }
2196
Adam Cohendcd297f2013-06-18 13:13:40 -07002197 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002198 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002199 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 folderInfo.title = getText(R.string.folder_name);
2201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002203 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002204 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002205 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206
2207 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002208 FolderIcon newFolder =
2209 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002210 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002211 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002212 // Force measure the new folder icon
2213 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2214 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002215 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
Romain Guycbb89e42009-06-08 15:52:54 -07002217
Joe Onorato9c1289c2009-08-17 11:03:03 -04002218 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002219 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002220 }
2221
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002222 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002223 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002224 }
2225
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002226 /**
2227 * Registers various content observers. The current implementation registers
2228 * only a favorites observer to keep track of the favorites applications.
2229 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002230 private void registerContentObservers() {
2231 ContentResolver resolver = getContentResolver();
2232 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2233 true, mWidgetObserver);
2234 }
2235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 @Override
2237 public boolean dispatchKeyEvent(KeyEvent event) {
2238 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2239 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002241 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002242 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002243 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002244 dumpState();
2245 return true;
2246 }
2247 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002248 }
2249 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2250 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002251 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 return true;
2253 }
2254 }
2255
2256 return super.dispatchKeyEvent(event);
2257 }
2258
Joe Onorato88ec0992009-11-19 13:16:06 -08002259 @Override
2260 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002261 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002262 if (mAppsCustomizeContent.getContentType() ==
2263 AppsCustomizePagedView.ContentType.Applications) {
2264 showWorkspace(true);
2265 } else {
2266 showOverviewMode(true);
2267 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002268 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002269 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002270 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002271 Folder openFolder = mWorkspace.getOpenFolder();
2272 if (openFolder.isEditingName()) {
2273 openFolder.dismissEditingName();
2274 } else {
2275 closeFolder();
2276 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002277 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002278 mWorkspace.exitWidgetResizeMode();
2279
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002280 // Back button is a no-op here, but give at least some feedback for the button press
2281 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002282 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002283 }
2284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002286 * Re-listen when widgets are reset.
2287 */
2288 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002289 if (mAppWidgetHost != null) {
2290 mAppWidgetHost.startListening();
2291 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002292 }
2293
2294 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 * Launches the intent referred by the clicked shortcut.
2296 *
2297 * @param v The view representing the clicked shortcut.
2298 */
2299 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002300 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2301 // view has detached (it's possible for this to happen if the view is removed mid touch).
2302 if (v.getWindowToken() == null) {
2303 return;
2304 }
2305
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002306 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002307 return;
2308 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002309
Adam Cohen1697b792013-09-17 19:08:21 -07002310 if (v instanceof Workspace) {
2311 if (mWorkspace.isInOverviewMode()) {
2312 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002313 }
2314 return;
2315 }
2316
2317 if (v instanceof CellLayout) {
2318 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002319 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002320 }
2321 }
2322
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002324 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002325 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002327 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002328 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002329 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002330 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002331 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 }
2333 }
2334
Michael Jurka0e260592010-06-30 17:07:39 -07002335 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002336 return false;
2337 }
2338
Michael Jurkaaf442092010-06-10 17:01:57 -07002339 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002340 * Event handler for the search button
2341 *
2342 * @param v The view that was clicked.
2343 */
2344 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002345 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2346
Amith Yamasania135ba82011-08-09 17:42:01 -07002347 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002348 }
2349
2350 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002351 * Event handler for the voice button
2352 *
2353 * @param v The view that was clicked.
2354 */
2355 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002356 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002357
Bjorn Bringertc459e522013-06-07 19:36:01 +01002358 startVoice();
2359 }
2360
2361 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002362 try {
2363 final SearchManager searchManager =
2364 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2365 ComponentName activityName = searchManager.getGlobalSearchActivity();
2366 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002367 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002368 if (activityName != null) {
2369 intent.setPackage(activityName.getPackageName());
2370 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002371 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002372 } catch (ActivityNotFoundException e) {
2373 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002374 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002375 startActivitySafely(null, intent, "onClickVoiceButton");
2376 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002377 }
2378
2379 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002380 * Event handler for the "grid" button that appears on the home screen, which
2381 * enters all apps mode.
2382 *
2383 * @param v The view that was clicked.
2384 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002385 protected void onClickAllAppsButton(View v) {
2386 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2387 if (isAllAppsVisible()) {
2388 showWorkspace(true);
2389 } else {
2390 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2391 }
2392 }
2393
2394 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002395 * Event handler for a paged view icon click.
2396 * @param v The view that was clicked.
2397 * @param appInfo The {link AppInfo} of the view.
2398 */
2399 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2400 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2401 startActivitySafely(v, appInfo.intent, appInfo);
2402 getStats().recordLaunch(appInfo.intent);
2403 }
2404
2405 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002406 * Event handler for an app shortcut click.
2407 *
2408 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2409 */
2410 protected void onClickAppShortcut(View v) {
2411 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2412 Object tag = v.getTag();
2413 if (!(tag instanceof ShortcutInfo)) {
2414 throw new IllegalArgumentException("Input must be a Shortcut");
2415 }
2416
2417 // Open shortcut
2418 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2419 final Intent intent = shortcut.intent;
2420
2421 // Check for special shortcuts
2422 if (intent.getComponent() != null) {
2423 final String shortcutClass = intent.getComponent().getClassName();
2424
2425 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2426 MemoryDumpActivity.startDump(this);
2427 return;
2428 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2429 toggleShowWeightWatcher();
2430 return;
2431 }
2432 }
2433
2434 // Start activities
2435 int[] pos = new int[2];
2436 v.getLocationOnScreen(pos);
2437 intent.setSourceBounds(new Rect(pos[0], pos[1],
2438 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2439
2440 boolean success = startActivitySafely(v, intent, tag);
2441
2442 mStats.recordLaunch(intent, shortcut);
2443
2444 if (success && v instanceof BubbleTextView) {
2445 mWaitingForResume = (BubbleTextView) v;
2446 mWaitingForResume.setStayPressed(true);
2447 }
2448 }
2449
2450 /**
2451 * Event handler for a folder icon click.
2452 *
2453 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2454 */
2455 protected void onClickFolderIcon(View v) {
2456 if (LOGD) Log.d(TAG, "onClickFolder");
2457 if (!(v instanceof FolderIcon)){
2458 throw new IllegalArgumentException("Input must be a FolderIcon");
2459 }
2460
2461 FolderIcon folderIcon = (FolderIcon) v;
2462 final FolderInfo info = folderIcon.getFolderInfo();
2463 Folder openFolder = mWorkspace.getFolderForTag(info);
2464
2465 // If the folder info reports that the associated folder is open, then verify that
2466 // it is actually opened. There have been a few instances where this gets out of sync.
2467 if (info.opened && openFolder == null) {
2468 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2469 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2470 info.opened = false;
2471 }
2472
2473 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2474 // Close any open folder
2475 closeFolder();
2476 // Open the requested folder
2477 openFolder(folderIcon);
2478 } else {
2479 // Find the open folder...
2480 int folderScreen;
2481 if (openFolder != null) {
2482 folderScreen = mWorkspace.getPageForView(openFolder);
2483 // .. and close it
2484 closeFolder(openFolder);
2485 if (folderScreen != mWorkspace.getCurrentPage()) {
2486 // Close any folder open on the current screen
2487 closeFolder();
2488 // Pull the folder onto this screen
2489 openFolder(folderIcon);
2490 }
2491 }
2492 }
Michael Jurka5130e402011-10-13 04:55:35 -07002493 }
2494
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002495 /**
2496 * Event handler for the (Add) Widgets button that appears after a long press
2497 * on the home screen.
2498 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002499 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002500 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002501 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2502 }
2503
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002504 /**
2505 * Event handler for the wallpaper picker button that appears after a long press
2506 * on the home screen.
2507 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002508 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002509 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2510 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2511 pickWallpaper.setComponent(getWallpaperPickerComponent());
2512 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2513 }
2514
2515 /**
2516 * Event handler for a click on the settings button that appears after a long press
2517 * on the home screen.
2518 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002519 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002520 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2521 }
2522
Michael Jurka5130e402011-10-13 04:55:35 -07002523 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002524 // Provide the same haptic feedback that the system offers for virtual keys.
2525 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002526 }
2527
Adam Cohen61f560d2013-09-30 15:58:20 -07002528 public void performHapticFeedbackOnTouchDown(View v) {
2529 // Provide the same haptic feedback that the system offers for virtual keys.
2530 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2531 }
2532
2533 public View.OnTouchListener getHapticFeedbackTouchListener() {
2534 if (mHapticFeedbackTouchListener == null) {
2535 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2536 @Override
2537 public boolean onTouch(View v, MotionEvent event) {
2538 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2539 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2540 }
2541 return false;
2542 }
2543 };
2544 }
2545 return mHapticFeedbackTouchListener;
2546 }
2547
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002548 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002549 if (!DISABLE_MARKET_BUTTON) {
2550 if (mAppMarketIntent != null) {
2551 startActivitySafely(v, mAppMarketIntent, "app market");
2552 } else {
2553 Log.e(TAG, "Invalid app market intent.");
2554 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002555 }
2556 }
2557
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002558 public void onDragStarted(View view) {}
2559
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002560 /**
2561 * Called when the user stops interacting with the launcher.
2562 * This implies that the user is now on the homescreen and is not doing housekeeping.
2563 */
2564 protected void onInteractionEnd() {}
2565
2566 /**
2567 * Called when the user starts interacting with the launcher.
2568 * The possible interactions are:
2569 * - open all apps
2570 * - reorder an app shortcut, or a widget
2571 * - open the overview mode.
2572 * This is a good time to stop doing things that only make sense
2573 * when the user is on the homescreen and not doing housekeeping.
2574 */
2575 protected void onInteractionBegin() {}
2576
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002577 void startApplicationDetailsActivity(ComponentName componentName) {
2578 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002579 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2580 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002581 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2582 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002583 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002584 }
2585
Michael Jurka1e2f4652013-07-08 18:03:46 -07002586 // returns true if the activity was started
2587 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002588 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002589 // System applications cannot be installed. For now, show a toast explaining that.
2590 // We may give them the option of disabling apps this way.
2591 int messageId = R.string.uninstall_system_app_text;
2592 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002593 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002594 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002595 String packageName = componentName.getPackageName();
2596 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002597 Intent intent = new Intent(
2598 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002599 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2600 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002601 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002602 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002603 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002604 }
2605
Michael Jurka86a720e2012-05-09 11:23:23 -07002606 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyed131872014-04-30 03:02:21 +01002607 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002608 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002609 // Only launch using the new animation if the shortcut has not opted out (this is a
2610 // private contract between launcher and may be ignored in the future).
2611 boolean useLaunchAnimation = (v != null) &&
2612 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002613 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2614 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2615 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, 0);
2616 UserHandleCompat user = serialNumber == 0 ? null :
2617 userManager.getUserForSerialNumber(serialNumber);
2618
Winson Chung2672ff92012-05-04 16:22:30 -07002619 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002620 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2621 v.getMeasuredWidth(), v.getMeasuredHeight());
Kenny Guyed131872014-04-30 03:02:21 +01002622 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2623 // Could be launching some bookkeeping activity
2624 startActivity(intent, opts.toBundle());
2625 } else {
2626 launcherApps.startActivityForProfile(intent.getComponent(),
2627 intent.getSourceBounds(),
2628 opts.toBundle(), user);
2629 }
Winson Chungc7450e32012-04-17 17:34:08 -07002630 } else {
2631 startActivity(intent);
2632 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002633 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 } catch (SecurityException e) {
2635 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002636 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002638 "or use the exported attribute for this activity. "
2639 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002640 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002641 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002642 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002643
Michael Jurka86a720e2012-05-09 11:23:23 -07002644 boolean startActivitySafely(View v, Intent intent, Object tag) {
2645 boolean success = false;
2646 try {
2647 success = startActivity(v, intent, tag);
2648 } catch (ActivityNotFoundException e) {
2649 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2650 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2651 }
2652 return success;
2653 }
2654
Adam Cohen268c4752012-06-06 17:47:33 -07002655 /**
2656 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2657 * in the DragLayer in the exact absolute location of the original FolderIcon.
2658 */
2659 private void copyFolderIconToImage(FolderIcon fi) {
2660 final int width = fi.getMeasuredWidth();
2661 final int height = fi.getMeasuredHeight();
2662
2663 // Lazy load ImageView, Bitmap and Canvas
2664 if (mFolderIconImageView == null) {
2665 mFolderIconImageView = new ImageView(this);
2666 }
2667 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2668 mFolderIconBitmap.getHeight() != height) {
2669 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2670 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2671 }
2672
2673 DragLayer.LayoutParams lp;
2674 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2675 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2676 } else {
2677 lp = new DragLayer.LayoutParams(width, height);
2678 }
2679
Adam Cohen307fe232012-08-16 17:55:58 -07002680 // The layout from which the folder is being opened may be scaled, adjust the starting
2681 // view size by this scale factor.
2682 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002683 lp.customPosition = true;
2684 lp.x = mRectForFolderAnimation.left;
2685 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002686 lp.width = (int) (scale * width);
2687 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002688
2689 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2690 fi.draw(mFolderIconCanvas);
2691 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002692 if (fi.getFolder() != null) {
2693 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2694 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002695 }
Adam Cohen268c4752012-06-06 17:47:33 -07002696 // Just in case this image view is still in the drag layer from a previous animation,
2697 // we remove it and re-add it.
2698 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2699 mDragLayer.removeView(mFolderIconImageView);
2700 }
2701 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002702 if (fi.getFolder() != null) {
2703 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002704 }
Adam Cohen268c4752012-06-06 17:47:33 -07002705 }
2706
Adam Cohen2801caf2011-05-13 20:57:39 -07002707 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002708 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002709 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2710 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2711 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2712
Adam Cohenc51934b2011-07-26 21:07:43 -07002713 FolderInfo info = (FolderInfo) fi.getTag();
2714 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2715 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002716 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2717 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002718 }
2719
Adam Cohen268c4752012-06-06 17:47:33 -07002720 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2721 copyFolderIconToImage(fi);
2722 fi.setVisibility(View.INVISIBLE);
2723
Michael Jurka2ecf9952012-06-18 12:52:28 -07002724 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002725 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002726 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2727 oa.start();
2728 }
2729
Adam Cohen268c4752012-06-06 17:47:33 -07002730 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002731 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002732 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2733 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2734 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2735
Adam Cohen268c4752012-06-06 17:47:33 -07002736 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002737
Adam Cohen268c4752012-06-06 17:47:33 -07002738 // We remove and re-draw the FolderIcon in-case it has changed
2739 mDragLayer.removeView(mFolderIconImageView);
2740 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002741 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002742 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002743 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002744 oa.addListener(new AnimatorListenerAdapter() {
2745 @Override
2746 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002747 if (cl != null) {
2748 cl.clearFolderLeaveBehind();
2749 // Remove the ImageView copy of the FolderIcon and make the original visible.
2750 mDragLayer.removeView(mFolderIconImageView);
2751 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002752 }
2753 }
2754 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002755 oa.start();
2756 }
2757
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002758 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002759 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002760 * is animated relative to the specified View. If the View is null, no animation
2761 * is played.
2762 *
2763 * @param folderInfo The FolderInfo describing the folder to open.
2764 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002765 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002766 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002767 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002768
Adam Cohena9cf38f2011-05-02 15:36:58 -07002769 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002770
Adam Cohen4554ee12011-08-03 16:13:21 -07002771 // Just verify that the folder hasn't already been added to the DragLayer.
2772 // There was a one-off crash where the folder had a parent already.
2773 if (folder.getParent() == null) {
2774 mDragLayer.addView(folder);
2775 mDragController.addDropTarget((DropTarget) folder);
2776 } else {
2777 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2778 folder.getParent() + ").");
2779 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002780 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002781 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002782
2783 // Notify the accessibility manager that this folder "window" has appeared and occluded
2784 // the workspace items
2785 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2786 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002787 }
2788
2789 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002790 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002791 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002792 if (folder.isEditingName()) {
2793 folder.dismissEditingName();
2794 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002795 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002796
2797 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002798 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002799 }
2800 }
2801
2802 void closeFolder(Folder folder) {
2803 folder.getInfo().opened = false;
2804
2805 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2806 if (parent != null) {
2807 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2808 shrinkAndFadeInFolderIcon(fi);
2809 }
2810 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002811
2812 // Notify the accessibility manager that this folder "window" has disappeard and no
2813 // longer occludeds the workspace items
2814 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002815 }
2816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002818 if (!isDraggingEnabled()) return false;
2819 if (isWorkspaceLocked()) return false;
2820 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002821
Adam Cohen1697b792013-09-17 19:08:21 -07002822 if (v instanceof Workspace) {
2823 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002824 if (mWorkspace.enterOverviewMode()) {
2825 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2826 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2827 return true;
2828 } else {
2829 return false;
2830 }
Adam Cohen1697b792013-09-17 19:08:21 -07002831 }
Adam Cohen1697b792013-09-17 19:08:21 -07002832 }
2833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002835 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 }
2837
Michael Jurka0280c3b2010-09-17 15:00:07 -07002838 resetAddInfo();
2839 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002841 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002842 return true;
2843 }
2844
Winson Chung3d503fb2011-07-13 17:25:49 -07002845 // The hotseat touch handling does not go through Workspace, and we always allow long press
2846 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002847 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002848 final boolean inHotseat = isHotseatLayout(v);
2849 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002850 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002851 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002852 // User long pressed on empty space
2853 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2854 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2855 // Disabling reordering until we sort out some issues.
2856 if (mWorkspace.isInOverviewMode()) {
2857 mWorkspace.startReordering(v);
2858 } else {
2859 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002862 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2863 mHotseat.getOrderInHotseat(
2864 longClickCellInfo.cellX,
2865 longClickCellInfo.cellY));
2866 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002868 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002869 }
2870 }
2871 }
2872 return true;
2873 }
2874
Winson Chung3d503fb2011-07-13 17:25:49 -07002875 boolean isHotseatLayout(View layout) {
2876 return mHotseat != null && layout != null &&
2877 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2878 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002879
Winson Chung3d503fb2011-07-13 17:25:49 -07002880 /**
2881 * Returns the CellLayout of the specified container at the specified screen.
2882 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002883 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002884 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2885 if (mHotseat != null) {
2886 return mHotseat.getLayout();
2887 } else {
2888 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002889 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002890 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002891 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002892 }
2893 }
2894
Daniel Sandler843e8602010-06-07 14:59:01 -04002895 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002896 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002897 }
2898
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002899 /**
2900 * Helper method for the cameraZoomIn/cameraZoomOut animations
2901 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002902 * @param scaleFactor The scale factor used for the zoom
2903 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002904 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002905 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002906 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002907 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002908
Craig Mautner360310b2012-10-26 15:13:08 -07002909 private void setWorkspaceBackground(boolean workspace) {
2910 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002911 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002912 }
2913
Selim Cinek3a8a8f72014-01-16 10:38:38 -08002914 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002915 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2916 int curflags = getWindow().getAttributes().flags
2917 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2918 if (wpflags != curflags) {
2919 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2920 }
Craig Mautner360310b2012-10-26 15:13:08 -07002921 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002922 }
2923
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002924 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2925 if (v instanceof LauncherTransitionable) {
2926 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2927 }
2928 }
2929
Michael Jurkabed61d22012-02-14 22:51:29 -08002930 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2931 if (v instanceof LauncherTransitionable) {
2932 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2933 }
Winson Chung70442722012-02-10 15:43:22 -08002934
2935 // Update the workspace transition step as well
2936 dispatchOnLauncherTransitionStep(v, 0f);
2937 }
2938
2939 private void dispatchOnLauncherTransitionStep(View v, float t) {
2940 if (v instanceof LauncherTransitionable) {
2941 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2942 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002943 }
2944
2945 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2946 if (v instanceof LauncherTransitionable) {
2947 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2948 }
Winson Chung70442722012-02-10 15:43:22 -08002949
2950 // Update the workspace transition step as well
2951 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002952 }
2953
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002954 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002955 * Things to test when changing the following seven functions.
2956 * - Home from workspace
2957 * - from center screen
2958 * - from other screens
2959 * - Home from all apps
2960 * - from center screen
2961 * - from other screens
2962 * - Back from all apps
2963 * - from center screen
2964 * - from other screens
2965 * - Launch app from workspace and quit
2966 * - with back
2967 * - with home
2968 * - Launch app from all apps and quit
2969 * - with back
2970 * - with home
2971 * - Go to a screen that's not the default, then all
2972 * apps, and launch and app, and go back
2973 * - with back
2974 * -with home
2975 * - On workspace, long press power and go back
2976 * - with back
2977 * - with home
2978 * - On all apps, long press power and go back
2979 * - with back
2980 * - with home
2981 * - On workspace, power off
2982 * - On all apps, power off
2983 * - Launch an app and turn off the screen while in that app
2984 * - Go back with home key
2985 * - Go back with back key TODO: make this not go to workspace
2986 * - From all apps
2987 * - From workspace
2988 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2989 * - From all apps
2990 * - From the center workspace
2991 * - From another workspace
2992 */
2993
2994 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002995 * Zoom the camera out from the workspace to reveal 'toView'.
2996 * Assumes that the view to show is anchored at either the very top or very bottom
2997 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002998 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002999 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003000 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3001 showAppsCustomizeHelper(animated, springLoaded, contentType);
3002 }
3003 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3004 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003005 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003006 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003007 mStateAnimation.cancel();
3008 mStateAnimation = null;
3009 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003010 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003011
Winson Chungf0ea4d32011-06-06 14:27:16 -07003012 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3013 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3014 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003015 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003016 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003017 final int startDelay =
3018 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003019
Michael Jurkab3e22d92011-10-31 15:58:33 -07003020 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003021
Michael Jurkad74c9842011-07-10 12:44:21 -07003022 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003023 Animator workspaceAnim =
3024 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003025 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003026 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3027 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003028 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3029 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003030
3031 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003032 toView.setScaleX(scale);
3033 toView.setScaleY(scale);
3034 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3035 scaleAnim.
3036 scaleX(1f).scaleY(1f).
3037 setDuration(duration).
3038 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003039
Winson Chungf0ea4d32011-06-06 14:27:16 -07003040 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003041 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003042 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003043 .ofFloat(toView, "alpha", 0f, 1f)
3044 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003045 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003046 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3047 @Override
3048 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003049 if (animation == null) {
3050 throw new RuntimeException("animation is null");
3051 }
Winson Chung70442722012-02-10 15:43:22 -08003052 float t = (Float) animation.getAnimatedValue();
3053 dispatchOnLauncherTransitionStep(fromView, t);
3054 dispatchOnLauncherTransitionStep(toView, t);
3055 }
3056 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003057
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003058 // toView should appear right at the end of the workspace shrink
3059 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003060 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003061 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003062 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003063
3064 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003065 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003066 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003067 // Prepare the position
3068 toView.setTranslationX(0.0f);
3069 toView.setTranslationY(0.0f);
3070 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003071 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003072 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003073 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003074 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003075 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3076 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003077
Winson Chungc7d2b602012-05-16 17:02:20 -07003078 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003079 if (mSearchDropTargetBar != null) {
3080 mSearchDropTargetBar.hideSearchBar(false);
3081 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003082 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003083 });
3084
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003085 if (workspaceAnim != null) {
3086 mStateAnimation.play(workspaceAnim);
3087 }
Michael Jurka1899a362011-11-03 13:50:45 -07003088
3089 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003090
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003091 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3092 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003093
3094 // If any of the objects being animated haven't been measured/laid out
3095 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003096 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003097 (mWorkspace.getMeasuredWidth() == 0) ||
3098 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003099 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003100 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003101
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003102 final AnimatorSet stateAnimation = mStateAnimation;
3103 final Runnable startAnimRunnable = new Runnable() {
3104 public void run() {
3105 // Check that mStateAnimation hasn't changed while
3106 // we waited for a layout/draw pass
3107 if (mStateAnimation != stateAnimation)
3108 return;
3109 setPivotsForZoom(toView, scale);
3110 dispatchOnLauncherTransitionStart(fromView, animated, false);
3111 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003112 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003113 }
3114 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003115 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003116 final ViewTreeObserver observer = toView.getViewTreeObserver();
3117 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3118 public void onGlobalLayout() {
3119 startAnimRunnable.run();
3120 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3121 }
3122 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003123 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003124 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003125 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003126 } else {
3127 toView.setTranslationX(0.0f);
3128 toView.setTranslationY(0.0f);
3129 toView.setScaleX(1.0f);
3130 toView.setScaleY(1.0f);
3131 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003132 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003133
Daniel Sandlere4f98912013-06-25 15:13:26 -04003134 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003135 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003136 if (mSearchDropTargetBar != null) {
3137 mSearchDropTargetBar.hideSearchBar(false);
3138 }
Michael Jurkaabded662011-03-04 12:06:57 -08003139 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003140 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003141 dispatchOnLauncherTransitionStart(fromView, animated, false);
3142 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003143 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003144 dispatchOnLauncherTransitionStart(toView, animated, false);
3145 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003146 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003147 }
3148
3149 /**
3150 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003151 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003152 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003153 */
Adam Cohened307df2013-10-02 09:37:31 -07003154 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003155 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003156
Michael Jurkab3e22d92011-10-31 15:58:33 -07003157 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003158 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003159 mStateAnimation.cancel();
3160 mStateAnimation = null;
3161 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003162 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003163
Winson Chungf0ea4d32011-06-06 14:27:16 -07003164 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003165 final int fadeOutDuration =
3166 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003167 final float scaleFactor = (float)
3168 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3169 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003170 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003171 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003172 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003173 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3174 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003175 toState, animated, stagger, -1);
3176 } else if (toState == Workspace.State.SPRING_LOADED ||
3177 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003178 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003179 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003180 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003181
Michael Jurkab3e22d92011-10-31 15:58:33 -07003182 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003183 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003184 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003185 final LauncherViewPropertyAnimator scaleAnim =
3186 new LauncherViewPropertyAnimator(fromView);
3187 scaleAnim.
3188 scaleX(scaleFactor).scaleY(scaleFactor).
3189 setDuration(duration).
3190 setInterpolator(new Workspace.ZoomInInterpolator());
3191
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003192 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003193 .ofFloat(fromView, "alpha", 1f, 0f)
3194 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003195 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003196 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3197 @Override
3198 public void onAnimationUpdate(ValueAnimator animation) {
3199 float t = 1f - (Float) animation.getAnimatedValue();
3200 dispatchOnLauncherTransitionStep(fromView, t);
3201 dispatchOnLauncherTransitionStep(toView, t);
3202 }
3203 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003204
Michael Jurka2ecf9952012-06-18 12:52:28 -07003205 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003206
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003207 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3208 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003209 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003210
3211 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003212 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003213 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003214 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003215 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3216 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003217 if (onCompleteRunnable != null) {
3218 onCompleteRunnable.run();
3219 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003220 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003221 }
3222 });
3223
Winson Chungf0ea4d32011-06-06 14:27:16 -07003224 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003225 if (workspaceAnim != null) {
3226 mStateAnimation.play(workspaceAnim);
3227 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003228 dispatchOnLauncherTransitionStart(fromView, animated, true);
3229 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003230 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003231 } else {
3232 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003233 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003234 dispatchOnLauncherTransitionStart(fromView, animated, true);
3235 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003236 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003237 dispatchOnLauncherTransitionStart(toView, animated, true);
3238 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003239 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003240 }
3241
Michael Jurkae326f182011-11-21 14:05:46 -08003242 @Override
3243 public void onTrimMemory(int level) {
3244 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003245 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003246 mAppsCustomizeTabHost.onTrimMemory();
3247 }
3248 }
3249
Adam Cohened307df2013-10-02 09:37:31 -07003250 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003251 showWorkspace(animated, null);
3252 }
3253
Adam Cohened307df2013-10-02 09:37:31 -07003254 protected void showWorkspace() {
3255 showWorkspace(true);
3256 }
3257
Adam Cohened66b2b2012-01-23 17:28:51 -08003258 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003259 if (mWorkspace.isInOverviewMode()) {
3260 mWorkspace.exitOverviewMode(animated);
3261 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003262 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003263 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003264 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003265 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003266
Winson Chungc7d2b602012-05-16 17:02:20 -07003267 // Show the search bar (only animate if we were showing the drop target bar in spring
3268 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003269 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003270 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003271 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003272
Michael Jurkab3e22d92011-10-31 15:58:33 -07003273 // Set focus to the AppsCustomize button
3274 if (mAllAppsButton != null) {
3275 mAllAppsButton.requestFocus();
3276 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003277 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003278
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003279 // Change the state *after* we've called all the transition code
3280 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003281
Winson Chung5fb63472011-02-02 17:03:37 -08003282 // Resume the auto-advance of widgets
3283 mUserPresent = true;
3284 updateRunning();
3285
alanv1d4fde62012-10-17 13:15:47 -07003286 // Send an accessibility event to announce the context change
3287 getWindow().getDecorView()
3288 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003289
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003290 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003291 }
3292
Adam Cohened307df2013-10-02 09:37:31 -07003293 void showOverviewMode(boolean animated) {
3294 mWorkspace.setVisibility(View.VISIBLE);
3295 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3296 mState = State.WORKSPACE;
3297 onWorkspaceShown(animated);
3298 }
3299
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003300 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003301 }
3302
Winson Chung82963d52013-10-09 11:20:57 -07003303 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3304 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003305 if (mState != State.WORKSPACE) return;
3306
Winson Chung82963d52013-10-09 11:20:57 -07003307 if (resetPageToZero) {
3308 mAppsCustomizeTabHost.reset();
3309 }
Winson Chungc58497e2013-09-03 17:48:37 -07003310 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003311 mAppsCustomizeTabHost.requestFocus();
3312
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313 // Change the state *after* we've called all the transition code
3314 mState = State.APPS_CUSTOMIZE;
3315
3316 // Pause the auto-advance of widgets until we are out of AllApps
3317 mUserPresent = false;
3318 updateRunning();
3319 closeFolder();
3320
3321 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003322 getWindow().getDecorView()
3323 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003324 }
3325
Adam Cohen7777d962011-08-18 18:58:38 -07003326 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003327 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003328 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003329 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003330 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003331 }
Adam Cohen7777d962011-08-18 18:58:38 -07003332
Adam Cohenad4e15c2013-10-17 16:21:35 -07003333 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003334 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003335 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3336
Winson Chunge7a03942011-08-05 15:05:12 -07003337 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003338 @Override
3339 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003340 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003341 // Before we show workspace, hide all apps again because
3342 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3343 // clean up our state transition functions
3344 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003345 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003346 } else {
3347 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003348 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003349 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003350 }, delay);
3351
Winson Chung557d6ed2011-07-08 15:34:52 -07003352 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003353
Michael Jurkad3ef3062010-11-23 16:23:58 -08003354 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003355 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003356 final boolean animated = true;
3357 final boolean springLoaded = true;
3358 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003359 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003360 }
3361 // Otherwise, we are not in spring loaded mode, so don't do anything.
3362 }
3363
Michael Jurkab3e22d92011-10-31 15:58:33 -07003364 void lockAllApps() {
3365 // TODO
3366 }
3367
3368 void unlockAllApps() {
3369 // TODO
3370 }
3371
Winson Chungf0ea4d32011-06-06 14:27:16 -07003372 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003373 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003374 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003375 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003376 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003377 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003378 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003379 int duration = 0;
3380 if (mSearchDropTargetBar != null) {
3381 duration = mSearchDropTargetBar.getTransitionInDuration();
3382 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003383 mHotseat.animate().alpha(1f).setDuration(duration);
3384 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003385 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003386 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003387 }
3388 }
3389 }
3390
3391 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003392 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003393 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003394 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003395 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003396 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003397 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003398 int duration = 0;
3399 if (mSearchDropTargetBar != null) {
3400 duration = mSearchDropTargetBar.getTransitionOutDuration();
3401 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003402 mHotseat.animate().alpha(0f).setDuration(duration);
3403 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003404 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003405 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003406 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003407 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003408 }
3409
Patrick Dubroy5f445422011-02-18 14:35:21 -08003410 /**
3411 * Add an item from all apps or customize onto the given workspace screen.
3412 * If layout is null, add to the current screen.
3413 */
3414 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003415 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003416 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003417 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003418 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003419
Winson Chungdff8ebb2011-09-08 17:25:31 -07003420 /** Maps the current orientation to an index for referencing orientation correct global icons */
3421 private int getCurrentOrientationIndexForGlobalIcons() {
3422 // default - 0, landscape - 1
3423 switch (getResources().getConfiguration().orientation) {
3424 case Configuration.ORIENTATION_LANDSCAPE:
3425 return 1;
3426 default:
3427 return 0;
3428 }
3429 }
3430
Michael Jurkaae65ee32012-04-30 11:45:54 -07003431 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003432 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003433 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003434 // Look for the toolbar icon specified in the activity meta-data
3435 Bundle metaData = packageManager.getActivityInfo(
3436 activityName, PackageManager.GET_META_DATA).metaData;
3437 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003438 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003439 if (iconResId != 0) {
3440 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003441 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003442 }
3443 }
3444 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003445 // This can happen if the activity defines an invalid drawable
3446 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3447 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003448 } catch (Resources.NotFoundException nfe) {
3449 // This can happen if the activity defines an invalid drawable
3450 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3451 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003452 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003453 return null;
3454 }
3455
3456 // if successful in getting icon, return it; otherwise, set button to use default drawable
3457 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003458 int buttonId, ComponentName activityName, int fallbackDrawableId,
3459 String toolbarResourceName) {
3460 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003461 Resources r = getResources();
3462 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3463 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003464
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003465 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003466 // If we were unable to find the icon via the meta-data, use a generic one
3467 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003468 toolbarIcon = r.getDrawable(fallbackDrawableId);
3469 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003470 if (button != null) {
3471 button.setCompoundDrawables(toolbarIcon, null, null, null);
3472 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003473 return null;
3474 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003475 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003476 if (button != null) {
3477 button.setCompoundDrawables(toolbarIcon, null, null, null);
3478 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003479 return toolbarIcon.getConstantState();
3480 }
3481 }
3482
3483 // if successful in getting icon, return it; otherwise, set button to use default drawable
3484 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003485 int buttonId, ComponentName activityName, int fallbackDrawableId,
3486 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003487 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003488 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003489
Michael Jurka19e0fc52011-07-22 18:00:21 -07003490 if (button != null) {
3491 // If we were unable to find the icon via the meta-data, use a
3492 // generic one
3493 if (toolbarIcon == null) {
3494 button.setImageResource(fallbackDrawableId);
3495 } else {
3496 button.setImageDrawable(toolbarIcon);
3497 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003498 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003499
3500 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3501
Michael Jurka0423dcf2010-10-05 14:56:18 -07003502 }
3503
Winson Chung1b884092012-06-08 17:13:02 -07003504 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003505 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003506 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003507 }
3508
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003509 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003510 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003511 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003512 }
3513
Winson Chungbb185bd2011-11-21 12:31:42 -08003514 private void invalidatePressedFocusedStates(View container, View button) {
3515 if (container instanceof HolographicLinearLayout) {
3516 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3517 layout.invalidatePressedFocusedStates();
3518 } else if (button instanceof HolographicImageView) {
3519 HolographicImageView view = (HolographicImageView) button;
3520 view.invalidatePressedFocusedStates();
3521 }
3522 }
3523
Cristina Stancu476493b2013-08-07 17:20:14 +01003524 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003525 if (mQsb == null) {
3526 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3527 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003528 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003529 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003530 }
3531
3532 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003533 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003534 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003535 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003536 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003537
Winson Chung1cad91e2011-05-25 17:41:01 -07003538 final SearchManager searchManager =
3539 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3540 ComponentName activityName = searchManager.getGlobalSearchActivity();
3541 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003542 int coi = getCurrentOrientationIndexForGlobalIcons();
3543 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003544 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3545 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3546 if (sGlobalSearchIcon[coi] == null) {
3547 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3548 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3549 TOOLBAR_ICON_METADATA_NAME);
3550 }
3551
Winson Chungc51db6a2011-10-05 11:44:49 -07003552 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3553 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003554 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003555 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003556 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003557 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003558 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3559 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003560 if (searchButton != null) searchButton.setVisibility(View.GONE);
3561 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003562 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003563 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003564 }
3565 }
3566
Cristina Stancu476493b2013-08-07 17:20:14 +01003567 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003568 final View searchButtonContainer = findViewById(R.id.search_button_container);
3569 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003570 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003571 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003572 }
3573
Cristina Stancu476493b2013-08-07 17:20:14 +01003574 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003575 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003576 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003577
Winson Chungc51db6a2011-10-05 11:44:49 -07003578 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003579 final SearchManager searchManager =
3580 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3581 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3582
3583 ComponentName activityName = null;
3584 if (globalSearchActivity != null) {
3585 // Check if the global search activity handles voice search
3586 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3587 intent.setPackage(globalSearchActivity.getPackageName());
3588 activityName = intent.resolveActivity(getPackageManager());
3589 }
3590
3591 if (activityName == null) {
3592 // Fallback: check if an activity other than the global search activity
3593 // resolves this
3594 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3595 activityName = intent.resolveActivity(getPackageManager());
3596 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003597 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003598 int coi = getCurrentOrientationIndexForGlobalIcons();
3599 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003600 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3601 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3602 if (sVoiceSearchIcon[coi] == null) {
3603 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3604 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3605 TOOLBAR_ICON_METADATA_NAME);
3606 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003607 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003608 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003609 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003610 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003611 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003612 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003613 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003614 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003615 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003616 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003617 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003618 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003619
Cristina Stancu476493b2013-08-07 17:20:14 +01003620 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003621 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3622 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003623 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003624 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003625 }
3626
Winson Chung5841efa2013-09-30 18:06:44 -07003627 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003628 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3629 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003630 boolean visible = !forceDisableVoiceButtonProxy &&
3631 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003632 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003633 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003634 }
3635 }
Winson Chung5841efa2013-09-30 18:06:44 -07003636
3637 /**
3638 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3639 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3640 */
3641 public void disableVoiceButtonProxy(boolean disabled) {
3642 updateVoiceButtonProxyVisible(disabled);
3643 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003644 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003645 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003646 */
3647 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003648 if (!DISABLE_MARKET_BUTTON) {
3649 final View marketButton = findViewById(R.id.market_button);
3650 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3651 // Find the app market activity by resolving an intent.
3652 // (If multiple app markets are installed, it will return the ResolverActivity.)
3653 ComponentName activityName = intent.resolveActivity(getPackageManager());
3654 if (activityName != null) {
3655 int coi = getCurrentOrientationIndexForGlobalIcons();
3656 mAppMarketIntent = intent;
3657 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3658 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3659 TOOLBAR_ICON_METADATA_NAME);
3660 marketButton.setVisibility(View.VISIBLE);
3661 } else {
3662 // We should hide and disable the view so that we don't try and restore the visibility
3663 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3664 marketButton.setVisibility(View.GONE);
3665 marketButton.setEnabled(false);
3666 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003667 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003668 }
3669
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003670 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003671 if (!DISABLE_MARKET_BUTTON) {
3672 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3673 Resources r = getResources();
3674 Drawable marketIconDrawable = d.newDrawable(r);
3675 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3676 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3677 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003678
Winson Chungd64a6662013-09-30 11:06:59 -07003679 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3680 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003681 }
3682
Michael Jurkad7c28052012-04-27 15:43:36 -07003683 @Override
3684 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003685 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003686 final List<CharSequence> text = event.getText();
3687 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003688 // Populate event with a fake title based on the current state.
3689 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003690 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003691 } else {
3692 text.add(getString(R.string.all_apps_home_button_label));
3693 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003694 return result;
3695 }
3696
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003697 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003698 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003699 */
3700 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3701 @Override
3702 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003703 closeSystemDialogs();
3704 }
3705 }
3706
3707 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003708 * Receives notifications whenever the appwidgets are reset.
3709 */
3710 private class AppWidgetResetObserver extends ContentObserver {
3711 public AppWidgetResetObserver() {
3712 super(new Handler());
3713 }
3714
3715 @Override
3716 public void onChange(boolean selfChange) {
3717 onAppWidgetReset();
3718 }
3719 }
3720
3721 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003722 * If the activity is currently paused, signal that we need to run the passed Runnable
3723 * in onResume.
3724 *
3725 * This needs to be called from incoming places where resources might have been loaded
3726 * while we are paused. That is becaues the Configuration might be wrong
3727 * when we're not running, and if it comes back to what it was when we
3728 * were paused, we are not restarted.
3729 *
3730 * Implementation of the method from LauncherModel.Callbacks.
3731 *
3732 * @return true if we are currently paused. The caller might be able to
3733 * skip some work in that case since we will come back again.
3734 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003735 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003736 if (mPaused) {
3737 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003738 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003739 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003740 }
3741 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003742 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003743 return true;
3744 } else {
3745 return false;
3746 }
3747 }
3748
Michael Jurkac402cd92013-05-20 15:49:32 +02003749 private boolean waitUntilResume(Runnable run) {
3750 return waitUntilResume(run, false);
3751 }
3752
Michael Jurka1e2f4652013-07-08 18:03:46 -07003753 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003754 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003755 }
3756
Michael Jurka7607c2f2013-04-03 14:33:19 -07003757 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003758 * If the activity is currently paused, signal that we need to re-run the loader
3759 * in onResume.
3760 *
3761 * This needs to be called from incoming places where resources might have been loaded
3762 * while we are paused. That is becaues the Configuration might be wrong
3763 * when we're not running, and if it comes back to what it was when we
3764 * were paused, we are not restarted.
3765 *
3766 * Implementation of the method from LauncherModel.Callbacks.
3767 *
3768 * @return true if we are currently paused. The caller might be able to
3769 * skip some work in that case since we will come back again.
3770 */
3771 public boolean setLoadOnResume() {
3772 if (mPaused) {
3773 Log.i(TAG, "setLoadOnResume");
3774 mOnResumeNeedsLoad = true;
3775 return true;
3776 } else {
3777 return false;
3778 }
3779 }
3780
3781 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003783 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003784 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003785 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003786 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003787 } else {
3788 return SCREEN_COUNT / 2;
3789 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003790 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003791
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 /**
3793 * Refreshes the shortcuts shown on the workspace.
3794 *
3795 * Implementation of the method from LauncherModel.Callbacks.
3796 */
3797 public void startBinding() {
Adam Cohen517a7f52014-03-01 12:12:59 -08003798 mWorkspaceLoading = true;
3799
Michael Jurka7607c2f2013-04-03 14:33:19 -07003800 // If we're starting binding all over again, clear any bind calls we'd postponed in
3801 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3802 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003803 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003804
Winson Chungd64d1762013-08-20 14:37:16 -07003805 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003806 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003807 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003808
Michael Jurka05bf6442011-11-30 20:28:53 -08003809 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003810 if (mHotseat != null) {
3811 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003812 }
3813 }
3814
Adam Cohendcd297f2013-06-18 13:13:40 -07003815 @Override
3816 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003817 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003818
Adam Cohen5084cba2013-09-03 12:01:16 -07003819 // If there are no screens, we need to have an empty screen
3820 if (orderedScreenIds.size() == 0) {
3821 mWorkspace.addExtraEmptyScreen();
3822 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003823
3824 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003825 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003826 if (hasCustomContentToLeft()) {
3827 mWorkspace.createCustomContentContainer();
3828 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003829 }
Winson Chung64359a52013-07-08 17:17:08 -07003830 }
3831
3832 @Override
3833 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003834 // Log to disk
3835 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3836 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3837 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003838 int count = orderedScreenIds.size();
3839 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003840 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003841 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003842 }
3843
Adam Cohen39a06042013-07-19 14:30:12 -07003844 private boolean shouldShowWeightWatcher() {
3845 String spKey = LauncherAppState.getSharedPreferencesKey();
3846 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003847 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003848
3849 return show;
3850 }
3851
3852 private void toggleShowWeightWatcher() {
3853 String spKey = LauncherAppState.getSharedPreferencesKey();
3854 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3855 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3856
3857 show = !show;
3858
3859 SharedPreferences.Editor editor = sp.edit();
3860 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3861 editor.commit();
3862
3863 if (mWeightWatcher != null) {
3864 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3865 }
3866 }
3867
Winson Chungd64d1762013-08-20 14:37:16 -07003868 public void bindAppsAdded(final ArrayList<Long> newScreens,
3869 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003870 final ArrayList<ItemInfo> addAnimated,
3871 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003872 Runnable r = new Runnable() {
3873 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003874 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003875 }
3876 };
3877 if (waitUntilResume(r)) {
3878 return;
3879 }
3880
Winson Chungd64d1762013-08-20 14:37:16 -07003881 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003882 if (newScreens != null) {
3883 bindAddScreens(newScreens);
3884 }
Winson Chungd64d1762013-08-20 14:37:16 -07003885
3886 // We add the items without animation on non-visible pages, and with
3887 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003888 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003889 bindItems(addNotAnimated, 0,
3890 addNotAnimated.size(), false);
3891 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003892 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003893 bindItems(addAnimated, 0,
3894 addAnimated.size(), true);
3895 }
Winson Chungc58497e2013-09-03 17:48:37 -07003896
Winson Chung87412982013-10-03 18:34:14 -07003897 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003898 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003899
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003900 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003901 addedApps != null && mAppsCustomizeContent != null) {
3902 mAppsCustomizeContent.addApps(addedApps);
3903 }
Winson Chungd64d1762013-08-20 14:37:16 -07003904 }
3905
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003906 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003907 * Bind the items start-end from the list.
3908 *
3909 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003910 */
Winson Chung64359a52013-07-08 17:17:08 -07003911 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3912 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003913 Runnable r = new Runnable() {
3914 public void run() {
3915 bindItems(shortcuts, start, end, forceAnimateIcons);
3916 }
3917 };
3918 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003919 return;
3920 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003921
Winson Chungf0c6ae02012-03-21 16:10:31 -07003922 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003923 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3924 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003925 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003926 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003927 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003928 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003929 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003930
3931 // Short circuit if we are loading dock items for a configuration which has no dock
3932 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3933 mHotseat == null) {
3934 continue;
3935 }
3936
Joe Onorato9c1289c2009-08-17 11:03:03 -04003937 switch (item.itemType) {
3938 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3939 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003940 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003941 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003942
Winson Chung64359a52013-07-08 17:17:08 -07003943 /*
3944 * TODO: FIX collision case
3945 */
Winson Chungce376632013-07-11 16:12:41 -07003946 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3947 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3948 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003949 View v = cl.getChildAt(item.cellX, item.cellY);
3950 Object tag = v.getTag();
3951 String desc = "Collision while binding workspace item: " + item
3952 + ". Collides with " + tag;
3953 if (LauncherAppState.isDogfoodBuild()) {
3954 throw (new RuntimeException(desc));
3955 } else {
3956 Log.d(TAG, desc);
3957 }
Winson Chungce376632013-07-11 16:12:41 -07003958 }
Winson Chung64359a52013-07-08 17:17:08 -07003959 }
3960
Adam Cohendcd297f2013-06-18 13:13:40 -07003961 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3962 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003963 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003964 // Animate all the applications up now
3965 shortcut.setAlpha(0f);
3966 shortcut.setScaleX(0f);
3967 shortcut.setScaleY(0f);
3968 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003969 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003970 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003971 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003972 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003973 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003974 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003975 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003976 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3977 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003978 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003979 default:
3980 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003982 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003983
Winson Chung997a9232013-07-24 15:33:46 -07003984 if (animateIcons) {
3985 // Animate to the correct page
3986 if (newShortcutsScreenId > -1) {
3987 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003988 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003989 final Runnable startBounceAnimRunnable = new Runnable() {
3990 public void run() {
3991 anim.playTogether(bounceAnims);
3992 anim.start();
3993 }
3994 };
Winson Chung997a9232013-07-24 15:33:46 -07003995 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003996 // We post the animation slightly delayed to prevent slowdowns
3997 // when we are loading right after we return to launcher.
3998 mWorkspace.postDelayed(new Runnable() {
3999 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004000 if (mWorkspace != null) {
4001 mWorkspace.snapToPage(newScreenIndex);
4002 mWorkspace.postDelayed(startBounceAnimRunnable,
4003 NEW_APPS_ANIMATION_DELAY);
4004 }
Winson Chung94d67682013-09-25 16:29:40 -07004005 }
4006 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004007 } else {
4008 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004009 }
4010 }
Winson Chung64359a52013-07-08 17:17:08 -07004011 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004012 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004013 }
4014
Joe Onorato9c1289c2009-08-17 11:03:03 -04004015 /**
4016 * Implementation of the method from LauncherModel.Callbacks.
4017 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004018 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004019 Runnable r = new Runnable() {
4020 public void run() {
4021 bindFolders(folders);
4022 }
4023 };
4024 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004025 return;
4026 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004027 sFolders.clear();
4028 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004029 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030
4031 /**
4032 * Add the views for a widget to the workspace.
4033 *
4034 * Implementation of the method from LauncherModel.Callbacks.
4035 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004036 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004037 Runnable r = new Runnable() {
4038 public void run() {
4039 bindAppWidget(item);
4040 }
4041 };
4042 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004043 return;
4044 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004045
Daniel Sandler843e8602010-06-07 14:59:01 -04004046 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4047 if (DEBUG_WIDGETS) {
4048 Log.d(TAG, "bindAppWidget: " + item);
4049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004050 final Workspace workspace = mWorkspace;
4051
4052 final int appWidgetId = item.appWidgetId;
4053 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004054 if (DEBUG_WIDGETS) {
4055 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4056 }
4057
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4059
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004061 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062
Adam Cohendcd297f2013-06-18 13:13:40 -07004063 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004065 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4066
Joe Onorato9c1289c2009-08-17 11:03:03 -04004067 workspace.requestLayout();
4068
Daniel Sandler843e8602010-06-07 14:59:01 -04004069 if (DEBUG_WIDGETS) {
4070 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4071 + (SystemClock.uptimeMillis()-start) + "ms");
4072 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 }
4074
Adam Cohen1462de32012-07-24 22:34:36 -07004075 public void onPageBoundSynchronously(int page) {
4076 mSynchronouslyBoundPages.add(page);
4077 }
4078
Joe Onorato9c1289c2009-08-17 11:03:03 -04004079 /**
4080 * Callback saying that there aren't any more items to bind.
4081 *
4082 * Implementation of the method from LauncherModel.Callbacks.
4083 */
Adam Cohene25af792013-06-06 23:08:25 -07004084 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004085 Runnable r = new Runnable() {
4086 public void run() {
4087 finishBindingItems(upgradePath);
4088 }
4089 };
4090 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004091 return;
4092 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093 if (mSavedState != null) {
4094 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004095 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004096 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004097 mSavedState = null;
4098 }
4099
Adam Cohen1462de32012-07-24 22:34:36 -07004100 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004101
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004102 // If we received the result of any pending adds while the loader was running (e.g. the
4103 // widget configuration forced an orientation change), process them now.
4104 for (int i = 0; i < sPendingAddList.size(); i++) {
4105 completeAdd(sPendingAddList.get(i));
4106 }
4107 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004108
Winson Chungc51db6a2011-10-05 11:44:49 -07004109 // Update the market app icon as necessary (the other icons will be managed in response to
4110 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004111 if (!DISABLE_MARKET_BUTTON) {
4112 updateAppMarketIcon();
4113 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004114
Winson Chungf0c6ae02012-03-21 16:10:31 -07004115 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004116 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004117 mWorkspace.getUniqueComponents(true, null);
4118 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004119 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004120 }
4121
Winson Chunga0b7e862013-09-05 16:03:15 -07004122 public boolean isAllAppsButtonRank(int rank) {
4123 if (mHotseat != null) {
4124 return mHotseat.isAllAppsButtonRank(rank);
4125 }
4126 return false;
4127 }
4128
Winson Chunga2413752012-04-03 14:22:34 -07004129 private boolean canRunNewAppsAnimation() {
4130 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4131 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4132 }
4133
Winson Chungc9168342013-06-26 14:54:55 -07004134 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4135 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4136 PropertyValuesHolder.ofFloat("alpha", 1f),
4137 PropertyValuesHolder.ofFloat("scaleX", 1f),
4138 PropertyValuesHolder.ofFloat("scaleY", 1f));
4139 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4140 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4141 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4142 return bounceAnim;
4143 }
4144
Adam Cohen27772732013-11-11 14:00:56 +00004145 public boolean useVerticalBarLayout() {
4146 return LauncherAppState.getInstance().getDynamicGrid().
4147 getDeviceProfile().isVerticalBarLayout();
4148 }
4149
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004150 protected Rect getSearchBarBounds() {
4151 return LauncherAppState.getInstance().getDynamicGrid().
4152 getDeviceProfile().getSearchBarBounds();
4153 }
4154
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004155 @Override
4156 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004157 boolean searchVisible = updateGlobalSearchIcon();
4158 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004159 if (mSearchDropTargetBar != null) {
4160 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4161 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004162 }
4163
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004164 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004165 * Add the icons for all apps.
4166 *
4167 * Implementation of the method from LauncherModel.Callbacks.
4168 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004169 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004170 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004171 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4172 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4173 getHotseat().addAllAppsFolder(mIconCache, apps,
4174 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4175 }
4176 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004177 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004178 if (mAppsCustomizeContent != null) {
4179 mAppsCustomizeContent.onPackagesUpdated(
4180 LauncherModel.getSortedWidgetsAndShortcuts(this));
4181 }
Winson Chungc58497e2013-09-03 17:48:37 -07004182 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004183 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004184 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004185 mAppsCustomizeContent.onPackagesUpdated(
4186 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004187 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004188 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004189 }
4190
4191 /**
4192 * A package was updated.
4193 *
4194 * Implementation of the method from LauncherModel.Callbacks.
4195 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004196 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004197 Runnable r = new Runnable() {
4198 public void run() {
4199 bindAppsUpdated(apps);
4200 }
4201 };
4202 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004203 return;
4204 }
4205
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004206 if (mWorkspace != null) {
4207 mWorkspace.updateShortcuts(apps);
4208 }
Winson Chungc58497e2013-09-03 17:48:37 -07004209
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004210 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004211 mAppsCustomizeContent != null) {
4212 mAppsCustomizeContent.updateApps(apps);
4213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004214 }
4215
4216 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004217 * Update the state of a package, typically related to install state.
4218 *
4219 * Implementation of the method from LauncherModel.Callbacks.
4220 */
4221 public void updatePackageState(String pkgName, int state) {
4222 if (mWorkspace != null) {
4223 mWorkspace.updatePackageState(pkgName, state);
4224 }
4225 }
4226
4227 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004228 * A package was uninstalled. We take both the super set of packageNames
4229 * in addition to specific applications to remove, the reason being that
4230 * this can be called when a package is updated as well. In that scenario,
4231 * we only remove specific components from the workspace, where as
4232 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004233 *
4234 * Implementation of the method from LauncherModel.Callbacks.
4235 */
Winson Chung83892cc2013-05-01 16:53:33 -07004236 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004237 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004238 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004239 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004240 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004241 }
Winson Chungd64d1762013-08-20 14:37:16 -07004242 };
4243 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004244 return;
4245 }
4246
Winson Chungdf95eb12013-10-16 14:57:07 -07004247 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004248 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004249 }
4250 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004251 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004252 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004253
Winson Chunga1820962011-10-03 16:31:06 -07004254 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004255 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004256
Winson Chungdf95eb12013-10-16 14:57:07 -07004257 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004258 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004259 mAppsCustomizeContent != null) {
4260 mAppsCustomizeContent.removeApps(appInfos);
4261 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004262 }
Joe Onoratobe386092009-11-17 17:32:16 -08004263
4264 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004265 * A number of packages were updated.
4266 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004267 private ArrayList<Object> mWidgetsAndShortcuts;
4268 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004269 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004270 bindPackagesUpdated(mWidgetsAndShortcuts);
4271 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004272 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004273 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004274 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4275 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4276 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004277 return;
4278 }
4279
Winson Chung64359a52013-07-08 17:17:08 -07004280 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004281 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004282 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004283 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004284 }
4285
Winson Chung400438b2011-01-16 17:53:48 -08004286 private int mapConfigurationOriActivityInfoOri(int configOri) {
4287 final Display d = getWindowManager().getDefaultDisplay();
4288 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4289 switch (d.getRotation()) {
4290 case Surface.ROTATION_0:
4291 case Surface.ROTATION_180:
4292 // We are currently in the same basic orientation as the natural orientation
4293 naturalOri = configOri;
4294 break;
4295 case Surface.ROTATION_90:
4296 case Surface.ROTATION_270:
4297 // We are currently in the other basic orientation to the natural orientation
4298 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4299 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4300 break;
4301 }
4302
4303 int[] oriMap = {
4304 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4305 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4306 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4307 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4308 };
4309 // Since the map starts at portrait, we need to offset if this device's natural orientation
4310 // is landscape.
4311 int indexOffset = 0;
4312 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4313 indexOffset = 1;
4314 }
4315 return oriMap[(d.getRotation() + indexOffset) % 4];
4316 }
Adam Cohen446e9402011-09-15 18:21:21 -07004317
Winson Chung4b919f82012-05-01 10:44:08 -07004318 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004319 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004320 getResources().getBoolean(R.bool.allow_rotation);
4321 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004322 }
Winson Chung4b919f82012-05-01 10:44:08 -07004323 public void lockScreenOrientation() {
4324 if (isRotationEnabled()) {
4325 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4326 .getConfiguration().orientation));
4327 }
4328 }
4329 public void unlockScreenOrientation(boolean immediate) {
4330 if (isRotationEnabled()) {
4331 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004332 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004333 } else {
4334 mHandler.postDelayed(new Runnable() {
4335 public void run() {
4336 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4337 }
4338 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004339 }
Winson Chung4b919f82012-05-01 10:44:08 -07004340 }
Winson Chung400438b2011-01-16 17:53:48 -08004341 }
4342
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004343 /**
4344 * Called when the SearchBar hint should be changed.
4345 *
4346 * @param hint the hint to be displayed in the search bar.
4347 */
4348 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004349 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004350 }
4351
Winson Chunge43a1e72014-01-15 10:33:02 -08004352 protected boolean isLauncherPreinstalled() {
4353 PackageManager pm = getPackageManager();
4354 try {
4355 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4356 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4357 return true;
4358 } else {
4359 return false;
4360 }
4361 } catch (NameNotFoundException e) {
4362 e.printStackTrace();
4363 return false;
4364 }
4365 }
4366
Winson Chunge6eabff2013-09-24 11:01:23 -07004367 protected String getFirstRunClingSearchBarHint() {
4368 return "";
4369 }
4370 protected String getFirstRunCustomContentHint() {
4371 return "";
4372 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004373 protected int getFirstRunFocusedHotseatAppDrawableId() {
4374 return -1;
4375 }
4376 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4377 return null;
4378 }
4379 protected int getFirstRunFocusedHotseatAppRank() {
4380 return -1;
4381 }
4382 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4383 return "";
4384 }
4385 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4386 return "";
4387 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004388
Winson Chungaf40f202013-09-18 18:26:31 -07004389 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004390 mLauncherClings.dismissFirstRunCling(v);
4391 }
4392 public void dismissMigrationClingCopyApps(View v) {
4393 mLauncherClings.dismissMigrationClingCopyApps(v);
4394 }
4395 public void dismissMigrationClingUseDefault(View v) {
4396 mLauncherClings.dismissMigrationClingUseDefault(v);
4397 }
4398 public void dismissMigrationWorkspaceCling(View v) {
4399 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004400 }
Winson Chung82f55532011-08-09 14:14:23 -07004401 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004402 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004403 }
4404 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004405 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004406 }
4407
Adam Cohen432609a2014-03-13 17:03:22 -07004408
4409 /**
4410 * To be overridden by subclasses to indicate that there is an activity to launch
4411 * before showing the standard launcher experience.
4412 */
4413 protected boolean hasFirstRunActivity() {
4414 return false;
4415 }
4416
4417 /**
4418 * To be overridden by subclasses to launch any first run activity
4419 */
4420 protected Intent getFirstRunActivity() {
4421 return null;
4422 }
4423
Winson Chunga6945242014-01-08 14:04:34 -08004424 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004425 return !ActivityManager.isRunningInTestHarness() &&
4426 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004427 }
4428
Adam Cohen4a9423d2014-03-28 14:22:31 -07004429 protected boolean hasRunFirstRunActivity() {
4430 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4431 }
4432
Adam Cohen432609a2014-03-13 17:03:22 -07004433 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004434 if (shouldRunFirstRunActivity() &&
4435 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004436 Intent firstRunIntent = getFirstRunActivity();
4437 if (firstRunIntent != null) {
4438 startActivity(firstRunIntent);
4439 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004440 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004441 }
4442 }
Adam Cohen432609a2014-03-13 17:03:22 -07004443 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004444 }
4445
4446 private void markFirstRunActivityShown() {
4447 SharedPreferences.Editor editor = mSharedPrefs.edit();
4448 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4449 editor.apply();
4450 }
4451
Adam Cohen432609a2014-03-13 17:03:22 -07004452 /**
4453 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4454 * screen that must be displayed and dismissed.
4455 */
4456 protected boolean hasDismissableIntroScreen() {
4457 return false;
4458 }
4459
4460 /**
4461 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4462 */
4463 protected View getIntroScreen() {
4464 return null;
4465 }
4466
4467 /**
4468 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4469 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4470 */
4471 private boolean shouldShowIntroScreen() {
4472 return hasDismissableIntroScreen() &&
4473 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4474 }
4475
4476 protected void showIntroScreen() {
4477 View introScreen = getIntroScreen();
4478 changeWallpaperVisiblity(false);
4479 if (introScreen != null) {
4480 mDragLayer.showOverlayView(introScreen);
4481 }
4482 }
4483
4484 public void dismissIntroScreen() {
4485 markIntroScreenDismissed();
4486 if (showFirstRunActivity()) {
4487 // We delay hiding the intro view until the first run activity is showing. This
4488 // avoids a blip.
4489 mWorkspace.postDelayed(new Runnable() {
4490 @Override
4491 public void run() {
4492 mDragLayer.dismissOverlayView();
4493 }
4494 }, ACTIVITY_START_DELAY);
4495 } else {
4496 mDragLayer.dismissOverlayView();
4497 }
4498 changeWallpaperVisiblity(true);
4499 }
4500
4501 private void markIntroScreenDismissed() {
4502 SharedPreferences.Editor editor = mSharedPrefs.edit();
4503 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4504 editor.apply();
4505 }
4506
Winson Chunga6945242014-01-08 14:04:34 -08004507 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004508 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4509 if (mHotseat != null) mHotseat.setAlpha(1f);
4510 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4511 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004512 }
4513
4514 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004515 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4516 if (mHotseat != null) mHotseat.setAlpha(0f);
4517 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4518 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004519 }
4520
Mathew Inwood72fbec12013-11-19 15:45:07 +00004521 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004522 // Called from search suggestion, not supported in other profiles.
4523 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4524 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4525 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4526 myUser);
4527 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004528 return null;
4529 }
Kenny Guyed131872014-04-30 03:02:21 +01004530 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004531 }
4532
4533 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4534 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004535 // Called from search suggestion, not supported in other profiles.
4536 return createShortcutDragInfo(shortcutIntent, caption, icon,
4537 UserHandleCompat.myUserHandle());
4538 }
4539
4540 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4541 Bitmap icon, UserHandleCompat user) {
4542 return new ShortcutInfo(shortcutIntent, caption, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004543 }
4544
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004545 protected void moveWorkspaceToDefaultScreen() {
4546 mWorkspace.moveToDefaultScreen(false);
4547 }
4548
Mathew Inwood72fbec12013-11-19 15:45:07 +00004549 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4550 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004551 mWorkspace.onExternalDragStartedWithItem(dragView);
4552 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004553 }
4554
Anjali Koppalf5d29132014-02-28 13:33:27 -08004555 @Override
4556 public void onPageSwitch(View newPage, int newPageIndex) {
4557 }
4558
Winson Chung80baf5a2010-08-09 16:03:15 -07004559 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004560 * Prints out out state for debugging.
4561 */
4562 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004563 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004564 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004565 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4566 Log.d(TAG, "mRestoring=" + mRestoring);
4567 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4568 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004569 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004570 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004571
Winson Chung785d2eb2011-04-14 16:08:02 -07004572 if (mAppsCustomizeContent != null) {
4573 mAppsCustomizeContent.dumpState();
4574 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004575 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004576 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004577
4578 @Override
4579 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4580 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004581 synchronized (sDumpLogs) {
4582 writer.println(" ");
4583 writer.println("Debug logs: ");
4584 for (int i = 0; i < sDumpLogs.size(); i++) {
4585 writer.println(" " + sDumpLogs.get(i));
4586 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004587 }
4588 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004589
4590 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004591 if (DEBUG_DUMP_LOG) {
4592 synchronized (sDumpLogs) {
4593 Log.d(TAG, "");
4594 Log.d(TAG, "*********************");
4595 Log.d(TAG, "Launcher debug logs: ");
4596 for (int i = 0; i < sDumpLogs.size(); i++) {
4597 Log.d(TAG, " " + sDumpLogs.get(i));
4598 }
4599 Log.d(TAG, "*********************");
4600 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004601 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004602 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004603 }
4604
4605 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004606 addDumpLog(tag, log, null, debugLog);
4607 }
4608
4609 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004610 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004611 if (e != null) {
4612 Log.d(tag, log, e);
4613 } else {
4614 Log.d(tag, log);
4615 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004616 }
Winson Chungede41292013-09-19 16:27:36 -07004617 if (DEBUG_DUMP_LOG) {
4618 sDateStamp.setTime(System.currentTimeMillis());
4619 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004620 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4621 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004622 }
Winson Chungede41292013-09-19 16:27:36 -07004623 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004624 }
4625
Winson Chungede41292013-09-19 16:27:36 -07004626 public void dumpLogsToLocalData() {
4627 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004628 new AsyncTask<Void, Void, Void>() {
4629 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004630 boolean success = false;
4631 sDateStamp.setTime(sRunStart);
4632 String FILENAME = sDateStamp.getMonth() + "-"
4633 + sDateStamp.getDay() + "_"
4634 + sDateStamp.getHours() + "-"
4635 + sDateStamp.getMinutes() + "_"
4636 + sDateStamp.getSeconds() + ".txt";
4637
4638 FileOutputStream fos = null;
4639 File outFile = null;
4640 try {
4641 outFile = new File(getFilesDir(), FILENAME);
4642 outFile.createNewFile();
4643 fos = new FileOutputStream(outFile);
4644 } catch (Exception e) {
4645 e.printStackTrace();
4646 }
4647 if (fos != null) {
4648 PrintWriter writer = new PrintWriter(fos);
4649
4650 writer.println(" ");
4651 writer.println("Debug logs: ");
4652 synchronized (sDumpLogs) {
4653 for (int i = 0; i < sDumpLogs.size(); i++) {
4654 writer.println(" " + sDumpLogs.get(i));
4655 }
4656 }
4657 writer.close();
4658 }
4659 try {
4660 if (fos != null) {
4661 fos.close();
4662 success = true;
4663 }
4664 } catch (IOException e) {
4665 e.printStackTrace();
4666 }
Michael Jurka43467462013-11-14 12:03:58 +01004667 return null;
Winson Chungede41292013-09-19 16:27:36 -07004668 }
Michael Jurka43467462013-11-14 12:03:58 +01004669 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004670 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004672}
Michael Jurka2763be32011-02-24 11:19:57 -08004673
Michael Jurkaabded662011-03-04 12:06:57 -08004674interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004675 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004676 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004677 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004678 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004679 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004680}
Dan Sandlerd5024042014-01-09 15:01:33 -05004681
4682interface DebugIntents {
4683 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4684 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004685}