blob: ffa98c5cf5be101df824e2db23c1bde1a95941fa [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;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070089import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080094import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070096
Sunny Goyal651077b2014-06-30 14:15:31 -070097import com.android.launcher3.DropTarget.DragObject;
98import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070099import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700106import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700107import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700108import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700109import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700110import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700111import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700112import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700113
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700115import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700121import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700124import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700125import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000126import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128/**
129 * Default launcher application.
130 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100131public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700132 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700133 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700134 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700135 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Daniel Sandlerf061f822013-06-27 13:59:36 -0400137 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700138 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700139 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700141 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700142
Dan Sandlerd5024042014-01-09 15:01:33 -0500143 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700148 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Michael Jurka8b805b12012-04-18 14:23:14 -0700150 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700151 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700152
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700153 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
154 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
155 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
156
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700157 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
158
Mathew Inwood876a8462013-06-14 14:12:41 +0100159 /**
160 * IntentStarter uses request codes starting with this. This must be greater than all activity
161 * request codes used internally.
162 */
163 protected static final int REQUEST_LAST = 100;
164
Michael Jurka0a457bf2012-11-19 14:05:05 -0800165 // To turn on these properties, type
166 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Winson Chung2672ff92012-05-04 16:22:30 -0700169 // The Intent extra that defines whether to ignore the launch animation
170 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400171 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
174 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700175 // Type: int
176 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700178 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
179 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
181 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700182 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700185 // Type: int
186 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
187 // Type: int
188 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000193 // Type: int[]
194 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Adam Cohen432609a2014-03-13 17:03:22 -0700196 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800197 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
198
Adam Cohen3ed316a2014-07-23 18:21:20 -0700199 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
200 static final String ACTION_FIRST_LOAD_COMPLETE =
201 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
202
Adam Cohen39a06042013-07-19 14:30:12 -0700203 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700204 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700205
Sunny Goyal594d76d2014-11-06 10:12:54 -0800206 private static final String QSB_WIDGET_ID = "qsb_widget_id";
207 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
208
Winson Chunga6945242014-01-08 14:04:34 -0800209 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
210
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700212 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
213
Adam Cohen091440a2015-03-18 14:16:05 -0700214 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700215 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700216
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700217 private boolean mIsSafeModeEnabled;
218
Adam Cohenc2d6e892014-10-16 09:49:24 -0700219 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
220 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700221 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700222
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700224 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
225 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700226 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000228 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
229 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
230
Winson Chunga2413752012-04-03 14:22:34 -0700231 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700232 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
233 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700235
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800236 private final BroadcastReceiver mCloseSystemDialogsReceiver
237 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private LayoutInflater mInflater;
240
Adam Cohen091440a2015-03-18 14:16:05 -0700241 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700242 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800243 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800245 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700246 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700247
Sunny Goyalffe83f12014-08-14 17:39:34 -0700248 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700249 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700250
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700251 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800252 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800253 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700254
Michael Jurka0280c3b2010-09-17 15:00:07 -0700255 private int[] mTmpAddItemCellCoordinates = new int[2];
256
Sunny Goyal316490e2015-06-02 09:38:28 -0700257 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800258 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700259
Michael Jurka838a4ca2011-02-07 13:33:06 -0800260 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700261 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700262
Winson Chungc51db6a2011-10-05 11:44:49 -0700263 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700264
265 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700266 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700267
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700268 // Main container view and the model for the widget tray screen.
269 @Thunk WidgetsContainerView mWidgetsView;
270 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700271
Winson Chungf0ea4d32011-06-06 14:27:16 -0700272 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800273 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700276 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
277 // scroll issues (because the workspace may not have been measured yet) and extra work.
278 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
279 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
281 private SpannableStringBuilder mDefaultKeySsb = null;
282
Adam Cohen091440a2015-03-18 14:16:05 -0700283 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800285 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 private boolean mRestoring;
287 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700288 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289
Michael Jurka1e2f4652013-07-08 18:03:46 -0700290 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700291 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
292
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700295 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800296 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700297 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800298 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299
Sunny Goyale2df0622015-04-24 11:27:00 -0700300 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700301
Adam Cohen61f560d2013-09-30 15:58:20 -0700302 private View.OnTouchListener mHapticFeedbackTouchListener;
303
Adam Cohended9f8d2010-11-03 13:25:16 -0700304 // Related to the auto-advancing of widgets
305 private final int ADVANCE_MSG = 1;
306 private final int mAdvanceInterval = 20000;
307 private final int mAdvanceStagger = 250;
308 private long mAutoAdvanceSentTime;
309 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700310 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700311 new HashMap<View, AppWidgetProviderInfo>();
312
Winson Chung400438b2011-01-16 17:53:48 -0800313 // Determines how long to wait after a rotation before restoring the screen orientation to
314 // match the sensor state.
315 private final int mRestoreScreenOrientationDelay = 500;
316
Adam Cohen091440a2015-03-18 14:16:05 -0700317 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700318
Adam Cohen1462de32012-07-24 22:34:36 -0700319 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700320 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700321
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700322 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700323 static Date sDateStamp = new Date();
324 static DateFormat sDateFormat =
325 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
326 static long sRunStart = System.currentTimeMillis();
327 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700328
Winson Chung46353de2012-02-16 14:05:10 -0800329 // We only want to get the SharedPreferences once since it does an FS stat each time we get
330 // it from the context.
331 private SharedPreferences mSharedPrefs;
332
Winson Chungf0c6ae02012-03-21 16:10:31 -0700333 // Holds the page that we need to animate to, and the icon views that we need to animate up
334 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700335 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700336 private Bitmap mFolderIconBitmap;
337 private Canvas mFolderIconCanvas;
338 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700339
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700340 private DeviceProfile mDeviceProfile;
341
Winson Chung13eb5272015-05-11 16:30:13 -0700342 // This is set to the view that launched the activity that navigated the user away from
343 // launcher. Since there is no callback for when the activity has finished launching, enable
344 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800345 private BubbleTextView mWaitingForResume;
346
Adam Cohen59400422014-03-05 18:07:04 -0800347 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
348 new HashMap<String, CustomAppWidget>();
349
350 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
351 static {
352 if (ENABLE_CUSTOM_WIDGET_TEST) {
353 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
354 }
355 }
356
Adam Cohen091440a2015-03-18 14:16:05 -0700357 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700358 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700359 if (mWorkspace != null) {
360 mWorkspace.buildPageHardwareLayers();
361 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700362 }
363 };
364
Adam Cohendb364c32014-05-20 14:23:40 -0700365 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366
Adam Cohen091440a2015-03-18 14:16:05 -0700367 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368 int requestCode;
369 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700370 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700371 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372 int cellX;
373 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700374 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800375 }
376
Daniel Sandlerff02d492013-08-05 02:12:05 -0400377 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700378 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700379 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400380
381 @Thunk void setOrientation() {
382 if (mRotationEnabled) {
383 unlockScreenOrientation(true);
384 } else {
385 setRequestedOrientation(
386 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700387 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400388 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700389
Sunny Goyal7779d622015-06-11 16:18:39 -0700390 private Runnable mUpdateOrientationRunnable = new Runnable() {
391 public void run() {
392 setOrientation();
393 }
394 };
395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 @Override
397 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700398 if (DEBUG_STRICT_MODE) {
399 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
400 .detectDiskReads()
401 .detectDiskWrites()
402 .detectNetwork() // or .detectAll() for all detectable problems
403 .penaltyLog()
404 .build());
405 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
406 .detectLeakedSqlLiteObjects()
407 .detectLeakedClosableObjects()
408 .penaltyLog()
409 .penaltyDeath()
410 .build());
411 }
412
Adam Cohen9211d422014-10-07 18:14:53 -0700413 if (mLauncherCallbacks != null) {
414 mLauncherCallbacks.preOnCreate();
415 }
416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400418
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400419 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400420 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700421
Adam Cohen2e6da152015-05-06 11:42:25 -0700422 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700423 mDeviceProfile = getResources().getConfiguration().orientation
424 == Configuration.ORIENTATION_LANDSCAPE ?
425 app.getInvariantDeviceProfile().landscapeProfile
426 : app.getInvariantDeviceProfile().portraitProfile;
427
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400428 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700429 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700430 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800431 mModel = app.setLauncher(this);
432 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700433
Joe Onorato41a12d22009-10-31 18:30:00 -0400434 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 mInflater = getLayoutInflater();
Winson Chung006ee262015-08-03 14:40:11 -0700436 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Daniel Sandlerff02d492013-08-05 02:12:05 -0400438 mStats = new Stats(this);
439
Sunny Goyalffe83f12014-08-14 17:39:34 -0700440 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700441
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700442 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
443 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700444
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700445 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
446 // this also ensures that any synchronous binding below doesn't re-trigger another
447 // LauncherModel load.
448 mPaused = false;
449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200451 android.os.Debug.startMethodTracing(
452 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700458 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459
Joe Onorato7c312c12009-08-13 21:36:53 -0700460 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 mSavedState = savedInstanceState;
463 restoreState(mSavedState);
464
465 if (PROFILE_STARTUP) {
466 android.os.Debug.stopMethodTracing();
467 }
468
469 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700470 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 // If the user leaves launcher, then we should just load items asynchronously when
472 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700473 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700474 } else {
475 // We only load the page synchronously if the user rotates (or triggers a
476 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700477 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
480
481 // For handling default keys
482 mDefaultKeySsb = new SpannableStringBuilder();
483 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800484
485 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
486 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800487
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700488 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
489 // In case we are on a device with locked rotation, we should look at preferences to check
490 // if the user has specifically allowed rotation.
491 if (!mRotationEnabled) {
Rahul Chaturvedie863fed2015-06-15 14:09:42 -0400492 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext(), false);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700493 }
494
495 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
496 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400497 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700498
Adam Cohen9211d422014-10-07 18:14:53 -0700499 if (mLauncherCallbacks != null) {
500 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700501 if (mLauncherCallbacks.hasLauncherOverlay()) {
502 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700503 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
504 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
505 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700506 mWorkspace.setLauncherOverlay(mLauncherOverlay);
507 }
Adam Cohen9211d422014-10-07 18:14:53 -0700508 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700509
510 if (shouldShowIntroScreen()) {
511 showIntroScreen();
512 } else {
513 showFirstRunActivity();
514 showFirstRunClings();
515 }
Adam Cohen9211d422014-10-07 18:14:53 -0700516 }
517
Sunny Goyal7779d622015-06-11 16:18:39 -0700518 @Override
519 public void onSettingsChanged(String settings, boolean value) {
520 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
521 mRotationEnabled = value;
522 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
523 mUpdateOrientationRunnable.run();
524 }
525 }
526 }
527
Adam Cohen9211d422014-10-07 18:14:53 -0700528 private LauncherCallbacks mLauncherCallbacks;
529
530 public void onPostCreate(Bundle savedInstanceState) {
531 super.onPostCreate(savedInstanceState);
532 if (mLauncherCallbacks != null) {
533 mLauncherCallbacks.onPostCreate(savedInstanceState);
534 }
535 }
536
537 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
538 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700539 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700540 private boolean mWorkspaceImportanceStored = false;
541 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 private int mWorkspaceImportanceForAccessibility =
543 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
544 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
545
546 @Override
547 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700548 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700549 return;
550 }
551 // The underlying workspace and hotseat are temporarily suppressed by the search
552 // overlay. So they sholudn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700553 if (mWorkspace != null) {
554 mWorkspaceImportanceForAccessibility =
555 mWorkspace.getImportantForAccessibility();
556 mWorkspace.setImportantForAccessibility(
557 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
558 mWorkspaceImportanceStored = true;
559 }
560 if (mHotseat != null) {
561 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
562 mHotseat.setImportantForAccessibility(
563 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
564 mHotseatImportanceStored = true;
565 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700566 }
567
568 @Override
569 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700570 if (mWorkspaceImportanceStored && mWorkspace != null) {
571 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
572 }
573 if (mHotseatImportanceStored && mHotseat != null) {
574 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
575 }
576 mWorkspaceImportanceStored = false;
577 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700578 }
579 });
Adam Cohen9211d422014-10-07 18:14:53 -0700580 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700581 }
582
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700583 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700584 public void onLauncherProviderChange() {
585 if (mLauncherCallbacks != null) {
586 mLauncherCallbacks.onLauncherProviderChange();
587 }
588 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700589
Winson Chungef7f8742015-06-04 17:18:17 -0700590 /**
591 * Updates the bounds of all the overlays to match the new fixed bounds.
592 */
593 public void updateOverlayBounds(Rect newBounds) {
594 mAppsView.setSearchBarBounds(newBounds);
595 mWidgetsView.setSearchBarBounds(newBounds);
596 }
597
Adam Cohen9211d422014-10-07 18:14:53 -0700598 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700599 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700600 if (mLauncherCallbacks != null) {
601 return mLauncherCallbacks.hasCustomContentToLeft();
602 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700603 return false;
604 }
605
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500607 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600608 * {@link #addToCustomContentPage}. This will only be invoked if
609 * {@link #hasCustomContentToLeft()} is {@code true}.
610 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500611 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700612 if (mLauncherCallbacks != null) {
613 mLauncherCallbacks.populateCustomContentContainer();
614 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600615 }
616
617 /**
618 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
619 * ensure the custom content page is added or removed if necessary.
620 */
621 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600622 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600623 // Not bound yet, wait for bindScreens to be called.
624 return;
625 }
626
627 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
628 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500629 mWorkspace.createCustomContentContainer();
630 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600631 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
632 mWorkspace.removeCustomContentPage();
633 }
634 }
635
Anton Hanssona2f665f2013-09-26 12:18:32 +0100636 public Stats getStats() {
637 return mStats;
638 }
639
Bjorn Bringertc459e522013-06-07 19:36:01 +0100640 public LayoutInflater getInflater() {
641 return mInflater;
642 }
643
Hyunyoung Song3f471442015-04-08 19:01:34 -0700644 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700645 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
646 // that is subsequently removed from the workspace in startBinding().
647 return !mModel.isLoadingWorkspace();
648 }
649
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800650 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000651 public static int generateViewId() {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700652 if (Utilities.ATLEAST_JB_MR1) {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100653 return View.generateViewId();
654 } else {
655 // View.generateViewId() is not available. The following fallback logic is a copy
656 // of its implementation.
657 for (;;) {
658 final int result = sNextGeneratedId.get();
659 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
660 int newValue = result + 1;
661 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
662 if (sNextGeneratedId.compareAndSet(result, newValue)) {
663 return result;
664 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000665 }
666 }
667 }
668
669 public int getViewIdForItem(ItemInfo info) {
670 // This cast is safe given the > 2B range for int.
671 int itemId = (int) info.id;
672 if (mItemIdToViewId.containsKey(itemId)) {
673 return mItemIdToViewId.get(itemId);
674 }
675 int viewId = generateViewId();
676 mItemIdToViewId.put(itemId, viewId);
677 return viewId;
678 }
679
680 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700681 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
682 * a configuration step, this allows the proper animations to run after other transitions.
683 */
Adam Cohendb364c32014-05-20 14:23:40 -0700684 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700685 long screenId = args.screenId;
686 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
687 // When the screen id represents an actual screen (as opposed to a rank) we make sure
688 // that the drop page actually exists.
689 screenId = ensurePendingDropLayoutExists(args.screenId);
690 }
Adam Cohendb364c32014-05-20 14:23:40 -0700691
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800692 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700694 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700695 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700696 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700698 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700699 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700700 case REQUEST_RECONFIGURE_APPWIDGET:
701 completeRestoreAppWidget(args.appWidgetId);
702 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 }
Michael Jurka27614d22012-04-02 06:40:22 -0700704 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
705 // if you turned the screen off and then back while in All Apps, Launcher would not
706 // return to the workspace. Clearing mAddInfo.container here fixes this issue
707 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700708 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800709 }
710
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800711 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700713 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700714 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700715 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800716 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700717
Adam Cohenad4e15c2013-10-17 16:21:35 -0700718 Runnable exitSpringLoaded = new Runnable() {
719 @Override
720 public void run() {
721 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
722 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
723 }
724 };
725
Michael Jurka8b805b12012-04-18 14:23:14 -0700726 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700727 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700728 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
730 if (resultCode == RESULT_CANCELED) {
731 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700732 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700733 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800735 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700736 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700737
738 // When the user has granted permission to bind widgets, we should check to see if
739 // we can inflate the default search bar widget.
740 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 }
742 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200743 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
744 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700745 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200746 }
747 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700748 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200749
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700751 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700752
Adam Cohendb364c32014-05-20 14:23:40 -0700753 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 // We have special handling for widgets
755 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800756 final int appWidgetId;
757 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
758 : -1;
759 if (widgetId < 0) {
760 appWidgetId = pendingAddWidgetId;
761 } else {
762 appWidgetId = widgetId;
763 }
764
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800766 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700767 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
768 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 result = RESULT_CANCELED;
770 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700771 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700772 @Override
773 public void run() {
774 exitSpringLoadedDragModeDelayed(false, 0, null);
775 }
776 };
Adam Cohendb364c32014-05-20 14:23:40 -0700777 if (workspaceLocked) {
778 // No need to remove the empty screen if we're mid-binding, as the
779 // the bind will not add the empty screen.
780 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
781 } else {
782 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
783 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
784 }
Winson Chung5aaab772012-04-27 15:24:02 -0700785 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700786 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700787 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
788 // When the screen id represents an actual screen (as opposed to a rank)
789 // we make sure that the drop page actually exists.
790 mPendingAddInfo.screenId =
791 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
792 }
Adam Cohendb364c32014-05-20 14:23:40 -0700793 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
794
795 dropLayout.setDropPending(true);
796 final Runnable onComplete = new Runnable() {
797 @Override
798 public void run() {
799 completeTwoStageWidgetDrop(resultCode, appWidgetId);
800 dropLayout.setDropPending(false);
801 }
802 };
803 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
804 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
805 } else {
806 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
807 mPendingAddInfo);
808 sPendingAddItem = args;
809 }
Winson Chung5aaab772012-04-27 15:24:02 -0700810 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800811 return;
812 }
813
Sunny Goyalff572272014-07-23 13:58:07 -0700814 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
815 if (resultCode == RESULT_OK) {
816 // Update the widget view.
817 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
818 pendingAddWidgetId, mPendingAddInfo);
819 if (workspaceLocked) {
820 sPendingAddItem = args;
821 } else {
822 completeAdd(args);
823 }
824 }
825 // Leave the widget in the pending state if the user canceled the configure.
826 return;
827 }
828
Sunny Goyalaad90582015-07-09 14:22:50 -0700829 if (requestCode == REQUEST_CREATE_SHORTCUT) {
830 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
831 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
832 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
833 mPendingAddInfo);
834 if (isWorkspaceLocked()) {
835 sPendingAddItem = args;
836 } else {
837 completeAdd(args);
838 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
839 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
840 }
841 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700842 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
843 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800846 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800847
848 }
849
850 @Override
851 protected void onActivityResult(
852 final int requestCode, final int resultCode, final Intent data) {
853 handleActivityResult(requestCode, resultCode, data);
854 if (mLauncherCallbacks != null) {
855 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
856 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800857 }
858
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600859 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700860 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600861 int[] grantResults) {
862 if (mLauncherCallbacks != null) {
863 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
864 grantResults);
865 }
866 }
867
Adam Cohendb364c32014-05-20 14:23:40 -0700868 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
869 appWidgetId, ItemInfo info) {
870 PendingAddArguments args = new PendingAddArguments();
871 args.requestCode = requestCode;
872 args.intent = data;
873 args.container = info.container;
874 args.screenId = info.screenId;
875 args.cellX = info.cellX;
876 args.cellY = info.cellY;
877 args.appWidgetId = appWidgetId;
878 return args;
879 }
880
881 /**
882 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
883 *
884 * @param screenId the screen id to check
885 * @return the new screen, or screenId if it exists
886 */
887 private long ensurePendingDropLayoutExists(long screenId) {
888 CellLayout dropLayout =
889 (CellLayout) mWorkspace.getScreenWithId(screenId);
890 if (dropLayout == null) {
891 // it's possible that the add screen was removed because it was
892 // empty and a re-bind occurred
893 mWorkspace.addExtraEmptyScreen();
894 return mWorkspace.commitExtraEmptyScreen();
895 } else {
896 return screenId;
897 }
898 }
899
Adam Cohen091440a2015-03-18 14:16:05 -0700900 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700901 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700902 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 Runnable onCompleteRunnable = null;
904 int animationType = 0;
905
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700906 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 if (resultCode == RESULT_OK) {
908 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
909 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700910 mPendingAddWidgetInfo);
911 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 onCompleteRunnable = new Runnable() {
913 @Override
914 public void run() {
915 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700916 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700917 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
918 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 }
920 };
921 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800922 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700925 if (mDragLayer.getAnimatedView() != null) {
926 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
927 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
928 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700929 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700930 // The animated view may be null in the case of a rotation during widget configuration
931 onCompleteRunnable.run();
932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 }
934
935 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700936 protected void onStop() {
937 super.onStop();
938 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700939
940 if (mLauncherCallbacks != null) {
941 mLauncherCallbacks.onStop();
942 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 }
944
945 @Override
946 protected void onStart() {
947 super.onStart();
948 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700949
950 if (mLauncherCallbacks != null) {
951 mLauncherCallbacks.onStart();
952 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 }
954
955 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200957 long startTime = 0;
958 if (DEBUG_RESUME_TIME) {
959 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400960 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200961 }
Adam Cohen9211d422014-10-07 18:14:53 -0700962
963 if (mLauncherCallbacks != null) {
964 mLauncherCallbacks.preOnResume();
965 }
966
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700968
Winson Chung4a2afa32012-07-19 14:53:05 -0700969 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700970 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700971 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800972 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700973 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700974 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700975 // view after launching an app, as they may be depending on the UI to be static to
976 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700977 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700978 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800979 } else if (mOnResumeState == State.WIDGETS) {
980 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700981 }
982 mOnResumeState = State.NONE;
983
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700984 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700985 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
986 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700987
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800988 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700989 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700990 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700991 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700993 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700995 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200996 // We might have postponed some bind calls until onResume (see waitUntilResume) --
997 // execute them here
998 long startTimeCallbacks = 0;
999 if (DEBUG_RESUME_TIME) {
1000 startTimeCallbacks = System.currentTimeMillis();
1001 }
1002
Michael Jurka1e2f4652013-07-08 18:03:46 -07001003 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1004 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001005 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001006 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001007 if (DEBUG_RESUME_TIME) {
1008 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1009 (System.currentTimeMillis() - startTimeCallbacks));
1010 }
Michael Jurka447bf842013-05-15 14:52:15 +02001011 }
Michael Jurka54554252013-08-01 12:52:23 +02001012 if (mOnResumeCallbacks.size() > 0) {
1013 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1014 mOnResumeCallbacks.get(i).run();
1015 }
1016 mOnResumeCallbacks.clear();
1017 }
Winson Chunge4e50662012-01-23 14:45:13 -08001018
1019 // Reset the pressed state of icons that were locked in the press state while activities
1020 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001021 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001022 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001023 mWaitingForResume.setStayPressed(false);
1024 }
Winson Chung82963d52013-10-09 11:20:57 -07001025
Adam Cohen06dff352012-06-01 17:17:08 -07001026 // It is possible that widgets can receive updates while launcher is not in the foreground.
1027 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1028 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1029 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001030 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001031 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001032
Michael Jurka447bf842013-05-15 14:52:15 +02001033 if (DEBUG_RESUME_TIME) {
1034 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1035 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001036
1037 if (mWorkspace.getCustomContentCallbacks() != null) {
1038 // If we are resuming and the custom content is the current page, we call onShow().
1039 // It is also poassible that onShow will instead be called slightly after first layout
1040 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1041 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001042 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001043 }
1044 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001045 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001046 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001047
Sunny Goyal756adbc2015-04-16 15:20:51 -07001048 if (!isWorkspaceLoading()) {
1049 // Process any items that were added while Launcher was away.
1050 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1051 }
Adam Cohen9211d422014-10-07 18:14:53 -07001052
1053 if (mLauncherCallbacks != null) {
1054 mLauncherCallbacks.onResume();
1055 }
Adam Cohen06dff352012-06-01 17:17:08 -07001056 }
1057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001060 // Ensure that items added to Launcher are queued until Launcher returns
1061 InstallShortcutReceiver.enableInstallQueue();
1062
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001063 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001064 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001065 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001066 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001067
1068 // We call onHide() aggressively. The custom content callbacks should be able to
1069 // debounce excess onHide calls.
1070 if (mWorkspace.getCustomContentCallbacks() != null) {
1071 mWorkspace.getCustomContentCallbacks().onHide();
1072 }
Romain Guycbb89e42009-06-08 15:52:54 -07001073
Adam Cohen9211d422014-10-07 18:14:53 -07001074 if (mLauncherCallbacks != null) {
1075 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001076 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001077 }
1078
1079 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001080 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1081 // by a onResume or by scrolling otherwise.
1082 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001083
1084 // Custom content is completely hidden
1085 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001086
1087 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1088 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001089
1090 // Indicates whether the user is allowed to scroll away from the custom content.
1091 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
1093
Adam Cohenc2d6e892014-10-16 09:49:24 -07001094 public interface LauncherOverlay {
1095
1096 /**
1097 * Touch interaction leading to overscroll has begun
1098 */
1099 public void onScrollInteractionBegin();
1100
1101 /**
1102 * Touch interaction related to overscroll has ended
1103 */
1104 public void onScrollInteractionEnd();
1105
1106 /**
1107 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1108 * screen (or in the case of RTL, the rightmost screen).
1109 */
1110 public void onScrollChange(int progress, boolean rtl);
1111
1112 /**
1113 * Screen has stopped scrolling
1114 */
1115 public void onScrollSettled();
1116
1117 /**
1118 * This method can be called by the Launcher in order to force the LauncherOverlay
1119 * to exit fully immersive mode.
1120 */
1121 public void forceExitFullImmersion();
1122 }
1123
Jun Mukai8af0cd82015-05-12 12:32:12 -07001124 public interface LauncherSearchCallbacks {
1125 /**
1126 * Called when the search overlay is shown.
1127 */
1128 public void onSearchOverlayOpened();
1129
1130 /**
1131 * Called when the search overlay is dismissed.
1132 */
1133 public void onSearchOverlayClosed();
1134 }
1135
Adam Cohenc2d6e892014-10-16 09:49:24 -07001136 public interface LauncherOverlayCallbacks {
1137 /**
1138 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1139 * however it doesn't modify any state within the launcher.
1140 */
1141 public boolean canEnterFullImmersion();
1142
1143 /**
1144 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1145 * eg. by occupying the full screen and handling all touch events.
1146 *
1147 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1148 * case, Launcher will modify any necessary state and assumes the overlay is
1149 * handling all interaction. If false, the LauncherOverlay should cancel any
1150 *
1151 */
1152 public boolean enterFullImmersion();
1153
1154 /**
1155 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1156 * full control over UI and state.
1157 */
1158 public void exitFullImmersion();
1159 }
1160
1161 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1162
1163 @Override
1164 public boolean canEnterFullImmersion() {
1165 return mState == State.WORKSPACE;
1166 }
1167
1168 @Override
1169 public boolean enterFullImmersion() {
1170 if (mState == State.WORKSPACE) {
1171 // When fully immersed, disregard any touches which fall through.
1172 mDragLayer.setBlockTouch(true);
1173 return true;
1174 }
1175 return false;
1176 }
1177
1178 @Override
1179 public void exitFullImmersion() {
1180 mDragLayer.setBlockTouch(false);
1181 }
1182 }
1183
Jorim Jaggid017f882014-01-14 17:08:48 -08001184 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001185 if (mLauncherCallbacks != null) {
1186 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001187 } else {
1188 // On devices with a locked orientation, we will at least have the allow rotation
1189 // setting.
1190 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001191 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001192 }
1193
Adam Cohen9211d422014-10-07 18:14:53 -07001194 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001195 CustomContentCallbacks callbacks, String description) {
1196 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001197 }
1198
Adam Cohenedb40762013-07-18 16:45:45 -07001199 // The custom content needs to offset its content to account for the QSB
1200 public int getTopOffsetForCustomContent() {
1201 return mWorkspace.getPaddingTop();
1202 }
1203
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001204 @Override
1205 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001207 if (mModel.isCurrentCallbacks(this)) {
1208 mModel.stopLoader();
1209 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001210 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1211
Romain Guy13c2e7b2010-03-10 19:45:00 -08001212 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001213 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001214
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001215 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001216 @Override
1217 public void onWindowFocusChanged(boolean hasFocus) {
1218 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001219 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001220
1221 if (mLauncherCallbacks != null) {
1222 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1223 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001224 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 private boolean acceptFilter() {
1227 final InputMethodManager inputManager = (InputMethodManager)
1228 getSystemService(Context.INPUT_METHOD_SERVICE);
1229 return !inputManager.isFullscreenMode();
1230 }
1231
1232 @Override
1233 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001234 final int uniChar = event.getUnicodeChar();
1235 final boolean handled = super.onKeyDown(keyCode, event);
1236 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1237 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1239 keyCode, event);
1240 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001241 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001242 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001243 // showSearchDialog()
1244 // If there are multiple keystrokes before the search dialog takes focus,
1245 // onSearchRequested() will be called for every keystroke,
1246 // but it is idempotent, so it's fine.
1247 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
1249 }
1250
Joe Onorato8a9625e2010-01-28 15:55:35 -08001251 // Eat the long press event so the keyboard doesn't come up.
1252 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1253 return true;
1254 }
1255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 return handled;
1257 }
1258
Karl Rosaen138a0412009-04-23 19:00:21 -07001259 private String getTypedText() {
1260 return mDefaultKeySsb.toString();
1261 }
1262
1263 private void clearTypedText() {
1264 mDefaultKeySsb.clear();
1265 mDefaultKeySsb.clearSpans();
1266 Selection.setSelection(mDefaultKeySsb, 0);
1267 }
1268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001270 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1271 * State
1272 */
1273 private static State intToState(int stateOrdinal) {
1274 State state = State.WORKSPACE;
1275 final State[] stateValues = State.values();
1276 for (int i = 0; i < stateValues.length; i++) {
1277 if (stateValues[i].ordinal() == stateOrdinal) {
1278 state = stateValues[i];
1279 break;
1280 }
1281 }
1282 return state;
1283 }
1284
1285 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 * Restores the previous state, if it exists.
1287 *
1288 * @param savedState The previous state.
1289 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001290 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 private void restoreState(Bundle savedState) {
1292 if (savedState == null) {
1293 return;
1294 }
1295
Michael Jurka883f55b2010-10-21 15:47:14 -07001296 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001297 if (state == State.APPS || state == State.WIDGETS) {
1298 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001299 }
1300
Adam Cohen21cd0022013-10-09 18:57:02 -07001301 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1302 PagedView.INVALID_RESTORE_PAGE);
1303 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001304 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
1306
Winson Chung3d503fb2011-07-13 17:25:49 -07001307 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001308 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001309
Winson Chung3d503fb2011-07-13 17:25:49 -07001310 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1311 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001312 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001313 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1314 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001315 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1316 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001317 AppWidgetProviderInfo info = savedState.getParcelable(
1318 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001319 mPendingAddWidgetInfo = info == null ?
1320 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1321
Adam Cohen4637b5a2013-11-04 18:21:24 -08001322 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001323 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 mRestoring = true;
1325 }
1326
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001327 mItemIdToViewId = (HashMap<Integer, Integer>)
1328 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
1330
1331 /**
1332 * Finds all the views we need and configure them properly.
1333 */
1334 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001335 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001336
Craig Mautner360310b2012-10-26 15:13:08 -07001337 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001338 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001339 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1340 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001341 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001342 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001343
John Spurlock77e1f472013-09-11 10:09:51 -04001344 mLauncherView.setSystemUiVisibility(
1345 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001346 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347
Winson Chung4c98d922011-05-31 16:50:48 -07001348 // Setup the drag layer
1349 mDragLayer.setup(this, dragController);
1350
Winson Chung3d503fb2011-07-13 17:25:49 -07001351 // Setup the hotseat
1352 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1353 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001354 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001355 }
1356
Jorim Jaggid017f882014-01-14 17:08:48 -08001357 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001358 mWidgetsButton = findViewById(R.id.widget_button);
1359 mWidgetsButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001360 @Override
1361 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001362 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001363 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001364 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001365 }
1366 });
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001367 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen61f560d2013-09-30 15:58:20 -07001368
1369 View wallpaperButton = findViewById(R.id.wallpaper_button);
1370 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001371 @Override
1372 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001373 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001374 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001375 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001376 }
1377 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001378 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1379
1380 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001381 if (hasSettings()) {
1382 settingsButton.setOnClickListener(new OnClickListener() {
1383 @Override
1384 public void onClick(View arg0) {
1385 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001386 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001387 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001388 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001389 });
1390 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1391 } else {
1392 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001393 }
1394
Adam Cohendbdff6b2013-09-18 19:09:15 -07001395 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001396
Winson Chung4c98d922011-05-31 16:50:48 -07001397 // Setup the workspace
1398 mWorkspace.setHapticFeedbackEnabled(false);
1399 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001400 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001401 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001402
Winson Chungf0ea4d32011-06-06 14:27:16 -07001403 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001404 mSearchDropTargetBar = (SearchDropTargetBar)
1405 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001406
Winson Chungef7f8742015-06-04 17:18:17 -07001407 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001408 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001409 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001410 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1411 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1412 } else {
1413 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1414 }
Craig Mautner360310b2012-10-26 15:13:08 -07001415
Winson Chung3d503fb2011-07-13 17:25:49 -07001416 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001417 dragController.setDragScoller(mWorkspace);
1418 dragController.setScrollView(mDragLayer);
1419 dragController.setMoveTarget(mWorkspace);
1420 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001421 if (mSearchDropTargetBar != null) {
1422 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001423 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001424 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001425
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001426 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001427 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001428 mWeightWatcher = new WeightWatcher(this);
1429 mWeightWatcher.setAlpha(0.5f);
1430 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001431 new FrameLayout.LayoutParams(
1432 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001433 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001434 Gravity.BOTTOM)
1435 );
Adam Cohen39a06042013-07-19 14:30:12 -07001436
1437 boolean show = shouldShowWeightWatcher();
1438 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001439 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 }
1441
1442 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001443 * Sets the all apps button. This method is called from {@link Hotseat}.
1444 */
1445 public void setAllAppsButton(View allAppsButton) {
1446 mAllAppsButton = allAppsButton;
1447 }
1448
1449 public View getAllAppsButton() {
1450 return mAllAppsButton;
1451 }
1452
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001453 public View getWidgetsButton() {
1454 return mWidgetsButton;
1455 }
1456
Anjali Koppal5ad44842014-03-10 20:34:39 -07001457 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 * Creates a view representing a shortcut.
1459 *
1460 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001462 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001463 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
1465
1466 /**
1467 * Creates a view representing a shortcut inflated from the specified resource.
1468 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 * @param parent The group the shortcut belongs to.
1470 * @param info The data structure describing the shortcut.
1471 *
1472 * @return A View inflated from layoutResId.
1473 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001474 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001475 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001476 parent, false);
1477 favorite.applyFromShortcutInfo(info, mIconCache);
1478 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001480 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 return favorite;
1482 }
1483
1484 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * Add a shortcut to the workspace.
1486 *
1487 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001489 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001490 int cellY) {
1491 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001492 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001493
Sunny Goyal5c97f512015-05-19 16:03:28 -07001494 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001495 if (info == null) {
1496 return;
1497 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001498 final View view = createShortcut(info);
1499
Sunny Goyal5c97f512015-05-19 16:03:28 -07001500 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001501 // First we check if we already know the exact location where we want to add this item.
1502 if (cellX >= 0 && cellY >= 0) {
1503 cellXY[0] = cellX;
1504 cellXY[1] = cellY;
1505 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001506
1507 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001508 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001509 true, null,null)) {
1510 return;
1511 }
1512 DragObject dragObject = new DragObject();
1513 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001514 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1515 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 return;
1517 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001518 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001519 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001520 }
1521
1522 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001523 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001524 return;
1525 }
1526
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001527 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528
1529 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001530 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001531 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
1533 }
1534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001536 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001538 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 */
Adam Cohen091440a2015-03-18 14:16:05 -07001540 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001541 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1542
1543 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001544 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001545 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1546 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001547 }
Romain Guycbb89e42009-06-08 15:52:54 -07001548
Adam Cohen59400422014-03-05 18:07:04 -08001549 if (appWidgetInfo.isCustomWidget) {
1550 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001551 }
1552
Adam Cohen59400422014-03-05 18:07:04 -08001553 LauncherAppWidgetInfo launcherInfo;
1554 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1555 launcherInfo.spanX = info.spanX;
1556 launcherInfo.spanY = info.spanY;
1557 launcherInfo.minSpanX = info.minSpanX;
1558 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001559 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001562 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563
1564 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001565 if (hostView == null) {
1566 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001567 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1568 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 } else {
1570 // The AppWidgetHostView has already been inflated and instantiated
1571 launcherInfo.hostView = hostView;
1572 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001574 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001575 launcherInfo.notifyWidgetSizeChanged(this);
1576
Adam Cohen59400422014-03-05 18:07:04 -08001577 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1578 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001579
1580 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001582 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
Romain Guycbb89e42009-06-08 15:52:54 -07001584
Adam Cohended9f8d2010-11-03 13:25:16 -07001585 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1586 @Override
1587 public void onReceive(Context context, Intent intent) {
1588 final String action = intent.getAction();
1589 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1590 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001591 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001592 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001593
Winson Chungc100e8e2011-08-09 16:02:43 -07001594 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001595 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001596 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001597 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001598 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001599 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001600 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1601 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001602 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001603 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1604 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001605 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001606 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1607 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1608 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001609 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001610 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1611 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001612 }
1613 }
1614 };
1615
1616 @Override
1617 public void onAttachedToWindow() {
1618 super.onAttachedToWindow();
1619
1620 // Listen for broadcasts related to user-presence
1621 final IntentFilter filter = new IntentFilter();
1622 filter.addAction(Intent.ACTION_SCREEN_OFF);
1623 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001624 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001625 if (ENABLE_DEBUG_INTENTS) {
1626 filter.addAction(DebugIntents.DELETE_DATABASE);
1627 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1628 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001629 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001630 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001631 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001632 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001633 mVisible = true;
1634 }
1635
Adam Cohen0b395352014-06-09 22:54:36 +00001636 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001637 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001638 * This method is a no-op for other platform versions.
1639 */
Sunny Goyald0091012015-01-20 15:55:34 -08001640 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001641 private void setupTransparentSystemBarsForLollipop() {
1642 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001643 Window window = getWindow();
1644 window.getAttributes().systemUiVisibility |=
1645 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1646 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1647 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1648 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1649 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1650 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1651 window.setStatusBarColor(Color.TRANSPARENT);
1652 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001653 }
1654 }
1655
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 @Override
1657 public void onDetachedFromWindow() {
1658 super.onDetachedFromWindow();
1659 mVisible = false;
1660
Adam Cohend113e0c2010-11-11 10:48:05 -08001661 if (mAttached) {
1662 unregisterReceiver(mReceiver);
1663 mAttached = false;
1664 }
Winson Chungb745afb2015-03-02 11:51:23 -08001665 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 }
1667
1668 public void onWindowVisibilityChanged(int visibility) {
1669 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001670 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001671 // The following code used to be in onResume, but it turns out onResume is called when
1672 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1673 // is a more appropriate event to handle
1674 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001675 if (!mWorkspaceLoading) {
1676 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001677 // We want to let Launcher draw itself at least once before we force it to build
1678 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001679 // apps is nice and speedy.
1680 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001681 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001682 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001683 if (mStarted) return;
1684 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001685 // We delay the layer building a bit in order to give
1686 // other message processing a time to run. In particular
1687 // this avoids a delay in hiding the IME if it was
1688 // currently shown, because doing that may involve
1689 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001690 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001691 final ViewTreeObserver.OnDrawListener listener = this;
1692 mWorkspace.post(new Runnable() {
1693 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001694 if (mWorkspace != null &&
1695 mWorkspace.getViewTreeObserver() != null) {
1696 mWorkspace.getViewTreeObserver().
1697 removeOnDrawListener(listener);
1698 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001699 }
1700 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001701 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001702 }
1703 });
1704 }
1705 clearTypedText();
1706 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 }
1708
Adam Cohen091440a2015-03-18 14:16:05 -07001709 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 mHandler.removeMessages(ADVANCE_MSG);
1711 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1712 mHandler.sendMessageDelayed(msg, delay);
1713 mAutoAdvanceSentTime = System.currentTimeMillis();
1714 }
1715
Adam Cohen091440a2015-03-18 14:16:05 -07001716 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1718 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1719 mAutoAdvanceRunning = autoAdvanceRunning;
1720 if (autoAdvanceRunning) {
1721 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1722 sendAdvanceMessage(delay);
1723 } else {
1724 if (!mWidgetsToAdvance.isEmpty()) {
1725 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1726 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1727 }
1728 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001729 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 }
1731 }
1732 }
1733
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001734 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1735
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001737 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001738 if (msg.what == ADVANCE_MSG) {
1739 int i = 0;
1740 for (View key: mWidgetsToAdvance.keySet()) {
1741 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1742 final int delay = mAdvanceStagger * i;
1743 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001744 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 public void run() {
1746 ((Advanceable) v).advance();
1747 }
1748 }, delay);
1749 }
1750 i++;
1751 }
1752 sendAdvanceMessage(mAdvanceInterval);
1753 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001754 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001756 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001757
1758 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001759 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1761 if (v instanceof Advanceable) {
1762 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001763 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001764 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 }
1766 }
1767
1768 void removeWidgetToAutoAdvance(View hostView) {
1769 if (mWidgetsToAdvance.containsKey(hostView)) {
1770 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001771 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001773 }
1774
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001777 launcherInfo.hostView = null;
1778 }
1779
Hyunyoung Song3f471442015-04-08 19:01:34 -07001780 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001781 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1782 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001783 }
1784
Winson Chunga6945242014-01-08 14:04:34 -08001785 public DragLayer getDragLayer() {
1786 return mDragLayer;
1787 }
1788
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001789 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001790 return mAppsView;
1791 }
1792
Hyunyoung Song3f471442015-04-08 19:01:34 -07001793 public WidgetsContainerView getWidgetsView() {
1794 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001795 }
1796
Winson Chunga6945242014-01-08 14:04:34 -08001797 public Workspace getWorkspace() {
1798 return mWorkspace;
1799 }
1800
1801 public Hotseat getHotseat() {
1802 return mHotseat;
1803 }
1804
Jorim Jaggid017f882014-01-14 17:08:48 -08001805 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001806 return mOverviewPanel;
1807 }
1808
Winson Chung006ee262015-08-03 14:40:11 -07001809 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001810 return mSearchDropTargetBar;
1811 }
1812
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001813 public LauncherAppWidgetHost getAppWidgetHost() {
1814 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
Romain Guycbb89e42009-06-08 15:52:54 -07001816
Winson Chunga9abd0e2010-10-27 17:18:37 -07001817 public LauncherModel getModel() {
1818 return mModel;
1819 }
1820
Winson Chunga6945242014-01-08 14:04:34 -08001821 protected SharedPreferences getSharedPrefs() {
1822 return mSharedPrefs;
1823 }
1824
Adam Cohen2e6da152015-05-06 11:42:25 -07001825 public DeviceProfile getDeviceProfile() {
1826 return mDeviceProfile;
1827 }
1828
Bjorn Bringertc459e522013-06-07 19:36:01 +01001829 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001830 getWindow().closeAllPanels();
1831
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001832 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001833 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001834 }
1835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 @Override
1837 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001838 long startTime = 0;
1839 if (DEBUG_RESUME_TIME) {
1840 startTime = System.currentTimeMillis();
1841 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 super.onNewIntent(intent);
1843
1844 // Close the menu
1845 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001846 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001847 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001848
Adam Cohened307df2013-10-02 09:37:31 -07001849 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1850 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1851 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001852
Adam Cohen6fecd412013-10-02 17:41:50 -07001853 if (mWorkspace == null) {
1854 // Can be cases where mWorkspace is null, this prevents a NPE
1855 return;
1856 }
1857 Folder openFolder = mWorkspace.getOpenFolder();
1858 // In all these cases, only animate if we're already on home
1859 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001860
1861 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1862 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001863 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001864 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001865 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001866 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001867
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 closeFolder();
1869 exitSpringLoadedDragMode();
1870
1871 // If we are already on home, then just animate back to the workspace,
1872 // otherwise, just wait until onResume to set the state back to Workspace
1873 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001874 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 } else {
1876 mOnResumeState = State.WORKSPACE;
1877 }
1878
1879 final View v = getWindow().peekDecorView();
1880 if (v != null && v.getWindowToken() != null) {
1881 InputMethodManager imm = (InputMethodManager)getSystemService(
1882 INPUT_METHOD_SERVICE);
1883 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1884 }
1885
Winson Chungb745afb2015-03-02 11:51:23 -08001886 // Reset the apps view
1887 if (!alreadyOnHome && mAppsView != null) {
1888 mAppsView.scrollToTop();
1889 }
1890
Hyunyoung Song3f471442015-04-08 19:01:34 -07001891 // Reset the widgets view
1892 if (!alreadyOnHome && mWidgetsView != null) {
1893 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001894 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001895
Adam Cohen9211d422014-10-07 18:14:53 -07001896 if (mLauncherCallbacks != null) {
1897 mLauncherCallbacks.onHomeIntent();
1898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
Adam Cohened307df2013-10-02 09:37:31 -07001900
Michael Jurka447bf842013-05-15 14:52:15 +02001901 if (DEBUG_RESUME_TIME) {
1902 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904
Adam Cohen9211d422014-10-07 18:14:53 -07001905 if (mLauncherCallbacks != null) {
1906 mLauncherCallbacks.onNewIntent(intent);
1907 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001908 }
1909
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001911 public void onRestoreInstanceState(Bundle state) {
1912 super.onRestoreInstanceState(state);
1913 for (int page: mSynchronouslyBoundPages) {
1914 mWorkspace.restoreInstanceStateForChild(page);
1915 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917
1918 @Override
1919 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001920 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001921 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1922 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001923 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001924 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925
Michael Jurka883f55b2010-10-21 15:47:14 -07001926 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001927 // We close any open folder since it will not be re-opened, and we need to make sure
1928 // this state is reflected.
1929 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930
Adam Cohendcd297f2013-06-18 13:13:40 -07001931 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001932 mWaitingForResult) {
1933 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001934 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001935 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1936 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001937 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1938 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1939 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001940 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
1942
Hyunyoung Song3f471442015-04-08 19:01:34 -07001943 // Save the current widgets tray?
1944 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001945 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001946
1947 if (mLauncherCallbacks != null) {
1948 mLauncherCallbacks.onSaveInstanceState(outState);
1949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 }
1951
1952 @Override
1953 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001955
Winson Chunge7a03942011-08-05 15:05:12 -07001956 // Remove all pending runnables
1957 mHandler.removeMessages(ADVANCE_MSG);
1958 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001959 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001960
Winson Chungcd2b0142011-06-08 16:02:26 -07001961 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001962 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001963
1964 // It's possible to receive onDestroy after a new Launcher activity has
1965 // been created. In this case, don't interfere with the new Launcher.
1966 if (mModel.isCurrentCallbacks(this)) {
1967 mModel.stopLoader();
1968 app.setLauncher(null);
1969 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001972 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001974 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001976 mAppWidgetHost = null;
1977
1978 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979
1980 TextKeyListener.getInstance().release();
1981
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001982 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001983
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001984 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001985 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001986 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001987 mWorkspace = null;
1988 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001989
Michael Jurka2ecf9952012-06-18 12:52:28 -07001990 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001991
1992 if (mLauncherCallbacks != null) {
1993 mLauncherCallbacks.onDestroy();
1994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
1996
Adam Cohena9cf38f2011-05-02 15:36:58 -07001997 public DragController getDragController() {
1998 return mDragController;
1999 }
2000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 @Override
2002 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002003 onStartForResult(requestCode);
2004 super.startActivityForResult(intent, requestCode);
2005 }
2006
2007 @Override
2008 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2009 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2010 onStartForResult(requestCode);
2011 try {
2012 super.startIntentSenderForResult(intent, requestCode,
2013 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2014 } catch (IntentSender.SendIntentException e) {
2015 throw new ActivityNotFoundException();
2016 }
2017 }
2018
2019 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002020 if (requestCode >= 0) {
2021 setWaitingForResult(true);
2022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 }
2024
Winson Chung70d72102011-08-12 11:24:35 -07002025 /**
2026 * Indicates that we want global search for this activity by setting the globalSearch
2027 * argument for {@link #startSearch} to true.
2028 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002030 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002032
Karl Rosaen138a0412009-04-23 19:00:21 -07002033 if (initialQuery == null) {
2034 // Use any text typed in the launcher as the initial query
2035 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 if (appSearchData == null) {
2038 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002039 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 }
Winson Chungadf0c182012-08-23 14:59:07 -07002041 Rect sourceBounds = new Rect();
2042 if (mSearchDropTargetBar != null) {
2043 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2044 }
Romain Guycbb89e42009-06-08 15:52:54 -07002045
Ian Parkinson047036e2014-09-01 15:40:53 +01002046 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002047 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002048 if (clearTextImmediately) {
2049 clearTypedText();
2050 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002051
2052 // We need to show the workspace after starting the search
2053 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002054 }
2055
Ian Parkinson047036e2014-09-01 15:40:53 +01002056 /**
2057 * Start a text search.
2058 *
2059 * @return {@code true} if the search will start immediately, so any further keypresses
2060 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2061 * to buffer keypresses.
2062 */
2063 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002064 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002065 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2066 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2067 sourceBounds);
2068 }
2069
Michael Jurkaa33411c2012-06-14 16:18:21 -07002070 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002071 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002072 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002073 }
2074
2075 /**
2076 * Starts the global search activity. This code is a copied from SearchManager
2077 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002078 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002079 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002080 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002081 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2082 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2083 if (globalSearchActivity == null) {
2084 Log.w(TAG, "No global search activity found.");
2085 return;
2086 }
2087 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2088 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2089 intent.setComponent(globalSearchActivity);
2090 // Make sure that we have a Bundle to put source in
2091 if (appSearchData == null) {
2092 appSearchData = new Bundle();
2093 } else {
2094 appSearchData = new Bundle(appSearchData);
2095 }
Adam Cohen9211d422014-10-07 18:14:53 -07002096 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002097 if (!appSearchData.containsKey("source")) {
2098 appSearchData.putString("source", getPackageName());
2099 }
2100 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2101 if (!TextUtils.isEmpty(initialQuery)) {
2102 intent.putExtra(SearchManager.QUERY, initialQuery);
2103 }
2104 if (selectInitialQuery) {
2105 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2106 }
2107 intent.setSourceBounds(sourceBounds);
2108 try {
2109 startActivity(intent);
2110 } catch (ActivityNotFoundException ex) {
2111 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
2114
Winson Chungbedf9232015-07-10 12:38:30 -07002115 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2116 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2117 return;
2118 }
2119
2120 // If not handled, then just start the provided search intent
2121 startActivitySafely(v, searchIntent, null);
2122 }
2123
Yura4f93ec62014-02-04 14:15:21 +00002124 public boolean isOnCustomContent() {
2125 return mWorkspace.isOnOrMovingToCustomContent();
2126 }
2127
Winson Chung70d72102011-08-12 11:24:35 -07002128 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002129 public boolean onPrepareOptionsMenu(Menu menu) {
2130 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002131 if (!isOnCustomContent()) {
2132 // Close any open folders
2133 closeFolder();
2134 // Stop resizing any widgets
2135 mWorkspace.exitWidgetResizeMode();
2136 if (!mWorkspace.isInOverviewMode()) {
2137 // Show the overview mode
2138 showOverviewMode(true);
2139 } else {
2140 showWorkspace(true);
2141 }
Winson Chunge0298742014-01-17 12:03:00 -08002142 }
Adam Cohen9211d422014-10-07 18:14:53 -07002143 if (mLauncherCallbacks != null) {
2144 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2145 }
2146
Michael Jurkab94f3f82013-09-11 18:08:54 +02002147 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002150 @Override
2151 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002152 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002153 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002154 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002155 }
2156
Joe Onorato9c1289c2009-08-17 11:03:03 -04002157 public boolean isWorkspaceLocked() {
2158 return mWorkspaceLoading || mWaitingForResult;
2159 }
2160
Adam Cohen517a7f52014-03-01 12:12:59 -08002161 public boolean isWorkspaceLoading() {
2162 return mWorkspaceLoading;
2163 }
2164
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002165 private void setWorkspaceLoading(boolean value) {
2166 boolean isLocked = isWorkspaceLocked();
2167 mWorkspaceLoading = value;
2168 if (isLocked != isWorkspaceLocked()) {
2169 onWorkspaceLockedChanged();
2170 }
2171 }
2172
2173 private void setWaitingForResult(boolean value) {
2174 boolean isLocked = isWorkspaceLocked();
2175 mWaitingForResult = value;
2176 if (isLocked != isWorkspaceLocked()) {
2177 onWorkspaceLockedChanged();
2178 }
2179 }
2180
Adam Cohen9211d422014-10-07 18:14:53 -07002181 protected void onWorkspaceLockedChanged() {
2182 if (mLauncherCallbacks != null) {
2183 mLauncherCallbacks.onWorkspaceLockedChanged();
2184 }
2185 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002186
Michael Jurka0280c3b2010-09-17 15:00:07 -07002187 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002188 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002189 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002190 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2191 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002192 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002194
Sunny Goyale6b63a32015-01-16 16:14:29 -08002195 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002196 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002197 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2198 }
2199
Sunny Goyale6b63a32015-01-16 16:14:29 -08002200 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002201 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2202 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002203 if (appWidgetInfo.configure != null) {
2204 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002205 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002206
Bjorn Bringert7984c942009-12-09 15:38:25 +00002207 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002208 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2209 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002212 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002213 Runnable onComplete = new Runnable() {
2214 @Override
2215 public void run() {
2216 // Exit spring loaded mode if necessary after adding the widget
2217 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2218 null);
2219 }
2220 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002221 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002222 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002223 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 }
2225 }
Romain Guycbb89e42009-06-08 15:52:54 -07002226
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002227 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002228 // Close any folders that may be open.
2229 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002230 mWorkspace.moveToCustomContentScreen(animate);
2231 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002232
2233 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2234 int[] cell, int spanX, int spanY) {
2235 switch (info.itemType) {
2236 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2237 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2238 int span[] = new int[2];
2239 span[0] = spanX;
2240 span[1] = spanY;
2241 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2242 container, screenId, cell, span);
2243 break;
2244 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2245 processShortcutFromDrop(info.componentName, container, screenId, cell);
2246 break;
2247 default:
2248 throw new IllegalStateException("Unknown item type: " + info.itemType);
2249 }
2250 }
2251
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 /**
2253 * Process a shortcut drop.
2254 *
2255 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002256 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002257 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002259 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2260 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002261 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264
2265 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002266 mPendingAddInfo.cellX = cell[0];
2267 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269
2270 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2271 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002272 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002273 }
2274
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 /**
2276 * Process a widget drop.
2277 *
2278 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002279 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002282 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2283 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002286 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002287 mPendingAddInfo.minSpanX = info.minSpanX;
2288 mPendingAddInfo.minSpanY = info.minSpanY;
2289
Adam Cohenfbba09b2011-07-18 21:43:05 -07002290 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002291 mPendingAddInfo.cellX = cell[0];
2292 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002294 if (span != null) {
2295 mPendingAddInfo.spanX = span[0];
2296 mPendingAddInfo.spanY = span[1];
2297 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002298
Adam Cohened66b2b2012-01-23 17:28:51 -08002299 AppWidgetHostView hostView = info.boundWidget;
2300 int appWidgetId;
2301 if (hostView != null) {
2302 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002303 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002304
2305 // Clear the boundWidget so that it doesn't get destroyed.
2306 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002307 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002308 // In this case, we either need to start an activity to get permission to bind
2309 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002310 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002311 Bundle options = info.bindOptions;
2312
Sunny Goyalffe83f12014-08-14 17:39:34 -07002313 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2314 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002315 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002316 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002317 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002318 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002319 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2320 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2321 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002322 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2323 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002324 // TODO: we need to make sure that this accounts for the options bundle.
2325 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002326 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2327 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002328 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 }
2330
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002332 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002333 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 folderInfo.title = getText(R.string.folder_name);
2335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002337 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2338 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002339 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340
2341 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 FolderIcon newFolder =
2343 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002346 // Force measure the new folder icon
2347 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2348 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002349 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 }
Romain Guycbb89e42009-06-08 15:52:54 -07002351
Joe Onorato9c1289c2009-08-17 11:03:03 -04002352 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002353 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002354 }
2355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 @Override
2357 public boolean dispatchKeyEvent(KeyEvent event) {
2358 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2359 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002360 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002361 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002362 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002363 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002364 dumpState();
2365 return true;
2366 }
2367 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002368 }
2369 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2370 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002371 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 return true;
2373 }
2374 }
2375
2376 return super.dispatchKeyEvent(event);
2377 }
2378
Joe Onorato88ec0992009-11-19 13:16:06 -08002379 @Override
2380 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002381 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2382 return;
2383 }
2384
Sunny Goyal45478022015-06-08 16:52:41 -07002385 if (mDragController.isDragging()) {
2386 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002387 return;
2388 }
2389
Winson Chungb745afb2015-03-02 11:51:23 -08002390 if (isAppsViewVisible()) {
2391 showWorkspace(true);
2392 } else if (isWidgetsViewVisible()) {
2393 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002394 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002395 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002396 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002397 Folder openFolder = mWorkspace.getOpenFolder();
2398 if (openFolder.isEditingName()) {
2399 openFolder.dismissEditingName();
2400 } else {
2401 closeFolder();
2402 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002403 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002404 mWorkspace.exitWidgetResizeMode();
2405
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002406 // Back button is a no-op here, but give at least some feedback for the button press
2407 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002408 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002409 }
2410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002412 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002413 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002414 @Override
2415 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002416 if (mAppWidgetHost != null) {
2417 mAppWidgetHost.startListening();
2418 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002419 }
2420
2421 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 * Launches the intent referred by the clicked shortcut.
2423 *
2424 * @param v The view representing the clicked shortcut.
2425 */
2426 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002427 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2428 // view has detached (it's possible for this to happen if the view is removed mid touch).
2429 if (v.getWindowToken() == null) {
2430 return;
2431 }
2432
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002433 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002434 return;
2435 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002436
Adam Cohen1697b792013-09-17 19:08:21 -07002437 if (v instanceof Workspace) {
2438 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002439 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002440 }
2441 return;
2442 }
2443
2444 if (v instanceof CellLayout) {
2445 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002446 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2447 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002448 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002449 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002450 }
2451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002453 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002454 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002456 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002457 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002458 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002459 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002460 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002461 } else if (tag instanceof AppInfo) {
2462 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002463 } else if (tag instanceof LauncherAppWidgetInfo) {
2464 if (v instanceof PendingAppWidgetHostView) {
2465 onClickPendingWidget((PendingAppWidgetHostView) v);
2466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 }
2468 }
2469
Sunny Goyal70660032015-05-14 00:07:08 -07002470 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002471 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002472 return false;
2473 }
2474
Michael Jurkaaf442092010-06-10 17:01:57 -07002475 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002476 * Event handler for the app widget view which has not fully restored.
2477 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002478 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002479 if (mIsSafeModeEnabled) {
2480 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2481 return;
2482 }
2483
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002484 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002485 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002486 int widgetId = info.appWidgetId;
2487 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2488 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002489 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2490 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002491 mPendingAddInfo.copyFrom(info);
2492 mPendingAddWidgetId = widgetId;
2493
Sunny Goyalffe83f12014-08-14 17:39:34 -07002494 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2495 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002496 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002497 } else if (info.installProgress < 0) {
2498 // The install has not been queued
2499 final String packageName = info.providerName.getPackageName();
2500 showBrokenAppInstallDialog(packageName,
2501 new DialogInterface.OnClickListener() {
2502 public void onClick(DialogInterface dialog, int id) {
2503 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2504 }
2505 });
2506 } else {
2507 // Download has started.
2508 final String packageName = info.providerName.getPackageName();
2509 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002510 }
2511 }
2512
2513 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002514 * Event handler for the "grid" button that appears on the home screen, which
2515 * enters all apps mode.
2516 *
2517 * @param v The view that was clicked.
2518 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002519 protected void onClickAllAppsButton(View v) {
2520 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002521 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002522 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002523 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002524
2525 if (mLauncherCallbacks != null) {
2526 mLauncherCallbacks.onClickAllAppsButton(v);
2527 }
Winson Chung76648c52015-07-10 14:33:23 -07002528 }
2529 }
2530
2531 protected void onLongClickAllAppsButton(View v) {
2532 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2533 if (!isAppsViewVisible()) {
2534 showAppsView(true /* animated */, false /* resetListToTop */,
2535 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002536 }
2537 }
2538
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002539 private void showBrokenAppInstallDialog(final String packageName,
2540 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002541 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002542 .setTitle(R.string.abandoned_promises_title)
2543 .setMessage(R.string.abandoned_promise_explanation)
2544 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2545 .setNeutralButton(R.string.abandoned_clean_this,
2546 new DialogInterface.OnClickListener() {
2547 public void onClick(DialogInterface dialog, int id) {
2548 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2549 mWorkspace.removeAbandonedPromise(packageName, user);
2550 }
2551 })
2552 .create().show();
2553 return;
2554 }
2555
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002556 /**
2557 * Event handler for an app shortcut click.
2558 *
2559 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2560 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002561 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002562 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2563 Object tag = v.getTag();
2564 if (!(tag instanceof ShortcutInfo)) {
2565 throw new IllegalArgumentException("Input must be a Shortcut");
2566 }
2567
2568 // Open shortcut
2569 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002570
2571 if (shortcut.isDisabled != 0) {
2572 int error = R.string.activity_not_available;
2573 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2574 error = R.string.safemode_shortcut_error;
2575 }
2576 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2577 return;
2578 }
2579
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002580 final Intent intent = shortcut.intent;
2581
2582 // Check for special shortcuts
2583 if (intent.getComponent() != null) {
2584 final String shortcutClass = intent.getComponent().getClassName();
2585
2586 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2587 MemoryDumpActivity.startDump(this);
2588 return;
2589 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2590 toggleShowWeightWatcher();
2591 return;
2592 }
2593 }
2594
Chris Wren40c5ed32014-06-24 18:24:23 -04002595 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002596 if ((v instanceof BubbleTextView)
2597 && shortcut.isPromise()
2598 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002599 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002600 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002601 new DialogInterface.OnClickListener() {
2602 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002603 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002604 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002605 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002606 return;
2607 }
2608
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002609 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002610 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002611
2612 if (mLauncherCallbacks != null) {
2613 mLauncherCallbacks.onClickAppShortcut(v);
2614 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002615 }
2616
Adam Cohen091440a2015-03-18 14:16:05 -07002617 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002618 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002619 final ShortcutInfo shortcut;
2620 final Intent intent;
2621 if (tag instanceof ShortcutInfo) {
2622 shortcut = (ShortcutInfo) tag;
2623 intent = shortcut.intent;
2624 int[] pos = new int[2];
2625 v.getLocationOnScreen(pos);
2626 intent.setSourceBounds(new Rect(pos[0], pos[1],
2627 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002628
Sunny Goyal508da152014-08-14 10:53:27 -07002629 } else if (tag instanceof AppInfo) {
2630 shortcut = null;
2631 intent = ((AppInfo) tag).intent;
2632 } else {
2633 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2634 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002635
2636 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002637 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002638
2639 if (success && v instanceof BubbleTextView) {
2640 mWaitingForResume = (BubbleTextView) v;
2641 mWaitingForResume.setStayPressed(true);
2642 }
2643 }
2644
2645 /**
2646 * Event handler for a folder icon click.
2647 *
2648 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2649 */
2650 protected void onClickFolderIcon(View v) {
2651 if (LOGD) Log.d(TAG, "onClickFolder");
2652 if (!(v instanceof FolderIcon)){
2653 throw new IllegalArgumentException("Input must be a FolderIcon");
2654 }
2655
Sunny Goyal317698b2015-07-29 11:45:41 -07002656 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002657 FolderIcon folderIcon = (FolderIcon) v;
2658 final FolderInfo info = folderIcon.getFolderInfo();
2659 Folder openFolder = mWorkspace.getFolderForTag(info);
2660
2661 // If the folder info reports that the associated folder is open, then verify that
2662 // it is actually opened. There have been a few instances where this gets out of sync.
2663 if (info.opened && openFolder == null) {
2664 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2665 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2666 info.opened = false;
2667 }
2668
2669 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2670 // Close any open folder
2671 closeFolder();
2672 // Open the requested folder
2673 openFolder(folderIcon);
2674 } else {
2675 // Find the open folder...
2676 int folderScreen;
2677 if (openFolder != null) {
2678 folderScreen = mWorkspace.getPageForView(openFolder);
2679 // .. and close it
2680 closeFolder(openFolder);
2681 if (folderScreen != mWorkspace.getCurrentPage()) {
2682 // Close any folder open on the current screen
2683 closeFolder();
2684 // Pull the folder onto this screen
2685 openFolder(folderIcon);
2686 }
2687 }
2688 }
Adam Cohen9211d422014-10-07 18:14:53 -07002689
2690 if (mLauncherCallbacks != null) {
2691 mLauncherCallbacks.onClickFolderIcon(v);
2692 }
Michael Jurka5130e402011-10-13 04:55:35 -07002693 }
2694
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002695 /**
2696 * Event handler for the (Add) Widgets button that appears after a long press
2697 * on the home screen.
2698 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002699 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002700 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002701 if (mIsSafeModeEnabled) {
2702 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2703 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002704 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002705 if (mLauncherCallbacks != null) {
2706 mLauncherCallbacks.onClickAddWidgetButton(view);
2707 }
Adam Cohen9211d422014-10-07 18:14:53 -07002708 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002709 }
2710
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711 /**
2712 * Event handler for the wallpaper picker button that appears after a long press
2713 * on the home screen.
2714 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002715 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002717 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2718 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002719
2720 if (mLauncherCallbacks != null) {
2721 mLauncherCallbacks.onClickWallpaperPicker(v);
2722 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002723 }
2724
2725 /**
2726 * Event handler for a click on the settings button that appears after a long press
2727 * on the home screen.
2728 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002729 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002730 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002731 if (mLauncherCallbacks != null) {
2732 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002733 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002734 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002735 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002736 }
2737
Adam Cohen61f560d2013-09-30 15:58:20 -07002738 public View.OnTouchListener getHapticFeedbackTouchListener() {
2739 if (mHapticFeedbackTouchListener == null) {
2740 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002741 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002742 @Override
2743 public boolean onTouch(View v, MotionEvent event) {
2744 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2745 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2746 }
2747 return false;
2748 }
2749 };
2750 }
2751 return mHapticFeedbackTouchListener;
2752 }
2753
Adam Cohen9211d422014-10-07 18:14:53 -07002754 public void onDragStarted(View view) {
2755 if (isOnCustomContent()) {
2756 // Custom content screen doesn't participate in drag and drop. If on custom
2757 // content screen, move to default.
2758 moveWorkspaceToDefaultScreen();
2759 }
2760
2761 if (mLauncherCallbacks != null) {
2762 mLauncherCallbacks.onDragStarted(view);
2763 }
2764 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002765
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002766 /**
2767 * Called when the user stops interacting with the launcher.
2768 * This implies that the user is now on the homescreen and is not doing housekeeping.
2769 */
Adam Cohen9211d422014-10-07 18:14:53 -07002770 protected void onInteractionEnd() {
2771 if (mLauncherCallbacks != null) {
2772 mLauncherCallbacks.onInteractionEnd();
2773 }
2774 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002775
2776 /**
2777 * Called when the user starts interacting with the launcher.
2778 * The possible interactions are:
2779 * - open all apps
2780 * - reorder an app shortcut, or a widget
2781 * - open the overview mode.
2782 * This is a good time to stop doing things that only make sense
2783 * when the user is on the homescreen and not doing housekeeping.
2784 */
Adam Cohen9211d422014-10-07 18:14:53 -07002785 protected void onInteractionBegin() {
2786 if (mLauncherCallbacks != null) {
2787 mLauncherCallbacks.onInteractionBegin();
2788 }
2789 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002790
Winson Chungcd99cd32015-04-29 11:03:24 -07002791 /** Updates the interaction state. */
2792 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002793 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002794 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002795 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2796 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002797 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002798 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002799 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002800 onInteractionEnd();
2801 }
2802 }
2803
Kenny Guyf07af7b2014-07-31 11:39:16 +01002804 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002805 try {
2806 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002807 launcherApps.showAppDetailsForProfile(componentName, user);
2808 } catch (SecurityException e) {
2809 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2810 Log.e(TAG, "Launcher does not have permission to launch settings");
2811 } catch (ActivityNotFoundException e) {
2812 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2813 Log.e(TAG, "Unable to launch settings");
2814 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002815 }
2816
Michael Jurka1e2f4652013-07-08 18:03:46 -07002817 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002818 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2819 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002820 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002821 // System applications cannot be installed. For now, show a toast explaining that.
2822 // We may give them the option of disabling apps this way.
2823 int messageId = R.string.uninstall_system_app_text;
2824 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002825 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002826 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002827 String packageName = componentName.getPackageName();
2828 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002829 Intent intent = new Intent(
2830 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002831 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2832 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002833 if (user != null) {
2834 user.addToIntent(intent, Intent.EXTRA_USER);
2835 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002836 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002837 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002838 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002839 }
2840
Winson Chung8f1eff72015-05-28 17:33:40 -07002841 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002842 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002843 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002844 // Only launch using the new animation if the shortcut has not opted out (this is a
2845 // private contract between launcher and may be ignored in the future).
2846 boolean useLaunchAnimation = (v != null) &&
2847 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002848 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2849 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002850
Kenny Guy1317e2d2014-05-08 18:52:50 +01002851 UserHandleCompat user = null;
2852 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2853 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2854 user = userManager.getUserForSerialNumber(serialNumber);
2855 }
2856
2857 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002858 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002859 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002860 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002861 int left = 0, top = 0;
2862 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2863 if (v instanceof TextView) {
2864 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002865 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2866 if (icon != null) {
2867 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002868 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002869 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002870 width = bounds.width();
2871 height = bounds.height();
2872 }
2873 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002874 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2875 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002876 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002877 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002878 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002879 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002880 // On L devices, we use the device default slide-up transition.
2881 // On L MR1 devices, we a custom version of the slide-up transition which
2882 // doesn't have the delay present in the device default.
2883 opts = ActivityOptions.makeCustomAnimation(this,
2884 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002885 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002886 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002887 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002888
2889 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2890 // Could be launching some bookkeeping activity
2891 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002892 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002893 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002894 launcherApps.startActivityForProfile(intent.getComponent(), user,
2895 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002896 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002897 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 } catch (SecurityException e) {
2899 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002900 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002901 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002902 "or use the exported attribute for this activity. "
2903 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002904 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002905 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002906 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002907
Winson Chungbedf9232015-07-10 12:38:30 -07002908 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002909 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002910 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2911 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2912 return false;
2913 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002914 try {
2915 success = startActivity(v, intent, tag);
2916 } catch (ActivityNotFoundException e) {
2917 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2918 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2919 }
2920 return success;
2921 }
2922
Adam Cohen268c4752012-06-06 17:47:33 -07002923 /**
2924 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2925 * in the DragLayer in the exact absolute location of the original FolderIcon.
2926 */
2927 private void copyFolderIconToImage(FolderIcon fi) {
2928 final int width = fi.getMeasuredWidth();
2929 final int height = fi.getMeasuredHeight();
2930
2931 // Lazy load ImageView, Bitmap and Canvas
2932 if (mFolderIconImageView == null) {
2933 mFolderIconImageView = new ImageView(this);
2934 }
2935 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2936 mFolderIconBitmap.getHeight() != height) {
2937 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2938 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2939 }
2940
2941 DragLayer.LayoutParams lp;
2942 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2943 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2944 } else {
2945 lp = new DragLayer.LayoutParams(width, height);
2946 }
2947
Adam Cohen307fe232012-08-16 17:55:58 -07002948 // The layout from which the folder is being opened may be scaled, adjust the starting
2949 // view size by this scale factor.
2950 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002951 lp.customPosition = true;
2952 lp.x = mRectForFolderAnimation.left;
2953 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002954 lp.width = (int) (scale * width);
2955 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002956
2957 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2958 fi.draw(mFolderIconCanvas);
2959 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002960 if (fi.getFolder() != null) {
2961 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2962 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002963 }
Adam Cohen268c4752012-06-06 17:47:33 -07002964 // Just in case this image view is still in the drag layer from a previous animation,
2965 // we remove it and re-add it.
2966 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2967 mDragLayer.removeView(mFolderIconImageView);
2968 }
2969 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002970 if (fi.getFolder() != null) {
2971 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002972 }
Adam Cohen268c4752012-06-06 17:47:33 -07002973 }
2974
Adam Cohen2801caf2011-05-13 20:57:39 -07002975 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002976 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002977 FolderInfo info = (FolderInfo) fi.getTag();
2978 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2979 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002980 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2981 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002982 }
2983
Adam Cohen268c4752012-06-06 17:47:33 -07002984 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2985 copyFolderIconToImage(fi);
2986 fi.setVisibility(View.INVISIBLE);
2987
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002988 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2989 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002990 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002991 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2992 }
2993 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002994 oa.start();
2995 }
2996
Adam Cohen268c4752012-06-06 17:47:33 -07002997 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002998 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002999 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003000
Adam Cohen268c4752012-06-06 17:47:33 -07003001 // We remove and re-draw the FolderIcon in-case it has changed
3002 mDragLayer.removeView(mFolderIconImageView);
3003 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003004 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003005 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003006 oa.addListener(new AnimatorListenerAdapter() {
3007 @Override
3008 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003009 if (cl != null) {
3010 cl.clearFolderLeaveBehind();
3011 // Remove the ImageView copy of the FolderIcon and make the original visible.
3012 mDragLayer.removeView(mFolderIconImageView);
3013 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003014 }
3015 }
3016 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003017 oa.start();
3018 }
3019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003021 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003022 * is animated relative to the specified View. If the View is null, no animation
3023 * is played.
3024 *
3025 * @param folderInfo The FolderInfo describing the folder to open.
3026 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003027 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003028 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003029 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3030 if (openFolder != null && openFolder != folder) {
3031 // Close any open folder before opening a folder.
3032 closeFolder();
3033 }
3034
Adam Cohena9cf38f2011-05-02 15:36:58 -07003035 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003036
Adam Cohena9cf38f2011-05-02 15:36:58 -07003037 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038
Sunny Goyalf4066152015-04-15 09:42:19 -07003039 // While the folder is open, the position of the icon cannot change.
3040 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3041
Adam Cohen4554ee12011-08-03 16:13:21 -07003042 // Just verify that the folder hasn't already been added to the DragLayer.
3043 // There was a one-off crash where the folder had a parent already.
3044 if (folder.getParent() == null) {
3045 mDragLayer.addView(folder);
3046 mDragController.addDropTarget((DropTarget) folder);
3047 } else {
3048 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3049 folder.getParent() + ").");
3050 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003051 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003052 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003053
3054 // Notify the accessibility manager that this folder "window" has appeared and occluded
3055 // the workspace items
3056 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3057 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003058 }
3059
3060 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003061 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003063 if (folder.isEditingName()) {
3064 folder.dismissEditingName();
3065 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003066 closeFolder(folder);
3067 }
3068 }
3069
Sunny Goyal83a8f042015-05-19 12:52:12 -07003070 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003071 folder.getInfo().opened = false;
3072
3073 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3074 if (parent != null) {
3075 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3076 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003077 if (fi != null) {
3078 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3079 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003080 }
3081 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003082
3083 // Notify the accessibility manager that this folder "window" has disappeard and no
3084 // longer occludeds the workspace items
3085 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003086 }
3087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003088 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003089 if (!isDraggingEnabled()) return false;
3090 if (isWorkspaceLocked()) return false;
3091 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092
Winson Chung76648c52015-07-10 14:33:23 -07003093 if (v == mAllAppsButton) {
3094 onLongClickAllAppsButton(v);
3095 return true;
3096 }
3097
Adam Cohen1697b792013-09-17 19:08:21 -07003098 if (v instanceof Workspace) {
3099 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003100 if (!mWorkspace.isTouchActive()) {
3101 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003102 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3103 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3104 return true;
3105 } else {
3106 return false;
3107 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003108 } else {
3109 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003110 }
Adam Cohen1697b792013-09-17 19:08:21 -07003111 }
3112
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003113 CellLayout.CellInfo longClickCellInfo = null;
3114 View itemUnderLongClick = null;
3115 if (v.getTag() instanceof ItemInfo) {
3116 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003117 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003118 itemUnderLongClick = longClickCellInfo.cell;
3119 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 }
3121
Winson Chung3d503fb2011-07-13 17:25:49 -07003122 // The hotseat touch handling does not go through Workspace, and we always allow long press
3123 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003124 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003125 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003126 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003127 // User long pressed on empty space
3128 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3129 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003130 if (mWorkspace.isInOverviewMode()) {
3131 mWorkspace.startReordering(v);
3132 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003133 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003136 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3137 mHotseat.getOrderInHotseat(
3138 longClickCellInfo.cellX,
3139 longClickCellInfo.cellY));
3140 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003142 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 }
3144 }
3145 }
3146 return true;
3147 }
3148
Winson Chung3d503fb2011-07-13 17:25:49 -07003149 boolean isHotseatLayout(View layout) {
3150 return mHotseat != null && layout != null &&
3151 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3152 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003153
Winson Chung3d503fb2011-07-13 17:25:49 -07003154 /**
3155 * Returns the CellLayout of the specified container at the specified screen.
3156 */
Sunny Goyal92820592015-03-02 11:31:35 -08003157 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003158 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3159 if (mHotseat != null) {
3160 return mHotseat.getLayout();
3161 } else {
3162 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003163 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003164 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003165 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003166 }
3167 }
3168
Winson Chungb745afb2015-03-02 11:51:23 -08003169 /**
3170 * For overridden classes.
3171 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003172 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003173 return isAppsViewVisible();
3174 }
3175
3176 public boolean isAppsViewVisible() {
3177 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3178 }
3179
3180 public boolean isWidgetsViewVisible() {
3181 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003182 }
3183
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003184 private void setWorkspaceBackground(int background) {
3185 switch (background) {
3186 case WORKSPACE_BACKGROUND_TRANSPARENT:
3187 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3188 break;
3189 case WORKSPACE_BACKGROUND_BLACK:
3190 getWindow().setBackgroundDrawable(null);
3191 break;
3192 default:
3193 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3194 }
Craig Mautner360310b2012-10-26 15:13:08 -07003195 }
3196
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003197 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003198 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3199 int curflags = getWindow().getAttributes().flags
3200 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3201 if (wpflags != curflags) {
3202 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3203 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003204 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003205 }
3206
Michael Jurkae326f182011-11-21 14:05:46 -08003207 @Override
3208 public void onTrimMemory(int level) {
3209 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003210 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3211 // The widget preview db can result in holding onto over
3212 // 3MB of memory for caching which isn't necessary.
3213 SQLiteDatabase.releaseMemory();
3214
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003215 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003216 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003217 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003218 if (mLauncherCallbacks != null) {
3219 mLauncherCallbacks.onTrimMemory(level);
3220 }
Michael Jurkae326f182011-11-21 14:05:46 -08003221 }
3222
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003223 public void showWorkspace(boolean animated) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003224 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003225 }
3226
Sunny Goyal83a8f042015-05-19 12:52:12 -07003227 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003228 boolean changed = mState != State.WORKSPACE ||
3229 mWorkspace.getState() != Workspace.State.NORMAL;
3230 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003231 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003232 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003233 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003234
Michael Jurkab3e22d92011-10-31 15:58:33 -07003235 // Set focus to the AppsCustomize button
3236 if (mAllAppsButton != null) {
3237 mAllAppsButton.requestFocus();
3238 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003239 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003240
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003241 // Change the state *after* we've called all the transition code
3242 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003243
Winson Chung5fb63472011-02-02 17:03:37 -08003244 // Resume the auto-advance of widgets
3245 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003246 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003247
Winson Chung0f785722015-04-08 10:27:49 -07003248 if (changed) {
3249 // Send an accessibility event to announce the context change
3250 getWindow().getDecorView()
3251 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003252 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003253 }
3254
Adam Cohened307df2013-10-02 09:37:31 -07003255 void showOverviewMode(boolean animated) {
3256 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003257 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003258 Workspace.State.OVERVIEW, animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003259 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003260 }
3261
Winson Chungb745afb2015-03-02 11:51:23 -08003262 /**
3263 * Shows the apps view.
3264 */
Winson Chung76648c52015-07-10 14:33:23 -07003265 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3266 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003267 if (resetListToTop) {
3268 mAppsView.scrollToTop();
3269 }
Winson Chung4ac30062015-05-08 17:34:17 -07003270 if (updatePredictedApps) {
3271 tryAndUpdatePredictedApps();
3272 }
Winson Chung76648c52015-07-10 14:33:23 -07003273 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003274 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003275
Winson Chungb745afb2015-03-02 11:51:23 -08003276 /**
3277 * Shows the widgets view.
3278 */
3279 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003280 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003281 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003282 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003283 }
Winson Chung76648c52015-07-10 14:33:23 -07003284 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003285
3286 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003287 @Override
3288 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003289 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003290 }
3291 });
Winson Chungb745afb2015-03-02 11:51:23 -08003292 }
3293
3294 /**
3295 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003296 *
3297 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003298 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003299 // TODO: calling method should use the return value so that when {@code false} is returned
3300 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003301 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003302 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3303 mState != State.WIDGETS_SPRING_LOADED) {
3304 return false;
3305 }
3306 if (toState != State.APPS && toState != State.WIDGETS) {
3307 return false;
3308 }
Winson Chungb745afb2015-03-02 11:51:23 -08003309
3310 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003311 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3312 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003313 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003314 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003315 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003316
Michael Jurkab3e22d92011-10-31 15:58:33 -07003317 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003318 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003319
3320 // Pause the auto-advance of widgets until we are out of AllApps
3321 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003322 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003323 closeFolder();
3324
3325 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003326 getWindow().getDecorView()
3327 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003328 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003329 }
3330
Winson Chungcd99cd32015-04-29 11:03:24 -07003331 /**
3332 * Updates the workspace and interaction state on state change, and return the animation to this
3333 * new state.
3334 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003335 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003336 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003337 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003338 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003339 updateInteraction(fromState, toState);
3340 return anim;
3341 }
3342
Jun Mukaif0033da2015-08-04 18:34:30 -07003343 public void onLauncherClingShown() {
3344 // When a launcher cling appears, it should cover the underlying layers, so their focus
3345 // should be blocked.
3346 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3347 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3348 }
3349 }
3350
3351 public void onLauncherClingDismissed() {
3352 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3353 }
3354
Hyunyoung Song3f471442015-04-08 19:01:34 -07003355 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003356 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003357 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3358 mState == State.WIDGETS_SPRING_LOADED) {
3359 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003360 }
Winson Chungb745afb2015-03-02 11:51:23 -08003361
Winson Chung006ee262015-08-03 14:40:11 -07003362 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003363 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003364 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003365 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003366 }
Adam Cohen7777d962011-08-18 18:58:38 -07003367
Hyunyoung Song3f471442015-04-08 19:01:34 -07003368 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003369 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003370 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003371
Winson Chunge7a03942011-08-05 15:05:12 -07003372 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003373 @Override
3374 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003375 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003376 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3377 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003378 // Before we show workspace, hide all apps again because
3379 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3380 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003381 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003382 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003383 } else {
3384 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003385 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003386 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003387 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003388 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003389
Michael Jurkad3ef3062010-11-23 16:23:58 -08003390 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003391 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003392 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003393 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003394 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003395 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003396 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003397 }
3398
Winson Chung4ac30062015-05-08 17:34:17 -07003399 /**
3400 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3401 * resumed.
3402 */
3403 private void tryAndUpdatePredictedApps() {
3404 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003405 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003406 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003407 mAppsView.setPredictedApps(apps);
3408 }
3409 }
3410 }
3411
Michael Jurkab3e22d92011-10-31 15:58:33 -07003412 void lockAllApps() {
3413 // TODO
3414 }
3415
3416 void unlockAllApps() {
3417 // TODO
3418 }
3419
Sunny Goyal594d76d2014-11-06 10:12:54 -08003420 protected void disableVoiceButtonProxy(boolean disable) {
3421 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003422 }
3423
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003424 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003425 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3426 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003427 }
3428
Adam Cohen24ce0b32014-01-14 16:18:14 -08003429 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003430 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3431 if (searchProvider == null) {
3432 return null;
3433 }
3434
3435 Bundle opts = new Bundle();
3436 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003437 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003438
3439 SharedPreferences sp = getSharedPreferences(
3440 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3441 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003442 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003443 if (!searchProvider.provider.flattenToString().equals(
3444 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003445 || (widgetInfo == null)
3446 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003447 // A valid widget is not already bound.
3448 if (widgetId > -1) {
3449 mAppWidgetHost.deleteAppWidgetId(widgetId);
3450 widgetId = -1;
3451 }
3452
3453 // Try to bind a new widget
3454 widgetId = mAppWidgetHost.allocateAppWidgetId();
3455
3456 if (!AppWidgetManagerCompat.getInstance(this)
3457 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3458 mAppWidgetHost.deleteAppWidgetId(widgetId);
3459 widgetId = -1;
3460 }
3461
3462 sp.edit()
3463 .putInt(QSB_WIDGET_ID, widgetId)
3464 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3465 .commit();
3466 }
3467
Sunny Goyal8d595092015-07-06 12:22:14 -07003468 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003469 if (widgetId != -1) {
3470 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3471 mQsb.updateAppWidgetOptions(opts);
3472 mQsb.setPadding(0, 0, 0, 0);
3473 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003474 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003475 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003476 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003477 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003478 }
3479
Sunny Goyal22235bc2015-04-03 09:23:43 -07003480 private void reinflateQSBIfNecessary() {
3481 if (mQsb instanceof LauncherAppWidgetHostView &&
3482 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3483 mSearchDropTargetBar.removeView(mQsb);
3484 mQsb = null;
3485 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3486 }
3487 }
3488
Michael Jurkad7c28052012-04-27 15:43:36 -07003489 @Override
3490 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003491 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003492 final List<CharSequence> text = event.getText();
3493 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003494 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003495 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003496 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003497 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003498 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003499 } else if (mWorkspace != null) {
3500 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003501 } else {
3502 text.add(getString(R.string.all_apps_home_button_label));
3503 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003504 return result;
3505 }
3506
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003507 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003508 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003509 */
Adam Cohen091440a2015-03-18 14:16:05 -07003510 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003511 @Override
3512 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003513 closeSystemDialogs();
3514 }
3515 }
3516
3517 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003518 * If the activity is currently paused, signal that we need to run the passed Runnable
3519 * in onResume.
3520 *
3521 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003522 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3523 * wrong when we're not running, and if the activity comes back to what the configuration was
3524 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003525 *
3526 * Implementation of the method from LauncherModel.Callbacks.
3527 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003528 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003529 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003530 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003531 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003532 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003533 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003534 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003535 }
3536 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003537 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003538 return true;
3539 } else {
3540 return false;
3541 }
3542 }
3543
Michael Jurkac402cd92013-05-20 15:49:32 +02003544 private boolean waitUntilResume(Runnable run) {
3545 return waitUntilResume(run, false);
3546 }
3547
Michael Jurka1e2f4652013-07-08 18:03:46 -07003548 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003549 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003550 }
3551
Michael Jurka7607c2f2013-04-03 14:33:19 -07003552 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003553 * If the activity is currently paused, signal that we need to re-run the loader
3554 * in onResume.
3555 *
3556 * This needs to be called from incoming places where resources might have been loaded
3557 * while we are paused. That is becaues the Configuration might be wrong
3558 * when we're not running, and if it comes back to what it was when we
3559 * were paused, we are not restarted.
3560 *
3561 * Implementation of the method from LauncherModel.Callbacks.
3562 *
3563 * @return true if we are currently paused. The caller might be able to
3564 * skip some work in that case since we will come back again.
3565 */
3566 public boolean setLoadOnResume() {
3567 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003568 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003569 mOnResumeNeedsLoad = true;
3570 return true;
3571 } else {
3572 return false;
3573 }
3574 }
3575
3576 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003578 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003579 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003580 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003581 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003582 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003583 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003584 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003585 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003586
Joe Onorato9c1289c2009-08-17 11:03:03 -04003587 /**
3588 * Refreshes the shortcuts shown on the workspace.
3589 *
3590 * Implementation of the method from LauncherModel.Callbacks.
3591 */
3592 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003593 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003594
Michael Jurka7607c2f2013-04-03 14:33:19 -07003595 // If we're starting binding all over again, clear any bind calls we'd postponed in
3596 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3597 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003598 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003599
Winson Chungd64d1762013-08-20 14:37:16 -07003600 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003601 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003602 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003603
Michael Jurka05bf644e2011-11-30 20:28:53 -08003604 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003605 if (mHotseat != null) {
3606 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003607 }
3608 }
3609
Adam Cohendcd297f2013-06-18 13:13:40 -07003610 @Override
3611 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003612 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003613
Adam Cohen5084cba2013-09-03 12:01:16 -07003614 // If there are no screens, we need to have an empty screen
3615 if (orderedScreenIds.size() == 0) {
3616 mWorkspace.addExtraEmptyScreen();
3617 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003618
3619 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003620 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003621 if (hasCustomContentToLeft()) {
3622 mWorkspace.createCustomContentContainer();
3623 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003624 }
Winson Chung64359a52013-07-08 17:17:08 -07003625 }
3626
3627 @Override
3628 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003629 int count = orderedScreenIds.size();
3630 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003631 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003632 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003633 }
3634
Adam Cohen39a06042013-07-19 14:30:12 -07003635 private boolean shouldShowWeightWatcher() {
3636 String spKey = LauncherAppState.getSharedPreferencesKey();
3637 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003638 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003639
3640 return show;
3641 }
3642
3643 private void toggleShowWeightWatcher() {
3644 String spKey = LauncherAppState.getSharedPreferencesKey();
3645 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3646 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3647
3648 show = !show;
3649
3650 SharedPreferences.Editor editor = sp.edit();
3651 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3652 editor.commit();
3653
3654 if (mWeightWatcher != null) {
3655 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3656 }
3657 }
3658
Winson Chungd64d1762013-08-20 14:37:16 -07003659 public void bindAppsAdded(final ArrayList<Long> newScreens,
3660 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003661 final ArrayList<ItemInfo> addAnimated,
3662 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003663 Runnable r = new Runnable() {
3664 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003665 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003666 }
3667 };
3668 if (waitUntilResume(r)) {
3669 return;
3670 }
3671
Winson Chungd64d1762013-08-20 14:37:16 -07003672 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003673 if (newScreens != null) {
3674 bindAddScreens(newScreens);
3675 }
Winson Chungd64d1762013-08-20 14:37:16 -07003676
3677 // We add the items without animation on non-visible pages, and with
3678 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003679 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003680 bindItems(addNotAnimated, 0,
3681 addNotAnimated.size(), false);
3682 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003683 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003684 bindItems(addAnimated, 0,
3685 addAnimated.size(), true);
3686 }
Winson Chungc58497e2013-09-03 17:48:37 -07003687
Winson Chung87412982013-10-03 18:34:14 -07003688 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003689 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003690
Winson Chungb745afb2015-03-02 11:51:23 -08003691 if (addedApps != null && mAppsView != null) {
3692 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003693 }
Winson Chungd64d1762013-08-20 14:37:16 -07003694 }
3695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003696 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 * Bind the items start-end from the list.
3698 *
3699 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003700 */
Winson Chung64359a52013-07-08 17:17:08 -07003701 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3702 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003703 Runnable r = new Runnable() {
3704 public void run() {
3705 bindItems(shortcuts, start, end, forceAnimateIcons);
3706 }
3707 };
3708 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003709 return;
3710 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003711
Winson Chungf0c6ae02012-03-21 16:10:31 -07003712 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003713 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3714 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003715 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003716 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003717 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003718 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003720
3721 // Short circuit if we are loading dock items for a configuration which has no dock
3722 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3723 mHotseat == null) {
3724 continue;
3725 }
3726
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 switch (item.itemType) {
3728 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3729 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003730 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003731 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003732
Winson Chung64359a52013-07-08 17:17:08 -07003733 /*
3734 * TODO: FIX collision case
3735 */
Winson Chungce376632013-07-11 16:12:41 -07003736 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3737 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3738 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003739 View v = cl.getChildAt(item.cellX, item.cellY);
3740 Object tag = v.getTag();
3741 String desc = "Collision while binding workspace item: " + item
3742 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003743 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003744 throw (new RuntimeException(desc));
3745 } else {
3746 Log.d(TAG, desc);
3747 }
Winson Chungce376632013-07-11 16:12:41 -07003748 }
Winson Chung64359a52013-07-08 17:17:08 -07003749 }
3750
Adam Cohendcd297f2013-06-18 13:13:40 -07003751 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3752 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003753 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003754 // Animate all the applications up now
3755 shortcut.setAlpha(0f);
3756 shortcut.setScaleX(0f);
3757 shortcut.setScaleY(0f);
3758 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003759 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003760 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003762 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003763 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003764 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003765 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003766 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3767 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003768 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003769 default:
3770 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003772 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003773
Winson Chung997a9232013-07-24 15:33:46 -07003774 if (animateIcons) {
3775 // Animate to the correct page
3776 if (newShortcutsScreenId > -1) {
3777 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003778 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003779 final Runnable startBounceAnimRunnable = new Runnable() {
3780 public void run() {
3781 anim.playTogether(bounceAnims);
3782 anim.start();
3783 }
3784 };
Winson Chung997a9232013-07-24 15:33:46 -07003785 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003786 // We post the animation slightly delayed to prevent slowdowns
3787 // when we are loading right after we return to launcher.
3788 mWorkspace.postDelayed(new Runnable() {
3789 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003790 if (mWorkspace != null) {
3791 mWorkspace.snapToPage(newScreenIndex);
3792 mWorkspace.postDelayed(startBounceAnimRunnable,
3793 NEW_APPS_ANIMATION_DELAY);
3794 }
Winson Chung94d67682013-09-25 16:29:40 -07003795 }
3796 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003797 } else {
3798 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003799 }
3800 }
Winson Chung64359a52013-07-08 17:17:08 -07003801 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003802 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003803 }
3804
Joe Onorato9c1289c2009-08-17 11:03:03 -04003805 /**
3806 * Implementation of the method from LauncherModel.Callbacks.
3807 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003808 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003809 Runnable r = new Runnable() {
3810 public void run() {
3811 bindFolders(folders);
3812 }
3813 };
3814 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003815 return;
3816 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003817 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003818 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003819
3820 /**
3821 * Add the views for a widget to the workspace.
3822 *
3823 * Implementation of the method from LauncherModel.Callbacks.
3824 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003825 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003826 Runnable r = new Runnable() {
3827 public void run() {
3828 bindAppWidget(item);
3829 }
3830 };
3831 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003832 return;
3833 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003834
Daniel Sandler843e8602010-06-07 14:59:01 -04003835 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3836 if (DEBUG_WIDGETS) {
3837 Log.d(TAG, "bindAppWidget: " + item);
3838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003839 final Workspace workspace = mWorkspace;
3840
Adam Cohen59400422014-03-05 18:07:04 -08003841 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003842 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003843
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003844 if (!mIsSafeModeEnabled
3845 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003846 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3847
Sunny Goyalff572272014-07-23 13:58:07 -07003848 if (appWidgetInfo == null) {
3849 if (DEBUG_WIDGETS) {
3850 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3851 + " belongs to component " + item.providerName
3852 + ", as the povider is null");
3853 }
3854 LauncherModel.deleteItemFromDatabase(this, item);
3855 return;
3856 }
Sunny Goyalff572272014-07-23 13:58:07 -07003857
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003858 // If we do not have a valid id, try to bind an id.
3859 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3860 // Note: This assumes that the id remap broadcast is received before this step.
3861 // If that is not the case, the id remap will be ignored and user may see the
3862 // click to setup view.
3863 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3864 pendingInfo.spanX = item.spanX;
3865 pendingInfo.spanY = item.spanY;
3866 pendingInfo.minSpanX = item.minSpanX;
3867 pendingInfo.minSpanY = item.minSpanY;
3868 Bundle options = null;
3869 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003870
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003871 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3872 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3873 newWidgetId, appWidgetInfo, options);
3874
3875 // TODO consider showing a permission dialog when the widget is clicked.
3876 if (!success) {
3877 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3878 if (DEBUG_WIDGETS) {
3879 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3880 + " belongs to component " + item.providerName
3881 + ", as the launcher is unable to bing a new widget id");
3882 }
3883 LauncherModel.deleteItemFromDatabase(this, item);
3884 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003885 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003886
3887 item.appWidgetId = newWidgetId;
3888
3889 // If the widget has a configure activity, it is still needs to set it up, otherwise
3890 // the widget is ready to go.
3891 item.restoreStatus = (appWidgetInfo.configure == null)
3892 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3893 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3894
3895 LauncherModel.updateItemInDatabase(this, item);
3896 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
3897 && (appWidgetInfo.configure == null)) {
3898 // If the ID is already valid, verify if we need to configure or not.
3899 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3900 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003901 }
Sunny Goyalff572272014-07-23 13:58:07 -07003902 }
3903
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003904 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003905 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003906 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003907 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3908 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003909 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003910
Sunny Goyal651077b2014-06-30 14:15:31 -07003911 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003912 item.minSpanX = appWidgetInfo.minSpanX;
3913 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07003914 } else {
3915 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003916 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3917 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003918 view.updateIcon(mIconCache);
3919 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003920 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003921 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003922 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923
Joe Onorato9c1289c2009-08-17 11:03:03 -04003924 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003925 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003926
Adam Cohendcd297f2013-06-18 13:13:40 -07003927 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003928 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003929 if (!item.isCustomWidget()) {
3930 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3931 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003932
Joe Onorato9c1289c2009-08-17 11:03:03 -04003933 workspace.requestLayout();
3934
Daniel Sandler843e8602010-06-07 14:59:01 -04003935 if (DEBUG_WIDGETS) {
3936 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3937 + (SystemClock.uptimeMillis()-start) + "ms");
3938 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003939 }
3940
Sunny Goyalff572272014-07-23 13:58:07 -07003941 /**
3942 * Restores a pending widget.
3943 *
3944 * @param appWidgetId The app widget id
3945 * @param cellInfo The position on screen where to create the widget.
3946 */
3947 private void completeRestoreAppWidget(final int appWidgetId) {
3948 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3949 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3950 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3951 return;
3952 }
3953
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003954 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003955 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3956
3957 mWorkspace.reinflateWidgetsIfNecessary();
3958 LauncherModel.updateItemInDatabase(this, info);
3959 }
3960
Adam Cohen1462de32012-07-24 22:34:36 -07003961 public void onPageBoundSynchronously(int page) {
3962 mSynchronouslyBoundPages.add(page);
3963 }
3964
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 /**
3966 * Callback saying that there aren't any more items to bind.
3967 *
3968 * Implementation of the method from LauncherModel.Callbacks.
3969 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003970 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003971 Runnable r = new Runnable() {
3972 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003973 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003974 }
3975 };
3976 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003977 return;
3978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003979 if (mSavedState != null) {
3980 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003981 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003983 mSavedState = null;
3984 }
3985
Adam Cohen1462de32012-07-24 22:34:36 -07003986 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003988 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07003989 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07003990
3991 // If we received the result of any pending adds while the loader was running (e.g. the
3992 // widget configuration forced an orientation change), process them now.
3993 if (sPendingAddItem != null) {
3994 final long screenId = completeAdd(sPendingAddItem);
3995
3996 // TODO: this moves the user to the page where the pending item was added. Ideally,
3997 // the screen would be guaranteed to exist after bind, and the page would be set through
3998 // the workspace restore process.
3999 mWorkspace.post(new Runnable() {
4000 @Override
4001 public void run() {
4002 mWorkspace.snapToScreenId(screenId);
4003 }
4004 });
4005 sPendingAddItem = null;
4006 }
4007
Sunny Goyal756adbc2015-04-16 15:20:51 -07004008 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004009
4010 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004011 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004012 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004013 }
4014
Adam Cohen3ed316a2014-07-23 18:21:20 -07004015 private void sendLoadingCompleteBroadcastIfNecessary() {
4016 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4017 String permission =
4018 getResources().getString(R.string.receive_first_load_broadcast_permission);
4019 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4020 sendBroadcast(intent, permission);
4021 SharedPreferences.Editor editor = mSharedPrefs.edit();
4022 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4023 editor.apply();
4024 }
4025 }
4026
Winson Chunga0b7e862013-09-05 16:03:15 -07004027 public boolean isAllAppsButtonRank(int rank) {
4028 if (mHotseat != null) {
4029 return mHotseat.isAllAppsButtonRank(rank);
4030 }
4031 return false;
4032 }
4033
Winson Chunga2413752012-04-03 14:22:34 -07004034 private boolean canRunNewAppsAnimation() {
4035 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4036 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4037 }
4038
Winson Chungc9168342013-06-26 14:54:55 -07004039 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004040 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004041 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4042 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004043 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004044 return bounceAnim;
4045 }
4046
Adam Cohen27772732013-11-11 14:00:56 +00004047 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004048 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004049 }
4050
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004051 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004052 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004053 }
4054
Winson Chung88fa7412015-08-03 14:25:28 -07004055 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004056 if (mSearchDropTargetBar == null) {
4057 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004058 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004059 if (mQsb != null) {
4060 mSearchDropTargetBar.removeView(mQsb);
4061 mQsb = null;
4062 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004063 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004064 }
4065
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004066 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004067 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4068 * multiple calls to bind the same list.)
4069 */
4070 @Thunk ArrayList<AppInfo> mTmpAppsList;
4071 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4072 public void run() {
4073 bindAllApplications(mTmpAppsList);
4074 mTmpAppsList = null;
4075 }
4076 };
4077
4078 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004079 * Add the icons for all apps.
4080 *
4081 * Implementation of the method from LauncherModel.Callbacks.
4082 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004083 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004084 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4085 mTmpAppsList = apps;
4086 return;
4087 }
4088
Winson Chungb745afb2015-03-02 11:51:23 -08004089 if (mAppsView != null) {
4090 mAppsView.setApps(apps);
4091 }
Adam Cohen9211d422014-10-07 18:14:53 -07004092 if (mLauncherCallbacks != null) {
4093 mLauncherCallbacks.bindAllApplications(apps);
4094 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004095 }
4096
4097 /**
4098 * A package was updated.
4099 *
4100 * Implementation of the method from LauncherModel.Callbacks.
4101 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004102 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004103 Runnable r = new Runnable() {
4104 public void run() {
4105 bindAppsUpdated(apps);
4106 }
4107 };
4108 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004109 return;
4110 }
4111
Winson Chungb745afb2015-03-02 11:51:23 -08004112 if (mAppsView != null) {
4113 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004114 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004115 }
4116
Sunny Goyal4390ace2014-10-13 11:33:11 -07004117 @Override
4118 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4119 Runnable r = new Runnable() {
4120 public void run() {
4121 bindWidgetsRestored(widgets);
4122 }
4123 };
4124 if (waitUntilResume(r)) {
4125 return;
4126 }
4127 mWorkspace.widgetsRestored(widgets);
4128 }
4129
Joe Onorato9c1289c2009-08-17 11:03:03 -04004130 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004131 * Some shortcuts were updated in the background.
4132 *
4133 * Implementation of the method from LauncherModel.Callbacks.
4134 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004135 @Override
4136 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4137 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004138 Runnable r = new Runnable() {
4139 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004140 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004141 }
4142 };
4143 if (waitUntilResume(r)) {
4144 return;
4145 }
4146
Sunny Goyal4390ace2014-10-13 11:33:11 -07004147 if (!updated.isEmpty()) {
4148 mWorkspace.updateShortcuts(updated);
4149 }
4150
4151 if (!removed.isEmpty()) {
4152 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4153 for (ShortcutInfo si : removed) {
4154 removedComponents.add(si.getTargetComponent());
4155 }
4156 mWorkspace.removeItemsByComponentName(removedComponents, user);
4157 // Notify the drag controller
4158 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004159 }
4160 }
4161
4162 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004163 * Update the state of a package, typically related to install state.
4164 *
4165 * Implementation of the method from LauncherModel.Callbacks.
4166 */
Sunny Goyale755d462014-07-22 13:48:29 -07004167 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004168 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4169 Runnable r = new Runnable() {
4170 public void run() {
4171 bindRestoreItemsChange(updates);
4172 }
4173 };
4174 if (waitUntilResume(r)) {
4175 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004176 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004177
Sunny Goyal756adbc2015-04-16 15:20:51 -07004178 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004179 }
4180
4181 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004182 * A package was uninstalled. We take both the super set of packageNames
4183 * in addition to specific applications to remove, the reason being that
4184 * this can be called when a package is updated as well. In that scenario,
4185 * we only remove specific components from the workspace, where as
4186 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004187 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004188 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004189 * Implementation of the method from LauncherModel.Callbacks.
4190 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004191 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004192 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004193 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004194 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004195 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004196 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004197 }
Winson Chungd64d1762013-08-20 14:37:16 -07004198 };
4199 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004200 return;
4201 }
4202
Sunny Goyal1a745e82014-10-02 15:58:31 -07004203 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004204 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4205 for (AppInfo info : appInfos) {
4206 removedComponents.add(info.componentName);
4207 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004208 if (!packageNames.isEmpty()) {
4209 mWorkspace.removeItemsByPackageName(packageNames, user);
4210 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004211 if (!removedComponents.isEmpty()) {
4212 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004213 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004214 // Notify the drag controller
4215 mDragController.onAppsRemoved(packageNames, removedComponents);
4216
Sunny Goyal1a745e82014-10-02 15:58:31 -07004217 } else {
4218 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004219 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004220
Winson Chungdf95eb12013-10-16 14:57:07 -07004221 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004222 if (mAppsView != null) {
4223 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004224 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004225 }
Joe Onoratobe386092009-11-17 17:32:16 -08004226
Michael Jurkac402cd92013-05-20 15:49:32 +02004227 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004228 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004229 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004230 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004231 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004232
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004233 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004234 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004235 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004236 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004237 return;
4238 }
4239
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004240 if (mWidgetsView != null && model != null) {
4241 mWidgetsView.addWidgets(model);
4242 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004243 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004244 }
4245
Winson Chung400438b2011-01-16 17:53:48 -08004246 private int mapConfigurationOriActivityInfoOri(int configOri) {
4247 final Display d = getWindowManager().getDefaultDisplay();
4248 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4249 switch (d.getRotation()) {
4250 case Surface.ROTATION_0:
4251 case Surface.ROTATION_180:
4252 // We are currently in the same basic orientation as the natural orientation
4253 naturalOri = configOri;
4254 break;
4255 case Surface.ROTATION_90:
4256 case Surface.ROTATION_270:
4257 // We are currently in the other basic orientation to the natural orientation
4258 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4259 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4260 break;
4261 }
4262
4263 int[] oriMap = {
4264 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4265 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4266 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4267 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4268 };
4269 // Since the map starts at portrait, we need to offset if this device's natural orientation
4270 // is landscape.
4271 int indexOffset = 0;
4272 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4273 indexOffset = 1;
4274 }
4275 return oriMap[(d.getRotation() + indexOffset) % 4];
4276 }
Adam Cohen446e9402011-09-15 18:21:21 -07004277
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004278 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004279 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004280 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004281 if (Utilities.ATLEAST_JB_MR2) {
4282 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4283 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004284 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4285 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004286 }
Winson Chung4b919f82012-05-01 10:44:08 -07004287 }
4288 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004289
Winson Chung4b919f82012-05-01 10:44:08 -07004290 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004291 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004292 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004293 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004294 } else {
4295 mHandler.postDelayed(new Runnable() {
4296 public void run() {
4297 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4298 }
4299 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004300 }
Winson Chung4b919f82012-05-01 10:44:08 -07004301 }
Winson Chung400438b2011-01-16 17:53:48 -08004302 }
4303
Winson Chunge43a1e72014-01-15 10:33:02 -08004304 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004305 if (mLauncherCallbacks != null) {
4306 return mLauncherCallbacks.isLauncherPreinstalled();
4307 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004308 PackageManager pm = getPackageManager();
4309 try {
4310 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4311 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4312 return true;
4313 } else {
4314 return false;
4315 }
4316 } catch (NameNotFoundException e) {
4317 e.printStackTrace();
4318 return false;
4319 }
4320 }
4321
Adam Cohenea90f832014-05-21 15:03:34 -07004322 /**
4323 * This method indicates whether or not we should suggest default wallpaper dimensions
4324 * when our wallpaper cropper was not yet used to set a wallpaper.
4325 */
4326 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004327 if (mLauncherCallbacks != null) {
4328 return mLauncherCallbacks.overrideWallpaperDimensions();
4329 }
Adam Cohenea90f832014-05-21 15:03:34 -07004330 return true;
4331 }
4332
Adam Cohen432609a2014-03-13 17:03:22 -07004333 /**
4334 * To be overridden by subclasses to indicate that there is an activity to launch
4335 * before showing the standard launcher experience.
4336 */
4337 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004338 if (mLauncherCallbacks != null) {
4339 return mLauncherCallbacks.hasFirstRunActivity();
4340 }
Adam Cohen432609a2014-03-13 17:03:22 -07004341 return false;
4342 }
4343
4344 /**
4345 * To be overridden by subclasses to launch any first run activity
4346 */
4347 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004348 if (mLauncherCallbacks != null) {
4349 return mLauncherCallbacks.getFirstRunActivity();
4350 }
Adam Cohen432609a2014-03-13 17:03:22 -07004351 return null;
4352 }
4353
Winson Chunga6945242014-01-08 14:04:34 -08004354 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004355 return !ActivityManager.isRunningInTestHarness() &&
4356 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004357 }
4358
Adam Cohen4a9423d2014-03-28 14:22:31 -07004359 protected boolean hasRunFirstRunActivity() {
4360 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4361 }
4362
Adam Cohen432609a2014-03-13 17:03:22 -07004363 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004364 if (shouldRunFirstRunActivity() &&
4365 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004366 Intent firstRunIntent = getFirstRunActivity();
4367 if (firstRunIntent != null) {
4368 startActivity(firstRunIntent);
4369 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004370 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004371 }
4372 }
Adam Cohen432609a2014-03-13 17:03:22 -07004373 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004374 }
4375
4376 private void markFirstRunActivityShown() {
4377 SharedPreferences.Editor editor = mSharedPrefs.edit();
4378 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4379 editor.apply();
4380 }
4381
Adam Cohen432609a2014-03-13 17:03:22 -07004382 /**
4383 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4384 * screen that must be displayed and dismissed.
4385 */
4386 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004387 if (mLauncherCallbacks != null) {
4388 return mLauncherCallbacks.hasDismissableIntroScreen();
4389 }
Adam Cohen432609a2014-03-13 17:03:22 -07004390 return false;
4391 }
4392
4393 /**
4394 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4395 */
4396 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004397 if (mLauncherCallbacks != null) {
4398 return mLauncherCallbacks.getIntroScreen();
4399 }
Adam Cohen432609a2014-03-13 17:03:22 -07004400 return null;
4401 }
4402
4403 /**
4404 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4405 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4406 */
4407 private boolean shouldShowIntroScreen() {
4408 return hasDismissableIntroScreen() &&
4409 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4410 }
4411
4412 protected void showIntroScreen() {
4413 View introScreen = getIntroScreen();
4414 changeWallpaperVisiblity(false);
4415 if (introScreen != null) {
4416 mDragLayer.showOverlayView(introScreen);
4417 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004418 if (mLauncherOverlayContainer != null) {
4419 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4420 }
Adam Cohen432609a2014-03-13 17:03:22 -07004421 }
4422
4423 public void dismissIntroScreen() {
4424 markIntroScreenDismissed();
4425 if (showFirstRunActivity()) {
4426 // We delay hiding the intro view until the first run activity is showing. This
4427 // avoids a blip.
4428 mWorkspace.postDelayed(new Runnable() {
4429 @Override
4430 public void run() {
4431 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004432 if (mLauncherOverlayContainer != null) {
4433 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4434 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004435 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004436 }
4437 }, ACTIVITY_START_DELAY);
4438 } else {
4439 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004440 if (mLauncherOverlayContainer != null) {
4441 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4442 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004443 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004444 }
4445 changeWallpaperVisiblity(true);
4446 }
4447
4448 private void markIntroScreenDismissed() {
4449 SharedPreferences.Editor editor = mSharedPrefs.edit();
4450 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4451 editor.apply();
4452 }
4453
Adam Cohen091440a2015-03-18 14:16:05 -07004454 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004455 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4456 // on the device, then we always show the first run cling experience (or if there is no
4457 // launcher2). Otherwise, we prompt the user upon started for migration
4458 LauncherClings launcherClings = new LauncherClings(this);
4459 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4460 if (mModel.canMigrateFromOldLauncherDb(this)) {
4461 launcherClings.showMigrationCling();
4462 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004463 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004464 }
4465 }
4466 }
4467
Winson Chunga6945242014-01-08 14:04:34 -08004468 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004469 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4470 if (mHotseat != null) mHotseat.setAlpha(1f);
4471 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004472 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4473 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004474 }
4475
4476 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004477 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4478 if (mHotseat != null) mHotseat.setAlpha(0f);
4479 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004480 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4481 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004482 }
4483
Winson Chung5ffd51d2015-06-25 11:36:50 -07004484 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004485 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004486 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004487 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004488 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004489 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004490 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4491 if (userHandle != null) {
4492 user = UserHandleCompat.fromUser(userHandle);
4493 }
4494 }
4495 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004496 }
4497
4498 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004499 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004500 if (user == null) {
4501 user = UserHandleCompat.myUserHandle();
4502 }
4503
4504 // Called from search suggestion, add the profile extra to the intent to ensure that we
4505 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004506 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004507 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004508 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004509 return null;
4510 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004511 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004512 }
4513
Winson Chung5ffd51d2015-06-25 11:36:50 -07004514 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004515 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4516 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004517 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004518 UserHandleCompat.myUserHandle());
4519 }
4520
Winson Chung5ffd51d2015-06-25 11:36:50 -07004521 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004522 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4523 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004524 mWorkspace.onExternalDragStartedWithItem(dragView);
4525 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004526 }
4527
Winson Chung5ffd51d2015-06-25 11:36:50 -07004528 protected void moveWorkspaceToDefaultScreen() {
4529 mWorkspace.moveToDefaultScreen(false);
4530 }
4531
Anjali Koppalf5d29132014-02-28 13:33:27 -08004532 @Override
4533 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004534 if (mLauncherCallbacks != null) {
4535 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4536 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004537 }
4538
Winson Chung80baf5a2010-08-09 16:03:15 -07004539 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004540 * Returns a FastBitmapDrawable with the icon, accurately sized.
4541 */
4542 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4543 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4544 d.setFilterBitmap(true);
4545 resizeIconDrawable(d);
4546 return d;
4547 }
4548
4549 /**
4550 * Resizes an icon drawable to the correct icon size.
4551 */
4552 public void resizeIconDrawable(Drawable icon) {
4553 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4554 }
4555
4556 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004557 * Prints out out state for debugging.
4558 */
4559 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004560 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004561 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004562 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4563 Log.d(TAG, "mRestoring=" + mRestoring);
4564 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004565 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004566 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004567 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004568
Daniel Sandler325dc232013-06-05 22:57:57 -04004569 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004570 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004571
4572 @Override
4573 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4574 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004575 synchronized (sDumpLogs) {
4576 writer.println(" ");
4577 writer.println("Debug logs: ");
4578 for (int i = 0; i < sDumpLogs.size(); i++) {
4579 writer.println(" " + sDumpLogs.get(i));
4580 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004581 }
Adam Cohen9211d422014-10-07 18:14:53 -07004582 if (mLauncherCallbacks != null) {
4583 mLauncherCallbacks.dump(prefix, fd, writer, args);
4584 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004585 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004586
4587 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004588 if (DEBUG_DUMP_LOG) {
4589 synchronized (sDumpLogs) {
4590 Log.d(TAG, "");
4591 Log.d(TAG, "*********************");
4592 Log.d(TAG, "Launcher debug logs: ");
4593 for (int i = 0; i < sDumpLogs.size(); i++) {
4594 Log.d(TAG, " " + sDumpLogs.get(i));
4595 }
4596 Log.d(TAG, "*********************");
4597 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004598 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004599 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004600 }
4601
4602 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004603 addDumpLog(tag, log, null, debugLog);
4604 }
4605
4606 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004607 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004608 if (e != null) {
4609 Log.d(tag, log, e);
4610 } else {
4611 Log.d(tag, log);
4612 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004613 }
Winson Chungede41292013-09-19 16:27:36 -07004614 if (DEBUG_DUMP_LOG) {
4615 sDateStamp.setTime(System.currentTimeMillis());
4616 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004617 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4618 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004619 }
Winson Chungede41292013-09-19 16:27:36 -07004620 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004621 }
4622
Adam Cohen59400422014-03-05 18:07:04 -08004623 public static CustomAppWidget getCustomAppWidget(String name) {
4624 return sCustomAppWidgets.get(name);
4625 }
4626
4627 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4628 return sCustomAppWidgets;
4629 }
4630
Winson Chungede41292013-09-19 16:27:36 -07004631 public void dumpLogsToLocalData() {
4632 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004633 new AsyncTask<Void, Void, Void>() {
4634 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004635 boolean success = false;
4636 sDateStamp.setTime(sRunStart);
4637 String FILENAME = sDateStamp.getMonth() + "-"
4638 + sDateStamp.getDay() + "_"
4639 + sDateStamp.getHours() + "-"
4640 + sDateStamp.getMinutes() + "_"
4641 + sDateStamp.getSeconds() + ".txt";
4642
4643 FileOutputStream fos = null;
4644 File outFile = null;
4645 try {
4646 outFile = new File(getFilesDir(), FILENAME);
4647 outFile.createNewFile();
4648 fos = new FileOutputStream(outFile);
4649 } catch (Exception e) {
4650 e.printStackTrace();
4651 }
4652 if (fos != null) {
4653 PrintWriter writer = new PrintWriter(fos);
4654
4655 writer.println(" ");
4656 writer.println("Debug logs: ");
4657 synchronized (sDumpLogs) {
4658 for (int i = 0; i < sDumpLogs.size(); i++) {
4659 writer.println(" " + sDumpLogs.get(i));
4660 }
4661 }
4662 writer.close();
4663 }
4664 try {
4665 if (fos != null) {
4666 fos.close();
4667 success = true;
4668 }
4669 } catch (IOException e) {
4670 e.printStackTrace();
4671 }
Michael Jurka43467462013-11-14 12:03:58 +01004672 return null;
Winson Chungede41292013-09-19 16:27:36 -07004673 }
Michael Jurka43467462013-11-14 12:03:58 +01004674 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004675 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004676 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004677}
Michael Jurka2763be32011-02-24 11:19:57 -08004678
Dan Sandlerd5024042014-01-09 15:01:33 -05004679interface DebugIntents {
4680 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4681 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004682}