blob: 5f578f7810169779510796c85d812c03d3129ba2 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070023import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070024import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040039import android.content.DialogInterface;
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;
Adam Cohen173f7112015-03-27 15:14:00 -070042import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070043import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080045import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070050import android.graphics.Bitmap;
51import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070052import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070055import android.graphics.drawable.ColorDrawable;
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;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070066import android.os.UserHandle;
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;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.HapticFeedbackConstants;
74import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.MotionEvent;
78import android.view.Surface;
79import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070080import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070083import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070085import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070088import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080092import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
96import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700103import com.android.launcher3.config.ProviderConfig;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700104import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700105import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700106import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700107import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700108import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700109import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700110import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700111import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700112
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700120import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700123import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700124import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000125import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127/**
128 * Default launcher application.
129 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100130public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700131 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700132 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700133 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700134 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Daniel Sandlerf061f822013-06-27 13:59:36 -0400136 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700138 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700140 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700141
Dan Sandlerd5024042014-01-09 15:01:33 -0500142 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurka8b805b12012-04-18 14:23:14 -0700149 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700150 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700151
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700152 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
153 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
154 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
155
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700156 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
157
Mathew Inwood876a8462013-06-14 14:12:41 +0100158 /**
159 * IntentStarter uses request codes starting with this. This must be greater than all activity
160 * request codes used internally.
161 */
162 protected static final int REQUEST_LAST = 100;
163
Michael Jurka0a457bf2012-11-19 14:05:05 -0800164 // To turn on these properties, type
165 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800166 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chung2672ff92012-05-04 16:22:30 -0700168 // The Intent extra that defines whether to ignore the launch animation
169 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400170 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
173 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700174 // Type: int
175 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
178 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700184 // Type: int
185 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
186 // Type: int
187 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
188 // Type: parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000190 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000192 // Type: int[]
193 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Adam Cohen432609a2014-03-13 17:03:22 -0700195 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800196 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
197
Adam Cohen3ed316a2014-07-23 18:21:20 -0700198 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
199 static final String ACTION_FIRST_LOAD_COMPLETE =
200 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
201
Sunny Goyal594d76d2014-11-06 10:12:54 -0800202 private static final String QSB_WIDGET_ID = "qsb_widget_id";
203 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
204
Winson Chunga6945242014-01-08 14:04:34 -0800205 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
206
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700208 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
209
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700213 private boolean mIsSafeModeEnabled;
214
Adam Cohenc2d6e892014-10-16 09:49:24 -0700215 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
216 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700217 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700218
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700220 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
221 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700222 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000224 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
225 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
226
Winson Chunga2413752012-04-03 14:22:34 -0700227 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700228 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
229 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700231
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800232 private final BroadcastReceiver mCloseSystemDialogsReceiver
233 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private LayoutInflater mInflater;
236
Adam Cohen091440a2015-03-18 14:16:05 -0700237 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700238 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800239 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700240 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800241 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700242
243 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700244
Sunny Goyalffe83f12014-08-14 17:39:34 -0700245 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700246 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700247
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700248 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800249 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800250 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700251
Michael Jurka0280c3b2010-09-17 15:00:07 -0700252 private int[] mTmpAddItemCellCoordinates = new int[2];
253
Sunny Goyal316490e2015-06-02 09:38:28 -0700254 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800255 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700256
Michael Jurka838a4ca2011-02-07 13:33:06 -0800257 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700258 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700259
Winson Chungc51db6a2011-10-05 11:44:49 -0700260 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700261
262 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700263 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700264
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700265 // Main container view and the model for the widget tray screen.
266 @Thunk WidgetsContainerView mWidgetsView;
267 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700268
Winson Chungf0ea4d32011-06-06 14:27:16 -0700269 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800270 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700273 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
274 // scroll issues (because the workspace may not have been measured yet) and extra work.
275 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
276 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
278 private SpannableStringBuilder mDefaultKeySsb = null;
279
Adam Cohen091440a2015-03-18 14:16:05 -0700280 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800282 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 private boolean mRestoring;
284 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700285 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286
Michael Jurka1e2f4652013-07-08 18:03:46 -0700287 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700288 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800291 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700292 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800293 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700294 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800295 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296
Sunny Goyale2df0622015-04-24 11:27:00 -0700297 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700298
Adam Cohen61f560d2013-09-30 15:58:20 -0700299 private View.OnTouchListener mHapticFeedbackTouchListener;
300
Adam Cohended9f8d2010-11-03 13:25:16 -0700301 // Related to the auto-advancing of widgets
302 private final int ADVANCE_MSG = 1;
303 private final int mAdvanceInterval = 20000;
304 private final int mAdvanceStagger = 250;
305 private long mAutoAdvanceSentTime;
306 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700307 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700308 new HashMap<View, AppWidgetProviderInfo>();
309
Winson Chung400438b2011-01-16 17:53:48 -0800310 // Determines how long to wait after a rotation before restoring the screen orientation to
311 // match the sensor state.
312 private final int mRestoreScreenOrientationDelay = 500;
313
Adam Cohen091440a2015-03-18 14:16:05 -0700314 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700315
Adam Cohen1462de32012-07-24 22:34:36 -0700316 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700317 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700318
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700319 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700320 static Date sDateStamp = new Date();
321 static DateFormat sDateFormat =
322 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
323 static long sRunStart = System.currentTimeMillis();
324 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700325
Winson Chung46353de2012-02-16 14:05:10 -0800326 // We only want to get the SharedPreferences once since it does an FS stat each time we get
327 // it from the context.
328 private SharedPreferences mSharedPrefs;
329
Winson Chungf0c6ae02012-03-21 16:10:31 -0700330 // Holds the page that we need to animate to, and the icon views that we need to animate up
331 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700332 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700333 private Bitmap mFolderIconBitmap;
334 private Canvas mFolderIconCanvas;
335 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700336
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700337 private DeviceProfile mDeviceProfile;
338
Winson Chung13eb5272015-05-11 16:30:13 -0700339 // This is set to the view that launched the activity that navigated the user away from
340 // launcher. Since there is no callback for when the activity has finished launching, enable
341 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800342 private BubbleTextView mWaitingForResume;
343
Adam Cohen59400422014-03-05 18:07:04 -0800344 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
345 new HashMap<String, CustomAppWidget>();
346
Adam Cohen59400422014-03-05 18:07:04 -0800347 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700348 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
349 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800350 }
351 }
352
Adam Cohen091440a2015-03-18 14:16:05 -0700353 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700354 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
Adam Cohendb364c32014-05-20 14:23:40 -0700361 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362
Adam Cohen091440a2015-03-18 14:16:05 -0700363 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 int requestCode;
365 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700366 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700367 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368 int cellX;
369 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700370 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800371 }
372
Daniel Sandlerff02d492013-08-05 02:12:05 -0400373 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700374 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700375 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400376
377 @Thunk void setOrientation() {
378 if (mRotationEnabled) {
379 unlockScreenOrientation(true);
380 } else {
381 setRequestedOrientation(
382 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700383 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400384 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700385
Sunny Goyal7779d622015-06-11 16:18:39 -0700386 private Runnable mUpdateOrientationRunnable = new Runnable() {
387 public void run() {
388 setOrientation();
389 }
390 };
391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 @Override
393 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700394 if (DEBUG_STRICT_MODE) {
395 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
396 .detectDiskReads()
397 .detectDiskWrites()
398 .detectNetwork() // or .detectAll() for all detectable problems
399 .penaltyLog()
400 .build());
401 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
402 .detectLeakedSqlLiteObjects()
403 .detectLeakedClosableObjects()
404 .penaltyLog()
405 .penaltyDeath()
406 .build());
407 }
408
Adam Cohen9211d422014-10-07 18:14:53 -0700409 if (mLauncherCallbacks != null) {
410 mLauncherCallbacks.preOnCreate();
411 }
412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400414
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400415 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400416 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700417
Adam Cohen2e6da152015-05-06 11:42:25 -0700418 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700419 mDeviceProfile = getResources().getConfiguration().orientation
420 == Configuration.ORIENTATION_LANDSCAPE ?
421 app.getInvariantDeviceProfile().landscapeProfile
422 : app.getInvariantDeviceProfile().portraitProfile;
423
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400424 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700425 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700426 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800427 mModel = app.setLauncher(this);
428 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700429
Joe Onorato41a12d22009-10-31 18:30:00 -0400430 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 mInflater = getLayoutInflater();
Winson Chung006ee262015-08-03 14:40:11 -0700432 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700433
Daniel Sandlerff02d492013-08-05 02:12:05 -0400434 mStats = new Stats(this);
435
Sunny Goyalffe83f12014-08-14 17:39:34 -0700436 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700437
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700438 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
439 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700441 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
442 // this also ensures that any synchronous binding below doesn't re-trigger another
443 // LauncherModel load.
444 mPaused = false;
445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200447 android.os.Debug.startMethodTracing(
448 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 }
450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700454 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455
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) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700466 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 // If the user leaves launcher, then we should just load items asynchronously when
468 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700469 mModel.startLoader(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
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700473 mModel.startLoader(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
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700484 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
485 // In case we are on a device with locked rotation, we should look at preferences to check
486 // if the user has specifically allowed rotation.
487 if (!mRotationEnabled) {
Rahul Chaturvedie863fed2015-06-15 14:09:42 -0400488 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext(), false);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700489 }
490
491 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
492 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400493 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700494
Adam Cohen9211d422014-10-07 18:14:53 -0700495 if (mLauncherCallbacks != null) {
496 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700497 if (mLauncherCallbacks.hasLauncherOverlay()) {
498 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700499 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
500 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
501 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700502 mWorkspace.setLauncherOverlay(mLauncherOverlay);
503 }
Adam Cohen9211d422014-10-07 18:14:53 -0700504 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700505
506 if (shouldShowIntroScreen()) {
507 showIntroScreen();
508 } else {
509 showFirstRunActivity();
510 showFirstRunClings();
511 }
Adam Cohen9211d422014-10-07 18:14:53 -0700512 }
513
Sunny Goyal7779d622015-06-11 16:18:39 -0700514 @Override
515 public void onSettingsChanged(String settings, boolean value) {
516 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
517 mRotationEnabled = value;
518 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
519 mUpdateOrientationRunnable.run();
520 }
521 }
522 }
523
Adam Cohen9211d422014-10-07 18:14:53 -0700524 private LauncherCallbacks mLauncherCallbacks;
525
526 public void onPostCreate(Bundle savedInstanceState) {
527 super.onPostCreate(savedInstanceState);
528 if (mLauncherCallbacks != null) {
529 mLauncherCallbacks.onPostCreate(savedInstanceState);
530 }
531 }
532
533 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
534 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700535 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700536 private boolean mWorkspaceImportanceStored = false;
537 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 private int mWorkspaceImportanceForAccessibility =
539 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
540 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
541
542 @Override
543 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 return;
546 }
547 // The underlying workspace and hotseat are temporarily suppressed by the search
548 // overlay. So they sholudn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700549 if (mWorkspace != null) {
550 mWorkspaceImportanceForAccessibility =
551 mWorkspace.getImportantForAccessibility();
552 mWorkspace.setImportantForAccessibility(
553 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
554 mWorkspaceImportanceStored = true;
555 }
556 if (mHotseat != null) {
557 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
558 mHotseat.setImportantForAccessibility(
559 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
560 mHotseatImportanceStored = true;
561 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700562 }
563
564 @Override
565 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700566 if (mWorkspaceImportanceStored && mWorkspace != null) {
567 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
568 }
569 if (mHotseatImportanceStored && mHotseat != null) {
570 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
571 }
572 mWorkspaceImportanceStored = false;
573 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700574 }
575 });
Adam Cohen9211d422014-10-07 18:14:53 -0700576 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700577 }
578
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700579 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700580 public void onLauncherProviderChange() {
581 if (mLauncherCallbacks != null) {
582 mLauncherCallbacks.onLauncherProviderChange();
583 }
584 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700585
Winson Chungef7f8742015-06-04 17:18:17 -0700586 /**
587 * Updates the bounds of all the overlays to match the new fixed bounds.
588 */
589 public void updateOverlayBounds(Rect newBounds) {
590 mAppsView.setSearchBarBounds(newBounds);
591 mWidgetsView.setSearchBarBounds(newBounds);
592 }
593
Adam Cohen9211d422014-10-07 18:14:53 -0700594 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700595 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700596 if (mLauncherCallbacks != null) {
597 return mLauncherCallbacks.hasCustomContentToLeft();
598 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700599 return false;
600 }
601
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500603 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 * {@link #addToCustomContentPage}. This will only be invoked if
605 * {@link #hasCustomContentToLeft()} is {@code true}.
606 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500607 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700608 if (mLauncherCallbacks != null) {
609 mLauncherCallbacks.populateCustomContentContainer();
610 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 }
612
613 /**
614 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
615 * ensure the custom content page is added or removed if necessary.
616 */
617 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600618 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 // Not bound yet, wait for bindScreens to be called.
620 return;
621 }
622
623 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
624 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500625 mWorkspace.createCustomContentContainer();
626 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600627 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
628 mWorkspace.removeCustomContentPage();
629 }
630 }
631
Anton Hanssona2f665f2013-09-26 12:18:32 +0100632 public Stats getStats() {
633 return mStats;
634 }
635
Bjorn Bringertc459e522013-06-07 19:36:01 +0100636 public LayoutInflater getInflater() {
637 return mInflater;
638 }
639
Hyunyoung Song3f471442015-04-08 19:01:34 -0700640 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700641 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
642 // that is subsequently removed from the workspace in startBinding().
643 return !mModel.isLoadingWorkspace();
644 }
645
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800646 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000647 public static int generateViewId() {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700648 if (Utilities.ATLEAST_JB_MR1) {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100649 return View.generateViewId();
650 } else {
651 // View.generateViewId() is not available. The following fallback logic is a copy
652 // of its implementation.
653 for (;;) {
654 final int result = sNextGeneratedId.get();
655 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
656 int newValue = result + 1;
657 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
658 if (sNextGeneratedId.compareAndSet(result, newValue)) {
659 return result;
660 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000661 }
662 }
663 }
664
665 public int getViewIdForItem(ItemInfo info) {
666 // This cast is safe given the > 2B range for int.
667 int itemId = (int) info.id;
668 if (mItemIdToViewId.containsKey(itemId)) {
669 return mItemIdToViewId.get(itemId);
670 }
671 int viewId = generateViewId();
672 mItemIdToViewId.put(itemId, viewId);
673 return viewId;
674 }
675
676 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700677 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
678 * a configuration step, this allows the proper animations to run after other transitions.
679 */
Adam Cohendb364c32014-05-20 14:23:40 -0700680 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700681 long screenId = args.screenId;
682 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
683 // When the screen id represents an actual screen (as opposed to a rank) we make sure
684 // that the drop page actually exists.
685 screenId = ensurePendingDropLayoutExists(args.screenId);
686 }
Adam Cohendb364c32014-05-20 14:23:40 -0700687
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800689 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700690 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700691 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700692 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700694 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700695 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700696 case REQUEST_RECONFIGURE_APPWIDGET:
697 completeRestoreAppWidget(args.appWidgetId);
698 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800699 }
Michael Jurka27614d22012-04-02 06:40:22 -0700700 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
701 // if you turned the screen off and then back while in All Apps, Launcher would not
702 // return to the workspace. Clearing mAddInfo.container here fixes this issue
703 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700704 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800705 }
706
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800707 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700708 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700709 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700710 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700711 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800712 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700713
Adam Cohenad4e15c2013-10-17 16:21:35 -0700714 Runnable exitSpringLoaded = new Runnable() {
715 @Override
716 public void run() {
717 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
718 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
719 }
720 };
721
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700723 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
726 if (resultCode == RESULT_CANCELED) {
727 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700728 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700729 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700730 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800731 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700732 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700733
734 // When the user has granted permission to bind widgets, we should check to see if
735 // we can inflate the default search bar widget.
736 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 }
738 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200739 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
740 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700741 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200742 }
743 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200745
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700747 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700748
Adam Cohendb364c32014-05-20 14:23:40 -0700749 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 // We have special handling for widgets
751 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800752 final int appWidgetId;
753 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
754 : -1;
755 if (widgetId < 0) {
756 appWidgetId = pendingAddWidgetId;
757 } else {
758 appWidgetId = widgetId;
759 }
760
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800762 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700763 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
764 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 result = RESULT_CANCELED;
766 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700767 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700768 @Override
769 public void run() {
770 exitSpringLoadedDragModeDelayed(false, 0, null);
771 }
772 };
Adam Cohendb364c32014-05-20 14:23:40 -0700773 if (workspaceLocked) {
774 // No need to remove the empty screen if we're mid-binding, as the
775 // the bind will not add the empty screen.
776 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
777 } else {
778 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
779 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
780 }
Winson Chung5aaab772012-04-27 15:24:02 -0700781 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700782 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700783 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
784 // When the screen id represents an actual screen (as opposed to a rank)
785 // we make sure that the drop page actually exists.
786 mPendingAddInfo.screenId =
787 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
788 }
Adam Cohendb364c32014-05-20 14:23:40 -0700789 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
790
791 dropLayout.setDropPending(true);
792 final Runnable onComplete = new Runnable() {
793 @Override
794 public void run() {
795 completeTwoStageWidgetDrop(resultCode, appWidgetId);
796 dropLayout.setDropPending(false);
797 }
798 };
799 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
800 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
801 } else {
802 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
803 mPendingAddInfo);
804 sPendingAddItem = args;
805 }
Winson Chung5aaab772012-04-27 15:24:02 -0700806 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800807 return;
808 }
809
Sunny Goyalff572272014-07-23 13:58:07 -0700810 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
811 if (resultCode == RESULT_OK) {
812 // Update the widget view.
813 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
814 pendingAddWidgetId, mPendingAddInfo);
815 if (workspaceLocked) {
816 sPendingAddItem = args;
817 } else {
818 completeAdd(args);
819 }
820 }
821 // Leave the widget in the pending state if the user canceled the configure.
822 return;
823 }
824
Sunny Goyalaad90582015-07-09 14:22:50 -0700825 if (requestCode == REQUEST_CREATE_SHORTCUT) {
826 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
827 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
828 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
829 mPendingAddInfo);
830 if (isWorkspaceLocked()) {
831 sPendingAddItem = args;
832 } else {
833 completeAdd(args);
834 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
835 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
836 }
837 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700838 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
839 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800842 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800843
844 }
845
846 @Override
847 protected void onActivityResult(
848 final int requestCode, final int resultCode, final Intent data) {
849 handleActivityResult(requestCode, resultCode, data);
850 if (mLauncherCallbacks != null) {
851 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
852 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800853 }
854
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600855 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700856 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600857 int[] grantResults) {
858 if (mLauncherCallbacks != null) {
859 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
860 grantResults);
861 }
862 }
863
Adam Cohendb364c32014-05-20 14:23:40 -0700864 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
865 appWidgetId, ItemInfo info) {
866 PendingAddArguments args = new PendingAddArguments();
867 args.requestCode = requestCode;
868 args.intent = data;
869 args.container = info.container;
870 args.screenId = info.screenId;
871 args.cellX = info.cellX;
872 args.cellY = info.cellY;
873 args.appWidgetId = appWidgetId;
874 return args;
875 }
876
877 /**
878 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
879 *
880 * @param screenId the screen id to check
881 * @return the new screen, or screenId if it exists
882 */
883 private long ensurePendingDropLayoutExists(long screenId) {
884 CellLayout dropLayout =
885 (CellLayout) mWorkspace.getScreenWithId(screenId);
886 if (dropLayout == null) {
887 // it's possible that the add screen was removed because it was
888 // empty and a re-bind occurred
889 mWorkspace.addExtraEmptyScreen();
890 return mWorkspace.commitExtraEmptyScreen();
891 } else {
892 return screenId;
893 }
894 }
895
Adam Cohen091440a2015-03-18 14:16:05 -0700896 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700897 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700898 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800899 Runnable onCompleteRunnable = null;
900 int animationType = 0;
901
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 if (resultCode == RESULT_OK) {
904 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
905 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700906 mPendingAddWidgetInfo);
907 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800908 onCompleteRunnable = new Runnable() {
909 @Override
910 public void run() {
911 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700912 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700913 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
914 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800915 }
916 };
917 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800918 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700921 if (mDragLayer.getAnimatedView() != null) {
922 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
923 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
924 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700925 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700926 // The animated view may be null in the case of a rotation during widget configuration
927 onCompleteRunnable.run();
928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 }
930
931 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700932 protected void onStop() {
933 super.onStop();
934 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700935
936 if (mLauncherCallbacks != null) {
937 mLauncherCallbacks.onStop();
938 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700939 }
940
941 @Override
942 protected void onStart() {
943 super.onStart();
944 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700945
946 if (mLauncherCallbacks != null) {
947 mLauncherCallbacks.onStart();
948 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700949 }
950
951 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200953 long startTime = 0;
954 if (DEBUG_RESUME_TIME) {
955 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400956 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200957 }
Adam Cohen9211d422014-10-07 18:14:53 -0700958
959 if (mLauncherCallbacks != null) {
960 mLauncherCallbacks.preOnResume();
961 }
962
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700964
Winson Chung4a2afa32012-07-19 14:53:05 -0700965 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700966 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700967 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800968 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700969 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700970 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700971 // view after launching an app, as they may be depending on the UI to be static to
972 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700973 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700974 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800975 } else if (mOnResumeState == State.WIDGETS) {
976 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700977 }
978 mOnResumeState = State.NONE;
979
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700980 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700981 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
982 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700983
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800984 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700985 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700986 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700987 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700989 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700991 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200992 // We might have postponed some bind calls until onResume (see waitUntilResume) --
993 // execute them here
994 long startTimeCallbacks = 0;
995 if (DEBUG_RESUME_TIME) {
996 startTimeCallbacks = System.currentTimeMillis();
997 }
998
Michael Jurka1e2f4652013-07-08 18:03:46 -0700999 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1000 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001001 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001002 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001003 if (DEBUG_RESUME_TIME) {
1004 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1005 (System.currentTimeMillis() - startTimeCallbacks));
1006 }
Michael Jurka447bf842013-05-15 14:52:15 +02001007 }
Michael Jurka54554252013-08-01 12:52:23 +02001008 if (mOnResumeCallbacks.size() > 0) {
1009 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1010 mOnResumeCallbacks.get(i).run();
1011 }
1012 mOnResumeCallbacks.clear();
1013 }
Winson Chunge4e50662012-01-23 14:45:13 -08001014
1015 // Reset the pressed state of icons that were locked in the press state while activities
1016 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001017 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001018 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001019 mWaitingForResume.setStayPressed(false);
1020 }
Winson Chung82963d52013-10-09 11:20:57 -07001021
Adam Cohen06dff352012-06-01 17:17:08 -07001022 // It is possible that widgets can receive updates while launcher is not in the foreground.
1023 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1024 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1025 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001026 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001027 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001028
Michael Jurka447bf842013-05-15 14:52:15 +02001029 if (DEBUG_RESUME_TIME) {
1030 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1031 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001032
1033 if (mWorkspace.getCustomContentCallbacks() != null) {
1034 // If we are resuming and the custom content is the current page, we call onShow().
1035 // It is also poassible that onShow will instead be called slightly after first layout
1036 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1037 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001038 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001039 }
1040 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001041 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001042 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001043
Sunny Goyal756adbc2015-04-16 15:20:51 -07001044 if (!isWorkspaceLoading()) {
1045 // Process any items that were added while Launcher was away.
1046 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1047 }
Adam Cohen9211d422014-10-07 18:14:53 -07001048
1049 if (mLauncherCallbacks != null) {
1050 mLauncherCallbacks.onResume();
1051 }
Adam Cohen06dff352012-06-01 17:17:08 -07001052 }
1053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001056 // Ensure that items added to Launcher are queued until Launcher returns
1057 InstallShortcutReceiver.enableInstallQueue();
1058
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001060 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001061 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001062 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001063
1064 // We call onHide() aggressively. The custom content callbacks should be able to
1065 // debounce excess onHide calls.
1066 if (mWorkspace.getCustomContentCallbacks() != null) {
1067 mWorkspace.getCustomContentCallbacks().onHide();
1068 }
Romain Guycbb89e42009-06-08 15:52:54 -07001069
Adam Cohen9211d422014-10-07 18:14:53 -07001070 if (mLauncherCallbacks != null) {
1071 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001072 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001073 }
1074
1075 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001076 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1077 // by a onResume or by scrolling otherwise.
1078 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001079
1080 // Custom content is completely hidden
1081 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001082
1083 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1084 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001085
1086 // Indicates whether the user is allowed to scroll away from the custom content.
1087 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001088 }
1089
Adam Cohenc2d6e892014-10-16 09:49:24 -07001090 public interface LauncherOverlay {
1091
1092 /**
1093 * Touch interaction leading to overscroll has begun
1094 */
1095 public void onScrollInteractionBegin();
1096
1097 /**
1098 * Touch interaction related to overscroll has ended
1099 */
1100 public void onScrollInteractionEnd();
1101
1102 /**
1103 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1104 * screen (or in the case of RTL, the rightmost screen).
1105 */
1106 public void onScrollChange(int progress, boolean rtl);
1107
1108 /**
1109 * Screen has stopped scrolling
1110 */
1111 public void onScrollSettled();
1112
1113 /**
1114 * This method can be called by the Launcher in order to force the LauncherOverlay
1115 * to exit fully immersive mode.
1116 */
1117 public void forceExitFullImmersion();
1118 }
1119
Jun Mukai8af0cd82015-05-12 12:32:12 -07001120 public interface LauncherSearchCallbacks {
1121 /**
1122 * Called when the search overlay is shown.
1123 */
1124 public void onSearchOverlayOpened();
1125
1126 /**
1127 * Called when the search overlay is dismissed.
1128 */
1129 public void onSearchOverlayClosed();
1130 }
1131
Adam Cohenc2d6e892014-10-16 09:49:24 -07001132 public interface LauncherOverlayCallbacks {
1133 /**
1134 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1135 * however it doesn't modify any state within the launcher.
1136 */
1137 public boolean canEnterFullImmersion();
1138
1139 /**
1140 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1141 * eg. by occupying the full screen and handling all touch events.
1142 *
1143 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1144 * case, Launcher will modify any necessary state and assumes the overlay is
1145 * handling all interaction. If false, the LauncherOverlay should cancel any
1146 *
1147 */
1148 public boolean enterFullImmersion();
1149
1150 /**
1151 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1152 * full control over UI and state.
1153 */
1154 public void exitFullImmersion();
1155 }
1156
1157 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1158
1159 @Override
1160 public boolean canEnterFullImmersion() {
1161 return mState == State.WORKSPACE;
1162 }
1163
1164 @Override
1165 public boolean enterFullImmersion() {
1166 if (mState == State.WORKSPACE) {
1167 // When fully immersed, disregard any touches which fall through.
1168 mDragLayer.setBlockTouch(true);
1169 return true;
1170 }
1171 return false;
1172 }
1173
1174 @Override
1175 public void exitFullImmersion() {
1176 mDragLayer.setBlockTouch(false);
1177 }
1178 }
1179
Jorim Jaggid017f882014-01-14 17:08:48 -08001180 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001181 if (mLauncherCallbacks != null) {
1182 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001183 } else {
1184 // On devices with a locked orientation, we will at least have the allow rotation
1185 // setting.
1186 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001187 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001188 }
1189
Adam Cohen9211d422014-10-07 18:14:53 -07001190 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001191 CustomContentCallbacks callbacks, String description) {
1192 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001193 }
1194
Adam Cohenedb40762013-07-18 16:45:45 -07001195 // The custom content needs to offset its content to account for the QSB
1196 public int getTopOffsetForCustomContent() {
1197 return mWorkspace.getPaddingTop();
1198 }
1199
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001200 @Override
1201 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001203 if (mModel.isCurrentCallbacks(this)) {
1204 mModel.stopLoader();
1205 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001206 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1207
Romain Guy13c2e7b2010-03-10 19:45:00 -08001208 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001209 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001210
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 @Override
1213 public void onWindowFocusChanged(boolean hasFocus) {
1214 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001215 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001216
1217 if (mLauncherCallbacks != null) {
1218 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1219 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 private boolean acceptFilter() {
1223 final InputMethodManager inputManager = (InputMethodManager)
1224 getSystemService(Context.INPUT_METHOD_SERVICE);
1225 return !inputManager.isFullscreenMode();
1226 }
1227
1228 @Override
1229 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001230 final int uniChar = event.getUnicodeChar();
1231 final boolean handled = super.onKeyDown(keyCode, event);
1232 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1233 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1235 keyCode, event);
1236 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001238 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001239 // showSearchDialog()
1240 // If there are multiple keystrokes before the search dialog takes focus,
1241 // onSearchRequested() will be called for every keystroke,
1242 // but it is idempotent, so it's fine.
1243 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245 }
1246
Joe Onorato8a9625e2010-01-28 15:55:35 -08001247 // Eat the long press event so the keyboard doesn't come up.
1248 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1249 return true;
1250 }
1251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 return handled;
1253 }
1254
Karl Rosaen138a0412009-04-23 19:00:21 -07001255 private String getTypedText() {
1256 return mDefaultKeySsb.toString();
1257 }
1258
1259 private void clearTypedText() {
1260 mDefaultKeySsb.clear();
1261 mDefaultKeySsb.clearSpans();
1262 Selection.setSelection(mDefaultKeySsb, 0);
1263 }
1264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001266 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1267 * State
1268 */
1269 private static State intToState(int stateOrdinal) {
1270 State state = State.WORKSPACE;
1271 final State[] stateValues = State.values();
1272 for (int i = 0; i < stateValues.length; i++) {
1273 if (stateValues[i].ordinal() == stateOrdinal) {
1274 state = stateValues[i];
1275 break;
1276 }
1277 }
1278 return state;
1279 }
1280
1281 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 * Restores the previous state, if it exists.
1283 *
1284 * @param savedState The previous state.
1285 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001286 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 private void restoreState(Bundle savedState) {
1288 if (savedState == null) {
1289 return;
1290 }
1291
Michael Jurka883f55b2010-10-21 15:47:14 -07001292 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001293 if (state == State.APPS || state == State.WIDGETS) {
1294 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001295 }
1296
Adam Cohen21cd0022013-10-09 18:57:02 -07001297 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1298 PagedView.INVALID_RESTORE_PAGE);
1299 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001300 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
Winson Chung3d503fb2011-07-13 17:25:49 -07001303 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001304 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001305
Winson Chung3d503fb2011-07-13 17:25:49 -07001306 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1307 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001308 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001309 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1310 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001311 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1312 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001313 AppWidgetProviderInfo info = savedState.getParcelable(
1314 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001315 mPendingAddWidgetInfo = info == null ?
1316 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1317
Adam Cohen4637b5a2013-11-04 18:21:24 -08001318 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001319 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 mRestoring = true;
1321 }
1322
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001323 mItemIdToViewId = (HashMap<Integer, Integer>)
1324 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326
1327 /**
1328 * Finds all the views we need and configure them properly.
1329 */
1330 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001331 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001332
Craig Mautner360310b2012-10-26 15:13:08 -07001333 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001334 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001335 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1336 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001337 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001338 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001339
John Spurlock77e1f472013-09-11 10:09:51 -04001340 mLauncherView.setSystemUiVisibility(
1341 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001342 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343
Winson Chung4c98d922011-05-31 16:50:48 -07001344 // Setup the drag layer
1345 mDragLayer.setup(this, dragController);
1346
Winson Chung3d503fb2011-07-13 17:25:49 -07001347 // Setup the hotseat
1348 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1349 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001350 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001351 }
1352
Jorim Jaggid017f882014-01-14 17:08:48 -08001353 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001354 mWidgetsButton = findViewById(R.id.widget_button);
1355 mWidgetsButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001356 @Override
1357 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001358 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001359 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001360 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001361 }
1362 });
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001363 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen61f560d2013-09-30 15:58:20 -07001364
1365 View wallpaperButton = findViewById(R.id.wallpaper_button);
1366 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001367 @Override
1368 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001369 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001370 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001371 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001372 }
1373 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001374 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1375
1376 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001377 if (hasSettings()) {
1378 settingsButton.setOnClickListener(new OnClickListener() {
1379 @Override
1380 public void onClick(View arg0) {
1381 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001382 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001383 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001384 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001385 });
1386 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1387 } else {
1388 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001389 }
1390
Adam Cohendbdff6b2013-09-18 19:09:15 -07001391 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001392
Winson Chung4c98d922011-05-31 16:50:48 -07001393 // Setup the workspace
1394 mWorkspace.setHapticFeedbackEnabled(false);
1395 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001396 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001397 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001398
Winson Chungf0ea4d32011-06-06 14:27:16 -07001399 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001400 mSearchDropTargetBar = (SearchDropTargetBar)
1401 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001402
Winson Chungef7f8742015-06-04 17:18:17 -07001403 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001404 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001405 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001406 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1407 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1408 } else {
1409 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1410 }
Craig Mautner360310b2012-10-26 15:13:08 -07001411
Winson Chung3d503fb2011-07-13 17:25:49 -07001412 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001413 dragController.setDragScoller(mWorkspace);
1414 dragController.setScrollView(mDragLayer);
1415 dragController.setMoveTarget(mWorkspace);
1416 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001417 if (mSearchDropTargetBar != null) {
1418 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001419 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001420 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001421
Sunny Goyal322d5562015-06-25 19:35:49 -07001422 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1423 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426
1427 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001428 * Sets the all apps button. This method is called from {@link Hotseat}.
1429 */
1430 public void setAllAppsButton(View allAppsButton) {
1431 mAllAppsButton = allAppsButton;
1432 }
1433
1434 public View getAllAppsButton() {
1435 return mAllAppsButton;
1436 }
1437
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001438 public View getWidgetsButton() {
1439 return mWidgetsButton;
1440 }
1441
Anjali Koppal5ad44842014-03-10 20:34:39 -07001442 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 * Creates a view representing a shortcut.
1444 *
1445 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001447 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001448 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 }
1450
1451 /**
1452 * Creates a view representing a shortcut inflated from the specified resource.
1453 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 * @param parent The group the shortcut belongs to.
1455 * @param info The data structure describing the shortcut.
1456 *
1457 * @return A View inflated from layoutResId.
1458 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001459 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001460 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001461 parent, false);
1462 favorite.applyFromShortcutInfo(info, mIconCache);
1463 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001465 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 return favorite;
1467 }
1468
1469 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 * Add a shortcut to the workspace.
1471 *
1472 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001474 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001475 int cellY) {
1476 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001477 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001478
Sunny Goyal5c97f512015-05-19 16:03:28 -07001479 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001480 if (info == null) {
1481 return;
1482 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001483 final View view = createShortcut(info);
1484
Sunny Goyal5c97f512015-05-19 16:03:28 -07001485 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001486 // First we check if we already know the exact location where we want to add this item.
1487 if (cellX >= 0 && cellY >= 0) {
1488 cellXY[0] = cellX;
1489 cellXY[1] = cellY;
1490 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001491
1492 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001493 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001494 true, null,null)) {
1495 return;
1496 }
1497 DragObject dragObject = new DragObject();
1498 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001499 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1500 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001501 return;
1502 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001503 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001504 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001505 }
1506
1507 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001508 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001509 return;
1510 }
1511
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001512 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513
1514 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001515 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001516 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 }
1518 }
1519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001521 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001523 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 */
Adam Cohen091440a2015-03-18 14:16:05 -07001525 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001526 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1527
1528 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001529 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001530 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1531 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001532 }
Romain Guycbb89e42009-06-08 15:52:54 -07001533
Adam Cohen59400422014-03-05 18:07:04 -08001534 if (appWidgetInfo.isCustomWidget) {
1535 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001536 }
1537
Adam Cohen59400422014-03-05 18:07:04 -08001538 LauncherAppWidgetInfo launcherInfo;
1539 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1540 launcherInfo.spanX = info.spanX;
1541 launcherInfo.spanY = info.spanY;
1542 launcherInfo.minSpanX = info.minSpanX;
1543 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001544 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001545
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001547 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548
1549 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001550 if (hostView == null) {
1551 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001552 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1553 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001554 } else {
1555 // The AppWidgetHostView has already been inflated and instantiated
1556 launcherInfo.hostView = hostView;
1557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001559 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001560 launcherInfo.notifyWidgetSizeChanged(this);
1561
Adam Cohen59400422014-03-05 18:07:04 -08001562 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1563 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001564
1565 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001567 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
Romain Guycbb89e42009-06-08 15:52:54 -07001569
Adam Cohended9f8d2010-11-03 13:25:16 -07001570 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1571 @Override
1572 public void onReceive(Context context, Intent intent) {
1573 final String action = intent.getAction();
1574 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1575 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001576 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001577 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001578
Winson Chungc100e8e2011-08-09 16:02:43 -07001579 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001580 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001581 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001582 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001583 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001584 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001585 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1586 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001587 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001588 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1589 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001590 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001591 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1592 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1593 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001594 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001595 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1596 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001597 }
1598 }
1599 };
1600
1601 @Override
1602 public void onAttachedToWindow() {
1603 super.onAttachedToWindow();
1604
1605 // Listen for broadcasts related to user-presence
1606 final IntentFilter filter = new IntentFilter();
1607 filter.addAction(Intent.ACTION_SCREEN_OFF);
1608 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001609 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001610 if (ENABLE_DEBUG_INTENTS) {
1611 filter.addAction(DebugIntents.DELETE_DATABASE);
1612 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1613 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001615 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001616 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001617 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001618 mVisible = true;
1619 }
1620
Adam Cohen0b395352014-06-09 22:54:36 +00001621 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001622 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001623 * This method is a no-op for other platform versions.
1624 */
Sunny Goyald0091012015-01-20 15:55:34 -08001625 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001626 private void setupTransparentSystemBarsForLollipop() {
1627 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001628 Window window = getWindow();
1629 window.getAttributes().systemUiVisibility |=
1630 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1631 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1632 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1633 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1634 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1635 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1636 window.setStatusBarColor(Color.TRANSPARENT);
1637 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001638 }
1639 }
1640
Adam Cohended9f8d2010-11-03 13:25:16 -07001641 @Override
1642 public void onDetachedFromWindow() {
1643 super.onDetachedFromWindow();
1644 mVisible = false;
1645
Adam Cohend113e0c2010-11-11 10:48:05 -08001646 if (mAttached) {
1647 unregisterReceiver(mReceiver);
1648 mAttached = false;
1649 }
Winson Chungb745afb2015-03-02 11:51:23 -08001650 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 }
1652
1653 public void onWindowVisibilityChanged(int visibility) {
1654 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001655 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001656 // The following code used to be in onResume, but it turns out onResume is called when
1657 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1658 // is a more appropriate event to handle
1659 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001660 if (!mWorkspaceLoading) {
1661 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001662 // We want to let Launcher draw itself at least once before we force it to build
1663 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001664 // apps is nice and speedy.
1665 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001666 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001667 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001668 if (mStarted) return;
1669 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001670 // We delay the layer building a bit in order to give
1671 // other message processing a time to run. In particular
1672 // this avoids a delay in hiding the IME if it was
1673 // currently shown, because doing that may involve
1674 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001675 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001676 final ViewTreeObserver.OnDrawListener listener = this;
1677 mWorkspace.post(new Runnable() {
1678 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001679 if (mWorkspace != null &&
1680 mWorkspace.getViewTreeObserver() != null) {
1681 mWorkspace.getViewTreeObserver().
1682 removeOnDrawListener(listener);
1683 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001684 }
1685 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001686 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001687 }
1688 });
1689 }
1690 clearTypedText();
1691 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001692 }
1693
Adam Cohen091440a2015-03-18 14:16:05 -07001694 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001695 mHandler.removeMessages(ADVANCE_MSG);
1696 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1697 mHandler.sendMessageDelayed(msg, delay);
1698 mAutoAdvanceSentTime = System.currentTimeMillis();
1699 }
1700
Adam Cohen091440a2015-03-18 14:16:05 -07001701 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1703 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1704 mAutoAdvanceRunning = autoAdvanceRunning;
1705 if (autoAdvanceRunning) {
1706 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1707 sendAdvanceMessage(delay);
1708 } else {
1709 if (!mWidgetsToAdvance.isEmpty()) {
1710 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1711 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1712 }
1713 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001714 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 }
1716 }
1717 }
1718
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001719 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1720
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001722 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 if (msg.what == ADVANCE_MSG) {
1724 int i = 0;
1725 for (View key: mWidgetsToAdvance.keySet()) {
1726 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1727 final int delay = mAdvanceStagger * i;
1728 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001729 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 public void run() {
1731 ((Advanceable) v).advance();
1732 }
1733 }, delay);
1734 }
1735 i++;
1736 }
1737 sendAdvanceMessage(mAdvanceInterval);
1738 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001739 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001741 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001742
1743 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001744 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1746 if (v instanceof Advanceable) {
1747 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001748 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001749 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 }
1751 }
1752
1753 void removeWidgetToAutoAdvance(View hostView) {
1754 if (mWidgetsToAdvance.containsKey(hostView)) {
1755 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001756 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001758 }
1759
Joe Onorato9c1289c2009-08-17 11:03:03 -04001760 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762 launcherInfo.hostView = null;
1763 }
1764
Hyunyoung Song3f471442015-04-08 19:01:34 -07001765 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001766 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1767 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
1769
Winson Chunga6945242014-01-08 14:04:34 -08001770 public DragLayer getDragLayer() {
1771 return mDragLayer;
1772 }
1773
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001774 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001775 return mAppsView;
1776 }
1777
Hyunyoung Song3f471442015-04-08 19:01:34 -07001778 public WidgetsContainerView getWidgetsView() {
1779 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001780 }
1781
Winson Chunga6945242014-01-08 14:04:34 -08001782 public Workspace getWorkspace() {
1783 return mWorkspace;
1784 }
1785
1786 public Hotseat getHotseat() {
1787 return mHotseat;
1788 }
1789
Jorim Jaggid017f882014-01-14 17:08:48 -08001790 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001791 return mOverviewPanel;
1792 }
1793
Winson Chung006ee262015-08-03 14:40:11 -07001794 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001795 return mSearchDropTargetBar;
1796 }
1797
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001798 public LauncherAppWidgetHost getAppWidgetHost() {
1799 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
Romain Guycbb89e42009-06-08 15:52:54 -07001801
Winson Chunga9abd0e2010-10-27 17:18:37 -07001802 public LauncherModel getModel() {
1803 return mModel;
1804 }
1805
Winson Chunga6945242014-01-08 14:04:34 -08001806 protected SharedPreferences getSharedPrefs() {
1807 return mSharedPrefs;
1808 }
1809
Adam Cohen2e6da152015-05-06 11:42:25 -07001810 public DeviceProfile getDeviceProfile() {
1811 return mDeviceProfile;
1812 }
1813
Bjorn Bringertc459e522013-06-07 19:36:01 +01001814 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001815 getWindow().closeAllPanels();
1816
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001817 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001818 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 @Override
1822 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001823 long startTime = 0;
1824 if (DEBUG_RESUME_TIME) {
1825 startTime = System.currentTimeMillis();
1826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 super.onNewIntent(intent);
1828
1829 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001830 Folder openFolder = mWorkspace.getOpenFolder();
1831 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1832 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1833 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1834 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1835 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001836 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001837 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001838
Adam Cohen6fecd412013-10-02 17:41:50 -07001839 if (mWorkspace == null) {
1840 // Can be cases where mWorkspace is null, this prevents a NPE
1841 return;
1842 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001843 // In all these cases, only animate if we're already on home
1844 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001845
Adam Cohen6fecd412013-10-02 17:41:50 -07001846 closeFolder();
1847 exitSpringLoadedDragMode();
1848
1849 // If we are already on home, then just animate back to the workspace,
1850 // otherwise, just wait until onResume to set the state back to Workspace
1851 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001852 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001853 } else {
1854 mOnResumeState = State.WORKSPACE;
1855 }
1856
1857 final View v = getWindow().peekDecorView();
1858 if (v != null && v.getWindowToken() != null) {
1859 InputMethodManager imm = (InputMethodManager)getSystemService(
1860 INPUT_METHOD_SERVICE);
1861 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1862 }
1863
Winson Chungb745afb2015-03-02 11:51:23 -08001864 // Reset the apps view
1865 if (!alreadyOnHome && mAppsView != null) {
1866 mAppsView.scrollToTop();
1867 }
1868
Hyunyoung Song3f471442015-04-08 19:01:34 -07001869 // Reset the widgets view
1870 if (!alreadyOnHome && mWidgetsView != null) {
1871 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001873
Adam Cohen9211d422014-10-07 18:14:53 -07001874 if (mLauncherCallbacks != null) {
1875 mLauncherCallbacks.onHomeIntent();
1876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 }
Adam Cohened307df2013-10-02 09:37:31 -07001878
Adam Cohen9211d422014-10-07 18:14:53 -07001879 if (mLauncherCallbacks != null) {
1880 mLauncherCallbacks.onNewIntent(intent);
1881 }
Winson15f8b172015-08-19 11:02:39 -07001882
1883 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1884 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1885 // animation.
1886 if (isActionMain) {
1887 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1888 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
1889 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1890 openFolder == null && moveToDefaultScreen) {
1891 mWorkspace.post(new Runnable() {
1892 @Override
1893 public void run() {
1894 mWorkspace.moveToDefaultScreen(true);
1895 }
1896 });
1897 }
1898 }
1899
1900 if (DEBUG_RESUME_TIME) {
1901 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1902 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001903 }
1904
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001906 public void onRestoreInstanceState(Bundle state) {
1907 super.onRestoreInstanceState(state);
1908 for (int page: mSynchronouslyBoundPages) {
1909 mWorkspace.restoreInstanceStateForChild(page);
1910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
1912
1913 @Override
1914 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001915 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001916 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1917 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001918 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001919 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920
Michael Jurka883f55b2010-10-21 15:47:14 -07001921 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001922 // We close any open folder since it will not be re-opened, and we need to make sure
1923 // this state is reflected.
1924 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925
Adam Cohendcd297f2013-06-18 13:13:40 -07001926 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001927 mWaitingForResult) {
1928 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001929 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001930 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1931 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001932 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1933 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1934 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001935 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
Hyunyoung Song3f471442015-04-08 19:01:34 -07001938 // Save the current widgets tray?
1939 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001940 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001941
1942 if (mLauncherCallbacks != null) {
1943 mLauncherCallbacks.onSaveInstanceState(outState);
1944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946
1947 @Override
1948 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001950
Winson Chunge7a03942011-08-05 15:05:12 -07001951 // Remove all pending runnables
1952 mHandler.removeMessages(ADVANCE_MSG);
1953 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001954 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001955
Winson Chungcd2b0142011-06-08 16:02:26 -07001956 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001957 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001958
1959 // It's possible to receive onDestroy after a new Launcher activity has
1960 // been created. In this case, don't interfere with the new Launcher.
1961 if (mModel.isCurrentCallbacks(this)) {
1962 mModel.stopLoader();
1963 app.setLauncher(null);
1964 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001967 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001969 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001971 mAppWidgetHost = null;
1972
1973 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974
1975 TextKeyListener.getInstance().release();
1976
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001977 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001978
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001979 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001980 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001981 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001982 mWorkspace = null;
1983 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001984
Michael Jurka2ecf9952012-06-18 12:52:28 -07001985 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001986
1987 if (mLauncherCallbacks != null) {
1988 mLauncherCallbacks.onDestroy();
1989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
1991
Adam Cohena9cf38f2011-05-02 15:36:58 -07001992 public DragController getDragController() {
1993 return mDragController;
1994 }
1995
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 @Override
1997 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001998 onStartForResult(requestCode);
1999 super.startActivityForResult(intent, requestCode);
2000 }
2001
2002 @Override
2003 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2004 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2005 onStartForResult(requestCode);
2006 try {
2007 super.startIntentSenderForResult(intent, requestCode,
2008 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2009 } catch (IntentSender.SendIntentException e) {
2010 throw new ActivityNotFoundException();
2011 }
2012 }
2013
2014 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002015 if (requestCode >= 0) {
2016 setWaitingForResult(true);
2017 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 }
2019
Winson Chung70d72102011-08-12 11:24:35 -07002020 /**
2021 * Indicates that we want global search for this activity by setting the globalSearch
2022 * argument for {@link #startSearch} to true.
2023 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002025 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002027
Karl Rosaen138a0412009-04-23 19:00:21 -07002028 if (initialQuery == null) {
2029 // Use any text typed in the launcher as the initial query
2030 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 if (appSearchData == null) {
2033 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002034 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
Winson Chungadf0c182012-08-23 14:59:07 -07002036 Rect sourceBounds = new Rect();
2037 if (mSearchDropTargetBar != null) {
2038 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2039 }
Romain Guycbb89e42009-06-08 15:52:54 -07002040
Ian Parkinson047036e2014-09-01 15:40:53 +01002041 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002042 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002043 if (clearTextImmediately) {
2044 clearTypedText();
2045 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002046
2047 // We need to show the workspace after starting the search
2048 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002049 }
2050
Ian Parkinson047036e2014-09-01 15:40:53 +01002051 /**
2052 * Start a text search.
2053 *
2054 * @return {@code true} if the search will start immediately, so any further keypresses
2055 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2056 * to buffer keypresses.
2057 */
2058 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002059 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002060 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2061 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2062 sourceBounds);
2063 }
2064
Michael Jurkaa33411c2012-06-14 16:18:21 -07002065 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002066 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002067 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002068 }
2069
2070 /**
2071 * Starts the global search activity. This code is a copied from SearchManager
2072 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002073 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002074 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002075 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002076 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2077 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2078 if (globalSearchActivity == null) {
2079 Log.w(TAG, "No global search activity found.");
2080 return;
2081 }
2082 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2083 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2084 intent.setComponent(globalSearchActivity);
2085 // Make sure that we have a Bundle to put source in
2086 if (appSearchData == null) {
2087 appSearchData = new Bundle();
2088 } else {
2089 appSearchData = new Bundle(appSearchData);
2090 }
Adam Cohen9211d422014-10-07 18:14:53 -07002091 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 if (!appSearchData.containsKey("source")) {
2093 appSearchData.putString("source", getPackageName());
2094 }
2095 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2096 if (!TextUtils.isEmpty(initialQuery)) {
2097 intent.putExtra(SearchManager.QUERY, initialQuery);
2098 }
2099 if (selectInitialQuery) {
2100 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2101 }
2102 intent.setSourceBounds(sourceBounds);
2103 try {
2104 startActivity(intent);
2105 } catch (ActivityNotFoundException ex) {
2106 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2107 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 }
2109
Winson Chungbedf9232015-07-10 12:38:30 -07002110 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2111 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2112 return;
2113 }
2114
2115 // If not handled, then just start the provided search intent
2116 startActivitySafely(v, searchIntent, null);
2117 }
2118
Yura4f93ec62014-02-04 14:15:21 +00002119 public boolean isOnCustomContent() {
2120 return mWorkspace.isOnOrMovingToCustomContent();
2121 }
2122
Winson Chung70d72102011-08-12 11:24:35 -07002123 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002124 public boolean onPrepareOptionsMenu(Menu menu) {
2125 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002126 if (!isOnCustomContent()) {
2127 // Close any open folders
2128 closeFolder();
2129 // Stop resizing any widgets
2130 mWorkspace.exitWidgetResizeMode();
2131 if (!mWorkspace.isInOverviewMode()) {
2132 // Show the overview mode
2133 showOverviewMode(true);
2134 } else {
2135 showWorkspace(true);
2136 }
Winson Chunge0298742014-01-17 12:03:00 -08002137 }
Adam Cohen9211d422014-10-07 18:14:53 -07002138 if (mLauncherCallbacks != null) {
2139 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2140 }
2141
Michael Jurkab94f3f82013-09-11 18:08:54 +02002142 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002145 @Override
2146 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002147 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002148 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002149 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002150 }
2151
Joe Onorato9c1289c2009-08-17 11:03:03 -04002152 public boolean isWorkspaceLocked() {
2153 return mWorkspaceLoading || mWaitingForResult;
2154 }
2155
Adam Cohen517a7f52014-03-01 12:12:59 -08002156 public boolean isWorkspaceLoading() {
2157 return mWorkspaceLoading;
2158 }
2159
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002160 private void setWorkspaceLoading(boolean value) {
2161 boolean isLocked = isWorkspaceLocked();
2162 mWorkspaceLoading = value;
2163 if (isLocked != isWorkspaceLocked()) {
2164 onWorkspaceLockedChanged();
2165 }
2166 }
2167
2168 private void setWaitingForResult(boolean value) {
2169 boolean isLocked = isWorkspaceLocked();
2170 mWaitingForResult = value;
2171 if (isLocked != isWorkspaceLocked()) {
2172 onWorkspaceLockedChanged();
2173 }
2174 }
2175
Adam Cohen9211d422014-10-07 18:14:53 -07002176 protected void onWorkspaceLockedChanged() {
2177 if (mLauncherCallbacks != null) {
2178 mLauncherCallbacks.onWorkspaceLockedChanged();
2179 }
2180 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002181
Michael Jurka0280c3b2010-09-17 15:00:07 -07002182 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002183 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002184 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002185 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2186 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002187 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002188 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002189
Sunny Goyale6b63a32015-01-16 16:14:29 -08002190 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002191 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002192 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2193 }
2194
Sunny Goyale6b63a32015-01-16 16:14:29 -08002195 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002196 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2197 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002198 if (appWidgetInfo.configure != null) {
2199 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002200 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002201
Bjorn Bringert7984c942009-12-09 15:38:25 +00002202 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002203 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2204 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002207 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002208 Runnable onComplete = new Runnable() {
2209 @Override
2210 public void run() {
2211 // Exit spring loaded mode if necessary after adding the widget
2212 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2213 null);
2214 }
2215 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002217 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002218 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 }
2220 }
Romain Guycbb89e42009-06-08 15:52:54 -07002221
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002222 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002223 // Close any folders that may be open.
2224 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002225 mWorkspace.moveToCustomContentScreen(animate);
2226 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002227
2228 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2229 int[] cell, int spanX, int spanY) {
2230 switch (info.itemType) {
2231 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2232 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2233 int span[] = new int[2];
2234 span[0] = spanX;
2235 span[1] = spanY;
2236 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2237 container, screenId, cell, span);
2238 break;
2239 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2240 processShortcutFromDrop(info.componentName, container, screenId, cell);
2241 break;
2242 default:
2243 throw new IllegalStateException("Unknown item type: " + info.itemType);
2244 }
2245 }
2246
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 /**
2248 * Process a shortcut drop.
2249 *
2250 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002254 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2255 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259
2260 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002261 mPendingAddInfo.cellX = cell[0];
2262 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264
2265 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2266 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002267 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 }
2269
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 /**
2271 * Process a widget drop.
2272 *
2273 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002277 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2278 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002281 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002282 mPendingAddInfo.minSpanX = info.minSpanX;
2283 mPendingAddInfo.minSpanY = info.minSpanY;
2284
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002286 mPendingAddInfo.cellX = cell[0];
2287 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002289 if (span != null) {
2290 mPendingAddInfo.spanX = span[0];
2291 mPendingAddInfo.spanY = span[1];
2292 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293
Adam Cohened66b2b2012-01-23 17:28:51 -08002294 AppWidgetHostView hostView = info.boundWidget;
2295 int appWidgetId;
2296 if (hostView != null) {
2297 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002298 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002299
2300 // Clear the boundWidget so that it doesn't get destroyed.
2301 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002302 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002303 // In this case, we either need to start an activity to get permission to bind
2304 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002305 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002306 Bundle options = info.bindOptions;
2307
Sunny Goyalffe83f12014-08-14 17:39:34 -07002308 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2309 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002310 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002311 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002312 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002313 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002314 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2315 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2316 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002317 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2318 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002319 // TODO: we need to make sure that this accounts for the options bundle.
2320 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002321 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2322 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002323 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324 }
2325
Adam Cohendcd297f2013-06-18 13:13:40 -07002326 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002327 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002328 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002329 folderInfo.title = getText(R.string.folder_name);
2330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002331 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002332 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2333 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002334 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335
2336 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 FolderIcon newFolder =
2338 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002339 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002341 // Force measure the new folder icon
2342 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2343 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002344 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 }
Romain Guycbb89e42009-06-08 15:52:54 -07002346
Joe Onorato9c1289c2009-08-17 11:03:03 -04002347 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002348 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002349 }
2350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 @Override
2352 public boolean dispatchKeyEvent(KeyEvent event) {
2353 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2354 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002356 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002357 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002358 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002359 dumpState();
2360 return true;
2361 }
2362 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002363 }
2364 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2365 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002366 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 return true;
2368 }
2369 }
2370
2371 return super.dispatchKeyEvent(event);
2372 }
2373
Joe Onorato88ec0992009-11-19 13:16:06 -08002374 @Override
2375 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002376 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2377 return;
2378 }
2379
Sunny Goyal45478022015-06-08 16:52:41 -07002380 if (mDragController.isDragging()) {
2381 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002382 return;
2383 }
2384
Winson Chungb745afb2015-03-02 11:51:23 -08002385 if (isAppsViewVisible()) {
2386 showWorkspace(true);
2387 } else if (isWidgetsViewVisible()) {
2388 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002389 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002390 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002391 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002392 Folder openFolder = mWorkspace.getOpenFolder();
2393 if (openFolder.isEditingName()) {
2394 openFolder.dismissEditingName();
2395 } else {
2396 closeFolder();
2397 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002398 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002399 mWorkspace.exitWidgetResizeMode();
2400
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002401 // Back button is a no-op here, but give at least some feedback for the button press
2402 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002403 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002404 }
2405
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002407 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002408 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002409 @Override
2410 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002411 if (mAppWidgetHost != null) {
2412 mAppWidgetHost.startListening();
2413 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002414 }
2415
2416 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 * Launches the intent referred by the clicked shortcut.
2418 *
2419 * @param v The view representing the clicked shortcut.
2420 */
2421 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002422 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2423 // view has detached (it's possible for this to happen if the view is removed mid touch).
2424 if (v.getWindowToken() == null) {
2425 return;
2426 }
2427
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002428 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002429 return;
2430 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002431
Adam Cohen1697b792013-09-17 19:08:21 -07002432 if (v instanceof Workspace) {
2433 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002434 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002435 }
2436 return;
2437 }
2438
2439 if (v instanceof CellLayout) {
2440 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002441 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2442 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002443 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002444 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002445 }
2446
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002448 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002449 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002451 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002452 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002453 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002454 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002455 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002456 } else if (tag instanceof AppInfo) {
2457 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002458 } else if (tag instanceof LauncherAppWidgetInfo) {
2459 if (v instanceof PendingAppWidgetHostView) {
2460 onClickPendingWidget((PendingAppWidgetHostView) v);
2461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002462 }
2463 }
2464
Sunny Goyal70660032015-05-14 00:07:08 -07002465 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002466 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002467 return false;
2468 }
2469
Michael Jurkaaf442092010-06-10 17:01:57 -07002470 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002471 * Event handler for the app widget view which has not fully restored.
2472 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002473 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002474 if (mIsSafeModeEnabled) {
2475 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2476 return;
2477 }
2478
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002479 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002480 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002481 int widgetId = info.appWidgetId;
2482 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2483 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002484 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2485 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002486 mPendingAddInfo.copyFrom(info);
2487 mPendingAddWidgetId = widgetId;
2488
Sunny Goyalffe83f12014-08-14 17:39:34 -07002489 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2490 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002491 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002492 } else if (info.installProgress < 0) {
2493 // The install has not been queued
2494 final String packageName = info.providerName.getPackageName();
2495 showBrokenAppInstallDialog(packageName,
2496 new DialogInterface.OnClickListener() {
2497 public void onClick(DialogInterface dialog, int id) {
2498 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2499 }
2500 });
2501 } else {
2502 // Download has started.
2503 final String packageName = info.providerName.getPackageName();
2504 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002505 }
2506 }
2507
2508 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002509 * Event handler for the "grid" button that appears on the home screen, which
2510 * enters all apps mode.
2511 *
2512 * @param v The view that was clicked.
2513 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002514 protected void onClickAllAppsButton(View v) {
2515 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002516 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002517 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002518 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002519
2520 if (mLauncherCallbacks != null) {
2521 mLauncherCallbacks.onClickAllAppsButton(v);
2522 }
Winson Chung76648c52015-07-10 14:33:23 -07002523 }
2524 }
2525
2526 protected void onLongClickAllAppsButton(View v) {
2527 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2528 if (!isAppsViewVisible()) {
2529 showAppsView(true /* animated */, false /* resetListToTop */,
2530 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002531 }
2532 }
2533
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002534 private void showBrokenAppInstallDialog(final String packageName,
2535 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002536 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002537 .setTitle(R.string.abandoned_promises_title)
2538 .setMessage(R.string.abandoned_promise_explanation)
2539 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2540 .setNeutralButton(R.string.abandoned_clean_this,
2541 new DialogInterface.OnClickListener() {
2542 public void onClick(DialogInterface dialog, int id) {
2543 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2544 mWorkspace.removeAbandonedPromise(packageName, user);
2545 }
2546 })
2547 .create().show();
2548 return;
2549 }
2550
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002551 /**
2552 * Event handler for an app shortcut click.
2553 *
2554 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2555 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002556 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002557 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2558 Object tag = v.getTag();
2559 if (!(tag instanceof ShortcutInfo)) {
2560 throw new IllegalArgumentException("Input must be a Shortcut");
2561 }
2562
2563 // Open shortcut
2564 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002565
2566 if (shortcut.isDisabled != 0) {
2567 int error = R.string.activity_not_available;
2568 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2569 error = R.string.safemode_shortcut_error;
2570 }
2571 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2572 return;
2573 }
2574
Chris Wren40c5ed32014-06-24 18:24:23 -04002575 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002576 if ((v instanceof BubbleTextView)
2577 && shortcut.isPromise()
2578 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002579 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002580 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002581 new DialogInterface.OnClickListener() {
2582 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002583 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002584 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002585 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002586 return;
2587 }
2588
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002589 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002590 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002591
2592 if (mLauncherCallbacks != null) {
2593 mLauncherCallbacks.onClickAppShortcut(v);
2594 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002595 }
2596
Adam Cohen091440a2015-03-18 14:16:05 -07002597 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002598 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002599 final ShortcutInfo shortcut;
2600 final Intent intent;
2601 if (tag instanceof ShortcutInfo) {
2602 shortcut = (ShortcutInfo) tag;
2603 intent = shortcut.intent;
2604 int[] pos = new int[2];
2605 v.getLocationOnScreen(pos);
2606 intent.setSourceBounds(new Rect(pos[0], pos[1],
2607 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002608
Sunny Goyal508da152014-08-14 10:53:27 -07002609 } else if (tag instanceof AppInfo) {
2610 shortcut = null;
2611 intent = ((AppInfo) tag).intent;
2612 } else {
2613 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2614 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002615
2616 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002617 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618
2619 if (success && v instanceof BubbleTextView) {
2620 mWaitingForResume = (BubbleTextView) v;
2621 mWaitingForResume.setStayPressed(true);
2622 }
2623 }
2624
2625 /**
2626 * Event handler for a folder icon click.
2627 *
2628 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2629 */
2630 protected void onClickFolderIcon(View v) {
2631 if (LOGD) Log.d(TAG, "onClickFolder");
2632 if (!(v instanceof FolderIcon)){
2633 throw new IllegalArgumentException("Input must be a FolderIcon");
2634 }
2635
Sunny Goyal317698b2015-07-29 11:45:41 -07002636 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002637 FolderIcon folderIcon = (FolderIcon) v;
2638 final FolderInfo info = folderIcon.getFolderInfo();
2639 Folder openFolder = mWorkspace.getFolderForTag(info);
2640
2641 // If the folder info reports that the associated folder is open, then verify that
2642 // it is actually opened. There have been a few instances where this gets out of sync.
2643 if (info.opened && openFolder == null) {
2644 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2645 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2646 info.opened = false;
2647 }
2648
2649 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2650 // Close any open folder
2651 closeFolder();
2652 // Open the requested folder
2653 openFolder(folderIcon);
2654 } else {
2655 // Find the open folder...
2656 int folderScreen;
2657 if (openFolder != null) {
2658 folderScreen = mWorkspace.getPageForView(openFolder);
2659 // .. and close it
2660 closeFolder(openFolder);
2661 if (folderScreen != mWorkspace.getCurrentPage()) {
2662 // Close any folder open on the current screen
2663 closeFolder();
2664 // Pull the folder onto this screen
2665 openFolder(folderIcon);
2666 }
2667 }
2668 }
Adam Cohen9211d422014-10-07 18:14:53 -07002669
2670 if (mLauncherCallbacks != null) {
2671 mLauncherCallbacks.onClickFolderIcon(v);
2672 }
Michael Jurka5130e402011-10-13 04:55:35 -07002673 }
2674
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002675 /**
2676 * Event handler for the (Add) Widgets button that appears after a long press
2677 * on the home screen.
2678 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002679 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002680 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002681 if (mIsSafeModeEnabled) {
2682 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2683 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002684 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002685 if (mLauncherCallbacks != null) {
2686 mLauncherCallbacks.onClickAddWidgetButton(view);
2687 }
Adam Cohen9211d422014-10-07 18:14:53 -07002688 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002689 }
2690
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691 /**
2692 * Event handler for the wallpaper picker button that appears after a long press
2693 * on the home screen.
2694 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002695 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002697 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2698 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002699
2700 if (mLauncherCallbacks != null) {
2701 mLauncherCallbacks.onClickWallpaperPicker(v);
2702 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002703 }
2704
2705 /**
2706 * Event handler for a click on the settings button that appears after a long press
2707 * on the home screen.
2708 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002709 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002710 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002711 if (mLauncherCallbacks != null) {
2712 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002713 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002714 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002715 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 }
2717
Adam Cohen61f560d2013-09-30 15:58:20 -07002718 public View.OnTouchListener getHapticFeedbackTouchListener() {
2719 if (mHapticFeedbackTouchListener == null) {
2720 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002721 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002722 @Override
2723 public boolean onTouch(View v, MotionEvent event) {
2724 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2725 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2726 }
2727 return false;
2728 }
2729 };
2730 }
2731 return mHapticFeedbackTouchListener;
2732 }
2733
Adam Cohen9211d422014-10-07 18:14:53 -07002734 public void onDragStarted(View view) {
2735 if (isOnCustomContent()) {
2736 // Custom content screen doesn't participate in drag and drop. If on custom
2737 // content screen, move to default.
2738 moveWorkspaceToDefaultScreen();
2739 }
2740
2741 if (mLauncherCallbacks != null) {
2742 mLauncherCallbacks.onDragStarted(view);
2743 }
2744 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002745
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002746 /**
2747 * Called when the user stops interacting with the launcher.
2748 * This implies that the user is now on the homescreen and is not doing housekeeping.
2749 */
Adam Cohen9211d422014-10-07 18:14:53 -07002750 protected void onInteractionEnd() {
2751 if (mLauncherCallbacks != null) {
2752 mLauncherCallbacks.onInteractionEnd();
2753 }
2754 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002755
2756 /**
2757 * Called when the user starts interacting with the launcher.
2758 * The possible interactions are:
2759 * - open all apps
2760 * - reorder an app shortcut, or a widget
2761 * - open the overview mode.
2762 * This is a good time to stop doing things that only make sense
2763 * when the user is on the homescreen and not doing housekeeping.
2764 */
Adam Cohen9211d422014-10-07 18:14:53 -07002765 protected void onInteractionBegin() {
2766 if (mLauncherCallbacks != null) {
2767 mLauncherCallbacks.onInteractionBegin();
2768 }
2769 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002770
Winson Chungcd99cd32015-04-29 11:03:24 -07002771 /** Updates the interaction state. */
2772 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002773 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002774 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002775 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2776 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002777 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002778 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002779 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002780 onInteractionEnd();
2781 }
2782 }
2783
Kenny Guyf07af7b2014-07-31 11:39:16 +01002784 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002785 try {
2786 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002787 launcherApps.showAppDetailsForProfile(componentName, user);
2788 } catch (SecurityException e) {
2789 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2790 Log.e(TAG, "Launcher does not have permission to launch settings");
2791 } catch (ActivityNotFoundException e) {
2792 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2793 Log.e(TAG, "Unable to launch settings");
2794 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002795 }
2796
Michael Jurka1e2f4652013-07-08 18:03:46 -07002797 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002798 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2799 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002800 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002801 // System applications cannot be installed. For now, show a toast explaining that.
2802 // We may give them the option of disabling apps this way.
2803 int messageId = R.string.uninstall_system_app_text;
2804 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002805 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002806 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002807 String packageName = componentName.getPackageName();
2808 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002809 Intent intent = new Intent(
2810 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002811 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2812 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002813 if (user != null) {
2814 user.addToIntent(intent, Intent.EXTRA_USER);
2815 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002816 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002817 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002818 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002819 }
2820
Winson Chung8f1eff72015-05-28 17:33:40 -07002821 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002822 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002824 // Only launch using the new animation if the shortcut has not opted out (this is a
2825 // private contract between launcher and may be ignored in the future).
2826 boolean useLaunchAnimation = (v != null) &&
2827 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002828 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2829 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002830
Kenny Guy1317e2d2014-05-08 18:52:50 +01002831 UserHandleCompat user = null;
2832 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2833 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2834 user = userManager.getUserForSerialNumber(serialNumber);
2835 }
2836
2837 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002838 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002839 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002840 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002841 int left = 0, top = 0;
2842 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2843 if (v instanceof TextView) {
2844 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002845 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2846 if (icon != null) {
2847 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002848 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002849 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002850 width = bounds.width();
2851 height = bounds.height();
2852 }
2853 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002854 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2855 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002856 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002857 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002858 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002859 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002860 // On L devices, we use the device default slide-up transition.
2861 // On L MR1 devices, we a custom version of the slide-up transition which
2862 // doesn't have the delay present in the device default.
2863 opts = ActivityOptions.makeCustomAnimation(this,
2864 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002865 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002866 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002867 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002868
2869 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2870 // Could be launching some bookkeeping activity
2871 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002872 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002873 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002874 launcherApps.startActivityForProfile(intent.getComponent(), user,
2875 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002876 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002877 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878 } catch (SecurityException e) {
2879 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002880 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002882 "or use the exported attribute for this activity. "
2883 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002885 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002887
Winson Chungbedf9232015-07-10 12:38:30 -07002888 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002889 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002890 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2891 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2892 return false;
2893 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002894 try {
2895 success = startActivity(v, intent, tag);
2896 } catch (ActivityNotFoundException e) {
2897 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2898 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2899 }
2900 return success;
2901 }
2902
Adam Cohen268c4752012-06-06 17:47:33 -07002903 /**
2904 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2905 * in the DragLayer in the exact absolute location of the original FolderIcon.
2906 */
2907 private void copyFolderIconToImage(FolderIcon fi) {
2908 final int width = fi.getMeasuredWidth();
2909 final int height = fi.getMeasuredHeight();
2910
2911 // Lazy load ImageView, Bitmap and Canvas
2912 if (mFolderIconImageView == null) {
2913 mFolderIconImageView = new ImageView(this);
2914 }
2915 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2916 mFolderIconBitmap.getHeight() != height) {
2917 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2918 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2919 }
2920
2921 DragLayer.LayoutParams lp;
2922 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2923 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2924 } else {
2925 lp = new DragLayer.LayoutParams(width, height);
2926 }
2927
Adam Cohen307fe232012-08-16 17:55:58 -07002928 // The layout from which the folder is being opened may be scaled, adjust the starting
2929 // view size by this scale factor.
2930 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002931 lp.customPosition = true;
2932 lp.x = mRectForFolderAnimation.left;
2933 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002934 lp.width = (int) (scale * width);
2935 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002936
2937 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2938 fi.draw(mFolderIconCanvas);
2939 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002940 if (fi.getFolder() != null) {
2941 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2942 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002943 }
Adam Cohen268c4752012-06-06 17:47:33 -07002944 // Just in case this image view is still in the drag layer from a previous animation,
2945 // we remove it and re-add it.
2946 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2947 mDragLayer.removeView(mFolderIconImageView);
2948 }
2949 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002950 if (fi.getFolder() != null) {
2951 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002952 }
Adam Cohen268c4752012-06-06 17:47:33 -07002953 }
2954
Adam Cohen2801caf2011-05-13 20:57:39 -07002955 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002956 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002957 FolderInfo info = (FolderInfo) fi.getTag();
2958 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2959 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002960 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2961 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002962 }
2963
Adam Cohen268c4752012-06-06 17:47:33 -07002964 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2965 copyFolderIconToImage(fi);
2966 fi.setVisibility(View.INVISIBLE);
2967
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002968 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2969 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002970 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002971 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2972 }
2973 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002974 oa.start();
2975 }
2976
Adam Cohen268c4752012-06-06 17:47:33 -07002977 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002978 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002979 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002980
Adam Cohen268c4752012-06-06 17:47:33 -07002981 // We remove and re-draw the FolderIcon in-case it has changed
2982 mDragLayer.removeView(mFolderIconImageView);
2983 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002984 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002985 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002986 oa.addListener(new AnimatorListenerAdapter() {
2987 @Override
2988 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002989 if (cl != null) {
2990 cl.clearFolderLeaveBehind();
2991 // Remove the ImageView copy of the FolderIcon and make the original visible.
2992 mDragLayer.removeView(mFolderIconImageView);
2993 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002994 }
2995 }
2996 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002997 oa.start();
2998 }
2999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003000 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003001 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003002 * is animated relative to the specified View. If the View is null, no animation
3003 * is played.
3004 *
3005 * @param folderInfo The FolderInfo describing the folder to open.
3006 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003007 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003008 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003009 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3010 if (openFolder != null && openFolder != folder) {
3011 // Close any open folder before opening a folder.
3012 closeFolder();
3013 }
3014
Adam Cohena9cf38f2011-05-02 15:36:58 -07003015 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016
Adam Cohena9cf38f2011-05-02 15:36:58 -07003017 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003018
Sunny Goyalf4066152015-04-15 09:42:19 -07003019 // While the folder is open, the position of the icon cannot change.
3020 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3021
Adam Cohen4554ee12011-08-03 16:13:21 -07003022 // Just verify that the folder hasn't already been added to the DragLayer.
3023 // There was a one-off crash where the folder had a parent already.
3024 if (folder.getParent() == null) {
3025 mDragLayer.addView(folder);
3026 mDragController.addDropTarget((DropTarget) folder);
3027 } else {
3028 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3029 folder.getParent() + ").");
3030 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003031 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003032 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003033
3034 // Notify the accessibility manager that this folder "window" has appeared and occluded
3035 // the workspace items
3036 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3037 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003038 }
3039
3040 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003041 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003042 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003043 if (folder.isEditingName()) {
3044 folder.dismissEditingName();
3045 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003046 closeFolder(folder);
3047 }
3048 }
3049
Sunny Goyal83a8f042015-05-19 12:52:12 -07003050 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003051 folder.getInfo().opened = false;
3052
3053 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3054 if (parent != null) {
3055 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3056 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003057 if (fi != null) {
3058 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3059 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003060 }
3061 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003062
3063 // Notify the accessibility manager that this folder "window" has disappeard and no
3064 // longer occludeds the workspace items
3065 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066 }
3067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003068 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003069 if (!isDraggingEnabled()) return false;
3070 if (isWorkspaceLocked()) return false;
3071 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072
Winson Chung76648c52015-07-10 14:33:23 -07003073 if (v == mAllAppsButton) {
3074 onLongClickAllAppsButton(v);
3075 return true;
3076 }
3077
Adam Cohen1697b792013-09-17 19:08:21 -07003078 if (v instanceof Workspace) {
3079 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003080 if (!mWorkspace.isTouchActive()) {
3081 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003082 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3083 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3084 return true;
3085 } else {
3086 return false;
3087 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003088 } else {
3089 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003090 }
Adam Cohen1697b792013-09-17 19:08:21 -07003091 }
3092
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003093 CellLayout.CellInfo longClickCellInfo = null;
3094 View itemUnderLongClick = null;
3095 if (v.getTag() instanceof ItemInfo) {
3096 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003097 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003098 itemUnderLongClick = longClickCellInfo.cell;
3099 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100 }
3101
Winson Chung3d503fb2011-07-13 17:25:49 -07003102 // The hotseat touch handling does not go through Workspace, and we always allow long press
3103 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003104 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003105 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003106 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003107 // User long pressed on empty space
3108 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3109 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003110 if (mWorkspace.isInOverviewMode()) {
3111 mWorkspace.startReordering(v);
3112 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003113 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003115 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003116 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3117 mHotseat.getOrderInHotseat(
3118 longClickCellInfo.cellX,
3119 longClickCellInfo.cellY));
3120 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003122 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123 }
3124 }
3125 }
3126 return true;
3127 }
3128
Winson Chung3d503fb2011-07-13 17:25:49 -07003129 boolean isHotseatLayout(View layout) {
3130 return mHotseat != null && layout != null &&
3131 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3132 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003133
Winson Chung3d503fb2011-07-13 17:25:49 -07003134 /**
3135 * Returns the CellLayout of the specified container at the specified screen.
3136 */
Sunny Goyal92820592015-03-02 11:31:35 -08003137 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003138 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3139 if (mHotseat != null) {
3140 return mHotseat.getLayout();
3141 } else {
3142 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003143 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003144 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003145 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003146 }
3147 }
3148
Winson Chungb745afb2015-03-02 11:51:23 -08003149 /**
3150 * For overridden classes.
3151 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003152 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003153 return isAppsViewVisible();
3154 }
3155
3156 public boolean isAppsViewVisible() {
3157 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3158 }
3159
3160 public boolean isWidgetsViewVisible() {
3161 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003162 }
3163
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003164 private void setWorkspaceBackground(int background) {
3165 switch (background) {
3166 case WORKSPACE_BACKGROUND_TRANSPARENT:
3167 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3168 break;
3169 case WORKSPACE_BACKGROUND_BLACK:
3170 getWindow().setBackgroundDrawable(null);
3171 break;
3172 default:
3173 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3174 }
Craig Mautner360310b2012-10-26 15:13:08 -07003175 }
3176
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003177 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003178 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3179 int curflags = getWindow().getAttributes().flags
3180 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3181 if (wpflags != curflags) {
3182 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3183 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003184 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003185 }
3186
Michael Jurkae326f182011-11-21 14:05:46 -08003187 @Override
3188 public void onTrimMemory(int level) {
3189 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003190 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3191 // The widget preview db can result in holding onto over
3192 // 3MB of memory for caching which isn't necessary.
3193 SQLiteDatabase.releaseMemory();
3194
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003195 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003196 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003197 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003198 if (mLauncherCallbacks != null) {
3199 mLauncherCallbacks.onTrimMemory(level);
3200 }
Michael Jurkae326f182011-11-21 14:05:46 -08003201 }
3202
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003203 public void showWorkspace(boolean animated) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003204 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003205 }
3206
Sunny Goyal83a8f042015-05-19 12:52:12 -07003207 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003208 boolean changed = mState != State.WORKSPACE ||
3209 mWorkspace.getState() != Workspace.State.NORMAL;
3210 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003211 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003212 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003213 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003214
Michael Jurkab3e22d92011-10-31 15:58:33 -07003215 // Set focus to the AppsCustomize button
3216 if (mAllAppsButton != null) {
3217 mAllAppsButton.requestFocus();
3218 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003219 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003220
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003221 // Change the state *after* we've called all the transition code
3222 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003223
Winson Chung5fb63472011-02-02 17:03:37 -08003224 // Resume the auto-advance of widgets
3225 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003226 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003227
Winson Chung0f785722015-04-08 10:27:49 -07003228 if (changed) {
3229 // Send an accessibility event to announce the context change
3230 getWindow().getDecorView()
3231 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003232 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003233 }
3234
Adam Cohened307df2013-10-02 09:37:31 -07003235 void showOverviewMode(boolean animated) {
3236 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003237 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003238 Workspace.State.OVERVIEW, animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003239 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003240 }
3241
Winson Chungb745afb2015-03-02 11:51:23 -08003242 /**
3243 * Shows the apps view.
3244 */
Winson Chung76648c52015-07-10 14:33:23 -07003245 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3246 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003247 if (resetListToTop) {
3248 mAppsView.scrollToTop();
3249 }
Winson Chung4ac30062015-05-08 17:34:17 -07003250 if (updatePredictedApps) {
3251 tryAndUpdatePredictedApps();
3252 }
Winson Chung76648c52015-07-10 14:33:23 -07003253 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003254 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003255
Winson Chungb745afb2015-03-02 11:51:23 -08003256 /**
3257 * Shows the widgets view.
3258 */
3259 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003260 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003261 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003262 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003263 }
Winson Chung76648c52015-07-10 14:33:23 -07003264 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003265
3266 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003267 @Override
3268 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003269 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003270 }
3271 });
Winson Chungb745afb2015-03-02 11:51:23 -08003272 }
3273
3274 /**
3275 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003276 *
3277 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003278 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003279 // TODO: calling method should use the return value so that when {@code false} is returned
3280 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003281 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003282 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3283 mState != State.WIDGETS_SPRING_LOADED) {
3284 return false;
3285 }
3286 if (toState != State.APPS && toState != State.WIDGETS) {
3287 return false;
3288 }
Winson Chungb745afb2015-03-02 11:51:23 -08003289
3290 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003291 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3292 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003293 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003294 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003295 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003296
Michael Jurkab3e22d92011-10-31 15:58:33 -07003297 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003298 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003299
3300 // Pause the auto-advance of widgets until we are out of AllApps
3301 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003302 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003303 closeFolder();
3304
3305 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003306 getWindow().getDecorView()
3307 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003308 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003309 }
3310
Winson Chungcd99cd32015-04-29 11:03:24 -07003311 /**
3312 * Updates the workspace and interaction state on state change, and return the animation to this
3313 * new state.
3314 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003315 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003316 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003317 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003318 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003319 updateInteraction(fromState, toState);
3320 return anim;
3321 }
3322
Jun Mukaif0033da2015-08-04 18:34:30 -07003323 public void onLauncherClingShown() {
3324 // When a launcher cling appears, it should cover the underlying layers, so their focus
3325 // should be blocked.
3326 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3327 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3328 }
3329 }
3330
3331 public void onLauncherClingDismissed() {
3332 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3333 }
3334
Hyunyoung Song3f471442015-04-08 19:01:34 -07003335 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003336 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003337 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3338 mState == State.WIDGETS_SPRING_LOADED) {
3339 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003340 }
Winson Chungb745afb2015-03-02 11:51:23 -08003341
Winson Chung006ee262015-08-03 14:40:11 -07003342 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003343 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003344 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003345 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003346 }
Adam Cohen7777d962011-08-18 18:58:38 -07003347
Hyunyoung Song3f471442015-04-08 19:01:34 -07003348 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003349 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003350 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003351
Winson Chunge7a03942011-08-05 15:05:12 -07003352 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003353 @Override
3354 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003355 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003356 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3357 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003358 // Before we show workspace, hide all apps again because
3359 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3360 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003361 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003362 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003363 } else {
3364 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003365 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003366 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003367 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003368 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369
Michael Jurkad3ef3062010-11-23 16:23:58 -08003370 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003371 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003372 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003373 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003374 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003375 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003376 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003377 }
3378
Winson Chung4ac30062015-05-08 17:34:17 -07003379 /**
3380 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3381 * resumed.
3382 */
3383 private void tryAndUpdatePredictedApps() {
3384 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003385 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003386 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003387 mAppsView.setPredictedApps(apps);
3388 }
3389 }
3390 }
3391
Michael Jurkab3e22d92011-10-31 15:58:33 -07003392 void lockAllApps() {
3393 // TODO
3394 }
3395
3396 void unlockAllApps() {
3397 // TODO
3398 }
3399
Sunny Goyal594d76d2014-11-06 10:12:54 -08003400 protected void disableVoiceButtonProxy(boolean disable) {
3401 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003402 }
3403
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003404 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003405 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3406 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003407 }
3408
Adam Cohen24ce0b32014-01-14 16:18:14 -08003409 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003410 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3411 if (searchProvider == null) {
3412 return null;
3413 }
3414
3415 Bundle opts = new Bundle();
3416 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003417 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003418
3419 SharedPreferences sp = getSharedPreferences(
3420 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3421 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003422 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003423 if (!searchProvider.provider.flattenToString().equals(
3424 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003425 || (widgetInfo == null)
3426 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003427 // A valid widget is not already bound.
3428 if (widgetId > -1) {
3429 mAppWidgetHost.deleteAppWidgetId(widgetId);
3430 widgetId = -1;
3431 }
3432
3433 // Try to bind a new widget
3434 widgetId = mAppWidgetHost.allocateAppWidgetId();
3435
3436 if (!AppWidgetManagerCompat.getInstance(this)
3437 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3438 mAppWidgetHost.deleteAppWidgetId(widgetId);
3439 widgetId = -1;
3440 }
3441
3442 sp.edit()
3443 .putInt(QSB_WIDGET_ID, widgetId)
3444 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3445 .commit();
3446 }
3447
Sunny Goyal8d595092015-07-06 12:22:14 -07003448 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003449 if (widgetId != -1) {
3450 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3451 mQsb.updateAppWidgetOptions(opts);
3452 mQsb.setPadding(0, 0, 0, 0);
3453 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003454 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003455 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003456 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003457 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003458 }
3459
Sunny Goyal22235bc2015-04-03 09:23:43 -07003460 private void reinflateQSBIfNecessary() {
3461 if (mQsb instanceof LauncherAppWidgetHostView &&
3462 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3463 mSearchDropTargetBar.removeView(mQsb);
3464 mQsb = null;
3465 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3466 }
3467 }
3468
Michael Jurkad7c28052012-04-27 15:43:36 -07003469 @Override
3470 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003471 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003472 final List<CharSequence> text = event.getText();
3473 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003474 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003475 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003476 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003477 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003478 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003479 } else if (mWorkspace != null) {
3480 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003481 } else {
3482 text.add(getString(R.string.all_apps_home_button_label));
3483 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003484 return result;
3485 }
3486
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003487 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003488 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003489 */
Adam Cohen091440a2015-03-18 14:16:05 -07003490 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003491 @Override
3492 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003493 closeSystemDialogs();
3494 }
3495 }
3496
3497 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003498 * If the activity is currently paused, signal that we need to run the passed Runnable
3499 * in onResume.
3500 *
3501 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003502 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3503 * wrong when we're not running, and if the activity comes back to what the configuration was
3504 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003505 *
3506 * Implementation of the method from LauncherModel.Callbacks.
3507 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003508 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003509 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003510 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003511 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003512 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003513 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003514 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003515 }
3516 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003517 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003518 return true;
3519 } else {
3520 return false;
3521 }
3522 }
3523
Michael Jurkac402cd92013-05-20 15:49:32 +02003524 private boolean waitUntilResume(Runnable run) {
3525 return waitUntilResume(run, false);
3526 }
3527
Michael Jurka1e2f4652013-07-08 18:03:46 -07003528 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003529 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003530 }
3531
Michael Jurka7607c2f2013-04-03 14:33:19 -07003532 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003533 * If the activity is currently paused, signal that we need to re-run the loader
3534 * in onResume.
3535 *
3536 * This needs to be called from incoming places where resources might have been loaded
3537 * while we are paused. That is becaues the Configuration might be wrong
3538 * when we're not running, and if it comes back to what it was when we
3539 * were paused, we are not restarted.
3540 *
3541 * Implementation of the method from LauncherModel.Callbacks.
3542 *
3543 * @return true if we are currently paused. The caller might be able to
3544 * skip some work in that case since we will come back again.
3545 */
3546 public boolean setLoadOnResume() {
3547 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003548 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003549 mOnResumeNeedsLoad = true;
3550 return true;
3551 } else {
3552 return false;
3553 }
3554 }
3555
3556 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003558 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003559 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003560 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003561 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003562 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003563 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003566
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 /**
3568 * Refreshes the shortcuts shown on the workspace.
3569 *
3570 * Implementation of the method from LauncherModel.Callbacks.
3571 */
3572 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003573 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003574
Michael Jurka7607c2f2013-04-03 14:33:19 -07003575 // If we're starting binding all over again, clear any bind calls we'd postponed in
3576 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3577 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003578 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003579
Winson Chungd64d1762013-08-20 14:37:16 -07003580 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003581 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003582 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003583
Michael Jurka05bf644e2011-11-30 20:28:53 -08003584 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003585 if (mHotseat != null) {
3586 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003587 }
3588 }
3589
Adam Cohendcd297f2013-06-18 13:13:40 -07003590 @Override
3591 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003592 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003593
Adam Cohen5084cba2013-09-03 12:01:16 -07003594 // If there are no screens, we need to have an empty screen
3595 if (orderedScreenIds.size() == 0) {
3596 mWorkspace.addExtraEmptyScreen();
3597 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003598
3599 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003600 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003601 if (hasCustomContentToLeft()) {
3602 mWorkspace.createCustomContentContainer();
3603 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003604 }
Winson Chung64359a52013-07-08 17:17:08 -07003605 }
3606
3607 @Override
3608 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003609 int count = orderedScreenIds.size();
3610 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003611 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003612 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003613 }
3614
Winson Chungd64d1762013-08-20 14:37:16 -07003615 public void bindAppsAdded(final ArrayList<Long> newScreens,
3616 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003617 final ArrayList<ItemInfo> addAnimated,
3618 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003619 Runnable r = new Runnable() {
3620 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003621 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003622 }
3623 };
3624 if (waitUntilResume(r)) {
3625 return;
3626 }
3627
Winson Chungd64d1762013-08-20 14:37:16 -07003628 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003629 if (newScreens != null) {
3630 bindAddScreens(newScreens);
3631 }
Winson Chungd64d1762013-08-20 14:37:16 -07003632
3633 // We add the items without animation on non-visible pages, and with
3634 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003635 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003636 bindItems(addNotAnimated, 0,
3637 addNotAnimated.size(), false);
3638 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003639 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003640 bindItems(addAnimated, 0,
3641 addAnimated.size(), true);
3642 }
Winson Chungc58497e2013-09-03 17:48:37 -07003643
Winson Chung87412982013-10-03 18:34:14 -07003644 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003645 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003646
Winson Chungb745afb2015-03-02 11:51:23 -08003647 if (addedApps != null && mAppsView != null) {
3648 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003649 }
Winson Chungd64d1762013-08-20 14:37:16 -07003650 }
3651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003652 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 * Bind the items start-end from the list.
3654 *
3655 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003656 */
Winson Chung64359a52013-07-08 17:17:08 -07003657 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3658 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003659 Runnable r = new Runnable() {
3660 public void run() {
3661 bindItems(shortcuts, start, end, forceAnimateIcons);
3662 }
3663 };
3664 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003665 return;
3666 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003667
Winson Chungf0c6ae02012-03-21 16:10:31 -07003668 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003669 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3670 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003671 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003672 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003673 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003674 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003675 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003676
3677 // Short circuit if we are loading dock items for a configuration which has no dock
3678 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3679 mHotseat == null) {
3680 continue;
3681 }
3682
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 switch (item.itemType) {
3684 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3685 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003686 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003687 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003688
Winson Chung64359a52013-07-08 17:17:08 -07003689 /*
3690 * TODO: FIX collision case
3691 */
Winson Chungce376632013-07-11 16:12:41 -07003692 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3693 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3694 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003695 View v = cl.getChildAt(item.cellX, item.cellY);
3696 Object tag = v.getTag();
3697 String desc = "Collision while binding workspace item: " + item
3698 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003699 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003700 throw (new RuntimeException(desc));
3701 } else {
3702 Log.d(TAG, desc);
3703 }
Winson Chungce376632013-07-11 16:12:41 -07003704 }
Winson Chung64359a52013-07-08 17:17:08 -07003705 }
3706
Adam Cohendcd297f2013-06-18 13:13:40 -07003707 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3708 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003709 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003710 // Animate all the applications up now
3711 shortcut.setAlpha(0f);
3712 shortcut.setScaleX(0f);
3713 shortcut.setScaleY(0f);
3714 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003715 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003716 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003717 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003718 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003719 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003720 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003721 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003722 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3723 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003725 default:
3726 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003728 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003729
Winson Chung997a9232013-07-24 15:33:46 -07003730 if (animateIcons) {
3731 // Animate to the correct page
3732 if (newShortcutsScreenId > -1) {
3733 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003734 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003735 final Runnable startBounceAnimRunnable = new Runnable() {
3736 public void run() {
3737 anim.playTogether(bounceAnims);
3738 anim.start();
3739 }
3740 };
Winson Chung997a9232013-07-24 15:33:46 -07003741 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003742 // We post the animation slightly delayed to prevent slowdowns
3743 // when we are loading right after we return to launcher.
3744 mWorkspace.postDelayed(new Runnable() {
3745 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003746 if (mWorkspace != null) {
3747 mWorkspace.snapToPage(newScreenIndex);
3748 mWorkspace.postDelayed(startBounceAnimRunnable,
3749 NEW_APPS_ANIMATION_DELAY);
3750 }
Winson Chung94d67682013-09-25 16:29:40 -07003751 }
3752 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003753 } else {
3754 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003755 }
3756 }
Winson Chung64359a52013-07-08 17:17:08 -07003757 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003758 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003759 }
3760
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761 /**
3762 * Implementation of the method from LauncherModel.Callbacks.
3763 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003764 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003765 Runnable r = new Runnable() {
3766 public void run() {
3767 bindFolders(folders);
3768 }
3769 };
3770 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003771 return;
3772 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003773 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003774 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003775
3776 /**
3777 * Add the views for a widget to the workspace.
3778 *
3779 * Implementation of the method from LauncherModel.Callbacks.
3780 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003781 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003782 Runnable r = new Runnable() {
3783 public void run() {
3784 bindAppWidget(item);
3785 }
3786 };
3787 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003788 return;
3789 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003790
Daniel Sandler843e8602010-06-07 14:59:01 -04003791 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3792 if (DEBUG_WIDGETS) {
3793 Log.d(TAG, "bindAppWidget: " + item);
3794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 final Workspace workspace = mWorkspace;
3796
Adam Cohen59400422014-03-05 18:07:04 -08003797 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003798 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003799
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003800 if (!mIsSafeModeEnabled
3801 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003802 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3803
Sunny Goyalff572272014-07-23 13:58:07 -07003804 if (appWidgetInfo == null) {
3805 if (DEBUG_WIDGETS) {
3806 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3807 + " belongs to component " + item.providerName
3808 + ", as the povider is null");
3809 }
3810 LauncherModel.deleteItemFromDatabase(this, item);
3811 return;
3812 }
Sunny Goyalff572272014-07-23 13:58:07 -07003813
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003814 // If we do not have a valid id, try to bind an id.
3815 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3816 // Note: This assumes that the id remap broadcast is received before this step.
3817 // If that is not the case, the id remap will be ignored and user may see the
3818 // click to setup view.
3819 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3820 pendingInfo.spanX = item.spanX;
3821 pendingInfo.spanY = item.spanY;
3822 pendingInfo.minSpanX = item.minSpanX;
3823 pendingInfo.minSpanY = item.minSpanY;
3824 Bundle options = null;
3825 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003826
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003827 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3828 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3829 newWidgetId, appWidgetInfo, options);
3830
3831 // TODO consider showing a permission dialog when the widget is clicked.
3832 if (!success) {
3833 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3834 if (DEBUG_WIDGETS) {
3835 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3836 + " belongs to component " + item.providerName
3837 + ", as the launcher is unable to bing a new widget id");
3838 }
3839 LauncherModel.deleteItemFromDatabase(this, item);
3840 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003841 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003842
3843 item.appWidgetId = newWidgetId;
3844
3845 // If the widget has a configure activity, it is still needs to set it up, otherwise
3846 // the widget is ready to go.
3847 item.restoreStatus = (appWidgetInfo.configure == null)
3848 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3849 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3850
3851 LauncherModel.updateItemInDatabase(this, item);
3852 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
3853 && (appWidgetInfo.configure == null)) {
3854 // If the ID is already valid, verify if we need to configure or not.
3855 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3856 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003857 }
Sunny Goyalff572272014-07-23 13:58:07 -07003858 }
3859
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003860 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003861 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003862 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003863 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3864 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003865 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003866
Sunny Goyal651077b2014-06-30 14:15:31 -07003867 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003868 item.minSpanX = appWidgetInfo.minSpanX;
3869 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07003870 } else {
3871 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003872 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3873 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003874 view.updateIcon(mIconCache);
3875 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003876 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003877 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879
Joe Onorato9c1289c2009-08-17 11:03:03 -04003880 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003881 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003882
Adam Cohendcd297f2013-06-18 13:13:40 -07003883 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003885 if (!item.isCustomWidget()) {
3886 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3887 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003888
Joe Onorato9c1289c2009-08-17 11:03:03 -04003889 workspace.requestLayout();
3890
Daniel Sandler843e8602010-06-07 14:59:01 -04003891 if (DEBUG_WIDGETS) {
3892 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3893 + (SystemClock.uptimeMillis()-start) + "ms");
3894 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895 }
3896
Sunny Goyalff572272014-07-23 13:58:07 -07003897 /**
3898 * Restores a pending widget.
3899 *
3900 * @param appWidgetId The app widget id
3901 * @param cellInfo The position on screen where to create the widget.
3902 */
3903 private void completeRestoreAppWidget(final int appWidgetId) {
3904 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3905 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3906 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3907 return;
3908 }
3909
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003910 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003911 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3912
3913 mWorkspace.reinflateWidgetsIfNecessary();
3914 LauncherModel.updateItemInDatabase(this, info);
3915 }
3916
Adam Cohen1462de32012-07-24 22:34:36 -07003917 public void onPageBoundSynchronously(int page) {
3918 mSynchronouslyBoundPages.add(page);
3919 }
3920
Joe Onorato9c1289c2009-08-17 11:03:03 -04003921 /**
3922 * Callback saying that there aren't any more items to bind.
3923 *
3924 * Implementation of the method from LauncherModel.Callbacks.
3925 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003926 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003927 Runnable r = new Runnable() {
3928 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003929 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003930 }
3931 };
3932 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003933 return;
3934 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935 if (mSavedState != null) {
3936 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003937 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003938 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003939 mSavedState = null;
3940 }
3941
Adam Cohen1462de32012-07-24 22:34:36 -07003942 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003943
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003944 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07003945 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07003946
3947 // If we received the result of any pending adds while the loader was running (e.g. the
3948 // widget configuration forced an orientation change), process them now.
3949 if (sPendingAddItem != null) {
3950 final long screenId = completeAdd(sPendingAddItem);
3951
3952 // TODO: this moves the user to the page where the pending item was added. Ideally,
3953 // the screen would be guaranteed to exist after bind, and the page would be set through
3954 // the workspace restore process.
3955 mWorkspace.post(new Runnable() {
3956 @Override
3957 public void run() {
3958 mWorkspace.snapToScreenId(screenId);
3959 }
3960 });
3961 sPendingAddItem = null;
3962 }
3963
Sunny Goyal756adbc2015-04-16 15:20:51 -07003964 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003965
3966 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003967 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003968 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003969 }
3970
Adam Cohen3ed316a2014-07-23 18:21:20 -07003971 private void sendLoadingCompleteBroadcastIfNecessary() {
3972 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
3973 String permission =
3974 getResources().getString(R.string.receive_first_load_broadcast_permission);
3975 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
3976 sendBroadcast(intent, permission);
3977 SharedPreferences.Editor editor = mSharedPrefs.edit();
3978 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
3979 editor.apply();
3980 }
3981 }
3982
Winson Chunga0b7e862013-09-05 16:03:15 -07003983 public boolean isAllAppsButtonRank(int rank) {
3984 if (mHotseat != null) {
3985 return mHotseat.isAllAppsButtonRank(rank);
3986 }
3987 return false;
3988 }
3989
Winson Chunga2413752012-04-03 14:22:34 -07003990 private boolean canRunNewAppsAnimation() {
3991 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3992 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3993 }
3994
Winson Chungc9168342013-06-26 14:54:55 -07003995 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003996 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003997 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3998 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003999 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004000 return bounceAnim;
4001 }
4002
Adam Cohen27772732013-11-11 14:00:56 +00004003 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004004 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004005 }
4006
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004007 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004008 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004009 }
4010
Winson Chung88fa7412015-08-03 14:25:28 -07004011 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004012 if (mSearchDropTargetBar == null) {
4013 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004014 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004015 if (mQsb != null) {
4016 mSearchDropTargetBar.removeView(mQsb);
4017 mQsb = null;
4018 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004019 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004020 }
4021
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004022 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004023 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4024 * multiple calls to bind the same list.)
4025 */
4026 @Thunk ArrayList<AppInfo> mTmpAppsList;
4027 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4028 public void run() {
4029 bindAllApplications(mTmpAppsList);
4030 mTmpAppsList = null;
4031 }
4032 };
4033
4034 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004035 * Add the icons for all apps.
4036 *
4037 * Implementation of the method from LauncherModel.Callbacks.
4038 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004039 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004040 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4041 mTmpAppsList = apps;
4042 return;
4043 }
4044
Winson Chungb745afb2015-03-02 11:51:23 -08004045 if (mAppsView != null) {
4046 mAppsView.setApps(apps);
4047 }
Adam Cohen9211d422014-10-07 18:14:53 -07004048 if (mLauncherCallbacks != null) {
4049 mLauncherCallbacks.bindAllApplications(apps);
4050 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004051 }
4052
4053 /**
4054 * A package was updated.
4055 *
4056 * Implementation of the method from LauncherModel.Callbacks.
4057 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004058 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004059 Runnable r = new Runnable() {
4060 public void run() {
4061 bindAppsUpdated(apps);
4062 }
4063 };
4064 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004065 return;
4066 }
4067
Winson Chungb745afb2015-03-02 11:51:23 -08004068 if (mAppsView != null) {
4069 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004070 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004071 }
4072
Sunny Goyal4390ace2014-10-13 11:33:11 -07004073 @Override
4074 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4075 Runnable r = new Runnable() {
4076 public void run() {
4077 bindWidgetsRestored(widgets);
4078 }
4079 };
4080 if (waitUntilResume(r)) {
4081 return;
4082 }
4083 mWorkspace.widgetsRestored(widgets);
4084 }
4085
Joe Onorato9c1289c2009-08-17 11:03:03 -04004086 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004087 * Some shortcuts were updated in the background.
4088 *
4089 * Implementation of the method from LauncherModel.Callbacks.
4090 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004091 @Override
4092 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4093 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004094 Runnable r = new Runnable() {
4095 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004096 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004097 }
4098 };
4099 if (waitUntilResume(r)) {
4100 return;
4101 }
4102
Sunny Goyal4390ace2014-10-13 11:33:11 -07004103 if (!updated.isEmpty()) {
4104 mWorkspace.updateShortcuts(updated);
4105 }
4106
4107 if (!removed.isEmpty()) {
4108 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4109 for (ShortcutInfo si : removed) {
4110 removedComponents.add(si.getTargetComponent());
4111 }
4112 mWorkspace.removeItemsByComponentName(removedComponents, user);
4113 // Notify the drag controller
4114 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004115 }
4116 }
4117
4118 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004119 * Update the state of a package, typically related to install state.
4120 *
4121 * Implementation of the method from LauncherModel.Callbacks.
4122 */
Sunny Goyale755d462014-07-22 13:48:29 -07004123 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004124 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4125 Runnable r = new Runnable() {
4126 public void run() {
4127 bindRestoreItemsChange(updates);
4128 }
4129 };
4130 if (waitUntilResume(r)) {
4131 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004132 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004133
Sunny Goyal756adbc2015-04-16 15:20:51 -07004134 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004135 }
4136
4137 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004138 * A package was uninstalled. We take both the super set of packageNames
4139 * in addition to specific applications to remove, the reason being that
4140 * this can be called when a package is updated as well. In that scenario,
4141 * we only remove specific components from the workspace, where as
4142 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004143 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004144 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004145 * Implementation of the method from LauncherModel.Callbacks.
4146 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004147 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004148 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004149 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004150 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004151 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004152 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004153 }
Winson Chungd64d1762013-08-20 14:37:16 -07004154 };
4155 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004156 return;
4157 }
4158
Sunny Goyal1a745e82014-10-02 15:58:31 -07004159 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004160 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4161 for (AppInfo info : appInfos) {
4162 removedComponents.add(info.componentName);
4163 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004164 if (!packageNames.isEmpty()) {
4165 mWorkspace.removeItemsByPackageName(packageNames, user);
4166 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004167 if (!removedComponents.isEmpty()) {
4168 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004169 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004170 // Notify the drag controller
4171 mDragController.onAppsRemoved(packageNames, removedComponents);
4172
Sunny Goyal1a745e82014-10-02 15:58:31 -07004173 } else {
4174 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004175 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004176
Winson Chungdf95eb12013-10-16 14:57:07 -07004177 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004178 if (mAppsView != null) {
4179 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004180 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004181 }
Joe Onoratobe386092009-11-17 17:32:16 -08004182
Michael Jurkac402cd92013-05-20 15:49:32 +02004183 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004184 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004185 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004186 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004187 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004188
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004189 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004190 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004191 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004192 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004193 return;
4194 }
4195
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004196 if (mWidgetsView != null && model != null) {
4197 mWidgetsView.addWidgets(model);
4198 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004199 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004200 }
4201
Winson Chung400438b2011-01-16 17:53:48 -08004202 private int mapConfigurationOriActivityInfoOri(int configOri) {
4203 final Display d = getWindowManager().getDefaultDisplay();
4204 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4205 switch (d.getRotation()) {
4206 case Surface.ROTATION_0:
4207 case Surface.ROTATION_180:
4208 // We are currently in the same basic orientation as the natural orientation
4209 naturalOri = configOri;
4210 break;
4211 case Surface.ROTATION_90:
4212 case Surface.ROTATION_270:
4213 // We are currently in the other basic orientation to the natural orientation
4214 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4215 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4216 break;
4217 }
4218
4219 int[] oriMap = {
4220 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4221 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4222 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4223 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4224 };
4225 // Since the map starts at portrait, we need to offset if this device's natural orientation
4226 // is landscape.
4227 int indexOffset = 0;
4228 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4229 indexOffset = 1;
4230 }
4231 return oriMap[(d.getRotation() + indexOffset) % 4];
4232 }
Adam Cohen446e9402011-09-15 18:21:21 -07004233
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004234 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004235 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004236 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004237 if (Utilities.ATLEAST_JB_MR2) {
4238 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4239 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004240 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4241 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004242 }
Winson Chung4b919f82012-05-01 10:44:08 -07004243 }
4244 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004245
Winson Chung4b919f82012-05-01 10:44:08 -07004246 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004247 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004248 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004249 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004250 } else {
4251 mHandler.postDelayed(new Runnable() {
4252 public void run() {
4253 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4254 }
4255 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004256 }
Winson Chung4b919f82012-05-01 10:44:08 -07004257 }
Winson Chung400438b2011-01-16 17:53:48 -08004258 }
4259
Winson Chunge43a1e72014-01-15 10:33:02 -08004260 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004261 if (mLauncherCallbacks != null) {
4262 return mLauncherCallbacks.isLauncherPreinstalled();
4263 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004264 PackageManager pm = getPackageManager();
4265 try {
4266 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4267 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4268 return true;
4269 } else {
4270 return false;
4271 }
4272 } catch (NameNotFoundException e) {
4273 e.printStackTrace();
4274 return false;
4275 }
4276 }
4277
Adam Cohenea90f832014-05-21 15:03:34 -07004278 /**
4279 * This method indicates whether or not we should suggest default wallpaper dimensions
4280 * when our wallpaper cropper was not yet used to set a wallpaper.
4281 */
4282 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004283 if (mLauncherCallbacks != null) {
4284 return mLauncherCallbacks.overrideWallpaperDimensions();
4285 }
Adam Cohenea90f832014-05-21 15:03:34 -07004286 return true;
4287 }
4288
Adam Cohen432609a2014-03-13 17:03:22 -07004289 /**
4290 * To be overridden by subclasses to indicate that there is an activity to launch
4291 * before showing the standard launcher experience.
4292 */
4293 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004294 if (mLauncherCallbacks != null) {
4295 return mLauncherCallbacks.hasFirstRunActivity();
4296 }
Adam Cohen432609a2014-03-13 17:03:22 -07004297 return false;
4298 }
4299
4300 /**
4301 * To be overridden by subclasses to launch any first run activity
4302 */
4303 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004304 if (mLauncherCallbacks != null) {
4305 return mLauncherCallbacks.getFirstRunActivity();
4306 }
Adam Cohen432609a2014-03-13 17:03:22 -07004307 return null;
4308 }
4309
Winson Chunga6945242014-01-08 14:04:34 -08004310 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004311 return !ActivityManager.isRunningInTestHarness() &&
4312 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004313 }
4314
Adam Cohen4a9423d2014-03-28 14:22:31 -07004315 protected boolean hasRunFirstRunActivity() {
4316 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4317 }
4318
Adam Cohen432609a2014-03-13 17:03:22 -07004319 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004320 if (shouldRunFirstRunActivity() &&
4321 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004322 Intent firstRunIntent = getFirstRunActivity();
4323 if (firstRunIntent != null) {
4324 startActivity(firstRunIntent);
4325 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004326 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004327 }
4328 }
Adam Cohen432609a2014-03-13 17:03:22 -07004329 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004330 }
4331
4332 private void markFirstRunActivityShown() {
4333 SharedPreferences.Editor editor = mSharedPrefs.edit();
4334 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4335 editor.apply();
4336 }
4337
Adam Cohen432609a2014-03-13 17:03:22 -07004338 /**
4339 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4340 * screen that must be displayed and dismissed.
4341 */
4342 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004343 if (mLauncherCallbacks != null) {
4344 return mLauncherCallbacks.hasDismissableIntroScreen();
4345 }
Adam Cohen432609a2014-03-13 17:03:22 -07004346 return false;
4347 }
4348
4349 /**
4350 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4351 */
4352 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004353 if (mLauncherCallbacks != null) {
4354 return mLauncherCallbacks.getIntroScreen();
4355 }
Adam Cohen432609a2014-03-13 17:03:22 -07004356 return null;
4357 }
4358
4359 /**
4360 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4361 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4362 */
4363 private boolean shouldShowIntroScreen() {
4364 return hasDismissableIntroScreen() &&
4365 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4366 }
4367
4368 protected void showIntroScreen() {
4369 View introScreen = getIntroScreen();
4370 changeWallpaperVisiblity(false);
4371 if (introScreen != null) {
4372 mDragLayer.showOverlayView(introScreen);
4373 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004374 if (mLauncherOverlayContainer != null) {
4375 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4376 }
Adam Cohen432609a2014-03-13 17:03:22 -07004377 }
4378
4379 public void dismissIntroScreen() {
4380 markIntroScreenDismissed();
4381 if (showFirstRunActivity()) {
4382 // We delay hiding the intro view until the first run activity is showing. This
4383 // avoids a blip.
4384 mWorkspace.postDelayed(new Runnable() {
4385 @Override
4386 public void run() {
4387 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004388 if (mLauncherOverlayContainer != null) {
4389 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4390 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004391 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004392 }
4393 }, ACTIVITY_START_DELAY);
4394 } else {
4395 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004396 if (mLauncherOverlayContainer != null) {
4397 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4398 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004399 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004400 }
4401 changeWallpaperVisiblity(true);
4402 }
4403
4404 private void markIntroScreenDismissed() {
4405 SharedPreferences.Editor editor = mSharedPrefs.edit();
4406 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4407 editor.apply();
4408 }
4409
Adam Cohen091440a2015-03-18 14:16:05 -07004410 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004411 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4412 // on the device, then we always show the first run cling experience (or if there is no
4413 // launcher2). Otherwise, we prompt the user upon started for migration
4414 LauncherClings launcherClings = new LauncherClings(this);
4415 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4416 if (mModel.canMigrateFromOldLauncherDb(this)) {
4417 launcherClings.showMigrationCling();
4418 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004419 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004420 }
4421 }
4422 }
4423
Winson Chunga6945242014-01-08 14:04:34 -08004424 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004425 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4426 if (mHotseat != null) mHotseat.setAlpha(1f);
4427 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004428 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4429 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004430 }
4431
4432 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004433 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4434 if (mHotseat != null) mHotseat.setAlpha(0f);
4435 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004436 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4437 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004438 }
4439
Winson Chung5ffd51d2015-06-25 11:36:50 -07004440 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004441 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004442 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004443 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004444 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004445 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004446 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4447 if (userHandle != null) {
4448 user = UserHandleCompat.fromUser(userHandle);
4449 }
4450 }
4451 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004452 }
4453
4454 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004455 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004456 if (user == null) {
4457 user = UserHandleCompat.myUserHandle();
4458 }
4459
4460 // Called from search suggestion, add the profile extra to the intent to ensure that we
4461 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004462 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004463 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004464 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004465 return null;
4466 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004467 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004468 }
4469
Winson Chung5ffd51d2015-06-25 11:36:50 -07004470 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004471 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4472 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004473 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004474 UserHandleCompat.myUserHandle());
4475 }
4476
Winson Chung5ffd51d2015-06-25 11:36:50 -07004477 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004478 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4479 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004480 mWorkspace.onExternalDragStartedWithItem(dragView);
4481 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004482 }
4483
Winson Chung5ffd51d2015-06-25 11:36:50 -07004484 protected void moveWorkspaceToDefaultScreen() {
4485 mWorkspace.moveToDefaultScreen(false);
4486 }
4487
Anjali Koppalf5d29132014-02-28 13:33:27 -08004488 @Override
4489 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004490 if (mLauncherCallbacks != null) {
4491 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4492 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004493 }
4494
Winson Chung80baf5a2010-08-09 16:03:15 -07004495 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004496 * Returns a FastBitmapDrawable with the icon, accurately sized.
4497 */
4498 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4499 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4500 d.setFilterBitmap(true);
4501 resizeIconDrawable(d);
4502 return d;
4503 }
4504
4505 /**
4506 * Resizes an icon drawable to the correct icon size.
4507 */
4508 public void resizeIconDrawable(Drawable icon) {
4509 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4510 }
4511
4512 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004513 * Prints out out state for debugging.
4514 */
4515 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004516 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004517 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004518 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4519 Log.d(TAG, "mRestoring=" + mRestoring);
4520 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004521 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004522 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004523 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004524
Daniel Sandler325dc232013-06-05 22:57:57 -04004525 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004526 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004527
4528 @Override
4529 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4530 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004531 synchronized (sDumpLogs) {
4532 writer.println(" ");
4533 writer.println("Debug logs: ");
4534 for (int i = 0; i < sDumpLogs.size(); i++) {
4535 writer.println(" " + sDumpLogs.get(i));
4536 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004537 }
Adam Cohen9211d422014-10-07 18:14:53 -07004538 if (mLauncherCallbacks != null) {
4539 mLauncherCallbacks.dump(prefix, fd, writer, args);
4540 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004541 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004542
4543 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004544 if (DEBUG_DUMP_LOG) {
4545 synchronized (sDumpLogs) {
4546 Log.d(TAG, "");
4547 Log.d(TAG, "*********************");
4548 Log.d(TAG, "Launcher debug logs: ");
4549 for (int i = 0; i < sDumpLogs.size(); i++) {
4550 Log.d(TAG, " " + sDumpLogs.get(i));
4551 }
4552 Log.d(TAG, "*********************");
4553 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004554 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004555 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004556 }
4557
4558 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004559 addDumpLog(tag, log, null, debugLog);
4560 }
4561
4562 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004563 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004564 if (e != null) {
4565 Log.d(tag, log, e);
4566 } else {
4567 Log.d(tag, log);
4568 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004569 }
Winson Chungede41292013-09-19 16:27:36 -07004570 if (DEBUG_DUMP_LOG) {
4571 sDateStamp.setTime(System.currentTimeMillis());
4572 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004573 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4574 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004575 }
Winson Chungede41292013-09-19 16:27:36 -07004576 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004577 }
4578
Adam Cohen59400422014-03-05 18:07:04 -08004579 public static CustomAppWidget getCustomAppWidget(String name) {
4580 return sCustomAppWidgets.get(name);
4581 }
4582
4583 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4584 return sCustomAppWidgets;
4585 }
4586
Winson Chungede41292013-09-19 16:27:36 -07004587 public void dumpLogsToLocalData() {
4588 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004589 new AsyncTask<Void, Void, Void>() {
4590 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004591 boolean success = false;
4592 sDateStamp.setTime(sRunStart);
4593 String FILENAME = sDateStamp.getMonth() + "-"
4594 + sDateStamp.getDay() + "_"
4595 + sDateStamp.getHours() + "-"
4596 + sDateStamp.getMinutes() + "_"
4597 + sDateStamp.getSeconds() + ".txt";
4598
4599 FileOutputStream fos = null;
4600 File outFile = null;
4601 try {
4602 outFile = new File(getFilesDir(), FILENAME);
4603 outFile.createNewFile();
4604 fos = new FileOutputStream(outFile);
4605 } catch (Exception e) {
4606 e.printStackTrace();
4607 }
4608 if (fos != null) {
4609 PrintWriter writer = new PrintWriter(fos);
4610
4611 writer.println(" ");
4612 writer.println("Debug logs: ");
4613 synchronized (sDumpLogs) {
4614 for (int i = 0; i < sDumpLogs.size(); i++) {
4615 writer.println(" " + sDumpLogs.get(i));
4616 }
4617 }
4618 writer.close();
4619 }
4620 try {
4621 if (fos != null) {
4622 fos.close();
4623 success = true;
4624 }
4625 } catch (IOException e) {
4626 e.printStackTrace();
4627 }
Michael Jurka43467462013-11-14 12:03:58 +01004628 return null;
Winson Chungede41292013-09-19 16:27:36 -07004629 }
Michael Jurka43467462013-11-14 12:03:58 +01004630 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004631 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004632 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004633}
Michael Jurka2763be32011-02-24 11:19:57 -08004634
Dan Sandlerd5024042014-01-09 15:01:33 -05004635interface DebugIntents {
4636 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4637 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004638}