blob: bf2a02dac6afc1a93d3a5b57be9ec775e7d388a5 [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 Sandler6053b8002013-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
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700765 setWaitingForResult(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 Chung07e045c62013-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) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700938 setWorkspaceLoading(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);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001192 setWaitingForResult(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);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001571 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1572 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1573 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001574 }
1575 }
1576 };
1577
1578 @Override
1579 public void onAttachedToWindow() {
1580 super.onAttachedToWindow();
1581
1582 // Listen for broadcasts related to user-presence
1583 final IntentFilter filter = new IntentFilter();
1584 filter.addAction(Intent.ACTION_SCREEN_OFF);
1585 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001586 // For handling managed profiles
1587 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1588 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001589 if (ENABLE_DEBUG_INTENTS) {
1590 filter.addAction(DebugIntents.DELETE_DATABASE);
1591 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1592 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001593 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001594 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001595 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001596 mVisible = true;
1597 }
1598
1599 @Override
1600 public void onDetachedFromWindow() {
1601 super.onDetachedFromWindow();
1602 mVisible = false;
1603
Adam Cohend113e0c2010-11-11 10:48:05 -08001604 if (mAttached) {
1605 unregisterReceiver(mReceiver);
1606 mAttached = false;
1607 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001608 updateRunning();
1609 }
1610
1611 public void onWindowVisibilityChanged(int visibility) {
1612 mVisible = visibility == View.VISIBLE;
1613 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001614 // The following code used to be in onResume, but it turns out onResume is called when
1615 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1616 // is a more appropriate event to handle
1617 if (mVisible) {
1618 mAppsCustomizeTabHost.onWindowVisible();
1619 if (!mWorkspaceLoading) {
1620 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001621 // We want to let Launcher draw itself at least once before we force it to build
1622 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001623 // apps is nice and speedy.
1624 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001625 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001626 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001627 if (mStarted) return;
1628 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001629 // We delay the layer building a bit in order to give
1630 // other message processing a time to run. In particular
1631 // this avoids a delay in hiding the IME if it was
1632 // currently shown, because doing that may involve
1633 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001634 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001635 final ViewTreeObserver.OnDrawListener listener = this;
1636 mWorkspace.post(new Runnable() {
1637 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001638 if (mWorkspace != null &&
1639 mWorkspace.getViewTreeObserver() != null) {
1640 mWorkspace.getViewTreeObserver().
1641 removeOnDrawListener(listener);
1642 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001643 }
1644 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001645 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001646 }
1647 });
1648 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001649 // When Launcher comes back to foreground, a different Activity might be responsible for
1650 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001651 if (!DISABLE_MARKET_BUTTON) {
1652 updateAppMarketIcon();
1653 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001654 clearTypedText();
1655 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 }
1657
1658 private void sendAdvanceMessage(long delay) {
1659 mHandler.removeMessages(ADVANCE_MSG);
1660 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1661 mHandler.sendMessageDelayed(msg, delay);
1662 mAutoAdvanceSentTime = System.currentTimeMillis();
1663 }
1664
1665 private void updateRunning() {
1666 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1667 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1668 mAutoAdvanceRunning = autoAdvanceRunning;
1669 if (autoAdvanceRunning) {
1670 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1671 sendAdvanceMessage(delay);
1672 } else {
1673 if (!mWidgetsToAdvance.isEmpty()) {
1674 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1675 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1676 }
1677 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001678 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 }
1680 }
1681 }
1682
1683 private final Handler mHandler = new Handler() {
1684 @Override
1685 public void handleMessage(Message msg) {
1686 if (msg.what == ADVANCE_MSG) {
1687 int i = 0;
1688 for (View key: mWidgetsToAdvance.keySet()) {
1689 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1690 final int delay = mAdvanceStagger * i;
1691 if (v instanceof Advanceable) {
1692 postDelayed(new Runnable() {
1693 public void run() {
1694 ((Advanceable) v).advance();
1695 }
1696 }, delay);
1697 }
1698 i++;
1699 }
1700 sendAdvanceMessage(mAdvanceInterval);
1701 }
1702 }
1703 };
1704
1705 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001706 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1708 if (v instanceof Advanceable) {
1709 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001710 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 updateRunning();
1712 }
1713 }
1714
1715 void removeWidgetToAutoAdvance(View hostView) {
1716 if (mWidgetsToAdvance.containsKey(hostView)) {
1717 mWidgetsToAdvance.remove(hostView);
1718 updateRunning();
1719 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001720 }
1721
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001724 launcherInfo.hostView = null;
1725 }
1726
Winson Chung93eef082012-03-23 15:59:27 -07001727 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1728 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1729 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 }
1731
Winson Chunga6945242014-01-08 14:04:34 -08001732 public DragLayer getDragLayer() {
1733 return mDragLayer;
1734 }
1735
1736 public Workspace getWorkspace() {
1737 return mWorkspace;
1738 }
1739
1740 public Hotseat getHotseat() {
1741 return mHotseat;
1742 }
1743
Jorim Jaggid017f882014-01-14 17:08:48 -08001744 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001745 return mOverviewPanel;
1746 }
1747
1748 public SearchDropTargetBar getSearchBar() {
1749 return mSearchDropTargetBar;
1750 }
1751
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001752 public LauncherAppWidgetHost getAppWidgetHost() {
1753 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
Romain Guycbb89e42009-06-08 15:52:54 -07001755
Winson Chunga9abd0e2010-10-27 17:18:37 -07001756 public LauncherModel getModel() {
1757 return mModel;
1758 }
1759
Winson Chunga6945242014-01-08 14:04:34 -08001760 public LauncherClings getLauncherClings() {
1761 return mLauncherClings;
1762 }
1763
1764 protected SharedPreferences getSharedPrefs() {
1765 return mSharedPrefs;
1766 }
1767
Bjorn Bringertc459e522013-06-07 19:36:01 +01001768 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001769 getWindow().closeAllPanels();
1770
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001771 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001772 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001773 }
1774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 @Override
1776 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001777 long startTime = 0;
1778 if (DEBUG_RESUME_TIME) {
1779 startTime = System.currentTimeMillis();
1780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 super.onNewIntent(intent);
1782
1783 // Close the menu
1784 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001785 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001786 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001787
Adam Cohened307df2013-10-02 09:37:31 -07001788 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1789 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1790 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001791
Adam Cohen6fecd412013-10-02 17:41:50 -07001792 if (mWorkspace == null) {
1793 // Can be cases where mWorkspace is null, this prevents a NPE
1794 return;
1795 }
1796 Folder openFolder = mWorkspace.getOpenFolder();
1797 // In all these cases, only animate if we're already on home
1798 mWorkspace.exitWidgetResizeMode();
1799 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001800 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001801 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001802 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001803
Adam Cohen6fecd412013-10-02 17:41:50 -07001804 closeFolder();
1805 exitSpringLoadedDragMode();
1806
1807 // If we are already on home, then just animate back to the workspace,
1808 // otherwise, just wait until onResume to set the state back to Workspace
1809 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001810 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001811 } else {
1812 mOnResumeState = State.WORKSPACE;
1813 }
1814
1815 final View v = getWindow().peekDecorView();
1816 if (v != null && v.getWindowToken() != null) {
1817 InputMethodManager imm = (InputMethodManager)getSystemService(
1818 INPUT_METHOD_SERVICE);
1819 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1820 }
1821
1822 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001823 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001824 mAppsCustomizeTabHost.reset();
1825 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001826
1827 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 }
Adam Cohened307df2013-10-02 09:37:31 -07001829
Michael Jurka447bf842013-05-15 14:52:15 +02001830 if (DEBUG_RESUME_TIME) {
1831 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 }
1834
Adam Coppa120b8e2013-11-12 16:26:04 +00001835 /**
1836 * Override point for subclasses to prevent movement to the default screen when the home
1837 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1838 */
1839 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1840 return true;
1841 }
1842
1843 /**
1844 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1845 */
1846 protected void onHomeIntent() {
1847 // Do nothing
1848 }
1849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001851 public void onRestoreInstanceState(Bundle state) {
1852 super.onRestoreInstanceState(state);
1853 for (int page: mSynchronouslyBoundPages) {
1854 mWorkspace.restoreInstanceStateForChild(page);
1855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 }
1857
1858 @Override
1859 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001860 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001861 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1862 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001863 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001864 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865
Michael Jurka883f55b2010-10-21 15:47:14 -07001866 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001867 // We close any open folder since it will not be re-opened, and we need to make sure
1868 // this state is reflected.
1869 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870
Adam Cohendcd297f2013-06-18 13:13:40 -07001871 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001872 mWaitingForResult) {
1873 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001874 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001875 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1876 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001877 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1878 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1879 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001880 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 }
1882
1883 if (mFolderInfo != null && mWaitingForResult) {
1884 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1885 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1886 }
Michael Jurka946ad472010-07-09 18:05:18 -07001887
Winson Chung785d2eb2011-04-14 16:08:02 -07001888 // Save the current AppsCustomize tab
1889 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001890 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1891 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001892 if (currentTabTag != null) {
1893 outState.putString("apps_customize_currentTab", currentTabTag);
1894 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001895 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1896 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001897 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001898 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
1900
1901 @Override
1902 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001904
Winson Chunge7a03942011-08-05 15:05:12 -07001905 // Remove all pending runnables
1906 mHandler.removeMessages(ADVANCE_MSG);
1907 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001908 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001909
Winson Chungcd2b0142011-06-08 16:02:26 -07001910 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001911 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001912 mModel.stopLoader();
1913 app.setLauncher(null);
1914
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001916 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001918 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001920 mAppWidgetHost = null;
1921
1922 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923
1924 TextKeyListener.getInstance().release();
1925
Winson Chung81b52252012-08-27 15:34:29 -07001926 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1927 // to prevent leaking Launcher activities on orientation change.
1928 if (mModel != null) {
1929 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1930 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001931
1932 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001933 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001934
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001935 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001936 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001937 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001938 mWorkspace = null;
1939 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001940
Michael Jurka2ecf9952012-06-18 12:52:28 -07001941 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
1943
Adam Cohena9cf38f2011-05-02 15:36:58 -07001944 public DragController getDragController() {
1945 return mDragController;
1946 }
1947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 @Override
1949 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001950 if (requestCode >= 0) {
1951 setWaitingForResult(true);
1952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 super.startActivityForResult(intent, requestCode);
1954 }
1955
Winson Chung70d72102011-08-12 11:24:35 -07001956 /**
1957 * Indicates that we want global search for this activity by setting the globalSearch
1958 * argument for {@link #startSearch} to true.
1959 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001961 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001963
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001964 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001965
Karl Rosaen138a0412009-04-23 19:00:21 -07001966 if (initialQuery == null) {
1967 // Use any text typed in the launcher as the initial query
1968 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 if (appSearchData == null) {
1971 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001972 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
Winson Chungadf0c182012-08-23 14:59:07 -07001974 Rect sourceBounds = new Rect();
1975 if (mSearchDropTargetBar != null) {
1976 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1977 }
Romain Guycbb89e42009-06-08 15:52:54 -07001978
Bjorn Bringertc459e522013-06-07 19:36:01 +01001979 startSearch(initialQuery, selectInitialQuery,
1980 appSearchData, sourceBounds);
1981 }
1982
1983 public void startSearch(String initialQuery,
1984 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001985 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001986 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001987 }
1988
1989 /**
1990 * Starts the global search activity. This code is a copied from SearchManager
1991 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001992 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001993 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001994 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001995 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1996 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1997 if (globalSearchActivity == null) {
1998 Log.w(TAG, "No global search activity found.");
1999 return;
2000 }
2001 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2002 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2003 intent.setComponent(globalSearchActivity);
2004 // Make sure that we have a Bundle to put source in
2005 if (appSearchData == null) {
2006 appSearchData = new Bundle();
2007 } else {
2008 appSearchData = new Bundle(appSearchData);
2009 }
2010 // Set source to package name of app that starts global search, if not set already.
2011 if (!appSearchData.containsKey("source")) {
2012 appSearchData.putString("source", getPackageName());
2013 }
2014 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2015 if (!TextUtils.isEmpty(initialQuery)) {
2016 intent.putExtra(SearchManager.QUERY, initialQuery);
2017 }
2018 if (selectInitialQuery) {
2019 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2020 }
2021 intent.setSourceBounds(sourceBounds);
2022 try {
2023 startActivity(intent);
2024 } catch (ActivityNotFoundException ex) {
2025 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
Yura4f93ec62014-02-04 14:15:21 +00002029 public boolean isOnCustomContent() {
2030 return mWorkspace.isOnOrMovingToCustomContent();
2031 }
2032
Winson Chung70d72102011-08-12 11:24:35 -07002033 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002034 public boolean onPrepareOptionsMenu(Menu menu) {
2035 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002036 if (!isOnCustomContent()) {
2037 // Close any open folders
2038 closeFolder();
2039 // Stop resizing any widgets
2040 mWorkspace.exitWidgetResizeMode();
2041 if (!mWorkspace.isInOverviewMode()) {
2042 // Show the overview mode
2043 showOverviewMode(true);
2044 } else {
2045 showWorkspace(true);
2046 }
Winson Chunge0298742014-01-17 12:03:00 -08002047 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002048 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002051 @Override
2052 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002053 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002054 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002055 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002056 }
2057
Joe Onorato9c1289c2009-08-17 11:03:03 -04002058 public boolean isWorkspaceLocked() {
2059 return mWorkspaceLoading || mWaitingForResult;
2060 }
2061
Adam Cohen517a7f52014-03-01 12:12:59 -08002062 public boolean isWorkspaceLoading() {
2063 return mWorkspaceLoading;
2064 }
2065
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002066 private void setWorkspaceLoading(boolean value) {
2067 boolean isLocked = isWorkspaceLocked();
2068 mWorkspaceLoading = value;
2069 if (isLocked != isWorkspaceLocked()) {
2070 onWorkspaceLockedChanged();
2071 }
2072 }
2073
2074 private void setWaitingForResult(boolean value) {
2075 boolean isLocked = isWorkspaceLocked();
2076 mWaitingForResult = value;
2077 if (isLocked != isWorkspaceLocked()) {
2078 onWorkspaceLockedChanged();
2079 }
2080 }
2081
2082 protected void onWorkspaceLockedChanged() { }
2083
Michael Jurka0280c3b2010-09-17 15:00:07 -07002084 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002085 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002086 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002087 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2088 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002089 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002090 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002091 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002092
Adam Cohenad4e15c2013-10-17 16:21:35 -07002093 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2094 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2095 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2096 }
2097
2098 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2099 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2100 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002101 if (appWidgetInfo.configure != null) {
2102 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002103 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002104
Bjorn Bringert7984c942009-12-09 15:38:25 +00002105 // Launch over to configure widget, if needed
2106 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002107 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002108 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002109 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002111 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002112 Runnable onComplete = new Runnable() {
2113 @Override
2114 public void run() {
2115 // Exit spring loaded mode if necessary after adding the widget
2116 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2117 null);
2118 }
2119 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002120 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002121 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002122 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 }
2124 }
Romain Guycbb89e42009-06-08 15:52:54 -07002125
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002126 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002127 // Close any folders that may be open.
2128 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002129 mWorkspace.moveToCustomContentScreen(animate);
2130 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002131 /**
2132 * Process a shortcut drop.
2133 *
2134 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002135 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002136 * @param cell The cell it should be added to, optional
2137 * @param position The location on the screen where it was dropped, optional
2138 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002139 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002140 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002141 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002142 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002143 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002144 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002145
2146 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002147 mPendingAddInfo.cellX = cell[0];
2148 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002149 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002150
2151 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2152 createShortcutIntent.setComponent(componentName);
2153 processShortcut(createShortcutIntent);
2154 }
2155
Adam Cohenfbba09b2011-07-18 21:43:05 -07002156 /**
2157 * Process a widget drop.
2158 *
2159 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002160 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002161 * @param cell The cell it should be added to, optional
2162 * @param position The location on the screen where it was dropped, optional
2163 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002164 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002165 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002166 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002167 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002168 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002169 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002170 mPendingAddInfo.minSpanX = info.minSpanX;
2171 mPendingAddInfo.minSpanY = info.minSpanY;
2172
Adam Cohenfbba09b2011-07-18 21:43:05 -07002173 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002174 mPendingAddInfo.cellX = cell[0];
2175 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002176 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002177 if (span != null) {
2178 mPendingAddInfo.spanX = span[0];
2179 mPendingAddInfo.spanY = span[1];
2180 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002181
Adam Cohened66b2b2012-01-23 17:28:51 -08002182 AppWidgetHostView hostView = info.boundWidget;
2183 int appWidgetId;
2184 if (hostView != null) {
2185 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002186 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002187 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002188 // In this case, we either need to start an activity to get permission to bind
2189 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002190 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002191 Bundle options = info.bindOptions;
2192
2193 boolean success = false;
2194 if (options != null) {
2195 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2196 info.componentName, options);
2197 } else {
2198 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2199 info.componentName);
2200 }
2201 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002202 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002203 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002204 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002205 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2206 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2207 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002208 // TODO: we need to make sure that this accounts for the options bundle.
2209 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002210 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2211 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002212 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002213 }
2214
Joe Onoratodeb98af2010-02-19 14:59:39 -08002215 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002216 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 }
2218
Winson Chung24ab2f12010-09-16 14:10:47 -07002219 void processWallpaper(Intent intent) {
2220 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2221 }
2222
Adam Cohendcd297f2013-06-18 13:13:40 -07002223 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002224 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002225 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 folderInfo.title = getText(R.string.folder_name);
2227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002228 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002229 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002230 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002231 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232
2233 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002234 FolderIcon newFolder =
2235 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002237 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002238 // Force measure the new folder icon
2239 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2240 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002241 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 }
Romain Guycbb89e42009-06-08 15:52:54 -07002243
Joe Onorato9c1289c2009-08-17 11:03:03 -04002244 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002245 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002246 }
2247
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002248 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002249 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002250 }
2251
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002252 /**
2253 * Registers various content observers. The current implementation registers
2254 * only a favorites observer to keep track of the favorites applications.
2255 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002256 private void registerContentObservers() {
2257 ContentResolver resolver = getContentResolver();
2258 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2259 true, mWidgetObserver);
2260 }
2261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 @Override
2263 public boolean dispatchKeyEvent(KeyEvent event) {
2264 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2265 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002267 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002268 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002269 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002270 dumpState();
2271 return true;
2272 }
2273 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002274 }
2275 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2276 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002277 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 return true;
2279 }
2280 }
2281
2282 return super.dispatchKeyEvent(event);
2283 }
2284
Joe Onorato88ec0992009-11-19 13:16:06 -08002285 @Override
2286 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002287 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002288 if (mAppsCustomizeContent.getContentType() ==
2289 AppsCustomizePagedView.ContentType.Applications) {
2290 showWorkspace(true);
2291 } else {
2292 showOverviewMode(true);
2293 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002294 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002295 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002296 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002297 Folder openFolder = mWorkspace.getOpenFolder();
2298 if (openFolder.isEditingName()) {
2299 openFolder.dismissEditingName();
2300 } else {
2301 closeFolder();
2302 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002303 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002304 mWorkspace.exitWidgetResizeMode();
2305
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002306 // Back button is a no-op here, but give at least some feedback for the button press
2307 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002308 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002309 }
2310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002312 * Re-listen when widgets are reset.
2313 */
2314 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002315 if (mAppWidgetHost != null) {
2316 mAppWidgetHost.startListening();
2317 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002318 }
2319
2320 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 * Launches the intent referred by the clicked shortcut.
2322 *
2323 * @param v The view representing the clicked shortcut.
2324 */
2325 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002326 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2327 // view has detached (it's possible for this to happen if the view is removed mid touch).
2328 if (v.getWindowToken() == null) {
2329 return;
2330 }
2331
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002332 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002333 return;
2334 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002335
Adam Cohen1697b792013-09-17 19:08:21 -07002336 if (v instanceof Workspace) {
2337 if (mWorkspace.isInOverviewMode()) {
2338 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002339 }
2340 return;
2341 }
2342
2343 if (v instanceof CellLayout) {
2344 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002345 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002346 }
2347 }
2348
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002350 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002351 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002353 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002354 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002355 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002356 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002357 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 }
2359 }
2360
Michael Jurka0e260592010-06-30 17:07:39 -07002361 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002362 return false;
2363 }
2364
Michael Jurkaaf442092010-06-10 17:01:57 -07002365 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002366 * Event handler for the search button
2367 *
2368 * @param v The view that was clicked.
2369 */
2370 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002371 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2372
Amith Yamasania135ba82011-08-09 17:42:01 -07002373 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002374 }
2375
2376 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002377 * Event handler for the voice button
2378 *
2379 * @param v The view that was clicked.
2380 */
2381 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002382 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002383
Bjorn Bringertc459e522013-06-07 19:36:01 +01002384 startVoice();
2385 }
2386
2387 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002388 try {
2389 final SearchManager searchManager =
2390 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2391 ComponentName activityName = searchManager.getGlobalSearchActivity();
2392 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002393 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002394 if (activityName != null) {
2395 intent.setPackage(activityName.getPackageName());
2396 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002397 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002398 } catch (ActivityNotFoundException e) {
2399 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002400 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002401 startActivitySafely(null, intent, "onClickVoiceButton");
2402 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002403 }
2404
2405 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002406 * Event handler for the "grid" button that appears on the home screen, which
2407 * enters all apps mode.
2408 *
2409 * @param v The view that was clicked.
2410 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002411 protected void onClickAllAppsButton(View v) {
2412 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2413 if (isAllAppsVisible()) {
2414 showWorkspace(true);
2415 } else {
2416 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2417 }
2418 }
2419
2420 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002421 * Event handler for a paged view icon click.
2422 * @param v The view that was clicked.
2423 * @param appInfo The {link AppInfo} of the view.
2424 */
2425 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2426 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2427 startActivitySafely(v, appInfo.intent, appInfo);
2428 getStats().recordLaunch(appInfo.intent);
2429 }
2430
2431 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002432 * Event handler for an app shortcut click.
2433 *
2434 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2435 */
2436 protected void onClickAppShortcut(View v) {
2437 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2438 Object tag = v.getTag();
2439 if (!(tag instanceof ShortcutInfo)) {
2440 throw new IllegalArgumentException("Input must be a Shortcut");
2441 }
2442
2443 // Open shortcut
2444 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2445 final Intent intent = shortcut.intent;
2446
2447 // Check for special shortcuts
2448 if (intent.getComponent() != null) {
2449 final String shortcutClass = intent.getComponent().getClassName();
2450
2451 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2452 MemoryDumpActivity.startDump(this);
2453 return;
2454 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2455 toggleShowWeightWatcher();
2456 return;
2457 }
2458 }
2459
2460 // Start activities
2461 int[] pos = new int[2];
2462 v.getLocationOnScreen(pos);
2463 intent.setSourceBounds(new Rect(pos[0], pos[1],
2464 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2465
2466 boolean success = startActivitySafely(v, intent, tag);
2467
2468 mStats.recordLaunch(intent, shortcut);
2469
2470 if (success && v instanceof BubbleTextView) {
2471 mWaitingForResume = (BubbleTextView) v;
2472 mWaitingForResume.setStayPressed(true);
2473 }
2474 }
2475
2476 /**
2477 * Event handler for a folder icon click.
2478 *
2479 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2480 */
2481 protected void onClickFolderIcon(View v) {
2482 if (LOGD) Log.d(TAG, "onClickFolder");
2483 if (!(v instanceof FolderIcon)){
2484 throw new IllegalArgumentException("Input must be a FolderIcon");
2485 }
2486
2487 FolderIcon folderIcon = (FolderIcon) v;
2488 final FolderInfo info = folderIcon.getFolderInfo();
2489 Folder openFolder = mWorkspace.getFolderForTag(info);
2490
2491 // If the folder info reports that the associated folder is open, then verify that
2492 // it is actually opened. There have been a few instances where this gets out of sync.
2493 if (info.opened && openFolder == null) {
2494 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2495 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2496 info.opened = false;
2497 }
2498
2499 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2500 // Close any open folder
2501 closeFolder();
2502 // Open the requested folder
2503 openFolder(folderIcon);
2504 } else {
2505 // Find the open folder...
2506 int folderScreen;
2507 if (openFolder != null) {
2508 folderScreen = mWorkspace.getPageForView(openFolder);
2509 // .. and close it
2510 closeFolder(openFolder);
2511 if (folderScreen != mWorkspace.getCurrentPage()) {
2512 // Close any folder open on the current screen
2513 closeFolder();
2514 // Pull the folder onto this screen
2515 openFolder(folderIcon);
2516 }
2517 }
2518 }
Michael Jurka5130e402011-10-13 04:55:35 -07002519 }
2520
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002521 /**
2522 * Event handler for the (Add) Widgets button that appears after a long press
2523 * on the home screen.
2524 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002525 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002526 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002527 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2528 }
2529
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 /**
2531 * Event handler for the wallpaper picker button that appears after a long press
2532 * on the home screen.
2533 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002534 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002535 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2536 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2537 pickWallpaper.setComponent(getWallpaperPickerComponent());
2538 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2539 }
2540
2541 /**
2542 * Event handler for a click on the settings button that appears after a long press
2543 * on the home screen.
2544 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002545 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002546 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2547 }
2548
Michael Jurka5130e402011-10-13 04:55:35 -07002549 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002550 // Provide the same haptic feedback that the system offers for virtual keys.
2551 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002552 }
2553
Adam Cohen61f560d2013-09-30 15:58:20 -07002554 public void performHapticFeedbackOnTouchDown(View v) {
2555 // Provide the same haptic feedback that the system offers for virtual keys.
2556 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2557 }
2558
2559 public View.OnTouchListener getHapticFeedbackTouchListener() {
2560 if (mHapticFeedbackTouchListener == null) {
2561 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2562 @Override
2563 public boolean onTouch(View v, MotionEvent event) {
2564 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2565 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2566 }
2567 return false;
2568 }
2569 };
2570 }
2571 return mHapticFeedbackTouchListener;
2572 }
2573
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002574 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002575 if (!DISABLE_MARKET_BUTTON) {
2576 if (mAppMarketIntent != null) {
2577 startActivitySafely(v, mAppMarketIntent, "app market");
2578 } else {
2579 Log.e(TAG, "Invalid app market intent.");
2580 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002581 }
2582 }
2583
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002584 public void onDragStarted(View view) {}
2585
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002586 /**
2587 * Called when the user stops interacting with the launcher.
2588 * This implies that the user is now on the homescreen and is not doing housekeeping.
2589 */
2590 protected void onInteractionEnd() {}
2591
2592 /**
2593 * Called when the user starts interacting with the launcher.
2594 * The possible interactions are:
2595 * - open all apps
2596 * - reorder an app shortcut, or a widget
2597 * - open the overview mode.
2598 * This is a good time to stop doing things that only make sense
2599 * when the user is on the homescreen and not doing housekeeping.
2600 */
2601 protected void onInteractionBegin() {}
2602
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002603 void startApplicationDetailsActivity(ComponentName componentName) {
2604 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002605 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2606 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002607 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2608 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002609 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002610 }
2611
Michael Jurka1e2f4652013-07-08 18:03:46 -07002612 // returns true if the activity was started
2613 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002614 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002615 // System applications cannot be installed. For now, show a toast explaining that.
2616 // We may give them the option of disabling apps this way.
2617 int messageId = R.string.uninstall_system_app_text;
2618 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002619 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002620 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002621 String packageName = componentName.getPackageName();
2622 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002623 Intent intent = new Intent(
2624 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002625 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2626 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002627 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002628 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002629 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002630 }
2631
Michael Jurka86a720e2012-05-09 11:23:23 -07002632 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyed131872014-04-30 03:02:21 +01002633 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002635 // Only launch using the new animation if the shortcut has not opted out (this is a
2636 // private contract between launcher and may be ignored in the future).
2637 boolean useLaunchAnimation = (v != null) &&
2638 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002639 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2640 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2641 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, 0);
2642 UserHandleCompat user = serialNumber == 0 ? null :
2643 userManager.getUserForSerialNumber(serialNumber);
2644
Winson Chung2672ff92012-05-04 16:22:30 -07002645 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002646 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2647 v.getMeasuredWidth(), v.getMeasuredHeight());
Kenny Guyed131872014-04-30 03:02:21 +01002648 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2649 // Could be launching some bookkeeping activity
2650 startActivity(intent, opts.toBundle());
2651 } else {
2652 launcherApps.startActivityForProfile(intent.getComponent(),
2653 intent.getSourceBounds(),
2654 opts.toBundle(), user);
2655 }
Winson Chungc7450e32012-04-17 17:34:08 -07002656 } else {
2657 startActivity(intent);
2658 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002659 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660 } catch (SecurityException e) {
2661 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002662 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002663 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002664 "or use the exported attribute for this activity. "
2665 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002667 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002668 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002669
Michael Jurka86a720e2012-05-09 11:23:23 -07002670 boolean startActivitySafely(View v, Intent intent, Object tag) {
2671 boolean success = false;
2672 try {
2673 success = startActivity(v, intent, tag);
2674 } catch (ActivityNotFoundException e) {
2675 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2676 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2677 }
2678 return success;
2679 }
2680
Adam Cohen268c4752012-06-06 17:47:33 -07002681 /**
2682 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2683 * in the DragLayer in the exact absolute location of the original FolderIcon.
2684 */
2685 private void copyFolderIconToImage(FolderIcon fi) {
2686 final int width = fi.getMeasuredWidth();
2687 final int height = fi.getMeasuredHeight();
2688
2689 // Lazy load ImageView, Bitmap and Canvas
2690 if (mFolderIconImageView == null) {
2691 mFolderIconImageView = new ImageView(this);
2692 }
2693 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2694 mFolderIconBitmap.getHeight() != height) {
2695 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2696 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2697 }
2698
2699 DragLayer.LayoutParams lp;
2700 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2701 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2702 } else {
2703 lp = new DragLayer.LayoutParams(width, height);
2704 }
2705
Adam Cohen307fe232012-08-16 17:55:58 -07002706 // The layout from which the folder is being opened may be scaled, adjust the starting
2707 // view size by this scale factor.
2708 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002709 lp.customPosition = true;
2710 lp.x = mRectForFolderAnimation.left;
2711 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002712 lp.width = (int) (scale * width);
2713 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002714
2715 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2716 fi.draw(mFolderIconCanvas);
2717 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002718 if (fi.getFolder() != null) {
2719 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2720 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002721 }
Adam Cohen268c4752012-06-06 17:47:33 -07002722 // Just in case this image view is still in the drag layer from a previous animation,
2723 // we remove it and re-add it.
2724 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2725 mDragLayer.removeView(mFolderIconImageView);
2726 }
2727 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002728 if (fi.getFolder() != null) {
2729 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002730 }
Adam Cohen268c4752012-06-06 17:47:33 -07002731 }
2732
Adam Cohen2801caf2011-05-13 20:57:39 -07002733 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002734 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002735 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2736 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2737 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2738
Adam Cohenc51934b2011-07-26 21:07:43 -07002739 FolderInfo info = (FolderInfo) fi.getTag();
2740 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2741 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002742 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2743 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002744 }
2745
Adam Cohen268c4752012-06-06 17:47:33 -07002746 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2747 copyFolderIconToImage(fi);
2748 fi.setVisibility(View.INVISIBLE);
2749
Michael Jurka2ecf9952012-06-18 12:52:28 -07002750 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002751 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002752 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2753 oa.start();
2754 }
2755
Adam Cohen268c4752012-06-06 17:47:33 -07002756 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002757 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002758 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2759 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2760 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2761
Adam Cohen268c4752012-06-06 17:47:33 -07002762 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002763
Adam Cohen268c4752012-06-06 17:47:33 -07002764 // We remove and re-draw the FolderIcon in-case it has changed
2765 mDragLayer.removeView(mFolderIconImageView);
2766 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002767 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002768 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002769 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002770 oa.addListener(new AnimatorListenerAdapter() {
2771 @Override
2772 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002773 if (cl != null) {
2774 cl.clearFolderLeaveBehind();
2775 // Remove the ImageView copy of the FolderIcon and make the original visible.
2776 mDragLayer.removeView(mFolderIconImageView);
2777 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002778 }
2779 }
2780 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002781 oa.start();
2782 }
2783
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002785 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002786 * is animated relative to the specified View. If the View is null, no animation
2787 * is played.
2788 *
2789 * @param folderInfo The FolderInfo describing the folder to open.
2790 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002791 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002792 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002793 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794
Adam Cohena9cf38f2011-05-02 15:36:58 -07002795 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002796
Adam Cohen4554ee12011-08-03 16:13:21 -07002797 // Just verify that the folder hasn't already been added to the DragLayer.
2798 // There was a one-off crash where the folder had a parent already.
2799 if (folder.getParent() == null) {
2800 mDragLayer.addView(folder);
2801 mDragController.addDropTarget((DropTarget) folder);
2802 } else {
2803 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2804 folder.getParent() + ").");
2805 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002806 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002807 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002808
2809 // Notify the accessibility manager that this folder "window" has appeared and occluded
2810 // the workspace items
2811 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2812 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002813 }
2814
2815 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002816 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002817 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002818 if (folder.isEditingName()) {
2819 folder.dismissEditingName();
2820 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002821 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002822
2823 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002824 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002825 }
2826 }
2827
2828 void closeFolder(Folder folder) {
2829 folder.getInfo().opened = false;
2830
2831 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2832 if (parent != null) {
2833 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2834 shrinkAndFadeInFolderIcon(fi);
2835 }
2836 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002837
2838 // Notify the accessibility manager that this folder "window" has disappeard and no
2839 // longer occludeds the workspace items
2840 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002841 }
2842
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002843 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002844 if (!isDraggingEnabled()) return false;
2845 if (isWorkspaceLocked()) return false;
2846 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002847
Adam Cohen1697b792013-09-17 19:08:21 -07002848 if (v instanceof Workspace) {
2849 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002850 if (mWorkspace.enterOverviewMode()) {
2851 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2852 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2853 return true;
2854 } else {
2855 return false;
2856 }
Adam Cohen1697b792013-09-17 19:08:21 -07002857 }
Adam Cohen1697b792013-09-17 19:08:21 -07002858 }
2859
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002861 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 }
2863
Michael Jurka0280c3b2010-09-17 15:00:07 -07002864 resetAddInfo();
2865 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002866 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002867 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002868 return true;
2869 }
2870
Winson Chung3d503fb2011-07-13 17:25:49 -07002871 // The hotseat touch handling does not go through Workspace, and we always allow long press
2872 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002873 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002874 final boolean inHotseat = isHotseatLayout(v);
2875 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002876 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002877 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002878 // User long pressed on empty space
2879 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2880 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2881 // Disabling reordering until we sort out some issues.
2882 if (mWorkspace.isInOverviewMode()) {
2883 mWorkspace.startReordering(v);
2884 } else {
2885 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002888 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2889 mHotseat.getOrderInHotseat(
2890 longClickCellInfo.cellX,
2891 longClickCellInfo.cellY));
2892 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002893 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002894 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002895 }
2896 }
2897 }
2898 return true;
2899 }
2900
Winson Chung3d503fb2011-07-13 17:25:49 -07002901 boolean isHotseatLayout(View layout) {
2902 return mHotseat != null && layout != null &&
2903 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2904 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002905
Winson Chung3d503fb2011-07-13 17:25:49 -07002906 /**
2907 * Returns the CellLayout of the specified container at the specified screen.
2908 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002909 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002910 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2911 if (mHotseat != null) {
2912 return mHotseat.getLayout();
2913 } else {
2914 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002915 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002916 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002917 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002918 }
2919 }
2920
Daniel Sandler843e8602010-06-07 14:59:01 -04002921 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002922 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002923 }
2924
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002925 /**
2926 * Helper method for the cameraZoomIn/cameraZoomOut animations
2927 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002928 * @param scaleFactor The scale factor used for the zoom
2929 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002930 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002931 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002932 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002933 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002934
Craig Mautner360310b2012-10-26 15:13:08 -07002935 private void setWorkspaceBackground(boolean workspace) {
2936 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002937 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002938 }
2939
Selim Cinek3a8a8f72014-01-16 10:38:38 -08002940 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002941 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2942 int curflags = getWindow().getAttributes().flags
2943 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2944 if (wpflags != curflags) {
2945 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2946 }
Craig Mautner360310b2012-10-26 15:13:08 -07002947 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002948 }
2949
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002950 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2951 if (v instanceof LauncherTransitionable) {
2952 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2953 }
2954 }
2955
Michael Jurkabed61d22012-02-14 22:51:29 -08002956 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2957 if (v instanceof LauncherTransitionable) {
2958 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2959 }
Winson Chung70442722012-02-10 15:43:22 -08002960
2961 // Update the workspace transition step as well
2962 dispatchOnLauncherTransitionStep(v, 0f);
2963 }
2964
2965 private void dispatchOnLauncherTransitionStep(View v, float t) {
2966 if (v instanceof LauncherTransitionable) {
2967 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2968 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002969 }
2970
2971 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2972 if (v instanceof LauncherTransitionable) {
2973 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2974 }
Winson Chung70442722012-02-10 15:43:22 -08002975
2976 // Update the workspace transition step as well
2977 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002978 }
2979
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002980 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002981 * Things to test when changing the following seven functions.
2982 * - Home from workspace
2983 * - from center screen
2984 * - from other screens
2985 * - Home from all apps
2986 * - from center screen
2987 * - from other screens
2988 * - Back from all apps
2989 * - from center screen
2990 * - from other screens
2991 * - Launch app from workspace and quit
2992 * - with back
2993 * - with home
2994 * - Launch app from all apps and quit
2995 * - with back
2996 * - with home
2997 * - Go to a screen that's not the default, then all
2998 * apps, and launch and app, and go back
2999 * - with back
3000 * -with home
3001 * - On workspace, long press power and go back
3002 * - with back
3003 * - with home
3004 * - On all apps, long press power and go back
3005 * - with back
3006 * - with home
3007 * - On workspace, power off
3008 * - On all apps, power off
3009 * - Launch an app and turn off the screen while in that app
3010 * - Go back with home key
3011 * - Go back with back key TODO: make this not go to workspace
3012 * - From all apps
3013 * - From workspace
3014 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3015 * - From all apps
3016 * - From the center workspace
3017 * - From another workspace
3018 */
3019
3020 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003021 * Zoom the camera out from the workspace to reveal 'toView'.
3022 * Assumes that the view to show is anchored at either the very top or very bottom
3023 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003024 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003025 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003026 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3027 showAppsCustomizeHelper(animated, springLoaded, contentType);
3028 }
3029 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3030 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003031 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003032 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003033 mStateAnimation.cancel();
3034 mStateAnimation = null;
3035 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003036 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003037
Winson Chungf0ea4d32011-06-06 14:27:16 -07003038 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3039 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3040 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003041 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003042 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003043 final int startDelay =
3044 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003045
Michael Jurkab3e22d92011-10-31 15:58:33 -07003046 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003047
Michael Jurkad74c9842011-07-10 12:44:21 -07003048 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003049 Animator workspaceAnim =
3050 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003051 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003052 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3053 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003054 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3055 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003056
3057 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003058 toView.setScaleX(scale);
3059 toView.setScaleY(scale);
3060 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3061 scaleAnim.
3062 scaleX(1f).scaleY(1f).
3063 setDuration(duration).
3064 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003065
Winson Chungf0ea4d32011-06-06 14:27:16 -07003066 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003067 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003068 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003069 .ofFloat(toView, "alpha", 0f, 1f)
3070 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003071 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003072 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3073 @Override
3074 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003075 if (animation == null) {
3076 throw new RuntimeException("animation is null");
3077 }
Winson Chung70442722012-02-10 15:43:22 -08003078 float t = (Float) animation.getAnimatedValue();
3079 dispatchOnLauncherTransitionStep(fromView, t);
3080 dispatchOnLauncherTransitionStep(toView, t);
3081 }
3082 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003083
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003084 // toView should appear right at the end of the workspace shrink
3085 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003086 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003087 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003088 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003089
3090 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003091 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003092 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003093 // Prepare the position
3094 toView.setTranslationX(0.0f);
3095 toView.setTranslationY(0.0f);
3096 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003097 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003098 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003099 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003100 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003101 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3102 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003103
Winson Chungc7d2b602012-05-16 17:02:20 -07003104 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003105 if (mSearchDropTargetBar != null) {
3106 mSearchDropTargetBar.hideSearchBar(false);
3107 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003108 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003109 });
3110
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003111 if (workspaceAnim != null) {
3112 mStateAnimation.play(workspaceAnim);
3113 }
Michael Jurka1899a362011-11-03 13:50:45 -07003114
3115 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003116
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003117 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3118 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003119
3120 // If any of the objects being animated haven't been measured/laid out
3121 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003122 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003123 (mWorkspace.getMeasuredWidth() == 0) ||
3124 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003125 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003126 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003127
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003128 final AnimatorSet stateAnimation = mStateAnimation;
3129 final Runnable startAnimRunnable = new Runnable() {
3130 public void run() {
3131 // Check that mStateAnimation hasn't changed while
3132 // we waited for a layout/draw pass
3133 if (mStateAnimation != stateAnimation)
3134 return;
3135 setPivotsForZoom(toView, scale);
3136 dispatchOnLauncherTransitionStart(fromView, animated, false);
3137 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003138 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003139 }
3140 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003141 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003142 final ViewTreeObserver observer = toView.getViewTreeObserver();
3143 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3144 public void onGlobalLayout() {
3145 startAnimRunnable.run();
3146 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3147 }
3148 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003149 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003150 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003151 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003152 } else {
3153 toView.setTranslationX(0.0f);
3154 toView.setTranslationY(0.0f);
3155 toView.setScaleX(1.0f);
3156 toView.setScaleY(1.0f);
3157 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003158 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003159
Daniel Sandlere4f98912013-06-25 15:13:26 -04003160 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003161 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003162 if (mSearchDropTargetBar != null) {
3163 mSearchDropTargetBar.hideSearchBar(false);
3164 }
Michael Jurkaabded662011-03-04 12:06:57 -08003165 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003166 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003167 dispatchOnLauncherTransitionStart(fromView, animated, false);
3168 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003169 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003170 dispatchOnLauncherTransitionStart(toView, animated, false);
3171 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003172 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003173 }
3174
3175 /**
3176 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003177 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003178 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003179 */
Adam Cohened307df2013-10-02 09:37:31 -07003180 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003181 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003182
Michael Jurkab3e22d92011-10-31 15:58:33 -07003183 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003184 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003185 mStateAnimation.cancel();
3186 mStateAnimation = null;
3187 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003188 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003189
Winson Chungf0ea4d32011-06-06 14:27:16 -07003190 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003191 final int fadeOutDuration =
3192 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003193 final float scaleFactor = (float)
3194 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3195 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003196 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003197 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003198 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003199 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3200 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003201 toState, animated, stagger, -1);
3202 } else if (toState == Workspace.State.SPRING_LOADED ||
3203 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003204 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003205 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003206 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003207
Michael Jurkab3e22d92011-10-31 15:58:33 -07003208 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003209 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003210 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003211 final LauncherViewPropertyAnimator scaleAnim =
3212 new LauncherViewPropertyAnimator(fromView);
3213 scaleAnim.
3214 scaleX(scaleFactor).scaleY(scaleFactor).
3215 setDuration(duration).
3216 setInterpolator(new Workspace.ZoomInInterpolator());
3217
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003218 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003219 .ofFloat(fromView, "alpha", 1f, 0f)
3220 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003221 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003222 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3223 @Override
3224 public void onAnimationUpdate(ValueAnimator animation) {
3225 float t = 1f - (Float) animation.getAnimatedValue();
3226 dispatchOnLauncherTransitionStep(fromView, t);
3227 dispatchOnLauncherTransitionStep(toView, t);
3228 }
3229 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003230
Michael Jurka2ecf9952012-06-18 12:52:28 -07003231 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003232
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003233 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3234 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003235 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003236
3237 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003238 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003239 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003240 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003241 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3242 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003243 if (onCompleteRunnable != null) {
3244 onCompleteRunnable.run();
3245 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003246 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003247 }
3248 });
3249
Winson Chungf0ea4d32011-06-06 14:27:16 -07003250 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003251 if (workspaceAnim != null) {
3252 mStateAnimation.play(workspaceAnim);
3253 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003254 dispatchOnLauncherTransitionStart(fromView, animated, true);
3255 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003256 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003257 } else {
3258 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003259 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003260 dispatchOnLauncherTransitionStart(fromView, animated, true);
3261 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003262 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003263 dispatchOnLauncherTransitionStart(toView, animated, true);
3264 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003265 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003266 }
3267
Michael Jurkae326f182011-11-21 14:05:46 -08003268 @Override
3269 public void onTrimMemory(int level) {
3270 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003271 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003272 mAppsCustomizeTabHost.onTrimMemory();
3273 }
3274 }
3275
Adam Cohened307df2013-10-02 09:37:31 -07003276 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003277 showWorkspace(animated, null);
3278 }
3279
Adam Cohened307df2013-10-02 09:37:31 -07003280 protected void showWorkspace() {
3281 showWorkspace(true);
3282 }
3283
Adam Cohened66b2b2012-01-23 17:28:51 -08003284 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003285 if (mWorkspace.isInOverviewMode()) {
3286 mWorkspace.exitOverviewMode(animated);
3287 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003288 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003289 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003290 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003291 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003292
Winson Chungc7d2b602012-05-16 17:02:20 -07003293 // Show the search bar (only animate if we were showing the drop target bar in spring
3294 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003295 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003296 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003297 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003298
Michael Jurkab3e22d92011-10-31 15:58:33 -07003299 // Set focus to the AppsCustomize button
3300 if (mAllAppsButton != null) {
3301 mAllAppsButton.requestFocus();
3302 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003303 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003304
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003305 // Change the state *after* we've called all the transition code
3306 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003307
Winson Chung5fb63472011-02-02 17:03:37 -08003308 // Resume the auto-advance of widgets
3309 mUserPresent = true;
3310 updateRunning();
3311
alanv1d4fde62012-10-17 13:15:47 -07003312 // Send an accessibility event to announce the context change
3313 getWindow().getDecorView()
3314 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003315
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003316 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003317 }
3318
Adam Cohened307df2013-10-02 09:37:31 -07003319 void showOverviewMode(boolean animated) {
3320 mWorkspace.setVisibility(View.VISIBLE);
3321 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3322 mState = State.WORKSPACE;
3323 onWorkspaceShown(animated);
3324 }
3325
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003326 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003327 }
3328
Winson Chung82963d52013-10-09 11:20:57 -07003329 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3330 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003331 if (mState != State.WORKSPACE) return;
3332
Winson Chung82963d52013-10-09 11:20:57 -07003333 if (resetPageToZero) {
3334 mAppsCustomizeTabHost.reset();
3335 }
Winson Chungc58497e2013-09-03 17:48:37 -07003336 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003337 mAppsCustomizeTabHost.requestFocus();
3338
Michael Jurkab3e22d92011-10-31 15:58:33 -07003339 // Change the state *after* we've called all the transition code
3340 mState = State.APPS_CUSTOMIZE;
3341
3342 // Pause the auto-advance of widgets until we are out of AllApps
3343 mUserPresent = false;
3344 updateRunning();
3345 closeFolder();
3346
3347 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003348 getWindow().getDecorView()
3349 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003350 }
3351
Adam Cohen7777d962011-08-18 18:58:38 -07003352 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003353 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003354 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003355 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003356 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003357 }
Adam Cohen7777d962011-08-18 18:58:38 -07003358
Adam Cohenad4e15c2013-10-17 16:21:35 -07003359 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003360 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003361 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3362
Winson Chunge7a03942011-08-05 15:05:12 -07003363 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003364 @Override
3365 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003366 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003367 // Before we show workspace, hide all apps again because
3368 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3369 // clean up our state transition functions
3370 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003371 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003372 } else {
3373 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003374 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003375 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003376 }, delay);
3377
Winson Chung557d6ed2011-07-08 15:34:52 -07003378 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003379
Michael Jurkad3ef3062010-11-23 16:23:58 -08003380 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003381 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003382 final boolean animated = true;
3383 final boolean springLoaded = true;
3384 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003385 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003386 }
3387 // Otherwise, we are not in spring loaded mode, so don't do anything.
3388 }
3389
Michael Jurkab3e22d92011-10-31 15:58:33 -07003390 void lockAllApps() {
3391 // TODO
3392 }
3393
3394 void unlockAllApps() {
3395 // TODO
3396 }
3397
Winson Chungf0ea4d32011-06-06 14:27:16 -07003398 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003399 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003400 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003401 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003402 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003403 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003404 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003405 int duration = 0;
3406 if (mSearchDropTargetBar != null) {
3407 duration = mSearchDropTargetBar.getTransitionInDuration();
3408 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003409 mHotseat.animate().alpha(1f).setDuration(duration);
3410 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003411 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003412 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003413 }
3414 }
3415 }
3416
3417 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003418 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003419 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003420 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003421 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003422 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003423 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003424 int duration = 0;
3425 if (mSearchDropTargetBar != null) {
3426 duration = mSearchDropTargetBar.getTransitionOutDuration();
3427 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003428 mHotseat.animate().alpha(0f).setDuration(duration);
3429 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003430 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003431 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003432 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003433 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003434 }
3435
Patrick Dubroy5f445422011-02-18 14:35:21 -08003436 /**
3437 * Add an item from all apps or customize onto the given workspace screen.
3438 * If layout is null, add to the current screen.
3439 */
3440 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003441 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003442 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003443 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003444 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003445
Winson Chungdff8ebb2011-09-08 17:25:31 -07003446 /** Maps the current orientation to an index for referencing orientation correct global icons */
3447 private int getCurrentOrientationIndexForGlobalIcons() {
3448 // default - 0, landscape - 1
3449 switch (getResources().getConfiguration().orientation) {
3450 case Configuration.ORIENTATION_LANDSCAPE:
3451 return 1;
3452 default:
3453 return 0;
3454 }
3455 }
3456
Michael Jurkaae65ee32012-04-30 11:45:54 -07003457 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003458 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003459 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003460 // Look for the toolbar icon specified in the activity meta-data
3461 Bundle metaData = packageManager.getActivityInfo(
3462 activityName, PackageManager.GET_META_DATA).metaData;
3463 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003464 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003465 if (iconResId != 0) {
3466 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003467 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003468 }
3469 }
3470 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003471 // This can happen if the activity defines an invalid drawable
3472 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3473 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003474 } catch (Resources.NotFoundException nfe) {
3475 // This can happen if the activity defines an invalid drawable
3476 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3477 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003478 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003479 return null;
3480 }
3481
3482 // if successful in getting icon, return it; otherwise, set button to use default drawable
3483 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003484 int buttonId, ComponentName activityName, int fallbackDrawableId,
3485 String toolbarResourceName) {
3486 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003487 Resources r = getResources();
3488 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3489 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003490
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003491 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003492 // If we were unable to find the icon via the meta-data, use a generic one
3493 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003494 toolbarIcon = r.getDrawable(fallbackDrawableId);
3495 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003496 if (button != null) {
3497 button.setCompoundDrawables(toolbarIcon, null, null, null);
3498 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003499 return null;
3500 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003501 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003502 if (button != null) {
3503 button.setCompoundDrawables(toolbarIcon, null, null, null);
3504 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003505 return toolbarIcon.getConstantState();
3506 }
3507 }
3508
3509 // if successful in getting icon, return it; otherwise, set button to use default drawable
3510 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003511 int buttonId, ComponentName activityName, int fallbackDrawableId,
3512 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003513 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003514 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003515
Michael Jurka19e0fc52011-07-22 18:00:21 -07003516 if (button != null) {
3517 // If we were unable to find the icon via the meta-data, use a
3518 // generic one
3519 if (toolbarIcon == null) {
3520 button.setImageResource(fallbackDrawableId);
3521 } else {
3522 button.setImageDrawable(toolbarIcon);
3523 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003524 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003525
3526 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3527
Michael Jurka0423dcf2010-10-05 14:56:18 -07003528 }
3529
Winson Chung1b884092012-06-08 17:13:02 -07003530 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003531 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003532 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003533 }
3534
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003535 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003536 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003537 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003538 }
3539
Winson Chungbb185bd2011-11-21 12:31:42 -08003540 private void invalidatePressedFocusedStates(View container, View button) {
3541 if (container instanceof HolographicLinearLayout) {
3542 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3543 layout.invalidatePressedFocusedStates();
3544 } else if (button instanceof HolographicImageView) {
3545 HolographicImageView view = (HolographicImageView) button;
3546 view.invalidatePressedFocusedStates();
3547 }
3548 }
3549
Cristina Stancu476493b2013-08-07 17:20:14 +01003550 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003551 if (mQsb == null) {
3552 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3553 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003554 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003555 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003556 }
3557
3558 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003559 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003560 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003561 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003562 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003563
Winson Chung1cad91e2011-05-25 17:41:01 -07003564 final SearchManager searchManager =
3565 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3566 ComponentName activityName = searchManager.getGlobalSearchActivity();
3567 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003568 int coi = getCurrentOrientationIndexForGlobalIcons();
3569 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003570 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3571 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3572 if (sGlobalSearchIcon[coi] == null) {
3573 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3574 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3575 TOOLBAR_ICON_METADATA_NAME);
3576 }
3577
Winson Chungc51db6a2011-10-05 11:44:49 -07003578 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3579 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003580 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003581 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003582 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003583 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003584 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3585 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003586 if (searchButton != null) searchButton.setVisibility(View.GONE);
3587 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003588 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003589 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003590 }
3591 }
3592
Cristina Stancu476493b2013-08-07 17:20:14 +01003593 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003594 final View searchButtonContainer = findViewById(R.id.search_button_container);
3595 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003596 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003597 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003598 }
3599
Cristina Stancu476493b2013-08-07 17:20:14 +01003600 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003601 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003602 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003603
Winson Chungc51db6a2011-10-05 11:44:49 -07003604 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003605 final SearchManager searchManager =
3606 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3607 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3608
3609 ComponentName activityName = null;
3610 if (globalSearchActivity != null) {
3611 // Check if the global search activity handles voice search
3612 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3613 intent.setPackage(globalSearchActivity.getPackageName());
3614 activityName = intent.resolveActivity(getPackageManager());
3615 }
3616
3617 if (activityName == null) {
3618 // Fallback: check if an activity other than the global search activity
3619 // resolves this
3620 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3621 activityName = intent.resolveActivity(getPackageManager());
3622 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003623 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003624 int coi = getCurrentOrientationIndexForGlobalIcons();
3625 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003626 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3627 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3628 if (sVoiceSearchIcon[coi] == null) {
3629 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3630 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3631 TOOLBAR_ICON_METADATA_NAME);
3632 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003633 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003634 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003635 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003636 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003637 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003638 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003639 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003640 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003641 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003642 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003643 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003644 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003645
Cristina Stancu476493b2013-08-07 17:20:14 +01003646 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003647 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3648 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003649 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003650 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003651 }
3652
Winson Chung5841efa2013-09-30 18:06:44 -07003653 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003654 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3655 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003656 boolean visible = !forceDisableVoiceButtonProxy &&
3657 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003658 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003659 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003660 }
3661 }
Winson Chung5841efa2013-09-30 18:06:44 -07003662
3663 /**
3664 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3665 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3666 */
3667 public void disableVoiceButtonProxy(boolean disabled) {
3668 updateVoiceButtonProxyVisible(disabled);
3669 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003670 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003671 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003672 */
3673 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003674 if (!DISABLE_MARKET_BUTTON) {
3675 final View marketButton = findViewById(R.id.market_button);
3676 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3677 // Find the app market activity by resolving an intent.
3678 // (If multiple app markets are installed, it will return the ResolverActivity.)
3679 ComponentName activityName = intent.resolveActivity(getPackageManager());
3680 if (activityName != null) {
3681 int coi = getCurrentOrientationIndexForGlobalIcons();
3682 mAppMarketIntent = intent;
3683 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3684 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3685 TOOLBAR_ICON_METADATA_NAME);
3686 marketButton.setVisibility(View.VISIBLE);
3687 } else {
3688 // We should hide and disable the view so that we don't try and restore the visibility
3689 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3690 marketButton.setVisibility(View.GONE);
3691 marketButton.setEnabled(false);
3692 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003693 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003694 }
3695
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003696 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003697 if (!DISABLE_MARKET_BUTTON) {
3698 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3699 Resources r = getResources();
3700 Drawable marketIconDrawable = d.newDrawable(r);
3701 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3702 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3703 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003704
Winson Chungd64a6662013-09-30 11:06:59 -07003705 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3706 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003707 }
3708
Michael Jurkad7c28052012-04-27 15:43:36 -07003709 @Override
3710 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003711 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003712 final List<CharSequence> text = event.getText();
3713 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003714 // Populate event with a fake title based on the current state.
3715 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003716 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003717 } else {
3718 text.add(getString(R.string.all_apps_home_button_label));
3719 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003720 return result;
3721 }
3722
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003723 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003724 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003725 */
3726 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3727 @Override
3728 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003729 closeSystemDialogs();
3730 }
3731 }
3732
3733 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003734 * Receives notifications whenever the appwidgets are reset.
3735 */
3736 private class AppWidgetResetObserver extends ContentObserver {
3737 public AppWidgetResetObserver() {
3738 super(new Handler());
3739 }
3740
3741 @Override
3742 public void onChange(boolean selfChange) {
3743 onAppWidgetReset();
3744 }
3745 }
3746
3747 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003748 * If the activity is currently paused, signal that we need to run the passed Runnable
3749 * in onResume.
3750 *
3751 * This needs to be called from incoming places where resources might have been loaded
3752 * while we are paused. That is becaues the Configuration might be wrong
3753 * when we're not running, and if it comes back to what it was when we
3754 * were paused, we are not restarted.
3755 *
3756 * Implementation of the method from LauncherModel.Callbacks.
3757 *
3758 * @return true if we are currently paused. The caller might be able to
3759 * skip some work in that case since we will come back again.
3760 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003761 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003762 if (mPaused) {
3763 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003764 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003765 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003766 }
3767 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003768 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003769 return true;
3770 } else {
3771 return false;
3772 }
3773 }
3774
Michael Jurkac402cd92013-05-20 15:49:32 +02003775 private boolean waitUntilResume(Runnable run) {
3776 return waitUntilResume(run, false);
3777 }
3778
Michael Jurka1e2f4652013-07-08 18:03:46 -07003779 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003780 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003781 }
3782
Michael Jurka7607c2f2013-04-03 14:33:19 -07003783 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003784 * If the activity is currently paused, signal that we need to re-run the loader
3785 * in onResume.
3786 *
3787 * This needs to be called from incoming places where resources might have been loaded
3788 * while we are paused. That is becaues the Configuration might be wrong
3789 * when we're not running, and if it comes back to what it was when we
3790 * were paused, we are not restarted.
3791 *
3792 * Implementation of the method from LauncherModel.Callbacks.
3793 *
3794 * @return true if we are currently paused. The caller might be able to
3795 * skip some work in that case since we will come back again.
3796 */
3797 public boolean setLoadOnResume() {
3798 if (mPaused) {
3799 Log.i(TAG, "setLoadOnResume");
3800 mOnResumeNeedsLoad = true;
3801 return true;
3802 } else {
3803 return false;
3804 }
3805 }
3806
3807 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003809 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003811 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003812 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003813 } else {
3814 return SCREEN_COUNT / 2;
3815 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003816 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003817
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818 /**
3819 * Refreshes the shortcuts shown on the workspace.
3820 *
3821 * Implementation of the method from LauncherModel.Callbacks.
3822 */
3823 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003824 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003825
Michael Jurka7607c2f2013-04-03 14:33:19 -07003826 // If we're starting binding all over again, clear any bind calls we'd postponed in
3827 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3828 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003829 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003830
Winson Chungd64d1762013-08-20 14:37:16 -07003831 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003832 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003833 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003834
Michael Jurka05bf644e2011-11-30 20:28:53 -08003835 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003836 if (mHotseat != null) {
3837 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003838 }
3839 }
3840
Adam Cohendcd297f2013-06-18 13:13:40 -07003841 @Override
3842 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003843 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003844
Adam Cohen5084cba2013-09-03 12:01:16 -07003845 // If there are no screens, we need to have an empty screen
3846 if (orderedScreenIds.size() == 0) {
3847 mWorkspace.addExtraEmptyScreen();
3848 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003849
3850 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003851 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003852 if (hasCustomContentToLeft()) {
3853 mWorkspace.createCustomContentContainer();
3854 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003855 }
Winson Chung64359a52013-07-08 17:17:08 -07003856 }
3857
3858 @Override
3859 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003860 // Log to disk
3861 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3862 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3863 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003864 int count = orderedScreenIds.size();
3865 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003866 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003867 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003868 }
3869
Adam Cohen39a06042013-07-19 14:30:12 -07003870 private boolean shouldShowWeightWatcher() {
3871 String spKey = LauncherAppState.getSharedPreferencesKey();
3872 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003873 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003874
3875 return show;
3876 }
3877
3878 private void toggleShowWeightWatcher() {
3879 String spKey = LauncherAppState.getSharedPreferencesKey();
3880 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3881 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3882
3883 show = !show;
3884
3885 SharedPreferences.Editor editor = sp.edit();
3886 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3887 editor.commit();
3888
3889 if (mWeightWatcher != null) {
3890 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3891 }
3892 }
3893
Winson Chungd64d1762013-08-20 14:37:16 -07003894 public void bindAppsAdded(final ArrayList<Long> newScreens,
3895 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003896 final ArrayList<ItemInfo> addAnimated,
3897 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003898 Runnable r = new Runnable() {
3899 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003900 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003901 }
3902 };
3903 if (waitUntilResume(r)) {
3904 return;
3905 }
3906
Winson Chungd64d1762013-08-20 14:37:16 -07003907 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003908 if (newScreens != null) {
3909 bindAddScreens(newScreens);
3910 }
Winson Chungd64d1762013-08-20 14:37:16 -07003911
3912 // We add the items without animation on non-visible pages, and with
3913 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003914 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003915 bindItems(addNotAnimated, 0,
3916 addNotAnimated.size(), false);
3917 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003918 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003919 bindItems(addAnimated, 0,
3920 addAnimated.size(), true);
3921 }
Winson Chungc58497e2013-09-03 17:48:37 -07003922
Winson Chung87412982013-10-03 18:34:14 -07003923 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003924 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003925
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003926 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003927 addedApps != null && mAppsCustomizeContent != null) {
3928 mAppsCustomizeContent.addApps(addedApps);
3929 }
Winson Chungd64d1762013-08-20 14:37:16 -07003930 }
3931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003932 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003933 * Bind the items start-end from the list.
3934 *
3935 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003936 */
Winson Chung64359a52013-07-08 17:17:08 -07003937 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3938 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003939 Runnable r = new Runnable() {
3940 public void run() {
3941 bindItems(shortcuts, start, end, forceAnimateIcons);
3942 }
3943 };
3944 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003945 return;
3946 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003947
Winson Chungf0c6ae02012-03-21 16:10:31 -07003948 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003949 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3950 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003951 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003952 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003953 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003954 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003955 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003956
3957 // Short circuit if we are loading dock items for a configuration which has no dock
3958 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3959 mHotseat == null) {
3960 continue;
3961 }
3962
Joe Onorato9c1289c2009-08-17 11:03:03 -04003963 switch (item.itemType) {
3964 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3965 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003966 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003967 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003968
Winson Chung64359a52013-07-08 17:17:08 -07003969 /*
3970 * TODO: FIX collision case
3971 */
Winson Chungce376632013-07-11 16:12:41 -07003972 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3973 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3974 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003975 View v = cl.getChildAt(item.cellX, item.cellY);
3976 Object tag = v.getTag();
3977 String desc = "Collision while binding workspace item: " + item
3978 + ". Collides with " + tag;
3979 if (LauncherAppState.isDogfoodBuild()) {
3980 throw (new RuntimeException(desc));
3981 } else {
3982 Log.d(TAG, desc);
3983 }
Winson Chungce376632013-07-11 16:12:41 -07003984 }
Winson Chung64359a52013-07-08 17:17:08 -07003985 }
3986
Adam Cohendcd297f2013-06-18 13:13:40 -07003987 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3988 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003989 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003990 // Animate all the applications up now
3991 shortcut.setAlpha(0f);
3992 shortcut.setScaleX(0f);
3993 shortcut.setScaleY(0f);
3994 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003995 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003997 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003998 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003999 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004000 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004001 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004002 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4003 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004005 default:
4006 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004008 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004009
Winson Chung997a9232013-07-24 15:33:46 -07004010 if (animateIcons) {
4011 // Animate to the correct page
4012 if (newShortcutsScreenId > -1) {
4013 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004014 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004015 final Runnable startBounceAnimRunnable = new Runnable() {
4016 public void run() {
4017 anim.playTogether(bounceAnims);
4018 anim.start();
4019 }
4020 };
Winson Chung997a9232013-07-24 15:33:46 -07004021 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004022 // We post the animation slightly delayed to prevent slowdowns
4023 // when we are loading right after we return to launcher.
4024 mWorkspace.postDelayed(new Runnable() {
4025 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004026 if (mWorkspace != null) {
4027 mWorkspace.snapToPage(newScreenIndex);
4028 mWorkspace.postDelayed(startBounceAnimRunnable,
4029 NEW_APPS_ANIMATION_DELAY);
4030 }
Winson Chung94d67682013-09-25 16:29:40 -07004031 }
4032 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004033 } else {
4034 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004035 }
4036 }
Winson Chung64359a52013-07-08 17:17:08 -07004037 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004038 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004039 }
4040
Joe Onorato9c1289c2009-08-17 11:03:03 -04004041 /**
4042 * Implementation of the method from LauncherModel.Callbacks.
4043 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004044 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004045 Runnable r = new Runnable() {
4046 public void run() {
4047 bindFolders(folders);
4048 }
4049 };
4050 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004051 return;
4052 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004053 sFolders.clear();
4054 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056
4057 /**
4058 * Add the views for a widget to the workspace.
4059 *
4060 * Implementation of the method from LauncherModel.Callbacks.
4061 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004062 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004063 Runnable r = new Runnable() {
4064 public void run() {
4065 bindAppWidget(item);
4066 }
4067 };
4068 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004069 return;
4070 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004071
Daniel Sandler843e8602010-06-07 14:59:01 -04004072 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4073 if (DEBUG_WIDGETS) {
4074 Log.d(TAG, "bindAppWidget: " + item);
4075 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004076 final Workspace workspace = mWorkspace;
4077
4078 final int appWidgetId = item.appWidgetId;
4079 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004080 if (DEBUG_WIDGETS) {
4081 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4082 }
4083
Joe Onorato9c1289c2009-08-17 11:03:03 -04004084 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4085
Joe Onorato9c1289c2009-08-17 11:03:03 -04004086 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004087 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004088
Adam Cohendcd297f2013-06-18 13:13:40 -07004089 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004090 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004091 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4092
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093 workspace.requestLayout();
4094
Daniel Sandler843e8602010-06-07 14:59:01 -04004095 if (DEBUG_WIDGETS) {
4096 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4097 + (SystemClock.uptimeMillis()-start) + "ms");
4098 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004099 }
4100
Adam Cohen1462de32012-07-24 22:34:36 -07004101 public void onPageBoundSynchronously(int page) {
4102 mSynchronouslyBoundPages.add(page);
4103 }
4104
Joe Onorato9c1289c2009-08-17 11:03:03 -04004105 /**
4106 * Callback saying that there aren't any more items to bind.
4107 *
4108 * Implementation of the method from LauncherModel.Callbacks.
4109 */
Adam Cohene25af792013-06-06 23:08:25 -07004110 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004111 Runnable r = new Runnable() {
4112 public void run() {
4113 finishBindingItems(upgradePath);
4114 }
4115 };
4116 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004117 return;
4118 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119 if (mSavedState != null) {
4120 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004121 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004123 mSavedState = null;
4124 }
4125
Adam Cohen1462de32012-07-24 22:34:36 -07004126 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004127
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004128 // If we received the result of any pending adds while the loader was running (e.g. the
4129 // widget configuration forced an orientation change), process them now.
4130 for (int i = 0; i < sPendingAddList.size(); i++) {
4131 completeAdd(sPendingAddList.get(i));
4132 }
4133 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004134
Winson Chungc51db6a2011-10-05 11:44:49 -07004135 // Update the market app icon as necessary (the other icons will be managed in response to
4136 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004137 if (!DISABLE_MARKET_BUTTON) {
4138 updateAppMarketIcon();
4139 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004140
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004141 setWorkspaceLoading(false);
Adam Cohene25af792013-06-06 23:08:25 -07004142 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004143 mWorkspace.getUniqueComponents(true, null);
4144 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004145 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004146 }
4147
Winson Chunga0b7e862013-09-05 16:03:15 -07004148 public boolean isAllAppsButtonRank(int rank) {
4149 if (mHotseat != null) {
4150 return mHotseat.isAllAppsButtonRank(rank);
4151 }
4152 return false;
4153 }
4154
Winson Chunga2413752012-04-03 14:22:34 -07004155 private boolean canRunNewAppsAnimation() {
4156 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4157 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4158 }
4159
Winson Chungc9168342013-06-26 14:54:55 -07004160 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4161 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4162 PropertyValuesHolder.ofFloat("alpha", 1f),
4163 PropertyValuesHolder.ofFloat("scaleX", 1f),
4164 PropertyValuesHolder.ofFloat("scaleY", 1f));
4165 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4166 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4167 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4168 return bounceAnim;
4169 }
4170
Adam Cohen27772732013-11-11 14:00:56 +00004171 public boolean useVerticalBarLayout() {
4172 return LauncherAppState.getInstance().getDynamicGrid().
4173 getDeviceProfile().isVerticalBarLayout();
4174 }
4175
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004176 protected Rect getSearchBarBounds() {
4177 return LauncherAppState.getInstance().getDynamicGrid().
4178 getDeviceProfile().getSearchBarBounds();
4179 }
4180
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004181 @Override
4182 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004183 boolean searchVisible = updateGlobalSearchIcon();
4184 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004185 if (mSearchDropTargetBar != null) {
4186 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4187 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004188 }
4189
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004190 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004191 * Add the icons for all apps.
4192 *
4193 * Implementation of the method from LauncherModel.Callbacks.
4194 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004195 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004196 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004197 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4198 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4199 getHotseat().addAllAppsFolder(mIconCache, apps,
4200 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4201 }
4202 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004203 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004204 if (mAppsCustomizeContent != null) {
4205 mAppsCustomizeContent.onPackagesUpdated(
4206 LauncherModel.getSortedWidgetsAndShortcuts(this));
4207 }
Winson Chungc58497e2013-09-03 17:48:37 -07004208 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004209 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004210 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004211 mAppsCustomizeContent.onPackagesUpdated(
4212 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004213 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004214 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004215 }
4216
4217 /**
4218 * A package was updated.
4219 *
4220 * Implementation of the method from LauncherModel.Callbacks.
4221 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004222 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004223 Runnable r = new Runnable() {
4224 public void run() {
4225 bindAppsUpdated(apps);
4226 }
4227 };
4228 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004229 return;
4230 }
4231
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004232 if (mWorkspace != null) {
4233 mWorkspace.updateShortcuts(apps);
4234 }
Winson Chungc58497e2013-09-03 17:48:37 -07004235
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004236 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004237 mAppsCustomizeContent != null) {
4238 mAppsCustomizeContent.updateApps(apps);
4239 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004240 }
4241
4242 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004243 * Update the state of a package, typically related to install state.
4244 *
4245 * Implementation of the method from LauncherModel.Callbacks.
4246 */
4247 public void updatePackageState(String pkgName, int state) {
4248 if (mWorkspace != null) {
4249 mWorkspace.updatePackageState(pkgName, state);
4250 }
4251 }
4252
4253 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004254 * A package was uninstalled. We take both the super set of packageNames
4255 * in addition to specific applications to remove, the reason being that
4256 * this can be called when a package is updated as well. In that scenario,
4257 * we only remove specific components from the workspace, where as
4258 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004259 *
4260 * Implementation of the method from LauncherModel.Callbacks.
4261 */
Winson Chung83892cc2013-05-01 16:53:33 -07004262 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004263 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004264 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004265 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004266 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004267 }
Winson Chungd64d1762013-08-20 14:37:16 -07004268 };
4269 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004270 return;
4271 }
4272
Winson Chungdf95eb12013-10-16 14:57:07 -07004273 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004274 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004275 }
4276 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004277 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004278 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004279
Winson Chunga1820962011-10-03 16:31:06 -07004280 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004281 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004282
Winson Chungdf95eb12013-10-16 14:57:07 -07004283 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004284 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004285 mAppsCustomizeContent != null) {
4286 mAppsCustomizeContent.removeApps(appInfos);
4287 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004288 }
Joe Onoratobe386092009-11-17 17:32:16 -08004289
4290 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004291 * A number of packages were updated.
4292 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004293 private ArrayList<Object> mWidgetsAndShortcuts;
4294 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004295 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004296 bindPackagesUpdated(mWidgetsAndShortcuts);
4297 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004298 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004299 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004300 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4301 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4302 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004303 return;
4304 }
4305
Winson Chung64359a52013-07-08 17:17:08 -07004306 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004307 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004308 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004309 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004310 }
4311
Winson Chung400438b2011-01-16 17:53:48 -08004312 private int mapConfigurationOriActivityInfoOri(int configOri) {
4313 final Display d = getWindowManager().getDefaultDisplay();
4314 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4315 switch (d.getRotation()) {
4316 case Surface.ROTATION_0:
4317 case Surface.ROTATION_180:
4318 // We are currently in the same basic orientation as the natural orientation
4319 naturalOri = configOri;
4320 break;
4321 case Surface.ROTATION_90:
4322 case Surface.ROTATION_270:
4323 // We are currently in the other basic orientation to the natural orientation
4324 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4325 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4326 break;
4327 }
4328
4329 int[] oriMap = {
4330 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4331 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4332 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4333 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4334 };
4335 // Since the map starts at portrait, we need to offset if this device's natural orientation
4336 // is landscape.
4337 int indexOffset = 0;
4338 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4339 indexOffset = 1;
4340 }
4341 return oriMap[(d.getRotation() + indexOffset) % 4];
4342 }
Adam Cohen446e9402011-09-15 18:21:21 -07004343
Winson Chung4b919f82012-05-01 10:44:08 -07004344 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004345 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004346 getResources().getBoolean(R.bool.allow_rotation);
4347 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004348 }
Winson Chung4b919f82012-05-01 10:44:08 -07004349 public void lockScreenOrientation() {
4350 if (isRotationEnabled()) {
4351 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4352 .getConfiguration().orientation));
4353 }
4354 }
4355 public void unlockScreenOrientation(boolean immediate) {
4356 if (isRotationEnabled()) {
4357 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004358 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004359 } else {
4360 mHandler.postDelayed(new Runnable() {
4361 public void run() {
4362 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4363 }
4364 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004365 }
Winson Chung4b919f82012-05-01 10:44:08 -07004366 }
Winson Chung400438b2011-01-16 17:53:48 -08004367 }
4368
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004369 /**
4370 * Called when the SearchBar hint should be changed.
4371 *
4372 * @param hint the hint to be displayed in the search bar.
4373 */
4374 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004375 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004376 }
4377
Winson Chunge43a1e72014-01-15 10:33:02 -08004378 protected boolean isLauncherPreinstalled() {
4379 PackageManager pm = getPackageManager();
4380 try {
4381 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4382 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4383 return true;
4384 } else {
4385 return false;
4386 }
4387 } catch (NameNotFoundException e) {
4388 e.printStackTrace();
4389 return false;
4390 }
4391 }
4392
Winson Chunge6eabff2013-09-24 11:01:23 -07004393 protected String getFirstRunClingSearchBarHint() {
4394 return "";
4395 }
4396 protected String getFirstRunCustomContentHint() {
4397 return "";
4398 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004399 protected int getFirstRunFocusedHotseatAppDrawableId() {
4400 return -1;
4401 }
4402 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4403 return null;
4404 }
4405 protected int getFirstRunFocusedHotseatAppRank() {
4406 return -1;
4407 }
4408 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4409 return "";
4410 }
4411 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4412 return "";
4413 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004414
Winson Chungaf40f202013-09-18 18:26:31 -07004415 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004416 mLauncherClings.dismissFirstRunCling(v);
4417 }
4418 public void dismissMigrationClingCopyApps(View v) {
4419 mLauncherClings.dismissMigrationClingCopyApps(v);
4420 }
4421 public void dismissMigrationClingUseDefault(View v) {
4422 mLauncherClings.dismissMigrationClingUseDefault(v);
4423 }
4424 public void dismissMigrationWorkspaceCling(View v) {
4425 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004426 }
Winson Chung82f55532011-08-09 14:14:23 -07004427 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004428 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004429 }
4430 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004431 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004432 }
4433
Adam Cohen432609a2014-03-13 17:03:22 -07004434
4435 /**
4436 * To be overridden by subclasses to indicate that there is an activity to launch
4437 * before showing the standard launcher experience.
4438 */
4439 protected boolean hasFirstRunActivity() {
4440 return false;
4441 }
4442
4443 /**
4444 * To be overridden by subclasses to launch any first run activity
4445 */
4446 protected Intent getFirstRunActivity() {
4447 return null;
4448 }
4449
Winson Chunga6945242014-01-08 14:04:34 -08004450 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004451 return !ActivityManager.isRunningInTestHarness() &&
4452 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004453 }
4454
Adam Cohen4a9423d2014-03-28 14:22:31 -07004455 protected boolean hasRunFirstRunActivity() {
4456 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4457 }
4458
Adam Cohen432609a2014-03-13 17:03:22 -07004459 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004460 if (shouldRunFirstRunActivity() &&
4461 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004462 Intent firstRunIntent = getFirstRunActivity();
4463 if (firstRunIntent != null) {
4464 startActivity(firstRunIntent);
4465 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004466 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004467 }
4468 }
Adam Cohen432609a2014-03-13 17:03:22 -07004469 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004470 }
4471
4472 private void markFirstRunActivityShown() {
4473 SharedPreferences.Editor editor = mSharedPrefs.edit();
4474 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4475 editor.apply();
4476 }
4477
Adam Cohen432609a2014-03-13 17:03:22 -07004478 /**
4479 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4480 * screen that must be displayed and dismissed.
4481 */
4482 protected boolean hasDismissableIntroScreen() {
4483 return false;
4484 }
4485
4486 /**
4487 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4488 */
4489 protected View getIntroScreen() {
4490 return null;
4491 }
4492
4493 /**
4494 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4495 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4496 */
4497 private boolean shouldShowIntroScreen() {
4498 return hasDismissableIntroScreen() &&
4499 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4500 }
4501
4502 protected void showIntroScreen() {
4503 View introScreen = getIntroScreen();
4504 changeWallpaperVisiblity(false);
4505 if (introScreen != null) {
4506 mDragLayer.showOverlayView(introScreen);
4507 }
4508 }
4509
4510 public void dismissIntroScreen() {
4511 markIntroScreenDismissed();
4512 if (showFirstRunActivity()) {
4513 // We delay hiding the intro view until the first run activity is showing. This
4514 // avoids a blip.
4515 mWorkspace.postDelayed(new Runnable() {
4516 @Override
4517 public void run() {
4518 mDragLayer.dismissOverlayView();
4519 }
4520 }, ACTIVITY_START_DELAY);
4521 } else {
4522 mDragLayer.dismissOverlayView();
4523 }
4524 changeWallpaperVisiblity(true);
4525 }
4526
4527 private void markIntroScreenDismissed() {
4528 SharedPreferences.Editor editor = mSharedPrefs.edit();
4529 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4530 editor.apply();
4531 }
4532
Winson Chunga6945242014-01-08 14:04:34 -08004533 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004534 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4535 if (mHotseat != null) mHotseat.setAlpha(1f);
4536 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4537 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004538 }
4539
4540 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004541 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4542 if (mHotseat != null) mHotseat.setAlpha(0f);
4543 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4544 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004545 }
4546
Mathew Inwood72fbec12013-11-19 15:45:07 +00004547 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004548 // Called from search suggestion, not supported in other profiles.
4549 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4550 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4551 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4552 myUser);
4553 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004554 return null;
4555 }
Kenny Guyed131872014-04-30 03:02:21 +01004556 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004557 }
4558
4559 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4560 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004561 // Called from search suggestion, not supported in other profiles.
4562 return createShortcutDragInfo(shortcutIntent, caption, icon,
4563 UserHandleCompat.myUserHandle());
4564 }
4565
4566 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4567 Bitmap icon, UserHandleCompat user) {
4568 return new ShortcutInfo(shortcutIntent, caption, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004569 }
4570
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004571 protected void moveWorkspaceToDefaultScreen() {
4572 mWorkspace.moveToDefaultScreen(false);
4573 }
4574
Mathew Inwood72fbec12013-11-19 15:45:07 +00004575 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4576 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004577 mWorkspace.onExternalDragStartedWithItem(dragView);
4578 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004579 }
4580
Anjali Koppalf5d29132014-02-28 13:33:27 -08004581 @Override
4582 public void onPageSwitch(View newPage, int newPageIndex) {
4583 }
4584
Winson Chung80baf5a2010-08-09 16:03:15 -07004585 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004586 * Prints out out state for debugging.
4587 */
4588 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004589 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004590 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004591 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4592 Log.d(TAG, "mRestoring=" + mRestoring);
4593 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4594 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004595 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004596 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004597
Winson Chung785d2eb2011-04-14 16:08:02 -07004598 if (mAppsCustomizeContent != null) {
4599 mAppsCustomizeContent.dumpState();
4600 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004601 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004602 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004603
4604 @Override
4605 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4606 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004607 synchronized (sDumpLogs) {
4608 writer.println(" ");
4609 writer.println("Debug logs: ");
4610 for (int i = 0; i < sDumpLogs.size(); i++) {
4611 writer.println(" " + sDumpLogs.get(i));
4612 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004613 }
4614 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004615
4616 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004617 if (DEBUG_DUMP_LOG) {
4618 synchronized (sDumpLogs) {
4619 Log.d(TAG, "");
4620 Log.d(TAG, "*********************");
4621 Log.d(TAG, "Launcher debug logs: ");
4622 for (int i = 0; i < sDumpLogs.size(); i++) {
4623 Log.d(TAG, " " + sDumpLogs.get(i));
4624 }
4625 Log.d(TAG, "*********************");
4626 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004627 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004628 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004629 }
4630
4631 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004632 addDumpLog(tag, log, null, debugLog);
4633 }
4634
4635 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004636 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004637 if (e != null) {
4638 Log.d(tag, log, e);
4639 } else {
4640 Log.d(tag, log);
4641 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004642 }
Winson Chungede41292013-09-19 16:27:36 -07004643 if (DEBUG_DUMP_LOG) {
4644 sDateStamp.setTime(System.currentTimeMillis());
4645 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004646 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4647 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004648 }
Winson Chungede41292013-09-19 16:27:36 -07004649 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004650 }
4651
Winson Chungede41292013-09-19 16:27:36 -07004652 public void dumpLogsToLocalData() {
4653 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004654 new AsyncTask<Void, Void, Void>() {
4655 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004656 boolean success = false;
4657 sDateStamp.setTime(sRunStart);
4658 String FILENAME = sDateStamp.getMonth() + "-"
4659 + sDateStamp.getDay() + "_"
4660 + sDateStamp.getHours() + "-"
4661 + sDateStamp.getMinutes() + "_"
4662 + sDateStamp.getSeconds() + ".txt";
4663
4664 FileOutputStream fos = null;
4665 File outFile = null;
4666 try {
4667 outFile = new File(getFilesDir(), FILENAME);
4668 outFile.createNewFile();
4669 fos = new FileOutputStream(outFile);
4670 } catch (Exception e) {
4671 e.printStackTrace();
4672 }
4673 if (fos != null) {
4674 PrintWriter writer = new PrintWriter(fos);
4675
4676 writer.println(" ");
4677 writer.println("Debug logs: ");
4678 synchronized (sDumpLogs) {
4679 for (int i = 0; i < sDumpLogs.size(); i++) {
4680 writer.println(" " + sDumpLogs.get(i));
4681 }
4682 }
4683 writer.close();
4684 }
4685 try {
4686 if (fos != null) {
4687 fos.close();
4688 success = true;
4689 }
4690 } catch (IOException e) {
4691 e.printStackTrace();
4692 }
Michael Jurka43467462013-11-14 12:03:58 +01004693 return null;
Winson Chungede41292013-09-19 16:27:36 -07004694 }
Michael Jurka43467462013-11-14 12:03:58 +01004695 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004696 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004698}
Michael Jurka2763be32011-02-24 11:19:57 -08004699
Michael Jurkaabded662011-03-04 12:06:57 -08004700interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004701 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004702 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004703 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004704 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004705 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004706}
Dan Sandlerd5024042014-01-09 15:01:33 -05004707
4708interface DebugIntents {
4709 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4710 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004711}