blob: 847b29b42cd99a4abac33b65c173d13b99b38df6 [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;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080046import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
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 Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700104import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700105import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700106import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700107import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700108import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700109import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700110import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700111
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700116import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800117import java.lang.reflect.InvocationTargetException;
118import java.lang.reflect.Method;
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 Chungb745afb2015-03-02 11:51:23 -0800133 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
134 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700135 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700136 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Daniel Sandlerf061f822013-06-27 13:59:36 -0400138 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700139 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700140 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700142 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700143
Dan Sandlerd5024042014-01-09 15:01:33 -0500144 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700149 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Michael Jurka8b805b12012-04-18 14:23:14 -0700151 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700152 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700153
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700154 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
155 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
156 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
157
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700158 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
159
Mathew Inwood876a8462013-06-14 14:12:41 +0100160 /**
161 * IntentStarter uses request codes starting with this. This must be greater than all activity
162 * request codes used internally.
163 */
164 protected static final int REQUEST_LAST = 100;
165
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800166 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
169 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung2672ff92012-05-04 16:22:30 -0700172 // The Intent extra that defines whether to ignore the launch animation
173 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400174 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
177 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700178 // Type: int
179 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
182 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
184 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700185 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: int[]
197 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Adam Cohen432609a2014-03-13 17:03:22 -0700199 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800200 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
201
Adam Cohen3ed316a2014-07-23 18:21:20 -0700202 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
203 static final String ACTION_FIRST_LOAD_COMPLETE =
204 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
205
Adam Cohen39a06042013-07-19 14:30:12 -0700206 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700207 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700208
Sunny Goyal594d76d2014-11-06 10:12:54 -0800209 private static final String QSB_WIDGET_ID = "qsb_widget_id";
210 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
211
Winson Chunga6945242014-01-08 14:04:34 -0800212 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
213
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700214 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700215 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
216
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700219
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700220 private boolean mIsSafeModeEnabled;
221
Adam Cohenc2d6e892014-10-16 09:49:24 -0700222 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
223 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700224 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700227 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
228 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700229 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000231 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
232 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
233
Winson Chunga2413752012-04-03 14:22:34 -0700234 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700235 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
236 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700237 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700238
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800239 private final BroadcastReceiver mCloseSystemDialogsReceiver
240 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 private LayoutInflater mInflater;
243
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700245 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800246 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700247 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800248 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700249 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700250
Sunny Goyalffe83f12014-08-14 17:39:34 -0700251 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700252 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Adam Cohen091440a2015-03-18 14:16:05 -0700254 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800255 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800256 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700257
Michael Jurka0280c3b2010-09-17 15:00:07 -0700258 private int[] mTmpAddItemCellCoordinates = new int[2];
259
Sunny Goyal316490e2015-06-02 09:38:28 -0700260 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800261 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700262
Michael Jurka838a4ca2011-02-07 13:33:06 -0800263 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700264
Winson Chungc51db6a2011-10-05 11:44:49 -0700265 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700266
267 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700268 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700269
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700270 // Main container view and the model for the widget tray screen.
271 @Thunk WidgetsContainerView mWidgetsView;
272 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700273
Winson Chungf0ea4d32011-06-06 14:27:16 -0700274 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800275 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700278 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
279 // scroll issues (because the workspace may not have been measured yet) and extra work.
280 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
281 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
283 private SpannableStringBuilder mDefaultKeySsb = null;
284
Adam Cohen091440a2015-03-18 14:16:05 -0700285 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800287 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 private boolean mRestoring;
289 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700290 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291
Michael Jurka1e2f4652013-07-08 18:03:46 -0700292 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700293 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 private Bundle mSavedInstanceState;
296
Joe Onorato9c1289c2009-08-17 11:03:03 -0400297 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800298 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700299 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800300 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700301 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800302 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400303
Sunny Goyale2df0622015-04-24 11:27:00 -0700304 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700305
Adam Cohen61f560d2013-09-30 15:58:20 -0700306 private View.OnTouchListener mHapticFeedbackTouchListener;
307
Adam Cohended9f8d2010-11-03 13:25:16 -0700308 // Related to the auto-advancing of widgets
309 private final int ADVANCE_MSG = 1;
310 private final int mAdvanceInterval = 20000;
311 private final int mAdvanceStagger = 250;
312 private long mAutoAdvanceSentTime;
313 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700314 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700315 new HashMap<View, AppWidgetProviderInfo>();
316
Winson Chung400438b2011-01-16 17:53:48 -0800317 // Determines how long to wait after a rotation before restoring the screen orientation to
318 // match the sensor state.
319 private final int mRestoreScreenOrientationDelay = 500;
320
Adam Cohen091440a2015-03-18 14:16:05 -0700321 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700322
Adam Cohen1462de32012-07-24 22:34:36 -0700323 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700324 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700325
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700326 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700327 static Date sDateStamp = new Date();
328 static DateFormat sDateFormat =
329 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
330 static long sRunStart = System.currentTimeMillis();
331 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700332
Winson Chung46353de2012-02-16 14:05:10 -0800333 // We only want to get the SharedPreferences once since it does an FS stat each time we get
334 // it from the context.
335 private SharedPreferences mSharedPrefs;
336
Winson Chungf0c6ae02012-03-21 16:10:31 -0700337 // Holds the page that we need to animate to, and the icon views that we need to animate up
338 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700339 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700340 private Bitmap mFolderIconBitmap;
341 private Canvas mFolderIconCanvas;
342 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700343
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700344 private DeviceProfile mDeviceProfile;
345
Winson Chung13eb5272015-05-11 16:30:13 -0700346 // This is set to the view that launched the activity that navigated the user away from
347 // launcher. Since there is no callback for when the activity has finished launching, enable
348 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800349 private BubbleTextView mWaitingForResume;
350
Adam Cohen59400422014-03-05 18:07:04 -0800351 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
352 new HashMap<String, CustomAppWidget>();
353
354 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
355 static {
356 if (ENABLE_CUSTOM_WIDGET_TEST) {
357 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
358 }
359 }
360
Chet Haasea8f996d2015-02-17 12:56:04 -0800361 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
362 private static Method sClipRevealMethod = null;
363 static {
364 Class<?> activityOptionsClass = ActivityOptions.class;
365 try {
366 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
367 View.class, int.class, int.class, int.class, int.class);
368 } catch (Exception e) {
369 // Earlier version
370 }
371 }
372
Adam Cohen091440a2015-03-18 14:16:05 -0700373 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700374 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700375 if (mWorkspace != null) {
376 mWorkspace.buildPageHardwareLayers();
377 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700378 }
379 };
380
Adam Cohendb364c32014-05-20 14:23:40 -0700381 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800382
Adam Cohen091440a2015-03-18 14:16:05 -0700383 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800384 int requestCode;
385 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700386 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700387 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800388 int cellX;
389 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700390 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800391 }
392
Daniel Sandlerff02d492013-08-05 02:12:05 -0400393 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700394 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700395 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400396
397 @Thunk void setOrientation() {
398 if (mRotationEnabled) {
399 unlockScreenOrientation(true);
400 } else {
401 setRequestedOrientation(
402 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700403 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400404 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700405
Sunny Goyal7779d622015-06-11 16:18:39 -0700406 private Runnable mUpdateOrientationRunnable = new Runnable() {
407 public void run() {
408 setOrientation();
409 }
410 };
411
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 @Override
413 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700414 if (DEBUG_STRICT_MODE) {
415 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
416 .detectDiskReads()
417 .detectDiskWrites()
418 .detectNetwork() // or .detectAll() for all detectable problems
419 .penaltyLog()
420 .build());
421 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
422 .detectLeakedSqlLiteObjects()
423 .detectLeakedClosableObjects()
424 .penaltyLog()
425 .penaltyDeath()
426 .build());
427 }
428
Adam Cohen9211d422014-10-07 18:14:53 -0700429 if (mLauncherCallbacks != null) {
430 mLauncherCallbacks.preOnCreate();
431 }
432
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400434
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400435 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400436 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700437
Adam Cohen2e6da152015-05-06 11:42:25 -0700438 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700439 mDeviceProfile = getResources().getConfiguration().orientation
440 == Configuration.ORIENTATION_LANDSCAPE ?
441 app.getInvariantDeviceProfile().landscapeProfile
442 : app.getInvariantDeviceProfile().portraitProfile;
443
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400444 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700445 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700446 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800447 mModel = app.setLauncher(this);
448 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700449
Joe Onorato41a12d22009-10-31 18:30:00 -0400450 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800452 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700453
Daniel Sandlerff02d492013-08-05 02:12:05 -0400454 mStats = new Stats(this);
455
Sunny Goyalffe83f12014-08-14 17:39:34 -0700456 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700457
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700458 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
459 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700460
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700461 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
462 // this also ensures that any synchronous binding below doesn't re-trigger another
463 // LauncherModel load.
464 mPaused = false;
465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200467 android.os.Debug.startMethodTracing(
468 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700472
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700474 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475
Joe Onorato7c312c12009-08-13 21:36:53 -0700476 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700477
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 mSavedState = savedInstanceState;
479 restoreState(mSavedState);
480
481 if (PROFILE_STARTUP) {
482 android.os.Debug.stopMethodTracing();
483 }
484
485 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700486 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700487 // If the user leaves launcher, then we should just load items asynchronously when
488 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700489 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700490 } else {
491 // We only load the page synchronously if the user rotates (or triggers a
492 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700493 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700494 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 }
496
497 // For handling default keys
498 mDefaultKeySsb = new SpannableStringBuilder();
499 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800500
501 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
502 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800503
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700504 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
505 // In case we are on a device with locked rotation, we should look at preferences to check
506 // if the user has specifically allowed rotation.
507 if (!mRotationEnabled) {
Rahul Chaturvedie863fed2015-06-15 14:09:42 -0400508 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext(), false);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700509 }
510
511 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
512 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400513 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700514
Adam Cohen9211d422014-10-07 18:14:53 -0700515 if (mLauncherCallbacks != null) {
516 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700517 if (mLauncherCallbacks.hasLauncherOverlay()) {
518 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700519 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
520 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
521 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700522 mWorkspace.setLauncherOverlay(mLauncherOverlay);
523 }
Adam Cohen9211d422014-10-07 18:14:53 -0700524 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700525
526 if (shouldShowIntroScreen()) {
527 showIntroScreen();
528 } else {
529 showFirstRunActivity();
530 showFirstRunClings();
531 }
Adam Cohen9211d422014-10-07 18:14:53 -0700532 }
533
Sunny Goyal7779d622015-06-11 16:18:39 -0700534 @Override
535 public void onSettingsChanged(String settings, boolean value) {
536 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
537 mRotationEnabled = value;
538 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
539 mUpdateOrientationRunnable.run();
540 }
541 }
542 }
543
Adam Cohen9211d422014-10-07 18:14:53 -0700544 private LauncherCallbacks mLauncherCallbacks;
545
546 public void onPostCreate(Bundle savedInstanceState) {
547 super.onPostCreate(savedInstanceState);
548 if (mLauncherCallbacks != null) {
549 mLauncherCallbacks.onPostCreate(savedInstanceState);
550 }
551 }
552
553 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
554 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700555 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700556 private boolean mWorkspaceImportanceStored = false;
557 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700558 private int mWorkspaceImportanceForAccessibility =
559 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
560 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
561
562 @Override
563 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700564 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700565 return;
566 }
567 // The underlying workspace and hotseat are temporarily suppressed by the search
568 // overlay. So they sholudn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700569 if (mWorkspace != null) {
570 mWorkspaceImportanceForAccessibility =
571 mWorkspace.getImportantForAccessibility();
572 mWorkspace.setImportantForAccessibility(
573 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
574 mWorkspaceImportanceStored = true;
575 }
576 if (mHotseat != null) {
577 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
578 mHotseat.setImportantForAccessibility(
579 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
580 mHotseatImportanceStored = true;
581 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700582 }
583
584 @Override
585 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700586 if (mWorkspaceImportanceStored && mWorkspace != null) {
587 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
588 }
589 if (mHotseatImportanceStored && mHotseat != null) {
590 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
591 }
592 mWorkspaceImportanceStored = false;
593 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700594 }
595 });
Adam Cohen9211d422014-10-07 18:14:53 -0700596 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700597 }
598
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700599 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700600 public void onLauncherProviderChange() {
601 if (mLauncherCallbacks != null) {
602 mLauncherCallbacks.onLauncherProviderChange();
603 }
604 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700605
Winson Chungef7f8742015-06-04 17:18:17 -0700606 /**
607 * Updates the bounds of all the overlays to match the new fixed bounds.
608 */
609 public void updateOverlayBounds(Rect newBounds) {
610 mAppsView.setSearchBarBounds(newBounds);
611 mWidgetsView.setSearchBarBounds(newBounds);
612 }
613
Adam Cohen9211d422014-10-07 18:14:53 -0700614 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700615 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700616 if (mLauncherCallbacks != null) {
617 return mLauncherCallbacks.hasCustomContentToLeft();
618 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700619 return false;
620 }
621
Dave Hawkeya8881582013-09-17 15:55:33 -0600622 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500623 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600624 * {@link #addToCustomContentPage}. This will only be invoked if
625 * {@link #hasCustomContentToLeft()} is {@code true}.
626 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500627 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700628 if (mLauncherCallbacks != null) {
629 mLauncherCallbacks.populateCustomContentContainer();
630 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600631 }
632
633 /**
634 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
635 * ensure the custom content page is added or removed if necessary.
636 */
637 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600638 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600639 // Not bound yet, wait for bindScreens to be called.
640 return;
641 }
642
643 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
644 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500645 mWorkspace.createCustomContentContainer();
646 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600647 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
648 mWorkspace.removeCustomContentPage();
649 }
650 }
651
Anton Hanssona2f665f2013-09-26 12:18:32 +0100652 public Stats getStats() {
653 return mStats;
654 }
655
Bjorn Bringertc459e522013-06-07 19:36:01 +0100656 public LayoutInflater getInflater() {
657 return mInflater;
658 }
659
Hyunyoung Song3f471442015-04-08 19:01:34 -0700660 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700661 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
662 // that is subsequently removed from the workspace in startBinding().
663 return !mModel.isLoadingWorkspace();
664 }
665
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800666 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000667 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100668 if (Build.VERSION.SDK_INT >= 17) {
669 return View.generateViewId();
670 } else {
671 // View.generateViewId() is not available. The following fallback logic is a copy
672 // of its implementation.
673 for (;;) {
674 final int result = sNextGeneratedId.get();
675 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
676 int newValue = result + 1;
677 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
678 if (sNextGeneratedId.compareAndSet(result, newValue)) {
679 return result;
680 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000681 }
682 }
683 }
684
685 public int getViewIdForItem(ItemInfo info) {
686 // This cast is safe given the > 2B range for int.
687 int itemId = (int) info.id;
688 if (mItemIdToViewId.containsKey(itemId)) {
689 return mItemIdToViewId.get(itemId);
690 }
691 int viewId = generateViewId();
692 mItemIdToViewId.put(itemId, viewId);
693 return viewId;
694 }
695
696 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700697 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
698 * a configuration step, this allows the proper animations to run after other transitions.
699 */
Adam Cohendb364c32014-05-20 14:23:40 -0700700 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700701 long screenId = args.screenId;
702 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
703 // When the screen id represents an actual screen (as opposed to a rank) we make sure
704 // that the drop page actually exists.
705 screenId = ensurePendingDropLayoutExists(args.screenId);
706 }
Adam Cohendb364c32014-05-20 14:23:40 -0700707
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800708 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800709 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700710 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700711 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700712 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800713 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700714 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700715 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700716 case REQUEST_RECONFIGURE_APPWIDGET:
717 completeRestoreAppWidget(args.appWidgetId);
718 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800719 }
Michael Jurka27614d22012-04-02 06:40:22 -0700720 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
721 // if you turned the screen off and then back while in All Apps, Launcher would not
722 // return to the workspace. Clearing mAddInfo.container here fixes this issue
723 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700724 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800725 }
726
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800727 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700729 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700730 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700731 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800732 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700733
Adam Cohenad4e15c2013-10-17 16:21:35 -0700734 Runnable exitSpringLoaded = new Runnable() {
735 @Override
736 public void run() {
737 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
738 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
739 }
740 };
741
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700743 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
745 if (resultCode == RESULT_CANCELED) {
746 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700747 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700749 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800750 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 }
753 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200754 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
755 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700756 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200757 }
758 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200760
Adam Cohened66b2b2012-01-23 17:28:51 -0800761 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700762 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700763
Adam Cohendb364c32014-05-20 14:23:40 -0700764 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 // We have special handling for widgets
766 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800767 final int appWidgetId;
768 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
769 : -1;
770 if (widgetId < 0) {
771 appWidgetId = pendingAddWidgetId;
772 } else {
773 appWidgetId = widgetId;
774 }
775
Adam Cohenad4e15c2013-10-17 16:21:35 -0700776 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800777 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700778 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
779 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 result = RESULT_CANCELED;
781 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700782 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 @Override
784 public void run() {
785 exitSpringLoadedDragModeDelayed(false, 0, null);
786 }
787 };
Adam Cohendb364c32014-05-20 14:23:40 -0700788 if (workspaceLocked) {
789 // No need to remove the empty screen if we're mid-binding, as the
790 // the bind will not add the empty screen.
791 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
792 } else {
793 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
794 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
795 }
Winson Chung5aaab772012-04-27 15:24:02 -0700796 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700797 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700798 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
799 // When the screen id represents an actual screen (as opposed to a rank)
800 // we make sure that the drop page actually exists.
801 mPendingAddInfo.screenId =
802 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
803 }
Adam Cohendb364c32014-05-20 14:23:40 -0700804 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
805
806 dropLayout.setDropPending(true);
807 final Runnable onComplete = new Runnable() {
808 @Override
809 public void run() {
810 completeTwoStageWidgetDrop(resultCode, appWidgetId);
811 dropLayout.setDropPending(false);
812 }
813 };
814 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
815 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
816 } else {
817 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
818 mPendingAddInfo);
819 sPendingAddItem = args;
820 }
Winson Chung5aaab772012-04-27 15:24:02 -0700821 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800822 return;
823 }
824
Sunny Goyalff572272014-07-23 13:58:07 -0700825 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
826 if (resultCode == RESULT_OK) {
827 // Update the widget view.
828 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
829 pendingAddWidgetId, mPendingAddInfo);
830 if (workspaceLocked) {
831 sPendingAddItem = args;
832 } else {
833 completeAdd(args);
834 }
835 }
836 // Leave the widget in the pending state if the user canceled the configure.
837 return;
838 }
839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 // The pattern used here is that a user PICKs a specific application,
841 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
844 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700845 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700846 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
847 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800848 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700849 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800850 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700851 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700852 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
853 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 }
Adam Cohen00074722013-10-11 17:46:09 -0700855 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700856 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700857 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800859 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800860
861 }
862
863 @Override
864 protected void onActivityResult(
865 final int requestCode, final int resultCode, final Intent data) {
866 handleActivityResult(requestCode, resultCode, data);
867 if (mLauncherCallbacks != null) {
868 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
869 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 }
871
Adam Cohendb364c32014-05-20 14:23:40 -0700872 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
873 appWidgetId, ItemInfo info) {
874 PendingAddArguments args = new PendingAddArguments();
875 args.requestCode = requestCode;
876 args.intent = data;
877 args.container = info.container;
878 args.screenId = info.screenId;
879 args.cellX = info.cellX;
880 args.cellY = info.cellY;
881 args.appWidgetId = appWidgetId;
882 return args;
883 }
884
885 /**
886 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
887 *
888 * @param screenId the screen id to check
889 * @return the new screen, or screenId if it exists
890 */
891 private long ensurePendingDropLayoutExists(long screenId) {
892 CellLayout dropLayout =
893 (CellLayout) mWorkspace.getScreenWithId(screenId);
894 if (dropLayout == null) {
895 // it's possible that the add screen was removed because it was
896 // empty and a re-bind occurred
897 mWorkspace.addExtraEmptyScreen();
898 return mWorkspace.commitExtraEmptyScreen();
899 } else {
900 return screenId;
901 }
902 }
903
Adam Cohen091440a2015-03-18 14:16:05 -0700904 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700905 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700906 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 Runnable onCompleteRunnable = null;
908 int animationType = 0;
909
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700910 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800911 if (resultCode == RESULT_OK) {
912 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
913 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700914 mPendingAddWidgetInfo);
915 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800916 onCompleteRunnable = new Runnable() {
917 @Override
918 public void run() {
919 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700920 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700921 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
922 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 }
924 };
925 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800926 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700929 if (mDragLayer.getAnimatedView() != null) {
930 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
931 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
932 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700933 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700934 // The animated view may be null in the case of a rotation during widget configuration
935 onCompleteRunnable.run();
936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938
939 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700940 protected void onStop() {
941 super.onStop();
942 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700943
944 if (mLauncherCallbacks != null) {
945 mLauncherCallbacks.onStop();
946 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700947 }
948
949 @Override
950 protected void onStart() {
951 super.onStart();
952 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700953
954 if (mLauncherCallbacks != null) {
955 mLauncherCallbacks.onStart();
956 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700957 }
958
959 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200961 long startTime = 0;
962 if (DEBUG_RESUME_TIME) {
963 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400964 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200965 }
Adam Cohen9211d422014-10-07 18:14:53 -0700966
967 if (mLauncherCallbacks != null) {
968 mLauncherCallbacks.preOnResume();
969 }
970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700972
Winson Chung4a2afa32012-07-19 14:53:05 -0700973 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700974 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700975 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800976 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700977 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700978 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700979 // view after launching an app, as they may be depending on the UI to be static to
980 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700981 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -0700982 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800983 } else if (mOnResumeState == State.WIDGETS) {
984 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700985 }
986 mOnResumeState = State.NONE;
987
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700988 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700989 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
990 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700991
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800992 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700993 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700994 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700995 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700997 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700999 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001000 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1001 // execute them here
1002 long startTimeCallbacks = 0;
1003 if (DEBUG_RESUME_TIME) {
1004 startTimeCallbacks = System.currentTimeMillis();
1005 }
1006
Michael Jurka1e2f4652013-07-08 18:03:46 -07001007 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1008 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001009 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001010 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001011 if (DEBUG_RESUME_TIME) {
1012 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1013 (System.currentTimeMillis() - startTimeCallbacks));
1014 }
Michael Jurka447bf842013-05-15 14:52:15 +02001015 }
Michael Jurka54554252013-08-01 12:52:23 +02001016 if (mOnResumeCallbacks.size() > 0) {
1017 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1018 mOnResumeCallbacks.get(i).run();
1019 }
1020 mOnResumeCallbacks.clear();
1021 }
Winson Chunge4e50662012-01-23 14:45:13 -08001022
1023 // Reset the pressed state of icons that were locked in the press state while activities
1024 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001025 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001026 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001027 mWaitingForResume.setStayPressed(false);
1028 }
Winson Chung82963d52013-10-09 11:20:57 -07001029
Adam Cohen06dff352012-06-01 17:17:08 -07001030 // It is possible that widgets can receive updates while launcher is not in the foreground.
1031 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1032 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1033 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001034 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001035 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001036
Michael Jurka447bf842013-05-15 14:52:15 +02001037 if (DEBUG_RESUME_TIME) {
1038 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1039 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001040
1041 if (mWorkspace.getCustomContentCallbacks() != null) {
1042 // If we are resuming and the custom content is the current page, we call onShow().
1043 // It is also poassible that onShow will instead be called slightly after first layout
1044 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1045 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001046 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001047 }
1048 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001049 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001050 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001051
Sunny Goyal756adbc2015-04-16 15:20:51 -07001052 if (!isWorkspaceLoading()) {
1053 // Process any items that were added while Launcher was away.
1054 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1055 }
Adam Cohen9211d422014-10-07 18:14:53 -07001056
1057 if (mLauncherCallbacks != null) {
1058 mLauncherCallbacks.onResume();
1059 }
Adam Cohen06dff352012-06-01 17:17:08 -07001060 }
1061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001063 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001064 // Ensure that items added to Launcher are queued until Launcher returns
1065 InstallShortcutReceiver.enableInstallQueue();
1066
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001067 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001068 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001069 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001070 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001071
1072 // We call onHide() aggressively. The custom content callbacks should be able to
1073 // debounce excess onHide calls.
1074 if (mWorkspace.getCustomContentCallbacks() != null) {
1075 mWorkspace.getCustomContentCallbacks().onHide();
1076 }
Romain Guycbb89e42009-06-08 15:52:54 -07001077
Adam Cohen9211d422014-10-07 18:14:53 -07001078 if (mLauncherCallbacks != null) {
1079 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001080 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001081 }
1082
1083 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001084 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1085 // by a onResume or by scrolling otherwise.
1086 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001087
1088 // Custom content is completely hidden
1089 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001090
1091 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1092 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001093
1094 // Indicates whether the user is allowed to scroll away from the custom content.
1095 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
1097
Adam Cohenc2d6e892014-10-16 09:49:24 -07001098 public interface LauncherOverlay {
1099
1100 /**
1101 * Touch interaction leading to overscroll has begun
1102 */
1103 public void onScrollInteractionBegin();
1104
1105 /**
1106 * Touch interaction related to overscroll has ended
1107 */
1108 public void onScrollInteractionEnd();
1109
1110 /**
1111 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1112 * screen (or in the case of RTL, the rightmost screen).
1113 */
1114 public void onScrollChange(int progress, boolean rtl);
1115
1116 /**
1117 * Screen has stopped scrolling
1118 */
1119 public void onScrollSettled();
1120
1121 /**
1122 * This method can be called by the Launcher in order to force the LauncherOverlay
1123 * to exit fully immersive mode.
1124 */
1125 public void forceExitFullImmersion();
1126 }
1127
Jun Mukai8af0cd82015-05-12 12:32:12 -07001128 public interface LauncherSearchCallbacks {
1129 /**
1130 * Called when the search overlay is shown.
1131 */
1132 public void onSearchOverlayOpened();
1133
1134 /**
1135 * Called when the search overlay is dismissed.
1136 */
1137 public void onSearchOverlayClosed();
1138 }
1139
Adam Cohenc2d6e892014-10-16 09:49:24 -07001140 public interface LauncherOverlayCallbacks {
1141 /**
1142 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1143 * however it doesn't modify any state within the launcher.
1144 */
1145 public boolean canEnterFullImmersion();
1146
1147 /**
1148 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1149 * eg. by occupying the full screen and handling all touch events.
1150 *
1151 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1152 * case, Launcher will modify any necessary state and assumes the overlay is
1153 * handling all interaction. If false, the LauncherOverlay should cancel any
1154 *
1155 */
1156 public boolean enterFullImmersion();
1157
1158 /**
1159 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1160 * full control over UI and state.
1161 */
1162 public void exitFullImmersion();
1163 }
1164
1165 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1166
1167 @Override
1168 public boolean canEnterFullImmersion() {
1169 return mState == State.WORKSPACE;
1170 }
1171
1172 @Override
1173 public boolean enterFullImmersion() {
1174 if (mState == State.WORKSPACE) {
1175 // When fully immersed, disregard any touches which fall through.
1176 mDragLayer.setBlockTouch(true);
1177 return true;
1178 }
1179 return false;
1180 }
1181
1182 @Override
1183 public void exitFullImmersion() {
1184 mDragLayer.setBlockTouch(false);
1185 }
1186 }
1187
Jorim Jaggid017f882014-01-14 17:08:48 -08001188 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001189 if (mLauncherCallbacks != null) {
1190 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001191 } else {
1192 // On devices with a locked orientation, we will at least have the allow rotation
1193 // setting.
1194 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001195 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001196 }
1197
Adam Cohen9211d422014-10-07 18:14:53 -07001198 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001199 CustomContentCallbacks callbacks, String description) {
1200 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001201 }
1202
Adam Cohenedb40762013-07-18 16:45:45 -07001203 // The custom content needs to offset its content to account for the QSB
1204 public int getTopOffsetForCustomContent() {
1205 return mWorkspace.getPaddingTop();
1206 }
1207
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001208 @Override
1209 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001211 if (mModel.isCurrentCallbacks(this)) {
1212 mModel.stopLoader();
1213 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001214 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1215
Romain Guy13c2e7b2010-03-10 19:45:00 -08001216 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001217 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001218
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 @Override
1221 public void onWindowFocusChanged(boolean hasFocus) {
1222 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001223 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001224
1225 if (mLauncherCallbacks != null) {
1226 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1227 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001228 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 private boolean acceptFilter() {
1231 final InputMethodManager inputManager = (InputMethodManager)
1232 getSystemService(Context.INPUT_METHOD_SERVICE);
1233 return !inputManager.isFullscreenMode();
1234 }
1235
1236 @Override
1237 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001238 final int uniChar = event.getUnicodeChar();
1239 final boolean handled = super.onKeyDown(keyCode, event);
1240 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1241 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1243 keyCode, event);
1244 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001245 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001246 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001247 // showSearchDialog()
1248 // If there are multiple keystrokes before the search dialog takes focus,
1249 // onSearchRequested() will be called for every keystroke,
1250 // but it is idempotent, so it's fine.
1251 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
1253 }
1254
Joe Onorato8a9625e2010-01-28 15:55:35 -08001255 // Eat the long press event so the keyboard doesn't come up.
1256 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1257 return true;
1258 }
1259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 return handled;
1261 }
1262
Karl Rosaen138a0412009-04-23 19:00:21 -07001263 private String getTypedText() {
1264 return mDefaultKeySsb.toString();
1265 }
1266
1267 private void clearTypedText() {
1268 mDefaultKeySsb.clear();
1269 mDefaultKeySsb.clearSpans();
1270 Selection.setSelection(mDefaultKeySsb, 0);
1271 }
1272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001274 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1275 * State
1276 */
1277 private static State intToState(int stateOrdinal) {
1278 State state = State.WORKSPACE;
1279 final State[] stateValues = State.values();
1280 for (int i = 0; i < stateValues.length; i++) {
1281 if (stateValues[i].ordinal() == stateOrdinal) {
1282 state = stateValues[i];
1283 break;
1284 }
1285 }
1286 return state;
1287 }
1288
1289 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 * Restores the previous state, if it exists.
1291 *
1292 * @param savedState The previous state.
1293 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001294 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 private void restoreState(Bundle savedState) {
1296 if (savedState == null) {
1297 return;
1298 }
1299
Michael Jurka883f55b2010-10-21 15:47:14 -07001300 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001301 if (state == State.APPS || state == State.WIDGETS) {
1302 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001303 }
1304
Adam Cohen21cd0022013-10-09 18:57:02 -07001305 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1306 PagedView.INVALID_RESTORE_PAGE);
1307 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001308 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
1310
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001312 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001313
Winson Chung3d503fb2011-07-13 17:25:49 -07001314 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1315 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001316 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001317 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1318 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001319 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1320 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001321 AppWidgetProviderInfo info = savedState.getParcelable(
1322 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001323 mPendingAddWidgetInfo = info == null ?
1324 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1325
Adam Cohen4637b5a2013-11-04 18:21:24 -08001326 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001327 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 mRestoring = true;
1329 }
1330
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001331 mItemIdToViewId = (HashMap<Integer, Integer>)
1332 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334
1335 /**
1336 * Finds all the views we need and configure them properly.
1337 */
1338 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001339 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001340
Craig Mautner360310b2012-10-26 15:13:08 -07001341 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001342 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001343 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1344 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001345 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001346 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001347
John Spurlock77e1f472013-09-11 10:09:51 -04001348 mLauncherView.setSystemUiVisibility(
1349 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001350 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351
Winson Chung4c98d922011-05-31 16:50:48 -07001352 // Setup the drag layer
1353 mDragLayer.setup(this, dragController);
1354
Winson Chung3d503fb2011-07-13 17:25:49 -07001355 // Setup the hotseat
1356 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1357 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001358 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 }
1360
Jorim Jaggid017f882014-01-14 17:08:48 -08001361 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001362 View widgetButton = findViewById(R.id.widget_button);
1363 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001364 @Override
1365 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001366 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001367 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001368 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001369 }
1370 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001371 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1372
1373 View wallpaperButton = findViewById(R.id.wallpaper_button);
1374 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001375 @Override
1376 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001377 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001378 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001379 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001380 }
1381 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001382 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1383
1384 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001385 if (hasSettings()) {
1386 settingsButton.setOnClickListener(new OnClickListener() {
1387 @Override
1388 public void onClick(View arg0) {
1389 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001390 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001391 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001392 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001393 });
1394 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1395 } else {
1396 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001397 }
1398
Adam Cohendbdff6b2013-09-18 19:09:15 -07001399 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001400
Winson Chung4c98d922011-05-31 16:50:48 -07001401 // Setup the workspace
1402 mWorkspace.setHapticFeedbackEnabled(false);
1403 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001404 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001405 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001406
Winson Chungf0ea4d32011-06-06 14:27:16 -07001407 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001408 mSearchDropTargetBar = (SearchDropTargetBar)
1409 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001410
Winson Chungef7f8742015-06-04 17:18:17 -07001411 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001412 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001413 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001414 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1415 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1416 } else {
1417 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1418 }
Craig Mautner360310b2012-10-26 15:13:08 -07001419
Winson Chung3d503fb2011-07-13 17:25:49 -07001420 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001421 dragController.setDragScoller(mWorkspace);
1422 dragController.setScrollView(mDragLayer);
1423 dragController.setMoveTarget(mWorkspace);
1424 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001425 if (mSearchDropTargetBar != null) {
1426 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001427 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001428 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001429
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001430 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001431 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001432 mWeightWatcher = new WeightWatcher(this);
1433 mWeightWatcher.setAlpha(0.5f);
1434 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001435 new FrameLayout.LayoutParams(
1436 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001437 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001438 Gravity.BOTTOM)
1439 );
Adam Cohen39a06042013-07-19 14:30:12 -07001440
1441 boolean show = shouldShowWeightWatcher();
1442 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001443 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
1445
1446 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001447 * Sets the all apps button. This method is called from {@link Hotseat}.
1448 */
1449 public void setAllAppsButton(View allAppsButton) {
1450 mAllAppsButton = allAppsButton;
1451 }
1452
1453 public View getAllAppsButton() {
1454 return mAllAppsButton;
1455 }
1456
1457 /**
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;
1492 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001493 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001494
Sunny Goyal5c97f512015-05-19 16:03:28 -07001495 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001496 if (info == null) {
1497 return;
1498 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001499 final View view = createShortcut(info);
1500
Sunny Goyal5c97f512015-05-19 16:03:28 -07001501 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001502 // First we check if we already know the exact location where we want to add this item.
1503 if (cellX >= 0 && cellY >= 0) {
1504 cellXY[0] = cellX;
1505 cellXY[1] = cellY;
1506 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001507
1508 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001509 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001510 true, null,null)) {
1511 return;
1512 }
1513 DragObject dragObject = new DragObject();
1514 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001515 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1516 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001517 return;
1518 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001519 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001520 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001521 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001522 foundCellSpan = (result != null);
1523 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001524 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001525 }
1526
1527 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001528 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001529 return;
1530 }
1531
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001532 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533
1534 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001535 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001536 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
1538 }
1539
Adam Cohen2e6da152015-05-06 11:42:25 -07001540 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1541 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001542 // We want to account for the extra amount of padding that we are adding to the widget
1543 // to ensure that it gets the full amount of space that it has requested
1544 int requiredWidth = minWidth + padding.left + padding.right;
1545 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001546 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001547 }
1548
Adam Cohen2e6da152015-05-06 11:42:25 -07001549 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1550 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001551 }
1552
Adam Cohen2e6da152015-05-06 11:42:25 -07001553 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1554 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001555 }
1556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001558 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001560 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 */
Adam Cohen091440a2015-03-18 14:16:05 -07001562 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001563 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1564
1565 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001566 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001567 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1568 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Adam Cohen59400422014-03-05 18:07:04 -08001571 if (appWidgetInfo.isCustomWidget) {
1572 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001573 }
1574
Adam Cohen59400422014-03-05 18:07:04 -08001575 LauncherAppWidgetInfo launcherInfo;
1576 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1577 launcherInfo.spanX = info.spanX;
1578 launcherInfo.spanY = info.spanY;
1579 launcherInfo.minSpanX = info.minSpanX;
1580 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001581 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001584 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585
1586 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001587 if (hostView == null) {
1588 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001589 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1590 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001591 } else {
1592 // The AppWidgetHostView has already been inflated and instantiated
1593 launcherInfo.hostView = hostView;
1594 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001596 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001597 launcherInfo.notifyWidgetSizeChanged(this);
1598
Adam Cohen59400422014-03-05 18:07:04 -08001599 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1600 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001601
1602 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001604 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 }
Romain Guycbb89e42009-06-08 15:52:54 -07001606
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1608 @Override
1609 public void onReceive(Context context, Intent intent) {
1610 final String action = intent.getAction();
1611 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1612 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001613 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001614 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001615
Winson Chungc100e8e2011-08-09 16:02:43 -07001616 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001617 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001618 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001619 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001620 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001621 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001622 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1623 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001624 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001625 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1626 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001627 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001628 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1629 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1630 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001631 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001632 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1633 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 }
1635 }
1636 };
1637
1638 @Override
1639 public void onAttachedToWindow() {
1640 super.onAttachedToWindow();
1641
1642 // Listen for broadcasts related to user-presence
1643 final IntentFilter filter = new IntentFilter();
1644 filter.addAction(Intent.ACTION_SCREEN_OFF);
1645 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001646 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001647 if (ENABLE_DEBUG_INTENTS) {
1648 filter.addAction(DebugIntents.DELETE_DATABASE);
1649 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1650 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001652 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001653 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001654 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001655 mVisible = true;
1656 }
1657
Adam Cohen0b395352014-06-09 22:54:36 +00001658 /**
1659 * Sets up transparent navigation and status bars in LMP.
1660 * This method is a no-op for other platform versions.
1661 */
Sunny Goyald0091012015-01-20 15:55:34 -08001662 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001663 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001664 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001665 Window window = getWindow();
1666 window.getAttributes().systemUiVisibility |=
1667 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1668 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1669 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1670 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1671 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1672 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1673 window.setStatusBarColor(Color.TRANSPARENT);
1674 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001675 }
1676 }
1677
Adam Cohended9f8d2010-11-03 13:25:16 -07001678 @Override
1679 public void onDetachedFromWindow() {
1680 super.onDetachedFromWindow();
1681 mVisible = false;
1682
Adam Cohend113e0c2010-11-11 10:48:05 -08001683 if (mAttached) {
1684 unregisterReceiver(mReceiver);
1685 mAttached = false;
1686 }
Winson Chungb745afb2015-03-02 11:51:23 -08001687 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001688 }
1689
1690 public void onWindowVisibilityChanged(int visibility) {
1691 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001692 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001693 // The following code used to be in onResume, but it turns out onResume is called when
1694 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1695 // is a more appropriate event to handle
1696 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001697 if (!mWorkspaceLoading) {
1698 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001699 // We want to let Launcher draw itself at least once before we force it to build
1700 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001701 // apps is nice and speedy.
1702 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001703 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001704 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001705 if (mStarted) return;
1706 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001707 // We delay the layer building a bit in order to give
1708 // other message processing a time to run. In particular
1709 // this avoids a delay in hiding the IME if it was
1710 // currently shown, because doing that may involve
1711 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001712 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001713 final ViewTreeObserver.OnDrawListener listener = this;
1714 mWorkspace.post(new Runnable() {
1715 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001716 if (mWorkspace != null &&
1717 mWorkspace.getViewTreeObserver() != null) {
1718 mWorkspace.getViewTreeObserver().
1719 removeOnDrawListener(listener);
1720 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001721 }
1722 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001723 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001724 }
1725 });
1726 }
1727 clearTypedText();
1728 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
1730
Adam Cohen091440a2015-03-18 14:16:05 -07001731 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 mHandler.removeMessages(ADVANCE_MSG);
1733 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1734 mHandler.sendMessageDelayed(msg, delay);
1735 mAutoAdvanceSentTime = System.currentTimeMillis();
1736 }
1737
Adam Cohen091440a2015-03-18 14:16:05 -07001738 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1740 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1741 mAutoAdvanceRunning = autoAdvanceRunning;
1742 if (autoAdvanceRunning) {
1743 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1744 sendAdvanceMessage(delay);
1745 } else {
1746 if (!mWidgetsToAdvance.isEmpty()) {
1747 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1748 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1749 }
1750 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001751 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 }
1753 }
1754 }
1755
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001756 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1757
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001759 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 if (msg.what == ADVANCE_MSG) {
1761 int i = 0;
1762 for (View key: mWidgetsToAdvance.keySet()) {
1763 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1764 final int delay = mAdvanceStagger * i;
1765 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001766 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 public void run() {
1768 ((Advanceable) v).advance();
1769 }
1770 }, delay);
1771 }
1772 i++;
1773 }
1774 sendAdvanceMessage(mAdvanceInterval);
1775 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001776 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001778 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001779
1780 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001781 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1783 if (v instanceof Advanceable) {
1784 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001785 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001786 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 }
1788 }
1789
1790 void removeWidgetToAutoAdvance(View hostView) {
1791 if (mWidgetsToAdvance.containsKey(hostView)) {
1792 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001793 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001794 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001795 }
1796
Joe Onorato9c1289c2009-08-17 11:03:03 -04001797 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001798 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001799 launcherInfo.hostView = null;
1800 }
1801
Hyunyoung Song3f471442015-04-08 19:01:34 -07001802 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001803 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1804 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001805 }
1806
Winson Chunga6945242014-01-08 14:04:34 -08001807 public DragLayer getDragLayer() {
1808 return mDragLayer;
1809 }
1810
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001811 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001812 return mAppsView;
1813 }
1814
Hyunyoung Song3f471442015-04-08 19:01:34 -07001815 public WidgetsContainerView getWidgetsView() {
1816 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001817 }
1818
Winson Chunga6945242014-01-08 14:04:34 -08001819 public Workspace getWorkspace() {
1820 return mWorkspace;
1821 }
1822
1823 public Hotseat getHotseat() {
1824 return mHotseat;
1825 }
1826
Jorim Jaggid017f882014-01-14 17:08:48 -08001827 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001828 return mOverviewPanel;
1829 }
1830
1831 public SearchDropTargetBar getSearchBar() {
1832 return mSearchDropTargetBar;
1833 }
1834
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001835 public LauncherAppWidgetHost getAppWidgetHost() {
1836 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
Romain Guycbb89e42009-06-08 15:52:54 -07001838
Winson Chunga9abd0e2010-10-27 17:18:37 -07001839 public LauncherModel getModel() {
1840 return mModel;
1841 }
1842
Winson Chunga6945242014-01-08 14:04:34 -08001843 protected SharedPreferences getSharedPrefs() {
1844 return mSharedPrefs;
1845 }
1846
Adam Cohen2e6da152015-05-06 11:42:25 -07001847 public DeviceProfile getDeviceProfile() {
1848 return mDeviceProfile;
1849 }
1850
Bjorn Bringertc459e522013-06-07 19:36:01 +01001851 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001852 getWindow().closeAllPanels();
1853
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001854 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001855 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001856 }
1857
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 @Override
1859 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001860 long startTime = 0;
1861 if (DEBUG_RESUME_TIME) {
1862 startTime = System.currentTimeMillis();
1863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 super.onNewIntent(intent);
1865
1866 // Close the menu
1867 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001868 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001869 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001870
Adam Cohened307df2013-10-02 09:37:31 -07001871 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1872 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1873 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001874
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 if (mWorkspace == null) {
1876 // Can be cases where mWorkspace is null, this prevents a NPE
1877 return;
1878 }
1879 Folder openFolder = mWorkspace.getOpenFolder();
1880 // In all these cases, only animate if we're already on home
1881 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001882
1883 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1884 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001885 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001886 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001887 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001888 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001889
Adam Cohen6fecd412013-10-02 17:41:50 -07001890 closeFolder();
1891 exitSpringLoadedDragMode();
1892
1893 // If we are already on home, then just animate back to the workspace,
1894 // otherwise, just wait until onResume to set the state back to Workspace
1895 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001896 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001897 } else {
1898 mOnResumeState = State.WORKSPACE;
1899 }
1900
1901 final View v = getWindow().peekDecorView();
1902 if (v != null && v.getWindowToken() != null) {
1903 InputMethodManager imm = (InputMethodManager)getSystemService(
1904 INPUT_METHOD_SERVICE);
1905 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1906 }
1907
Winson Chungb745afb2015-03-02 11:51:23 -08001908 // Reset the apps view
1909 if (!alreadyOnHome && mAppsView != null) {
1910 mAppsView.scrollToTop();
1911 }
1912
Hyunyoung Song3f471442015-04-08 19:01:34 -07001913 // Reset the widgets view
1914 if (!alreadyOnHome && mWidgetsView != null) {
1915 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001916 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001917
Adam Cohen9211d422014-10-07 18:14:53 -07001918 if (mLauncherCallbacks != null) {
1919 mLauncherCallbacks.onHomeIntent();
1920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
Adam Cohened307df2013-10-02 09:37:31 -07001922
Michael Jurka447bf842013-05-15 14:52:15 +02001923 if (DEBUG_RESUME_TIME) {
1924 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926
Adam Cohen9211d422014-10-07 18:14:53 -07001927 if (mLauncherCallbacks != null) {
1928 mLauncherCallbacks.onNewIntent(intent);
1929 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001930 }
1931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001933 public void onRestoreInstanceState(Bundle state) {
1934 super.onRestoreInstanceState(state);
1935 for (int page: mSynchronouslyBoundPages) {
1936 mWorkspace.restoreInstanceStateForChild(page);
1937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 }
1939
1940 @Override
1941 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001942 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001943 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1944 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001945 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001946 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947
Michael Jurka883f55b2010-10-21 15:47:14 -07001948 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001949 // We close any open folder since it will not be re-opened, and we need to make sure
1950 // this state is reflected.
1951 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952
Adam Cohendcd297f2013-06-18 13:13:40 -07001953 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001954 mWaitingForResult) {
1955 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001956 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001957 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1958 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001959 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1960 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1961 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001962 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
1964
Hyunyoung Song3f471442015-04-08 19:01:34 -07001965 // Save the current widgets tray?
1966 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001967 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001968
1969 if (mLauncherCallbacks != null) {
1970 mLauncherCallbacks.onSaveInstanceState(outState);
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 @Override
1975 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001977
Winson Chunge7a03942011-08-05 15:05:12 -07001978 // Remove all pending runnables
1979 mHandler.removeMessages(ADVANCE_MSG);
1980 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001981 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001982
Winson Chungcd2b0142011-06-08 16:02:26 -07001983 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001984 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001985
1986 // It's possible to receive onDestroy after a new Launcher activity has
1987 // been created. In this case, don't interfere with the new Launcher.
1988 if (mModel.isCurrentCallbacks(this)) {
1989 mModel.stopLoader();
1990 app.setLauncher(null);
1991 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001994 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001996 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001998 mAppWidgetHost = null;
1999
2000 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001
2002 TextKeyListener.getInstance().release();
2003
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002004 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002005
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002006 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002007 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002008 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002009 mWorkspace = null;
2010 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002011
Michael Jurka2ecf9952012-06-18 12:52:28 -07002012 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002013
2014 if (mLauncherCallbacks != null) {
2015 mLauncherCallbacks.onDestroy();
2016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
Adam Cohena9cf38f2011-05-02 15:36:58 -07002019 public DragController getDragController() {
2020 return mDragController;
2021 }
2022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 @Override
2024 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002025 onStartForResult(requestCode);
2026 super.startActivityForResult(intent, requestCode);
2027 }
2028
2029 @Override
2030 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2031 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2032 onStartForResult(requestCode);
2033 try {
2034 super.startIntentSenderForResult(intent, requestCode,
2035 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2036 } catch (IntentSender.SendIntentException e) {
2037 throw new ActivityNotFoundException();
2038 }
2039 }
2040
2041 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002042 if (requestCode >= 0) {
2043 setWaitingForResult(true);
2044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046
Winson Chung70d72102011-08-12 11:24:35 -07002047 /**
2048 * Indicates that we want global search for this activity by setting the globalSearch
2049 * argument for {@link #startSearch} to true.
2050 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002052 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002054
Karl Rosaen138a0412009-04-23 19:00:21 -07002055 if (initialQuery == null) {
2056 // Use any text typed in the launcher as the initial query
2057 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 if (appSearchData == null) {
2060 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002061 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
Winson Chungadf0c182012-08-23 14:59:07 -07002063 Rect sourceBounds = new Rect();
2064 if (mSearchDropTargetBar != null) {
2065 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2066 }
Romain Guycbb89e42009-06-08 15:52:54 -07002067
Ian Parkinson047036e2014-09-01 15:40:53 +01002068 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002069 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002070 if (clearTextImmediately) {
2071 clearTypedText();
2072 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002073
2074 // We need to show the workspace after starting the search
2075 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002076 }
2077
Ian Parkinson047036e2014-09-01 15:40:53 +01002078 /**
2079 * Start a text search.
2080 *
2081 * @return {@code true} if the search will start immediately, so any further keypresses
2082 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2083 * to buffer keypresses.
2084 */
2085 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002086 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002087 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2088 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2089 sourceBounds);
2090 }
2091
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002093 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002094 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 }
2096
2097 /**
2098 * Starts the global search activity. This code is a copied from SearchManager
2099 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002100 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002101 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002102 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002103 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2104 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2105 if (globalSearchActivity == null) {
2106 Log.w(TAG, "No global search activity found.");
2107 return;
2108 }
2109 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2110 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2111 intent.setComponent(globalSearchActivity);
2112 // Make sure that we have a Bundle to put source in
2113 if (appSearchData == null) {
2114 appSearchData = new Bundle();
2115 } else {
2116 appSearchData = new Bundle(appSearchData);
2117 }
Adam Cohen9211d422014-10-07 18:14:53 -07002118 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002119 if (!appSearchData.containsKey("source")) {
2120 appSearchData.putString("source", getPackageName());
2121 }
2122 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2123 if (!TextUtils.isEmpty(initialQuery)) {
2124 intent.putExtra(SearchManager.QUERY, initialQuery);
2125 }
2126 if (selectInitialQuery) {
2127 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2128 }
2129 intent.setSourceBounds(sourceBounds);
2130 try {
2131 startActivity(intent);
2132 } catch (ActivityNotFoundException ex) {
2133 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 }
2136
Yura4f93ec62014-02-04 14:15:21 +00002137 public boolean isOnCustomContent() {
2138 return mWorkspace.isOnOrMovingToCustomContent();
2139 }
2140
Winson Chung70d72102011-08-12 11:24:35 -07002141 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002142 public boolean onPrepareOptionsMenu(Menu menu) {
2143 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002144 if (!isOnCustomContent()) {
2145 // Close any open folders
2146 closeFolder();
2147 // Stop resizing any widgets
2148 mWorkspace.exitWidgetResizeMode();
2149 if (!mWorkspace.isInOverviewMode()) {
2150 // Show the overview mode
2151 showOverviewMode(true);
2152 } else {
2153 showWorkspace(true);
2154 }
Winson Chunge0298742014-01-17 12:03:00 -08002155 }
Adam Cohen9211d422014-10-07 18:14:53 -07002156 if (mLauncherCallbacks != null) {
2157 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2158 }
2159
Michael Jurkab94f3f82013-09-11 18:08:54 +02002160 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002163 @Override
2164 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002165 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002166 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002167 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002168 }
2169
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 public boolean isWorkspaceLocked() {
2171 return mWorkspaceLoading || mWaitingForResult;
2172 }
2173
Adam Cohen517a7f52014-03-01 12:12:59 -08002174 public boolean isWorkspaceLoading() {
2175 return mWorkspaceLoading;
2176 }
2177
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002178 private void setWorkspaceLoading(boolean value) {
2179 boolean isLocked = isWorkspaceLocked();
2180 mWorkspaceLoading = value;
2181 if (isLocked != isWorkspaceLocked()) {
2182 onWorkspaceLockedChanged();
2183 }
2184 }
2185
2186 private void setWaitingForResult(boolean value) {
2187 boolean isLocked = isWorkspaceLocked();
2188 mWaitingForResult = value;
2189 if (isLocked != isWorkspaceLocked()) {
2190 onWorkspaceLockedChanged();
2191 }
2192 }
2193
Adam Cohen9211d422014-10-07 18:14:53 -07002194 protected void onWorkspaceLockedChanged() {
2195 if (mLauncherCallbacks != null) {
2196 mLauncherCallbacks.onWorkspaceLockedChanged();
2197 }
2198 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002199
Michael Jurka0280c3b2010-09-17 15:00:07 -07002200 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002201 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002202 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002203 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2204 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002205 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002206 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002207 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002208
Sunny Goyale6b63a32015-01-16 16:14:29 -08002209 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002210 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002211 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2212 }
2213
Sunny Goyale6b63a32015-01-16 16:14:29 -08002214 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002215 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2216 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002217 if (appWidgetInfo.configure != null) {
2218 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002219 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002220
Bjorn Bringert7984c942009-12-09 15:38:25 +00002221 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002222 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2223 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002226 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002227 Runnable onComplete = new Runnable() {
2228 @Override
2229 public void run() {
2230 // Exit spring loaded mode if necessary after adding the widget
2231 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2232 null);
2233 }
2234 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002235 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002236 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002237 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 }
2239 }
Romain Guycbb89e42009-06-08 15:52:54 -07002240
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002241 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002242 // Close any folders that may be open.
2243 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002244 mWorkspace.moveToCustomContentScreen(animate);
2245 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002246
2247 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2248 int[] cell, int spanX, int spanY) {
2249 switch (info.itemType) {
2250 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2251 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2252 int span[] = new int[2];
2253 span[0] = spanX;
2254 span[1] = spanY;
2255 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2256 container, screenId, cell, span);
2257 break;
2258 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2259 processShortcutFromDrop(info.componentName, container, screenId, cell);
2260 break;
2261 default:
2262 throw new IllegalStateException("Unknown item type: " + info.itemType);
2263 }
2264 }
2265
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 /**
2267 * Process a shortcut drop.
2268 *
2269 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002270 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002272 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002273 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2274 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002275 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002276 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002277 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002278 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279
2280 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002281 mPendingAddInfo.cellX = cell[0];
2282 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284
2285 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2286 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002287 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002288 }
2289
Adam Cohenfbba09b2011-07-18 21:43:05 -07002290 /**
2291 * Process a widget drop.
2292 *
2293 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002294 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002295 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002297 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2298 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002299 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002300 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002301 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002302 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002303 mPendingAddInfo.minSpanX = info.minSpanX;
2304 mPendingAddInfo.minSpanY = info.minSpanY;
2305
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002307 mPendingAddInfo.cellX = cell[0];
2308 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002310 if (span != null) {
2311 mPendingAddInfo.spanX = span[0];
2312 mPendingAddInfo.spanY = span[1];
2313 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314
Adam Cohened66b2b2012-01-23 17:28:51 -08002315 AppWidgetHostView hostView = info.boundWidget;
2316 int appWidgetId;
2317 if (hostView != null) {
2318 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002319 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002320
2321 // Clear the boundWidget so that it doesn't get destroyed.
2322 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002323 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002324 // In this case, we either need to start an activity to get permission to bind
2325 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002326 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002327 Bundle options = info.bindOptions;
2328
Sunny Goyalffe83f12014-08-14 17:39:34 -07002329 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2330 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002331 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002332 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002333 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002334 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002335 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2336 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2337 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002338 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2339 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002340 // TODO: we need to make sure that this accounts for the options bundle.
2341 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002342 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2343 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002344 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 }
2346
Adam Cohendcd297f2013-06-18 13:13:40 -07002347 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002348 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002349 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 folderInfo.title = getText(R.string.folder_name);
2351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002353 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2354 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002355 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356
2357 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 FolderIcon newFolder =
2359 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002360 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002361 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002362 // Force measure the new folder icon
2363 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2364 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002365 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
Romain Guycbb89e42009-06-08 15:52:54 -07002367
Joe Onorato9c1289c2009-08-17 11:03:03 -04002368 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002369 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002370 }
2371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 @Override
2373 public boolean dispatchKeyEvent(KeyEvent event) {
2374 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2375 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002377 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002378 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002379 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002380 dumpState();
2381 return true;
2382 }
2383 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002384 }
2385 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2386 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002387 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 return true;
2389 }
2390 }
2391
2392 return super.dispatchKeyEvent(event);
2393 }
2394
Joe Onorato88ec0992009-11-19 13:16:06 -08002395 @Override
2396 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002397 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2398 return;
2399 }
2400
Sunny Goyal45478022015-06-08 16:52:41 -07002401 if (mDragController.isDragging()) {
2402 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002403 return;
2404 }
2405
Winson Chungb745afb2015-03-02 11:51:23 -08002406 if (isAppsViewVisible()) {
2407 showWorkspace(true);
2408 } else if (isWidgetsViewVisible()) {
2409 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002410 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002411 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002412 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002413 Folder openFolder = mWorkspace.getOpenFolder();
2414 if (openFolder.isEditingName()) {
2415 openFolder.dismissEditingName();
2416 } else {
2417 closeFolder();
2418 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002419 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002420 mWorkspace.exitWidgetResizeMode();
2421
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002422 // Back button is a no-op here, but give at least some feedback for the button press
2423 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002424 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002425 }
2426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002428 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002429 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002430 @Override
2431 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002432 if (mAppWidgetHost != null) {
2433 mAppWidgetHost.startListening();
2434 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002435 }
2436
2437 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 * Launches the intent referred by the clicked shortcut.
2439 *
2440 * @param v The view representing the clicked shortcut.
2441 */
2442 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002443 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2444 // view has detached (it's possible for this to happen if the view is removed mid touch).
2445 if (v.getWindowToken() == null) {
2446 return;
2447 }
2448
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002449 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002450 return;
2451 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002452
Adam Cohen1697b792013-09-17 19:08:21 -07002453 if (v instanceof Workspace) {
2454 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002455 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002456 }
2457 return;
2458 }
2459
2460 if (v instanceof CellLayout) {
2461 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002462 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002463 }
2464 }
2465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002467 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002468 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002470 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002471 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002472 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002473 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002474 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002475 } else if (tag instanceof AppInfo) {
2476 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002477 } else if (tag instanceof LauncherAppWidgetInfo) {
2478 if (v instanceof PendingAppWidgetHostView) {
2479 onClickPendingWidget((PendingAppWidgetHostView) v);
2480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002481 }
2482 }
2483
Sunny Goyal70660032015-05-14 00:07:08 -07002484 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002485 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002486 return false;
2487 }
2488
Michael Jurkaaf442092010-06-10 17:01:57 -07002489 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002490 * Event handler for the app widget view which has not fully restored.
2491 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002492 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002493 if (mIsSafeModeEnabled) {
2494 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2495 return;
2496 }
2497
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002498 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002499 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002500 int widgetId = info.appWidgetId;
2501 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2502 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002503 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2504 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002505 mPendingAddInfo.copyFrom(info);
2506 mPendingAddWidgetId = widgetId;
2507
Sunny Goyalffe83f12014-08-14 17:39:34 -07002508 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2509 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002510 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002511 } else if (info.installProgress < 0) {
2512 // The install has not been queued
2513 final String packageName = info.providerName.getPackageName();
2514 showBrokenAppInstallDialog(packageName,
2515 new DialogInterface.OnClickListener() {
2516 public void onClick(DialogInterface dialog, int id) {
2517 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2518 }
2519 });
2520 } else {
2521 // Download has started.
2522 final String packageName = info.providerName.getPackageName();
2523 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002524 }
2525 }
2526
2527 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002528 * Event handler for the "grid" button that appears on the home screen, which
2529 * enters all apps mode.
2530 *
2531 * @param v The view that was clicked.
2532 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002533 protected void onClickAllAppsButton(View v) {
2534 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002535 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002536 showWorkspace(true);
2537 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002538 // Try and refresh the set of predicted apps before we enter launcher
2539 showAppsView(true /* animated */, false /* resetListToTop */,
2540 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002541 }
2542 }
2543
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002544 private void showBrokenAppInstallDialog(final String packageName,
2545 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002546 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002547 .setTitle(R.string.abandoned_promises_title)
2548 .setMessage(R.string.abandoned_promise_explanation)
2549 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2550 .setNeutralButton(R.string.abandoned_clean_this,
2551 new DialogInterface.OnClickListener() {
2552 public void onClick(DialogInterface dialog, int id) {
2553 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2554 mWorkspace.removeAbandonedPromise(packageName, user);
2555 }
2556 })
2557 .create().show();
2558 return;
2559 }
2560
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002561 /**
2562 * Event handler for an app shortcut click.
2563 *
2564 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2565 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002566 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002567 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2568 Object tag = v.getTag();
2569 if (!(tag instanceof ShortcutInfo)) {
2570 throw new IllegalArgumentException("Input must be a Shortcut");
2571 }
2572
2573 // Open shortcut
2574 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002575
2576 if (shortcut.isDisabled != 0) {
2577 int error = R.string.activity_not_available;
2578 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2579 error = R.string.safemode_shortcut_error;
2580 }
2581 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2582 return;
2583 }
2584
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002585 final Intent intent = shortcut.intent;
2586
2587 // Check for special shortcuts
2588 if (intent.getComponent() != null) {
2589 final String shortcutClass = intent.getComponent().getClassName();
2590
2591 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2592 MemoryDumpActivity.startDump(this);
2593 return;
2594 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2595 toggleShowWeightWatcher();
2596 return;
2597 }
2598 }
2599
Chris Wren40c5ed32014-06-24 18:24:23 -04002600 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002601 if ((v instanceof BubbleTextView)
2602 && shortcut.isPromise()
2603 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002604 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002605 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002606 new DialogInterface.OnClickListener() {
2607 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002608 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002609 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002610 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002611 return;
2612 }
2613
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002614 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002615 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002616
2617 if (mLauncherCallbacks != null) {
2618 mLauncherCallbacks.onClickAppShortcut(v);
2619 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002620 }
2621
Adam Cohen091440a2015-03-18 14:16:05 -07002622 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002623 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002624 final ShortcutInfo shortcut;
2625 final Intent intent;
2626 if (tag instanceof ShortcutInfo) {
2627 shortcut = (ShortcutInfo) tag;
2628 intent = shortcut.intent;
2629 int[] pos = new int[2];
2630 v.getLocationOnScreen(pos);
2631 intent.setSourceBounds(new Rect(pos[0], pos[1],
2632 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002633
Sunny Goyal508da152014-08-14 10:53:27 -07002634 } else if (tag instanceof AppInfo) {
2635 shortcut = null;
2636 intent = ((AppInfo) tag).intent;
2637 } else {
2638 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2639 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002640
2641 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002642 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643
2644 if (success && v instanceof BubbleTextView) {
2645 mWaitingForResume = (BubbleTextView) v;
2646 mWaitingForResume.setStayPressed(true);
2647 }
2648 }
2649
2650 /**
2651 * Event handler for a folder icon click.
2652 *
2653 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2654 */
2655 protected void onClickFolderIcon(View v) {
2656 if (LOGD) Log.d(TAG, "onClickFolder");
2657 if (!(v instanceof FolderIcon)){
2658 throw new IllegalArgumentException("Input must be a FolderIcon");
2659 }
2660
2661 FolderIcon folderIcon = (FolderIcon) v;
2662 final FolderInfo info = folderIcon.getFolderInfo();
2663 Folder openFolder = mWorkspace.getFolderForTag(info);
2664
2665 // If the folder info reports that the associated folder is open, then verify that
2666 // it is actually opened. There have been a few instances where this gets out of sync.
2667 if (info.opened && openFolder == null) {
2668 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2669 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2670 info.opened = false;
2671 }
2672
2673 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2674 // Close any open folder
2675 closeFolder();
2676 // Open the requested folder
2677 openFolder(folderIcon);
2678 } else {
2679 // Find the open folder...
2680 int folderScreen;
2681 if (openFolder != null) {
2682 folderScreen = mWorkspace.getPageForView(openFolder);
2683 // .. and close it
2684 closeFolder(openFolder);
2685 if (folderScreen != mWorkspace.getCurrentPage()) {
2686 // Close any folder open on the current screen
2687 closeFolder();
2688 // Pull the folder onto this screen
2689 openFolder(folderIcon);
2690 }
2691 }
2692 }
Adam Cohen9211d422014-10-07 18:14:53 -07002693
2694 if (mLauncherCallbacks != null) {
2695 mLauncherCallbacks.onClickFolderIcon(v);
2696 }
Michael Jurka5130e402011-10-13 04:55:35 -07002697 }
2698
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002699 /**
2700 * Event handler for the (Add) Widgets button that appears after a long press
2701 * on the home screen.
2702 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002703 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002705 if (mIsSafeModeEnabled) {
2706 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2707 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002708 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002709 if (mLauncherCallbacks != null) {
2710 mLauncherCallbacks.onClickAddWidgetButton(view);
2711 }
Adam Cohen9211d422014-10-07 18:14:53 -07002712 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002713 }
2714
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715 /**
2716 * Event handler for the wallpaper picker button that appears after a long press
2717 * on the home screen.
2718 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002719 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002720 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002721 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2722 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002723
2724 if (mLauncherCallbacks != null) {
2725 mLauncherCallbacks.onClickWallpaperPicker(v);
2726 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002727 }
2728
2729 /**
2730 * Event handler for a click on the settings button that appears after a long press
2731 * on the home screen.
2732 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002733 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002734 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002735 if (mLauncherCallbacks != null) {
2736 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002737 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002738 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002739 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002740 }
2741
Adam Cohen61f560d2013-09-30 15:58:20 -07002742 public View.OnTouchListener getHapticFeedbackTouchListener() {
2743 if (mHapticFeedbackTouchListener == null) {
2744 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002745 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002746 @Override
2747 public boolean onTouch(View v, MotionEvent event) {
2748 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2749 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2750 }
2751 return false;
2752 }
2753 };
2754 }
2755 return mHapticFeedbackTouchListener;
2756 }
2757
Adam Cohen9211d422014-10-07 18:14:53 -07002758 public void onDragStarted(View view) {
2759 if (isOnCustomContent()) {
2760 // Custom content screen doesn't participate in drag and drop. If on custom
2761 // content screen, move to default.
2762 moveWorkspaceToDefaultScreen();
2763 }
2764
2765 if (mLauncherCallbacks != null) {
2766 mLauncherCallbacks.onDragStarted(view);
2767 }
2768 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002769
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002770 /**
2771 * Called when the user stops interacting with the launcher.
2772 * This implies that the user is now on the homescreen and is not doing housekeeping.
2773 */
Adam Cohen9211d422014-10-07 18:14:53 -07002774 protected void onInteractionEnd() {
2775 if (mLauncherCallbacks != null) {
2776 mLauncherCallbacks.onInteractionEnd();
2777 }
2778 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002779
2780 /**
2781 * Called when the user starts interacting with the launcher.
2782 * The possible interactions are:
2783 * - open all apps
2784 * - reorder an app shortcut, or a widget
2785 * - open the overview mode.
2786 * This is a good time to stop doing things that only make sense
2787 * when the user is on the homescreen and not doing housekeeping.
2788 */
Adam Cohen9211d422014-10-07 18:14:53 -07002789 protected void onInteractionBegin() {
2790 if (mLauncherCallbacks != null) {
2791 mLauncherCallbacks.onInteractionBegin();
2792 }
2793 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002794
Winson Chungcd99cd32015-04-29 11:03:24 -07002795 /** Updates the interaction state. */
2796 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002797 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002798 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002799 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2800 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002801 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002802 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002803 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002804 onInteractionEnd();
2805 }
2806 }
2807
Kenny Guyf07af7b2014-07-31 11:39:16 +01002808 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002809 try {
2810 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002811 launcherApps.showAppDetailsForProfile(componentName, user);
2812 } catch (SecurityException e) {
2813 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2814 Log.e(TAG, "Launcher does not have permission to launch settings");
2815 } catch (ActivityNotFoundException e) {
2816 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2817 Log.e(TAG, "Unable to launch settings");
2818 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002819 }
2820
Michael Jurka1e2f4652013-07-08 18:03:46 -07002821 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002822 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2823 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002824 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002825 // System applications cannot be installed. For now, show a toast explaining that.
2826 // We may give them the option of disabling apps this way.
2827 int messageId = R.string.uninstall_system_app_text;
2828 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002829 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002830 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002831 String packageName = componentName.getPackageName();
2832 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002833 Intent intent = new Intent(
2834 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002835 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2836 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002837 if (user != null) {
2838 user.addToIntent(intent, Intent.EXTRA_USER);
2839 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002840 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002841 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002842 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002843 }
2844
Winson Chung8f1eff72015-05-28 17:33:40 -07002845 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002846 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002847 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002848 // Only launch using the new animation if the shortcut has not opted out (this is a
2849 // private contract between launcher and may be ignored in the future).
2850 boolean useLaunchAnimation = (v != null) &&
2851 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002852 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2853 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002854
Kenny Guy1317e2d2014-05-08 18:52:50 +01002855 UserHandleCompat user = null;
2856 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2857 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2858 user = userManager.getUserForSerialNumber(serialNumber);
2859 }
2860
2861 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002862 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002863 ActivityOptions opts = null;
2864 if (sClipRevealMethod != null) {
2865 // TODO: call method directly when Launcher3 can depend on M APIs
2866 int left = 0, top = 0;
2867 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2868 if (v instanceof TextView) {
2869 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002870 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2871 if (icon != null) {
2872 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002873 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002874 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002875 width = bounds.width();
2876 height = bounds.height();
2877 }
2878 }
2879 try {
2880 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2881 left, top, width, height);
2882 } catch (IllegalAccessException e) {
2883 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2884 sClipRevealMethod = null;
2885 } catch (InvocationTargetException e) {
2886 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2887 sClipRevealMethod = null;
2888 }
2889 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002890 if (opts == null && !Utilities.isLmpOrAbove()) {
2891 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002892 v.getMeasuredWidth(), v.getMeasuredHeight());
2893 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002894 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002895 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002896
2897 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2898 // Could be launching some bookkeeping activity
2899 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002900 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002901 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002902 launcherApps.startActivityForProfile(intent.getComponent(), user,
2903 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002904 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002905 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002906 } catch (SecurityException e) {
2907 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002908 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002909 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002910 "or use the exported attribute for this activity. "
2911 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002913 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002914 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002915
Sunny Goyal383c5072015-06-12 21:18:53 -07002916 @Thunk boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002917 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002918 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2919 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2920 return false;
2921 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002922 try {
2923 success = startActivity(v, intent, tag);
2924 } catch (ActivityNotFoundException e) {
2925 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2926 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2927 }
2928 return success;
2929 }
2930
Adam Cohen268c4752012-06-06 17:47:33 -07002931 /**
2932 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2933 * in the DragLayer in the exact absolute location of the original FolderIcon.
2934 */
2935 private void copyFolderIconToImage(FolderIcon fi) {
2936 final int width = fi.getMeasuredWidth();
2937 final int height = fi.getMeasuredHeight();
2938
2939 // Lazy load ImageView, Bitmap and Canvas
2940 if (mFolderIconImageView == null) {
2941 mFolderIconImageView = new ImageView(this);
2942 }
2943 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2944 mFolderIconBitmap.getHeight() != height) {
2945 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2946 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2947 }
2948
2949 DragLayer.LayoutParams lp;
2950 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2951 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2952 } else {
2953 lp = new DragLayer.LayoutParams(width, height);
2954 }
2955
Adam Cohen307fe232012-08-16 17:55:58 -07002956 // The layout from which the folder is being opened may be scaled, adjust the starting
2957 // view size by this scale factor.
2958 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002959 lp.customPosition = true;
2960 lp.x = mRectForFolderAnimation.left;
2961 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002962 lp.width = (int) (scale * width);
2963 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002964
2965 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2966 fi.draw(mFolderIconCanvas);
2967 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002968 if (fi.getFolder() != null) {
2969 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2970 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002971 }
Adam Cohen268c4752012-06-06 17:47:33 -07002972 // Just in case this image view is still in the drag layer from a previous animation,
2973 // we remove it and re-add it.
2974 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2975 mDragLayer.removeView(mFolderIconImageView);
2976 }
2977 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002978 if (fi.getFolder() != null) {
2979 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002980 }
Adam Cohen268c4752012-06-06 17:47:33 -07002981 }
2982
Adam Cohen2801caf2011-05-13 20:57:39 -07002983 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002984 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002985 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2986 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2987 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2988
Adam Cohenc51934b2011-07-26 21:07:43 -07002989 FolderInfo info = (FolderInfo) fi.getTag();
2990 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2991 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002992 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2993 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002994 }
2995
Adam Cohen268c4752012-06-06 17:47:33 -07002996 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2997 copyFolderIconToImage(fi);
2998 fi.setVisibility(View.INVISIBLE);
2999
Michael Jurka2ecf9952012-06-18 12:52:28 -07003000 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003001 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003002 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003003 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3004 }
3005 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003006 oa.start();
3007 }
3008
Adam Cohen268c4752012-06-06 17:47:33 -07003009 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003010 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003011 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3012 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3013 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3014
Adam Cohen268c4752012-06-06 17:47:33 -07003015 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003016
Adam Cohen268c4752012-06-06 17:47:33 -07003017 // We remove and re-draw the FolderIcon in-case it has changed
3018 mDragLayer.removeView(mFolderIconImageView);
3019 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003020 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003021 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003022 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003023 oa.addListener(new AnimatorListenerAdapter() {
3024 @Override
3025 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003026 if (cl != null) {
3027 cl.clearFolderLeaveBehind();
3028 // Remove the ImageView copy of the FolderIcon and make the original visible.
3029 mDragLayer.removeView(mFolderIconImageView);
3030 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003031 }
3032 }
3033 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003034 oa.start();
3035 }
3036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003037 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003038 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039 * is animated relative to the specified View. If the View is null, no animation
3040 * is played.
3041 *
3042 * @param folderInfo The FolderInfo describing the folder to open.
3043 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003044 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003045 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003046 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3047 if (openFolder != null && openFolder != folder) {
3048 // Close any open folder before opening a folder.
3049 closeFolder();
3050 }
3051
Adam Cohena9cf38f2011-05-02 15:36:58 -07003052 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053
Adam Cohena9cf38f2011-05-02 15:36:58 -07003054 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003055
Sunny Goyalf4066152015-04-15 09:42:19 -07003056 // While the folder is open, the position of the icon cannot change.
3057 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3058
Adam Cohen4554ee12011-08-03 16:13:21 -07003059 // Just verify that the folder hasn't already been added to the DragLayer.
3060 // There was a one-off crash where the folder had a parent already.
3061 if (folder.getParent() == null) {
3062 mDragLayer.addView(folder);
3063 mDragController.addDropTarget((DropTarget) folder);
3064 } else {
3065 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3066 folder.getParent() + ").");
3067 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003068 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003069 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003070
3071 // Notify the accessibility manager that this folder "window" has appeared and occluded
3072 // the workspace items
3073 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3074 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003075 }
3076
3077 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003078 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003079 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003080 if (folder.isEditingName()) {
3081 folder.dismissEditingName();
3082 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003083 closeFolder(folder);
3084 }
3085 }
3086
Sunny Goyal83a8f042015-05-19 12:52:12 -07003087 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003088 folder.getInfo().opened = false;
3089
3090 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3091 if (parent != null) {
3092 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3093 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003094 if (fi != null) {
3095 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3096 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003097 }
3098 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003099
3100 // Notify the accessibility manager that this folder "window" has disappeard and no
3101 // longer occludeds the workspace items
3102 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103 }
3104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003105 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003106 if (!isDraggingEnabled()) return false;
3107 if (isWorkspaceLocked()) return false;
3108 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109
Adam Cohen1697b792013-09-17 19:08:21 -07003110 if (v instanceof Workspace) {
3111 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003112 if (!mWorkspace.isTouchActive()) {
3113 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003114 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3115 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3116 return true;
3117 } else {
3118 return false;
3119 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003120 } else {
3121 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003122 }
Adam Cohen1697b792013-09-17 19:08:21 -07003123 }
3124
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003125 CellLayout.CellInfo longClickCellInfo = null;
3126 View itemUnderLongClick = null;
3127 if (v.getTag() instanceof ItemInfo) {
3128 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003129 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003130 itemUnderLongClick = longClickCellInfo.cell;
3131 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003132 }
3133
Winson Chung3d503fb2011-07-13 17:25:49 -07003134 // The hotseat touch handling does not go through Workspace, and we always allow long press
3135 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003136 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003137 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003138 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003139 // User long pressed on empty space
3140 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3141 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003142 if (mWorkspace.isInOverviewMode()) {
3143 mWorkspace.startReordering(v);
3144 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003145 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003148 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3149 mHotseat.getOrderInHotseat(
3150 longClickCellInfo.cellX,
3151 longClickCellInfo.cellY));
3152 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003154 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155 }
3156 }
3157 }
3158 return true;
3159 }
3160
Winson Chung3d503fb2011-07-13 17:25:49 -07003161 boolean isHotseatLayout(View layout) {
3162 return mHotseat != null && layout != null &&
3163 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3164 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003165
Winson Chung3d503fb2011-07-13 17:25:49 -07003166 /**
3167 * Returns the CellLayout of the specified container at the specified screen.
3168 */
Sunny Goyal92820592015-03-02 11:31:35 -08003169 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003170 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3171 if (mHotseat != null) {
3172 return mHotseat.getLayout();
3173 } else {
3174 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003175 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003176 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003177 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003178 }
3179 }
3180
Winson Chungb745afb2015-03-02 11:51:23 -08003181 /**
3182 * For overridden classes.
3183 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003184 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003185 return isAppsViewVisible();
3186 }
3187
3188 public boolean isAppsViewVisible() {
3189 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3190 }
3191
3192 public boolean isWidgetsViewVisible() {
3193 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003194 }
3195
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003196 private void setWorkspaceBackground(int background) {
3197 switch (background) {
3198 case WORKSPACE_BACKGROUND_TRANSPARENT:
3199 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3200 break;
3201 case WORKSPACE_BACKGROUND_BLACK:
3202 getWindow().setBackgroundDrawable(null);
3203 break;
3204 default:
3205 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3206 }
Craig Mautner360310b2012-10-26 15:13:08 -07003207 }
3208
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003209 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003210 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3211 int curflags = getWindow().getAttributes().flags
3212 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3213 if (wpflags != curflags) {
3214 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3215 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003216 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003217 }
3218
Michael Jurkae326f182011-11-21 14:05:46 -08003219 @Override
3220 public void onTrimMemory(int level) {
3221 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003222 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3223 // The widget preview db can result in holding onto over
3224 // 3MB of memory for caching which isn't necessary.
3225 SQLiteDatabase.releaseMemory();
3226
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003227 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003228 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003229 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003230 if (mLauncherCallbacks != null) {
3231 mLauncherCallbacks.onTrimMemory(level);
3232 }
Michael Jurkae326f182011-11-21 14:05:46 -08003233 }
3234
Winson Chungb745afb2015-03-02 11:51:23 -08003235 @Override
3236 public void onStateTransitionHideSearchBar() {
3237 // Hide the search bar
3238 if (mSearchDropTargetBar != null) {
3239 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3240 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003241 }
3242
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003243 public void showWorkspace(boolean animated) {
Winson Chungef7f8742015-06-04 17:18:17 -07003244 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003245 }
3246
Sunny Goyal83a8f042015-05-19 12:52:12 -07003247 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003248 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
Winson Chungef7f8742015-06-04 17:18:17 -07003249 onCompleteRunnable);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003250 }
3251
3252 protected void showWorkspace(int snapToPage, boolean animated) {
Winson Chungef7f8742015-06-04 17:18:17 -07003253 showWorkspace(snapToPage, animated, null);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003254 }
3255
Winson Chungef7f8742015-06-04 17:18:17 -07003256 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003257 boolean changed = mState != State.WORKSPACE ||
3258 mWorkspace.getState() != Workspace.State.NORMAL;
3259 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003260 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003261 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003262 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003263 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003264
Winson Chungc7d2b602012-05-16 17:02:20 -07003265 // Show the search bar (only animate if we were showing the drop target bar in spring
3266 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003267 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003268 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003269 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003270
Michael Jurkab3e22d92011-10-31 15:58:33 -07003271 // Set focus to the AppsCustomize button
3272 if (mAllAppsButton != null) {
3273 mAllAppsButton.requestFocus();
3274 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003275 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003276
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003277 // Change the state *after* we've called all the transition code
3278 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003279
Winson Chung5fb63472011-02-02 17:03:37 -08003280 // Resume the auto-advance of widgets
3281 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003282 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003283
Winson Chung0f785722015-04-08 10:27:49 -07003284 if (changed) {
3285 // Send an accessibility event to announce the context change
3286 getWindow().getDecorView()
3287 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003288 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003289 }
3290
Adam Cohened307df2013-10-02 09:37:31 -07003291 void showOverviewMode(boolean animated) {
3292 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003293 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003294 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3295 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003296 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003297 }
3298
Winson Chungb745afb2015-03-02 11:51:23 -08003299 /**
3300 * Shows the apps view.
3301 */
Winson Chung4ac30062015-05-08 17:34:17 -07003302 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003303 if (resetListToTop) {
3304 mAppsView.scrollToTop();
3305 }
Winson Chung4ac30062015-05-08 17:34:17 -07003306 if (updatePredictedApps) {
3307 tryAndUpdatePredictedApps();
3308 }
Winson Chungb745afb2015-03-02 11:51:23 -08003309 showAppsOrWidgets(animated, State.APPS);
3310 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003311
Winson Chungb745afb2015-03-02 11:51:23 -08003312 /**
3313 * Shows the widgets view.
3314 */
3315 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003316 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003317 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003318 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003319 }
Winson Chungb745afb2015-03-02 11:51:23 -08003320 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003321
3322 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003323 @Override
3324 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003325 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003326 }
3327 });
Winson Chungb745afb2015-03-02 11:51:23 -08003328 }
3329
3330 /**
3331 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003332 *
3333 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003334 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003335 // TODO: calling method should use the return value so that when {@code false} is returned
3336 // the workspace transition doesn't fall into invalid state.
3337 private boolean showAppsOrWidgets(boolean animated, State toState) {
3338 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3339 mState != State.WIDGETS_SPRING_LOADED) {
3340 return false;
3341 }
3342 if (toState != State.APPS && toState != State.WIDGETS) {
3343 return false;
3344 }
Winson Chungb745afb2015-03-02 11:51:23 -08003345
3346 if (toState == State.APPS) {
Winson Chungef7f8742015-06-04 17:18:17 -07003347 mStateTransitionAnimation.startAnimationToAllApps(mState, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003348 } else {
3349 mStateTransitionAnimation.startAnimationToWidgets(animated);
3350 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003351
Michael Jurkab3e22d92011-10-31 15:58:33 -07003352 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003353 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003354
3355 // Pause the auto-advance of widgets until we are out of AllApps
3356 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003357 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003358 closeFolder();
3359
3360 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003361 getWindow().getDecorView()
3362 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003363 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003364 }
3365
Winson Chungcd99cd32015-04-29 11:03:24 -07003366 /**
3367 * Updates the workspace and interaction state on state change, and return the animation to this
3368 * new state.
3369 */
3370 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
Winson Chungef7f8742015-06-04 17:18:17 -07003371 boolean animated, boolean hasOverlaySearchBar, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003372 Workspace.State fromState = mWorkspace.getState();
Winson Chungef7f8742015-06-04 17:18:17 -07003373 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated,
3374 hasOverlaySearchBar, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003375 updateInteraction(fromState, toState);
3376 return anim;
3377 }
3378
Hyunyoung Song3f471442015-04-08 19:01:34 -07003379 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003380 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003381 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3382 mState == State.WIDGETS_SPRING_LOADED) {
3383 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003384 }
Winson Chungb745afb2015-03-02 11:51:23 -08003385
3386 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003387 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3388 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003389 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003390 }
Adam Cohen7777d962011-08-18 18:58:38 -07003391
Hyunyoung Song3f471442015-04-08 19:01:34 -07003392 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003393 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003394 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003395
Winson Chunge7a03942011-08-05 15:05:12 -07003396 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003397 @Override
3398 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003399 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003400 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3401 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003402 // Before we show workspace, hide all apps again because
3403 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3404 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003405 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003406 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003407 } else {
3408 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003409 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003410 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003411 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003412 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413
Michael Jurkad3ef3062010-11-23 16:23:58 -08003414 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003415 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003416 showAppsView(true /* animated */, false /* resetListToTop */,
3417 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003418 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003419 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003420 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003421 }
3422
Winson Chung4ac30062015-05-08 17:34:17 -07003423 /**
3424 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3425 * resumed.
3426 */
3427 private void tryAndUpdatePredictedApps() {
3428 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003429 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003430 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003431 mAppsView.setPredictedApps(apps);
3432 }
3433 }
3434 }
3435
Michael Jurkab3e22d92011-10-31 15:58:33 -07003436 void lockAllApps() {
3437 // TODO
3438 }
3439
3440 void unlockAllApps() {
3441 // TODO
3442 }
3443
Sunny Goyal594d76d2014-11-06 10:12:54 -08003444 protected void disableVoiceButtonProxy(boolean disable) {
3445 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003446 }
3447
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003448 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003449 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3450 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003451 }
3452
Adam Cohen24ce0b32014-01-14 16:18:14 -08003453 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003454 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3455 if (searchProvider == null) {
3456 return null;
3457 }
3458
3459 Bundle opts = new Bundle();
3460 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003461 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003462
3463 SharedPreferences sp = getSharedPreferences(
3464 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3465 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003466 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003467 if (!searchProvider.provider.flattenToString().equals(
3468 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003469 || (widgetInfo == null)
3470 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003471 // A valid widget is not already bound.
3472 if (widgetId > -1) {
3473 mAppWidgetHost.deleteAppWidgetId(widgetId);
3474 widgetId = -1;
3475 }
3476
3477 // Try to bind a new widget
3478 widgetId = mAppWidgetHost.allocateAppWidgetId();
3479
3480 if (!AppWidgetManagerCompat.getInstance(this)
3481 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3482 mAppWidgetHost.deleteAppWidgetId(widgetId);
3483 widgetId = -1;
3484 }
3485
3486 sp.edit()
3487 .putInt(QSB_WIDGET_ID, widgetId)
3488 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3489 .commit();
3490 }
3491
3492 if (widgetId != -1) {
3493 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3494 mQsb.updateAppWidgetOptions(opts);
3495 mQsb.setPadding(0, 0, 0, 0);
3496 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003497 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003498 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003499 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003500 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003501 }
3502
Sunny Goyal22235bc2015-04-03 09:23:43 -07003503 private void reinflateQSBIfNecessary() {
3504 if (mQsb instanceof LauncherAppWidgetHostView &&
3505 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3506 mSearchDropTargetBar.removeView(mQsb);
3507 mQsb = null;
3508 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3509 }
3510 }
3511
Michael Jurkad7c28052012-04-27 15:43:36 -07003512 @Override
3513 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003514 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003515 final List<CharSequence> text = event.getText();
3516 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003517 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003518 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003519 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003520 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003521 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003522 } else {
3523 text.add(getString(R.string.all_apps_home_button_label));
3524 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003525 return result;
3526 }
3527
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003528 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003529 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003530 */
Adam Cohen091440a2015-03-18 14:16:05 -07003531 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003532 @Override
3533 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003534 closeSystemDialogs();
3535 }
3536 }
3537
3538 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003539 * If the activity is currently paused, signal that we need to run the passed Runnable
3540 * in onResume.
3541 *
3542 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003543 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3544 * wrong when we're not running, and if the activity comes back to what the configuration was
3545 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003546 *
3547 * Implementation of the method from LauncherModel.Callbacks.
3548 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003549 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003550 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003551 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003552 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003553 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003554 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003555 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003556 }
3557 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003558 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003559 return true;
3560 } else {
3561 return false;
3562 }
3563 }
3564
Michael Jurkac402cd92013-05-20 15:49:32 +02003565 private boolean waitUntilResume(Runnable run) {
3566 return waitUntilResume(run, false);
3567 }
3568
Michael Jurka1e2f4652013-07-08 18:03:46 -07003569 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003570 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003571 }
3572
Michael Jurka7607c2f2013-04-03 14:33:19 -07003573 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003574 * If the activity is currently paused, signal that we need to re-run the loader
3575 * in onResume.
3576 *
3577 * This needs to be called from incoming places where resources might have been loaded
3578 * while we are paused. That is becaues the Configuration might be wrong
3579 * when we're not running, and if it comes back to what it was when we
3580 * were paused, we are not restarted.
3581 *
3582 * Implementation of the method from LauncherModel.Callbacks.
3583 *
3584 * @return true if we are currently paused. The caller might be able to
3585 * skip some work in that case since we will come back again.
3586 */
3587 public boolean setLoadOnResume() {
3588 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003589 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003590 mOnResumeNeedsLoad = true;
3591 return true;
3592 } else {
3593 return false;
3594 }
3595 }
3596
3597 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003599 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003601 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003602 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003603 } else {
3604 return SCREEN_COUNT / 2;
3605 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003607
Joe Onorato9c1289c2009-08-17 11:03:03 -04003608 /**
3609 * Refreshes the shortcuts shown on the workspace.
3610 *
3611 * Implementation of the method from LauncherModel.Callbacks.
3612 */
3613 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003614 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003615
Michael Jurka7607c2f2013-04-03 14:33:19 -07003616 // If we're starting binding all over again, clear any bind calls we'd postponed in
3617 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3618 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003619 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003620
Winson Chungd64d1762013-08-20 14:37:16 -07003621 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003622 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003623 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003624
Michael Jurka05bf644e2011-11-30 20:28:53 -08003625 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003626 if (mHotseat != null) {
3627 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003628 }
3629 }
3630
Adam Cohendcd297f2013-06-18 13:13:40 -07003631 @Override
3632 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003633 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003634
Adam Cohen5084cba2013-09-03 12:01:16 -07003635 // If there are no screens, we need to have an empty screen
3636 if (orderedScreenIds.size() == 0) {
3637 mWorkspace.addExtraEmptyScreen();
3638 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003639
3640 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003641 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003642 if (hasCustomContentToLeft()) {
3643 mWorkspace.createCustomContentContainer();
3644 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003645 }
Winson Chung64359a52013-07-08 17:17:08 -07003646 }
3647
3648 @Override
3649 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003650 int count = orderedScreenIds.size();
3651 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003652 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003653 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003654 }
3655
Adam Cohen39a06042013-07-19 14:30:12 -07003656 private boolean shouldShowWeightWatcher() {
3657 String spKey = LauncherAppState.getSharedPreferencesKey();
3658 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003659 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003660
3661 return show;
3662 }
3663
3664 private void toggleShowWeightWatcher() {
3665 String spKey = LauncherAppState.getSharedPreferencesKey();
3666 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3667 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3668
3669 show = !show;
3670
3671 SharedPreferences.Editor editor = sp.edit();
3672 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3673 editor.commit();
3674
3675 if (mWeightWatcher != null) {
3676 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3677 }
3678 }
3679
Winson Chungd64d1762013-08-20 14:37:16 -07003680 public void bindAppsAdded(final ArrayList<Long> newScreens,
3681 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003682 final ArrayList<ItemInfo> addAnimated,
3683 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003684 Runnable r = new Runnable() {
3685 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003686 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003687 }
3688 };
3689 if (waitUntilResume(r)) {
3690 return;
3691 }
3692
Winson Chungd64d1762013-08-20 14:37:16 -07003693 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003694 if (newScreens != null) {
3695 bindAddScreens(newScreens);
3696 }
Winson Chungd64d1762013-08-20 14:37:16 -07003697
3698 // We add the items without animation on non-visible pages, and with
3699 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003700 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003701 bindItems(addNotAnimated, 0,
3702 addNotAnimated.size(), false);
3703 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003704 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003705 bindItems(addAnimated, 0,
3706 addAnimated.size(), true);
3707 }
Winson Chungc58497e2013-09-03 17:48:37 -07003708
Winson Chung87412982013-10-03 18:34:14 -07003709 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003710 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003711
Winson Chungb745afb2015-03-02 11:51:23 -08003712 if (addedApps != null && mAppsView != null) {
3713 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003714 }
Winson Chungd64d1762013-08-20 14:37:16 -07003715 }
3716
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003717 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 * Bind the items start-end from the list.
3719 *
3720 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003721 */
Winson Chung64359a52013-07-08 17:17:08 -07003722 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3723 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003724 Runnable r = new Runnable() {
3725 public void run() {
3726 bindItems(shortcuts, start, end, forceAnimateIcons);
3727 }
3728 };
3729 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003730 return;
3731 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003732
Winson Chungf0c6ae02012-03-21 16:10:31 -07003733 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003734 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3735 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003736 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003737 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003738 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003739 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003741
3742 // Short circuit if we are loading dock items for a configuration which has no dock
3743 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3744 mHotseat == null) {
3745 continue;
3746 }
3747
Joe Onorato9c1289c2009-08-17 11:03:03 -04003748 switch (item.itemType) {
3749 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3750 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003751 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003752 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003753
Winson Chung64359a52013-07-08 17:17:08 -07003754 /*
3755 * TODO: FIX collision case
3756 */
Winson Chungce376632013-07-11 16:12:41 -07003757 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3758 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3759 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003760 View v = cl.getChildAt(item.cellX, item.cellY);
3761 Object tag = v.getTag();
3762 String desc = "Collision while binding workspace item: " + item
3763 + ". Collides with " + tag;
3764 if (LauncherAppState.isDogfoodBuild()) {
3765 throw (new RuntimeException(desc));
3766 } else {
3767 Log.d(TAG, desc);
3768 }
Winson Chungce376632013-07-11 16:12:41 -07003769 }
Winson Chung64359a52013-07-08 17:17:08 -07003770 }
3771
Adam Cohendcd297f2013-06-18 13:13:40 -07003772 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3773 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003774 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003775 // Animate all the applications up now
3776 shortcut.setAlpha(0f);
3777 shortcut.setScaleX(0f);
3778 shortcut.setScaleY(0f);
3779 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003780 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003781 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003783 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003784 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003785 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003786 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003787 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3788 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003790 default:
3791 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003793 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003794
Winson Chung997a9232013-07-24 15:33:46 -07003795 if (animateIcons) {
3796 // Animate to the correct page
3797 if (newShortcutsScreenId > -1) {
3798 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003799 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003800 final Runnable startBounceAnimRunnable = new Runnable() {
3801 public void run() {
3802 anim.playTogether(bounceAnims);
3803 anim.start();
3804 }
3805 };
Winson Chung997a9232013-07-24 15:33:46 -07003806 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003807 // We post the animation slightly delayed to prevent slowdowns
3808 // when we are loading right after we return to launcher.
3809 mWorkspace.postDelayed(new Runnable() {
3810 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003811 if (mWorkspace != null) {
3812 mWorkspace.snapToPage(newScreenIndex);
3813 mWorkspace.postDelayed(startBounceAnimRunnable,
3814 NEW_APPS_ANIMATION_DELAY);
3815 }
Winson Chung94d67682013-09-25 16:29:40 -07003816 }
3817 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003818 } else {
3819 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003820 }
3821 }
Winson Chung64359a52013-07-08 17:17:08 -07003822 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003824 }
3825
Joe Onorato9c1289c2009-08-17 11:03:03 -04003826 /**
3827 * Implementation of the method from LauncherModel.Callbacks.
3828 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003829 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003830 Runnable r = new Runnable() {
3831 public void run() {
3832 bindFolders(folders);
3833 }
3834 };
3835 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003836 return;
3837 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003838 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003840
3841 /**
3842 * Add the views for a widget to the workspace.
3843 *
3844 * Implementation of the method from LauncherModel.Callbacks.
3845 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003846 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003847 Runnable r = new Runnable() {
3848 public void run() {
3849 bindAppWidget(item);
3850 }
3851 };
3852 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003853 return;
3854 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003855
Daniel Sandler843e8602010-06-07 14:59:01 -04003856 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3857 if (DEBUG_WIDGETS) {
3858 Log.d(TAG, "bindAppWidget: " + item);
3859 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 final Workspace workspace = mWorkspace;
3861
Adam Cohen59400422014-03-05 18:07:04 -08003862 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003863 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003864
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003865 if (!mIsSafeModeEnabled
3866 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3867 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003868 if (appWidgetInfo == null) {
3869 if (DEBUG_WIDGETS) {
3870 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3871 + " belongs to component " + item.providerName
3872 + ", as the povider is null");
3873 }
3874 LauncherModel.deleteItemFromDatabase(this, item);
3875 return;
3876 }
3877 // Note: This assumes that the id remap broadcast is received before this step.
3878 // If that is not the case, the id remap will be ignored and user may see the
3879 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07003880 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003881 pendingInfo.spanX = item.spanX;
3882 pendingInfo.spanY = item.spanY;
3883 pendingInfo.minSpanX = item.minSpanX;
3884 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003885 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003886 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003887
Sunny Goyalff572272014-07-23 13:58:07 -07003888 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003889 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3890 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003891
3892 // TODO consider showing a permission dialog when the widget is clicked.
3893 if (!success) {
3894 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3895 if (DEBUG_WIDGETS) {
3896 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3897 + " belongs to component " + item.providerName
3898 + ", as the launcher is unable to bing a new widget id");
3899 }
3900 LauncherModel.deleteItemFromDatabase(this, item);
3901 return;
3902 }
3903
3904 item.appWidgetId = newWidgetId;
3905
3906 // If the widget has a configure activity, it is still needs to set it up, otherwise
3907 // the widget is ready to go.
3908 item.restoreStatus = (appWidgetInfo.configure == null)
3909 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3910 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3911
3912 LauncherModel.updateItemInDatabase(this, item);
3913 }
3914
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003915 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003916 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003917 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003918 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3919 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003920 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003921
Sunny Goyal651077b2014-06-30 14:15:31 -07003922 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3923 } else {
3924 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003925 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3926 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003927 view.updateIcon(mIconCache);
3928 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003929 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003930 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003931 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003932
Joe Onorato9c1289c2009-08-17 11:03:03 -04003933 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003934 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935
Adam Cohendcd297f2013-06-18 13:13:40 -07003936 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003937 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003938 if (!item.isCustomWidget()) {
3939 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3940 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003941
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942 workspace.requestLayout();
3943
Daniel Sandler843e8602010-06-07 14:59:01 -04003944 if (DEBUG_WIDGETS) {
3945 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3946 + (SystemClock.uptimeMillis()-start) + "ms");
3947 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003948 }
3949
Sunny Goyalff572272014-07-23 13:58:07 -07003950 /**
3951 * Restores a pending widget.
3952 *
3953 * @param appWidgetId The app widget id
3954 * @param cellInfo The position on screen where to create the widget.
3955 */
3956 private void completeRestoreAppWidget(final int appWidgetId) {
3957 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3958 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3959 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3960 return;
3961 }
3962
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003963 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003964 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3965
3966 mWorkspace.reinflateWidgetsIfNecessary();
3967 LauncherModel.updateItemInDatabase(this, info);
3968 }
3969
Adam Cohen1462de32012-07-24 22:34:36 -07003970 public void onPageBoundSynchronously(int page) {
3971 mSynchronouslyBoundPages.add(page);
3972 }
3973
Joe Onorato9c1289c2009-08-17 11:03:03 -04003974 /**
3975 * Callback saying that there aren't any more items to bind.
3976 *
3977 * Implementation of the method from LauncherModel.Callbacks.
3978 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003979 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003980 Runnable r = new Runnable() {
3981 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003982 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003983 }
3984 };
3985 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003986 return;
3987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003988 if (mSavedState != null) {
3989 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003990 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003991 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003992 mSavedState = null;
3993 }
3994
Adam Cohen1462de32012-07-24 22:34:36 -07003995 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003996
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003997 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07003998 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07003999
4000 // If we received the result of any pending adds while the loader was running (e.g. the
4001 // widget configuration forced an orientation change), process them now.
4002 if (sPendingAddItem != null) {
4003 final long screenId = completeAdd(sPendingAddItem);
4004
4005 // TODO: this moves the user to the page where the pending item was added. Ideally,
4006 // the screen would be guaranteed to exist after bind, and the page would be set through
4007 // the workspace restore process.
4008 mWorkspace.post(new Runnable() {
4009 @Override
4010 public void run() {
4011 mWorkspace.snapToScreenId(screenId);
4012 }
4013 });
4014 sPendingAddItem = null;
4015 }
4016
Sunny Goyal756adbc2015-04-16 15:20:51 -07004017 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004018
4019 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004020 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004021 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004022 }
4023
Adam Cohen3ed316a2014-07-23 18:21:20 -07004024 private void sendLoadingCompleteBroadcastIfNecessary() {
4025 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4026 String permission =
4027 getResources().getString(R.string.receive_first_load_broadcast_permission);
4028 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4029 sendBroadcast(intent, permission);
4030 SharedPreferences.Editor editor = mSharedPrefs.edit();
4031 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4032 editor.apply();
4033 }
4034 }
4035
Winson Chunga0b7e862013-09-05 16:03:15 -07004036 public boolean isAllAppsButtonRank(int rank) {
4037 if (mHotseat != null) {
4038 return mHotseat.isAllAppsButtonRank(rank);
4039 }
4040 return false;
4041 }
4042
Winson Chunga2413752012-04-03 14:22:34 -07004043 private boolean canRunNewAppsAnimation() {
4044 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4045 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4046 }
4047
Winson Chungc9168342013-06-26 14:54:55 -07004048 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4049 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4050 PropertyValuesHolder.ofFloat("alpha", 1f),
4051 PropertyValuesHolder.ofFloat("scaleX", 1f),
4052 PropertyValuesHolder.ofFloat("scaleY", 1f));
4053 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4054 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004055 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004056 return bounceAnim;
4057 }
4058
Adam Cohen27772732013-11-11 14:00:56 +00004059 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004060 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004061 }
4062
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004063 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004064 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004065 }
4066
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004067 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004068 if (mSearchDropTargetBar == null) {
4069 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004070 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004071 if (mQsb != null) {
4072 mSearchDropTargetBar.removeView(mQsb);
4073 mQsb = null;
4074 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004075 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004076 }
4077
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004078 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004079 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4080 * multiple calls to bind the same list.)
4081 */
4082 @Thunk ArrayList<AppInfo> mTmpAppsList;
4083 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4084 public void run() {
4085 bindAllApplications(mTmpAppsList);
4086 mTmpAppsList = null;
4087 }
4088 };
4089
4090 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004091 * Add the icons for all apps.
4092 *
4093 * Implementation of the method from LauncherModel.Callbacks.
4094 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004095 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004096 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4097 mTmpAppsList = apps;
4098 return;
4099 }
4100
Winson Chungb745afb2015-03-02 11:51:23 -08004101 if (mAppsView != null) {
4102 mAppsView.setApps(apps);
4103 }
Adam Cohen9211d422014-10-07 18:14:53 -07004104 if (mLauncherCallbacks != null) {
4105 mLauncherCallbacks.bindAllApplications(apps);
4106 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107 }
4108
4109 /**
4110 * A package was updated.
4111 *
4112 * Implementation of the method from LauncherModel.Callbacks.
4113 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004114 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004115 Runnable r = new Runnable() {
4116 public void run() {
4117 bindAppsUpdated(apps);
4118 }
4119 };
4120 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004121 return;
4122 }
4123
Winson Chungb745afb2015-03-02 11:51:23 -08004124 if (mAppsView != null) {
4125 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004126 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004127 }
4128
Sunny Goyal4390ace2014-10-13 11:33:11 -07004129 @Override
4130 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4131 Runnable r = new Runnable() {
4132 public void run() {
4133 bindWidgetsRestored(widgets);
4134 }
4135 };
4136 if (waitUntilResume(r)) {
4137 return;
4138 }
4139 mWorkspace.widgetsRestored(widgets);
4140 }
4141
Joe Onorato9c1289c2009-08-17 11:03:03 -04004142 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004143 * Some shortcuts were updated in the background.
4144 *
4145 * Implementation of the method from LauncherModel.Callbacks.
4146 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004147 @Override
4148 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4149 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004150 Runnable r = new Runnable() {
4151 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004152 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004153 }
4154 };
4155 if (waitUntilResume(r)) {
4156 return;
4157 }
4158
Sunny Goyal4390ace2014-10-13 11:33:11 -07004159 if (!updated.isEmpty()) {
4160 mWorkspace.updateShortcuts(updated);
4161 }
4162
4163 if (!removed.isEmpty()) {
4164 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4165 for (ShortcutInfo si : removed) {
4166 removedComponents.add(si.getTargetComponent());
4167 }
4168 mWorkspace.removeItemsByComponentName(removedComponents, user);
4169 // Notify the drag controller
4170 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004171 }
4172 }
4173
4174 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004175 * Update the state of a package, typically related to install state.
4176 *
4177 * Implementation of the method from LauncherModel.Callbacks.
4178 */
Sunny Goyale755d462014-07-22 13:48:29 -07004179 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004180 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4181 Runnable r = new Runnable() {
4182 public void run() {
4183 bindRestoreItemsChange(updates);
4184 }
4185 };
4186 if (waitUntilResume(r)) {
4187 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004188 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004189
Sunny Goyal756adbc2015-04-16 15:20:51 -07004190 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004191 }
4192
4193 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004194 * A package was uninstalled. We take both the super set of packageNames
4195 * in addition to specific applications to remove, the reason being that
4196 * this can be called when a package is updated as well. In that scenario,
4197 * we only remove specific components from the workspace, where as
4198 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004199 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004200 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004201 * Implementation of the method from LauncherModel.Callbacks.
4202 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004203 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004204 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004205 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004206 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004207 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004208 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004209 }
Winson Chungd64d1762013-08-20 14:37:16 -07004210 };
4211 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004212 return;
4213 }
4214
Sunny Goyal1a745e82014-10-02 15:58:31 -07004215 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004216 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4217 for (AppInfo info : appInfos) {
4218 removedComponents.add(info.componentName);
4219 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004220 if (!packageNames.isEmpty()) {
4221 mWorkspace.removeItemsByPackageName(packageNames, user);
4222 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004223 if (!removedComponents.isEmpty()) {
4224 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004225 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004226 // Notify the drag controller
4227 mDragController.onAppsRemoved(packageNames, removedComponents);
4228
Sunny Goyal1a745e82014-10-02 15:58:31 -07004229 } else {
4230 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004231 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004232
Winson Chungdf95eb12013-10-16 14:57:07 -07004233 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004234 if (mAppsView != null) {
4235 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004237 }
Joe Onoratobe386092009-11-17 17:32:16 -08004238
Michael Jurkac402cd92013-05-20 15:49:32 +02004239 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004240 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004241 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004242 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004243 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004244
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004245 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004246 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004247 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004248 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004249 return;
4250 }
4251
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004252 if (mWidgetsView != null && model != null) {
4253 mWidgetsView.addWidgets(model);
4254 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004255 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004256 }
4257
Winson Chung400438b2011-01-16 17:53:48 -08004258 private int mapConfigurationOriActivityInfoOri(int configOri) {
4259 final Display d = getWindowManager().getDefaultDisplay();
4260 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4261 switch (d.getRotation()) {
4262 case Surface.ROTATION_0:
4263 case Surface.ROTATION_180:
4264 // We are currently in the same basic orientation as the natural orientation
4265 naturalOri = configOri;
4266 break;
4267 case Surface.ROTATION_90:
4268 case Surface.ROTATION_270:
4269 // We are currently in the other basic orientation to the natural orientation
4270 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4271 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4272 break;
4273 }
4274
4275 int[] oriMap = {
4276 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4277 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4278 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4279 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4280 };
4281 // Since the map starts at portrait, we need to offset if this device's natural orientation
4282 // is landscape.
4283 int indexOffset = 0;
4284 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4285 indexOffset = 1;
4286 }
4287 return oriMap[(d.getRotation() + indexOffset) % 4];
4288 }
Adam Cohen446e9402011-09-15 18:21:21 -07004289
Winson Chung4b919f82012-05-01 10:44:08 -07004290 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004291 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004292 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4293 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4294 .getConfiguration().orientation));
4295 } else {
4296 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4297 }
Winson Chung4b919f82012-05-01 10:44:08 -07004298 }
4299 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004300
Winson Chung4b919f82012-05-01 10:44:08 -07004301 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004302 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004303 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004304 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004305 } else {
4306 mHandler.postDelayed(new Runnable() {
4307 public void run() {
4308 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4309 }
4310 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004311 }
Winson Chung4b919f82012-05-01 10:44:08 -07004312 }
Winson Chung400438b2011-01-16 17:53:48 -08004313 }
4314
Winson Chunge43a1e72014-01-15 10:33:02 -08004315 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004316 if (mLauncherCallbacks != null) {
4317 return mLauncherCallbacks.isLauncherPreinstalled();
4318 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004319 PackageManager pm = getPackageManager();
4320 try {
4321 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4322 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4323 return true;
4324 } else {
4325 return false;
4326 }
4327 } catch (NameNotFoundException e) {
4328 e.printStackTrace();
4329 return false;
4330 }
4331 }
4332
Adam Cohenea90f832014-05-21 15:03:34 -07004333 /**
4334 * This method indicates whether or not we should suggest default wallpaper dimensions
4335 * when our wallpaper cropper was not yet used to set a wallpaper.
4336 */
4337 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004338 if (mLauncherCallbacks != null) {
4339 return mLauncherCallbacks.overrideWallpaperDimensions();
4340 }
Adam Cohenea90f832014-05-21 15:03:34 -07004341 return true;
4342 }
4343
Adam Cohen432609a2014-03-13 17:03:22 -07004344 /**
4345 * To be overridden by subclasses to indicate that there is an activity to launch
4346 * before showing the standard launcher experience.
4347 */
4348 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004349 if (mLauncherCallbacks != null) {
4350 return mLauncherCallbacks.hasFirstRunActivity();
4351 }
Adam Cohen432609a2014-03-13 17:03:22 -07004352 return false;
4353 }
4354
4355 /**
4356 * To be overridden by subclasses to launch any first run activity
4357 */
4358 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004359 if (mLauncherCallbacks != null) {
4360 return mLauncherCallbacks.getFirstRunActivity();
4361 }
Adam Cohen432609a2014-03-13 17:03:22 -07004362 return null;
4363 }
4364
Winson Chunga6945242014-01-08 14:04:34 -08004365 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004366 return !ActivityManager.isRunningInTestHarness() &&
4367 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004368 }
4369
Adam Cohen4a9423d2014-03-28 14:22:31 -07004370 protected boolean hasRunFirstRunActivity() {
4371 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4372 }
4373
Adam Cohen432609a2014-03-13 17:03:22 -07004374 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004375 if (shouldRunFirstRunActivity() &&
4376 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004377 Intent firstRunIntent = getFirstRunActivity();
4378 if (firstRunIntent != null) {
4379 startActivity(firstRunIntent);
4380 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004381 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004382 }
4383 }
Adam Cohen432609a2014-03-13 17:03:22 -07004384 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004385 }
4386
4387 private void markFirstRunActivityShown() {
4388 SharedPreferences.Editor editor = mSharedPrefs.edit();
4389 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4390 editor.apply();
4391 }
4392
Adam Cohen432609a2014-03-13 17:03:22 -07004393 /**
4394 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4395 * screen that must be displayed and dismissed.
4396 */
4397 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004398 if (mLauncherCallbacks != null) {
4399 return mLauncherCallbacks.hasDismissableIntroScreen();
4400 }
Adam Cohen432609a2014-03-13 17:03:22 -07004401 return false;
4402 }
4403
4404 /**
4405 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4406 */
4407 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004408 if (mLauncherCallbacks != null) {
4409 return mLauncherCallbacks.getIntroScreen();
4410 }
Adam Cohen432609a2014-03-13 17:03:22 -07004411 return null;
4412 }
4413
4414 /**
4415 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4416 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4417 */
4418 private boolean shouldShowIntroScreen() {
4419 return hasDismissableIntroScreen() &&
4420 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4421 }
4422
4423 protected void showIntroScreen() {
4424 View introScreen = getIntroScreen();
4425 changeWallpaperVisiblity(false);
4426 if (introScreen != null) {
4427 mDragLayer.showOverlayView(introScreen);
4428 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004429 if (mLauncherOverlayContainer != null) {
4430 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4431 }
Adam Cohen432609a2014-03-13 17:03:22 -07004432 }
4433
4434 public void dismissIntroScreen() {
4435 markIntroScreenDismissed();
4436 if (showFirstRunActivity()) {
4437 // We delay hiding the intro view until the first run activity is showing. This
4438 // avoids a blip.
4439 mWorkspace.postDelayed(new Runnable() {
4440 @Override
4441 public void run() {
4442 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004443 if (mLauncherOverlayContainer != null) {
4444 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4445 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004446 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004447 }
4448 }, ACTIVITY_START_DELAY);
4449 } else {
4450 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004451 if (mLauncherOverlayContainer != null) {
4452 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4453 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004454 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004455 }
4456 changeWallpaperVisiblity(true);
4457 }
4458
4459 private void markIntroScreenDismissed() {
4460 SharedPreferences.Editor editor = mSharedPrefs.edit();
4461 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4462 editor.apply();
4463 }
4464
Adam Cohen091440a2015-03-18 14:16:05 -07004465 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004466 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4467 // on the device, then we always show the first run cling experience (or if there is no
4468 // launcher2). Otherwise, we prompt the user upon started for migration
4469 LauncherClings launcherClings = new LauncherClings(this);
4470 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4471 if (mModel.canMigrateFromOldLauncherDb(this)) {
4472 launcherClings.showMigrationCling();
4473 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004474 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004475 }
4476 }
4477 }
4478
Winson Chunga6945242014-01-08 14:04:34 -08004479 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004480 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4481 if (mHotseat != null) mHotseat.setAlpha(1f);
4482 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4483 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004484 }
4485
4486 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004487 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4488 if (mHotseat != null) mHotseat.setAlpha(0f);
4489 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4490 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004491 }
4492
Mathew Inwood72fbec12013-11-19 15:45:07 +00004493 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004494 // Called from search suggestion, not supported in other profiles.
4495 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4496 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4497 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4498 myUser);
4499 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004500 return null;
4501 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004502 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004503 }
4504
4505 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4506 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004507 // Called from search suggestion, not supported in other profiles.
4508 return createShortcutDragInfo(shortcutIntent, caption, icon,
4509 UserHandleCompat.myUserHandle());
4510 }
4511
4512 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4513 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004514 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004515 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004516 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004517 }
4518
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004519 protected void moveWorkspaceToDefaultScreen() {
4520 mWorkspace.moveToDefaultScreen(false);
4521 }
4522
Mathew Inwood72fbec12013-11-19 15:45:07 +00004523 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4524 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004525 mWorkspace.onExternalDragStartedWithItem(dragView);
4526 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004527 }
4528
Anjali Koppalf5d29132014-02-28 13:33:27 -08004529 @Override
4530 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004531 if (mLauncherCallbacks != null) {
4532 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4533 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004534 }
4535
Winson Chung80baf5a2010-08-09 16:03:15 -07004536 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004537 * Returns a FastBitmapDrawable with the icon, accurately sized.
4538 */
4539 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4540 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4541 d.setFilterBitmap(true);
4542 resizeIconDrawable(d);
4543 return d;
4544 }
4545
4546 /**
4547 * Resizes an icon drawable to the correct icon size.
4548 */
4549 public void resizeIconDrawable(Drawable icon) {
4550 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4551 }
4552
4553 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004554 * Prints out out state for debugging.
4555 */
4556 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004557 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004558 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004559 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4560 Log.d(TAG, "mRestoring=" + mRestoring);
4561 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4562 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004563 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004564 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004565 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004566
Daniel Sandler325dc232013-06-05 22:57:57 -04004567 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004568 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004569
4570 @Override
4571 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4572 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004573 synchronized (sDumpLogs) {
4574 writer.println(" ");
4575 writer.println("Debug logs: ");
4576 for (int i = 0; i < sDumpLogs.size(); i++) {
4577 writer.println(" " + sDumpLogs.get(i));
4578 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004579 }
Adam Cohen9211d422014-10-07 18:14:53 -07004580 if (mLauncherCallbacks != null) {
4581 mLauncherCallbacks.dump(prefix, fd, writer, args);
4582 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004583 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004584
4585 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004586 if (DEBUG_DUMP_LOG) {
4587 synchronized (sDumpLogs) {
4588 Log.d(TAG, "");
4589 Log.d(TAG, "*********************");
4590 Log.d(TAG, "Launcher debug logs: ");
4591 for (int i = 0; i < sDumpLogs.size(); i++) {
4592 Log.d(TAG, " " + sDumpLogs.get(i));
4593 }
4594 Log.d(TAG, "*********************");
4595 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004596 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004597 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004598 }
4599
4600 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004601 addDumpLog(tag, log, null, debugLog);
4602 }
4603
4604 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004605 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004606 if (e != null) {
4607 Log.d(tag, log, e);
4608 } else {
4609 Log.d(tag, log);
4610 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004611 }
Winson Chungede41292013-09-19 16:27:36 -07004612 if (DEBUG_DUMP_LOG) {
4613 sDateStamp.setTime(System.currentTimeMillis());
4614 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004615 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4616 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004617 }
Winson Chungede41292013-09-19 16:27:36 -07004618 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004619 }
4620
Adam Cohen59400422014-03-05 18:07:04 -08004621 public static CustomAppWidget getCustomAppWidget(String name) {
4622 return sCustomAppWidgets.get(name);
4623 }
4624
4625 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4626 return sCustomAppWidgets;
4627 }
4628
Winson Chungede41292013-09-19 16:27:36 -07004629 public void dumpLogsToLocalData() {
4630 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004631 new AsyncTask<Void, Void, Void>() {
4632 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004633 boolean success = false;
4634 sDateStamp.setTime(sRunStart);
4635 String FILENAME = sDateStamp.getMonth() + "-"
4636 + sDateStamp.getDay() + "_"
4637 + sDateStamp.getHours() + "-"
4638 + sDateStamp.getMinutes() + "_"
4639 + sDateStamp.getSeconds() + ".txt";
4640
4641 FileOutputStream fos = null;
4642 File outFile = null;
4643 try {
4644 outFile = new File(getFilesDir(), FILENAME);
4645 outFile.createNewFile();
4646 fos = new FileOutputStream(outFile);
4647 } catch (Exception e) {
4648 e.printStackTrace();
4649 }
4650 if (fos != null) {
4651 PrintWriter writer = new PrintWriter(fos);
4652
4653 writer.println(" ");
4654 writer.println("Debug logs: ");
4655 synchronized (sDumpLogs) {
4656 for (int i = 0; i < sDumpLogs.size(); i++) {
4657 writer.println(" " + sDumpLogs.get(i));
4658 }
4659 }
4660 writer.close();
4661 }
4662 try {
4663 if (fos != null) {
4664 fos.close();
4665 success = true;
4666 }
4667 } catch (IOException e) {
4668 e.printStackTrace();
4669 }
Michael Jurka43467462013-11-14 12:03:58 +01004670 return null;
Winson Chungede41292013-09-19 16:27:36 -07004671 }
Michael Jurka43467462013-11-14 12:03:58 +01004672 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004673 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004675}
Michael Jurka2763be32011-02-24 11:19:57 -08004676
Dan Sandlerd5024042014-01-09 15:01:33 -05004677interface DebugIntents {
4678 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4679 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004680}