blob: 191fdf45732be1d4e7421e33a35829f58632def3 [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;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070086import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070088import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070091import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Advanceable;
94import android.widget.FrameLayout;
95import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080096import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070097import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070098
Sunny Goyal651077b2014-06-30 14:15:31 -070099import com.android.launcher3.DropTarget.DragObject;
100import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700101import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal5683f872015-05-29 14:54:40 -0700102import com.android.launcher3.allapps.AppSearchManager;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700103import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100104import com.android.launcher3.compat.LauncherActivityInfoCompat;
105import com.android.launcher3.compat.LauncherAppsCompat;
106import com.android.launcher3.compat.UserHandleCompat;
107import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700108import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700109import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700110import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700111import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700112import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700113import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700114
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700116import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800120import java.lang.reflect.InvocationTargetException;
121import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700124import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
137 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700138 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700139 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Winson Chung83f59ab2015-05-05 17:21:58 -0700141 // Temporary flag
142 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
143
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700145 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700146 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700148 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700149
Dan Sandlerd5024042014-01-09 15:01:33 -0500150 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700158 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700159
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700160 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
161 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
162 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
163
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700164 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
165
Mathew Inwood876a8462013-06-14 14:12:41 +0100166 /**
167 * IntentStarter uses request codes starting with this. This must be greater than all activity
168 * request codes used internally.
169 */
170 protected static final int REQUEST_LAST = 100;
171
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800172 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Michael Jurka0a457bf2012-11-19 14:05:05 -0800174 // To turn on these properties, type
175 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chung2672ff92012-05-04 16:22:30 -0700178 // The Intent extra that defines whether to ignore the launch animation
179 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400180 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
183 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700184 // Type: int
185 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
188 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
190 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700191 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700193 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700194 // Type: int
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
196 // Type: int
197 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
198 // Type: parcelable
199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000200 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800201 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000202 // Type: int[]
203 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Adam Cohen432609a2014-03-13 17:03:22 -0700205 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800206 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
207
Adam Cohen3ed316a2014-07-23 18:21:20 -0700208 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
209 static final String ACTION_FIRST_LOAD_COMPLETE =
210 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
211
Adam Cohen39a06042013-07-19 14:30:12 -0700212 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700213 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700214
Sunny Goyal594d76d2014-11-06 10:12:54 -0800215 private static final String QSB_WIDGET_ID = "qsb_widget_id";
216 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
217
Winson Chunga6945242014-01-08 14:04:34 -0800218 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
219
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700220 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700221 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
222
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700225
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700226 private boolean mIsSafeModeEnabled;
227
Adam Cohenc2d6e892014-10-16 09:49:24 -0700228 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
229 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700230 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700233 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
234 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700235 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000237 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
238 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
239
Winson Chunga2413752012-04-03 14:22:34 -0700240 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700241 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
242 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700243 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700244
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800245 private final BroadcastReceiver mCloseSystemDialogsReceiver
246 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800247 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private LayoutInflater mInflater;
250
Adam Cohen091440a2015-03-18 14:16:05 -0700251 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700252 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800253 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700254 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800255 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700256 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700257
Sunny Goyalffe83f12014-08-14 17:39:34 -0700258 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700259 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700260
Adam Cohen091440a2015-03-18 14:16:05 -0700261 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800262 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800263 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700264
Michael Jurka0280c3b2010-09-17 15:00:07 -0700265 private int[] mTmpAddItemCellCoordinates = new int[2];
266
Sunny Goyal316490e2015-06-02 09:38:28 -0700267 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800268 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700269
Michael Jurka838a4ca2011-02-07 13:33:06 -0800270 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700271
Winson Chungc51db6a2011-10-05 11:44:49 -0700272 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700273
274 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700275 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700276
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700277 // Main container view and the model for the widget tray screen.
278 @Thunk WidgetsContainerView mWidgetsView;
279 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700280
Winson Chungf0ea4d32011-06-06 14:27:16 -0700281 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800282 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700285 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
286 // scroll issues (because the workspace may not have been measured yet) and extra work.
287 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
288 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289
290 private SpannableStringBuilder mDefaultKeySsb = null;
291
Adam Cohen091440a2015-03-18 14:16:05 -0700292 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800294 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 private boolean mRestoring;
296 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700297 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298
Michael Jurka1e2f4652013-07-08 18:03:46 -0700299 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700300 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 private Bundle mSavedInstanceState;
303
Joe Onorato9c1289c2009-08-17 11:03:03 -0400304 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800305 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700306 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800307 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700308 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800309 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400310
Sunny Goyale2df0622015-04-24 11:27:00 -0700311 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700312
Adam Cohen61f560d2013-09-30 15:58:20 -0700313 private View.OnTouchListener mHapticFeedbackTouchListener;
314
Adam Cohended9f8d2010-11-03 13:25:16 -0700315 // Related to the auto-advancing of widgets
316 private final int ADVANCE_MSG = 1;
317 private final int mAdvanceInterval = 20000;
318 private final int mAdvanceStagger = 250;
319 private long mAutoAdvanceSentTime;
320 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700321 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700322 new HashMap<View, AppWidgetProviderInfo>();
323
Winson Chung400438b2011-01-16 17:53:48 -0800324 // Determines how long to wait after a rotation before restoring the screen orientation to
325 // match the sensor state.
326 private final int mRestoreScreenOrientationDelay = 500;
327
Adam Cohen091440a2015-03-18 14:16:05 -0700328 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700329
Adam Cohen1462de32012-07-24 22:34:36 -0700330 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700331 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700332
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700334 static Date sDateStamp = new Date();
335 static DateFormat sDateFormat =
336 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
337 static long sRunStart = System.currentTimeMillis();
338 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700339
Winson Chung46353de2012-02-16 14:05:10 -0800340 // We only want to get the SharedPreferences once since it does an FS stat each time we get
341 // it from the context.
342 private SharedPreferences mSharedPrefs;
343
Winson Chungf0c6ae02012-03-21 16:10:31 -0700344 // Holds the page that we need to animate to, and the icon views that we need to animate up
345 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700346 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700347 private Bitmap mFolderIconBitmap;
348 private Canvas mFolderIconCanvas;
349 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700350
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700351 private DeviceProfile mDeviceProfile;
352
Winson Chung13eb5272015-05-11 16:30:13 -0700353 // This is set to the view that launched the activity that navigated the user away from
354 // launcher. Since there is no callback for when the activity has finished launching, enable
355 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800356 private BubbleTextView mWaitingForResume;
357
Adam Cohen59400422014-03-05 18:07:04 -0800358 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
359 new HashMap<String, CustomAppWidget>();
360
361 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
362 static {
363 if (ENABLE_CUSTOM_WIDGET_TEST) {
364 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
365 }
366 }
367
Chet Haasea8f996d2015-02-17 12:56:04 -0800368 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
369 private static Method sClipRevealMethod = null;
370 static {
371 Class<?> activityOptionsClass = ActivityOptions.class;
372 try {
373 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
374 View.class, int.class, int.class, int.class, int.class);
375 } catch (Exception e) {
376 // Earlier version
377 }
378 }
379
Adam Cohen091440a2015-03-18 14:16:05 -0700380 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700381 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700382 if (mWorkspace != null) {
383 mWorkspace.buildPageHardwareLayers();
384 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700385 }
386 };
387
Adam Cohendb364c32014-05-20 14:23:40 -0700388 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800389
Adam Cohen091440a2015-03-18 14:16:05 -0700390 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800391 int requestCode;
392 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700393 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700394 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800395 int cellX;
396 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700397 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800398 }
399
Daniel Sandlerff02d492013-08-05 02:12:05 -0400400 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700401 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700402 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400403
404 @Thunk void setOrientation() {
405 if (mRotationEnabled) {
406 unlockScreenOrientation(true);
407 } else {
408 setRequestedOrientation(
409 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700410 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400411 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700412
Sunny Goyal7779d622015-06-11 16:18:39 -0700413 private Runnable mUpdateOrientationRunnable = new Runnable() {
414 public void run() {
415 setOrientation();
416 }
417 };
418
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 @Override
420 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700421 if (DEBUG_STRICT_MODE) {
422 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
423 .detectDiskReads()
424 .detectDiskWrites()
425 .detectNetwork() // or .detectAll() for all detectable problems
426 .penaltyLog()
427 .build());
428 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
429 .detectLeakedSqlLiteObjects()
430 .detectLeakedClosableObjects()
431 .penaltyLog()
432 .penaltyDeath()
433 .build());
434 }
435
Adam Cohen9211d422014-10-07 18:14:53 -0700436 if (mLauncherCallbacks != null) {
437 mLauncherCallbacks.preOnCreate();
438 }
439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400441
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400442 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400443 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700444 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700445
Adam Cohen2e6da152015-05-06 11:42:25 -0700446 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700447 mDeviceProfile = getResources().getConfiguration().orientation
448 == Configuration.ORIENTATION_LANDSCAPE ?
449 app.getInvariantDeviceProfile().landscapeProfile
450 : app.getInvariantDeviceProfile().portraitProfile;
451
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400452 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700453 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700454 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800455 mModel = app.setLauncher(this);
456 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700457
Joe Onorato41a12d22009-10-31 18:30:00 -0400458 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800460 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700461
Daniel Sandlerff02d492013-08-05 02:12:05 -0400462 mStats = new Stats(this);
463
Sunny Goyalffe83f12014-08-14 17:39:34 -0700464 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700465
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700466 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
467 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700468
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700469 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
470 // this also ensures that any synchronous binding below doesn't re-trigger another
471 // LauncherModel load.
472 mPaused = false;
473
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200475 android.os.Debug.startMethodTracing(
476 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700482 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800484 registerContentObservers();
485
Joe Onorato7c312c12009-08-13 21:36:53 -0700486 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700487
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 mSavedState = savedInstanceState;
489 restoreState(mSavedState);
490
491 if (PROFILE_STARTUP) {
492 android.os.Debug.stopMethodTracing();
493 }
494
495 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700496 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700497 // If the user leaves launcher, then we should just load items asynchronously when
498 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700499 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700500 } else {
501 // We only load the page synchronously if the user rotates (or triggers a
502 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700503 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 }
506
507 // For handling default keys
508 mDefaultKeySsb = new SpannableStringBuilder();
509 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800510
511 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
512 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800513
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700514 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
515 // In case we are on a device with locked rotation, we should look at preferences to check
516 // if the user has specifically allowed rotation.
517 if (!mRotationEnabled) {
Rahul Chaturvedie863fed2015-06-15 14:09:42 -0400518 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext(), false);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700519 }
520
521 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
522 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400523 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700524
Adam Cohen9211d422014-10-07 18:14:53 -0700525 if (mLauncherCallbacks != null) {
526 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700527 if (mLauncherCallbacks.hasLauncherOverlay()) {
528 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700529 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
530 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
531 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700532 mWorkspace.setLauncherOverlay(mLauncherOverlay);
533 }
Adam Cohen9211d422014-10-07 18:14:53 -0700534 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700535
536 if (shouldShowIntroScreen()) {
537 showIntroScreen();
538 } else {
539 showFirstRunActivity();
540 showFirstRunClings();
541 }
Adam Cohen9211d422014-10-07 18:14:53 -0700542 }
543
Sunny Goyal7779d622015-06-11 16:18:39 -0700544 @Override
545 public void onSettingsChanged(String settings, boolean value) {
546 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
547 mRotationEnabled = value;
548 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
549 mUpdateOrientationRunnable.run();
550 }
551 }
552 }
553
Adam Cohen9211d422014-10-07 18:14:53 -0700554 private LauncherCallbacks mLauncherCallbacks;
555
556 public void onPostCreate(Bundle savedInstanceState) {
557 super.onPostCreate(savedInstanceState);
558 if (mLauncherCallbacks != null) {
559 mLauncherCallbacks.onPostCreate(savedInstanceState);
560 }
561 }
562
563 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
564 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700565 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
566 @Override
567 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700568 if (LOGD) {
569 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
570 }
Winson Chung94804152015-05-08 13:06:44 -0700571 mAppsView.setFixedBounds(bounds);
572 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700573 }
574
575 @Override
576 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700577 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
578 // Dismiss All Apps if we aren't already paused/invisible
579 if (!mPaused) {
580 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
581 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
582 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700583 }
Winson Chung0f785722015-04-08 10:27:49 -0700584 }
Sunny Goyal5683f872015-05-29 14:54:40 -0700585
586 @Override
587 public void setSearchManager(AppSearchManager manager) {
588 mAppsView.setSearchManager(manager);
589 }
Winson Chung0f785722015-04-08 10:27:49 -0700590 });
Jun Mukai8af0cd82015-05-12 12:32:12 -0700591 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
592 private boolean mImportanceStored = false;
593 private int mWorkspaceImportanceForAccessibility =
594 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
595 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
596
597 @Override
598 public void onSearchOverlayOpened() {
599 if (mImportanceStored) {
600 return;
601 }
602 // The underlying workspace and hotseat are temporarily suppressed by the search
603 // overlay. So they sholudn't be accessible.
604 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
605 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
606 mWorkspace.setImportantForAccessibility(
607 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
608 mHotseat.setImportantForAccessibility(
609 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
610 mImportanceStored = true;
611 }
612
613 @Override
614 public void onSearchOverlayClosed() {
615 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
616 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
617 mImportanceStored = false;
618 }
619 });
Adam Cohen9211d422014-10-07 18:14:53 -0700620 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700621 }
622
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700623 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700624 public void onLauncherProviderChange() {
625 if (mLauncherCallbacks != null) {
626 mLauncherCallbacks.onLauncherProviderChange();
627 }
628 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700629
Adam Cohen9211d422014-10-07 18:14:53 -0700630 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700631 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700632 if (mLauncherCallbacks != null) {
633 return mLauncherCallbacks.hasCustomContentToLeft();
634 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700635 return false;
636 }
637
Dave Hawkeya8881582013-09-17 15:55:33 -0600638 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500639 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600640 * {@link #addToCustomContentPage}. This will only be invoked if
641 * {@link #hasCustomContentToLeft()} is {@code true}.
642 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500643 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700644 if (mLauncherCallbacks != null) {
645 mLauncherCallbacks.populateCustomContentContainer();
646 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600647 }
648
649 /**
650 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
651 * ensure the custom content page is added or removed if necessary.
652 */
653 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600654 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600655 // Not bound yet, wait for bindScreens to be called.
656 return;
657 }
658
659 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
660 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500661 mWorkspace.createCustomContentContainer();
662 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600663 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
664 mWorkspace.removeCustomContentPage();
665 }
666 }
667
Anton Hanssona2f665f2013-09-26 12:18:32 +0100668 public Stats getStats() {
669 return mStats;
670 }
671
Bjorn Bringertc459e522013-06-07 19:36:01 +0100672 public LayoutInflater getInflater() {
673 return mInflater;
674 }
675
Hyunyoung Song3f471442015-04-08 19:01:34 -0700676 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700677 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
678 // that is subsequently removed from the workspace in startBinding().
679 return !mModel.isLoadingWorkspace();
680 }
681
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800682 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000683 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100684 if (Build.VERSION.SDK_INT >= 17) {
685 return View.generateViewId();
686 } else {
687 // View.generateViewId() is not available. The following fallback logic is a copy
688 // of its implementation.
689 for (;;) {
690 final int result = sNextGeneratedId.get();
691 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
692 int newValue = result + 1;
693 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
694 if (sNextGeneratedId.compareAndSet(result, newValue)) {
695 return result;
696 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000697 }
698 }
699 }
700
701 public int getViewIdForItem(ItemInfo info) {
702 // This cast is safe given the > 2B range for int.
703 int itemId = (int) info.id;
704 if (mItemIdToViewId.containsKey(itemId)) {
705 return mItemIdToViewId.get(itemId);
706 }
707 int viewId = generateViewId();
708 mItemIdToViewId.put(itemId, viewId);
709 return viewId;
710 }
711
712 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700713 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
714 * a configuration step, this allows the proper animations to run after other transitions.
715 */
Adam Cohendb364c32014-05-20 14:23:40 -0700716 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700717 long screenId = args.screenId;
718 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
719 // When the screen id represents an actual screen (as opposed to a rank) we make sure
720 // that the drop page actually exists.
721 screenId = ensurePendingDropLayoutExists(args.screenId);
722 }
Adam Cohendb364c32014-05-20 14:23:40 -0700723
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800724 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800725 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700726 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700727 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700728 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800729 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700730 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700731 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700732 case REQUEST_RECONFIGURE_APPWIDGET:
733 completeRestoreAppWidget(args.appWidgetId);
734 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800735 }
Michael Jurka27614d22012-04-02 06:40:22 -0700736 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
737 // if you turned the screen off and then back while in All Apps, Launcher would not
738 // return to the workspace. Clearing mAddInfo.container here fixes this issue
739 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700740 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 }
742
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800743 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700745 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700746 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700747 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800748 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700749
Adam Cohenad4e15c2013-10-17 16:21:35 -0700750 Runnable exitSpringLoaded = new Runnable() {
751 @Override
752 public void run() {
753 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
754 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
755 }
756 };
757
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700760 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
761 if (resultCode == RESULT_CANCELED) {
762 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700763 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800766 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700767 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 }
769 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200770 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
771 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700772 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200773 }
774 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200776
Adam Cohened66b2b2012-01-23 17:28:51 -0800777 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700778 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700779
Adam Cohendb364c32014-05-20 14:23:40 -0700780 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800781 // We have special handling for widgets
782 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800783 final int appWidgetId;
784 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
785 : -1;
786 if (widgetId < 0) {
787 appWidgetId = pendingAddWidgetId;
788 } else {
789 appWidgetId = widgetId;
790 }
791
Adam Cohenad4e15c2013-10-17 16:21:35 -0700792 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800793 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700794 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
795 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700796 result = RESULT_CANCELED;
797 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700798 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700799 @Override
800 public void run() {
801 exitSpringLoadedDragModeDelayed(false, 0, null);
802 }
803 };
Adam Cohendb364c32014-05-20 14:23:40 -0700804 if (workspaceLocked) {
805 // No need to remove the empty screen if we're mid-binding, as the
806 // the bind will not add the empty screen.
807 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
808 } else {
809 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
810 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
811 }
Winson Chung5aaab772012-04-27 15:24:02 -0700812 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700813 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700814 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
815 // When the screen id represents an actual screen (as opposed to a rank)
816 // we make sure that the drop page actually exists.
817 mPendingAddInfo.screenId =
818 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
819 }
Adam Cohendb364c32014-05-20 14:23:40 -0700820 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
821
822 dropLayout.setDropPending(true);
823 final Runnable onComplete = new Runnable() {
824 @Override
825 public void run() {
826 completeTwoStageWidgetDrop(resultCode, appWidgetId);
827 dropLayout.setDropPending(false);
828 }
829 };
830 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
831 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
832 } else {
833 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
834 mPendingAddInfo);
835 sPendingAddItem = args;
836 }
Winson Chung5aaab772012-04-27 15:24:02 -0700837 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800838 return;
839 }
840
Sunny Goyalff572272014-07-23 13:58:07 -0700841 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
842 if (resultCode == RESULT_OK) {
843 // Update the widget view.
844 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
845 pendingAddWidgetId, mPendingAddInfo);
846 if (workspaceLocked) {
847 sPendingAddItem = args;
848 } else {
849 completeAdd(args);
850 }
851 }
852 // Leave the widget in the pending state if the user canceled the configure.
853 return;
854 }
855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 // The pattern used here is that a user PICKs a specific application,
857 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
860 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700861 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700862 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
863 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800864 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700865 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800866 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700867 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700868 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
869 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
Adam Cohen00074722013-10-11 17:46:09 -0700871 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700872 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700873 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800876
877 }
878
879 @Override
880 protected void onActivityResult(
881 final int requestCode, final int resultCode, final Intent data) {
882 handleActivityResult(requestCode, resultCode, data);
883 if (mLauncherCallbacks != null) {
884 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
885 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 }
887
Adam Cohendb364c32014-05-20 14:23:40 -0700888 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
889 appWidgetId, ItemInfo info) {
890 PendingAddArguments args = new PendingAddArguments();
891 args.requestCode = requestCode;
892 args.intent = data;
893 args.container = info.container;
894 args.screenId = info.screenId;
895 args.cellX = info.cellX;
896 args.cellY = info.cellY;
897 args.appWidgetId = appWidgetId;
898 return args;
899 }
900
901 /**
902 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
903 *
904 * @param screenId the screen id to check
905 * @return the new screen, or screenId if it exists
906 */
907 private long ensurePendingDropLayoutExists(long screenId) {
908 CellLayout dropLayout =
909 (CellLayout) mWorkspace.getScreenWithId(screenId);
910 if (dropLayout == null) {
911 // it's possible that the add screen was removed because it was
912 // empty and a re-bind occurred
913 mWorkspace.addExtraEmptyScreen();
914 return mWorkspace.commitExtraEmptyScreen();
915 } else {
916 return screenId;
917 }
918 }
919
Adam Cohen091440a2015-03-18 14:16:05 -0700920 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700921 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700922 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 Runnable onCompleteRunnable = null;
924 int animationType = 0;
925
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700926 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 if (resultCode == RESULT_OK) {
928 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
929 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700930 mPendingAddWidgetInfo);
931 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 onCompleteRunnable = new Runnable() {
933 @Override
934 public void run() {
935 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700937 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
938 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 }
940 };
941 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800942 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700945 if (mDragLayer.getAnimatedView() != null) {
946 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
947 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
948 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700949 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 // The animated view may be null in the case of a rotation during widget configuration
951 onCompleteRunnable.run();
952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 }
954
955 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700956 protected void onStop() {
957 super.onStop();
958 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700959
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onStop();
962 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700963 }
964
965 @Override
966 protected void onStart() {
967 super.onStart();
968 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700969
970 if (mLauncherCallbacks != null) {
971 mLauncherCallbacks.onStart();
972 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700973 }
974
975 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200977 long startTime = 0;
978 if (DEBUG_RESUME_TIME) {
979 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400980 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200981 }
Adam Cohen9211d422014-10-07 18:14:53 -0700982
983 if (mLauncherCallbacks != null) {
984 mLauncherCallbacks.preOnResume();
985 }
986
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700988
Winson Chung4a2afa32012-07-19 14:53:05 -0700989 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700990 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700991 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800992 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700993 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700994 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700995 // view after launching an app, as they may be depending on the UI to be static to
996 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700997 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -0700998 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800999 } else if (mOnResumeState == State.WIDGETS) {
1000 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001001 }
1002 mOnResumeState = State.NONE;
1003
Winson Chungcd99cd32015-04-29 11:03:24 -07001004 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001005 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1006 // Otherwise, notify the callbacks if we are in all apps mode
1007 if (mState == State.APPS) {
1008 if (mLauncherCallbacks != null) {
1009 mLauncherCallbacks.onAllAppsShown();
1010 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001011 }
1012 }
1013
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001014 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001015 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1016 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001017
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001018 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001019 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001020 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001021 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001023 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001025 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1027 // execute them here
1028 long startTimeCallbacks = 0;
1029 if (DEBUG_RESUME_TIME) {
1030 startTimeCallbacks = System.currentTimeMillis();
1031 }
1032
Michael Jurka1e2f4652013-07-08 18:03:46 -07001033 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1034 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001035 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001036 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 if (DEBUG_RESUME_TIME) {
1038 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1039 (System.currentTimeMillis() - startTimeCallbacks));
1040 }
Michael Jurka447bf842013-05-15 14:52:15 +02001041 }
Michael Jurka54554252013-08-01 12:52:23 +02001042 if (mOnResumeCallbacks.size() > 0) {
1043 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1044 mOnResumeCallbacks.get(i).run();
1045 }
1046 mOnResumeCallbacks.clear();
1047 }
Winson Chunge4e50662012-01-23 14:45:13 -08001048
1049 // Reset the pressed state of icons that were locked in the press state while activities
1050 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001051 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001052 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001053 mWaitingForResume.setStayPressed(false);
1054 }
Winson Chung82963d52013-10-09 11:20:57 -07001055
Adam Cohen06dff352012-06-01 17:17:08 -07001056 // It is possible that widgets can receive updates while launcher is not in the foreground.
1057 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1058 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1059 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001060 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001061 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001062
Michael Jurka447bf842013-05-15 14:52:15 +02001063 if (DEBUG_RESUME_TIME) {
1064 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1065 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001066
1067 if (mWorkspace.getCustomContentCallbacks() != null) {
1068 // If we are resuming and the custom content is the current page, we call onShow().
1069 // It is also poassible that onShow will instead be called slightly after first layout
1070 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1071 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001072 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001073 }
1074 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001075 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001076 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001077
Sunny Goyal756adbc2015-04-16 15:20:51 -07001078 if (!isWorkspaceLoading()) {
1079 // Process any items that were added while Launcher was away.
1080 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1081 }
Adam Cohen9211d422014-10-07 18:14:53 -07001082
1083 if (mLauncherCallbacks != null) {
1084 mLauncherCallbacks.onResume();
1085 }
Adam Cohen06dff352012-06-01 17:17:08 -07001086 }
1087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001089 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001090 // Ensure that items added to Launcher are queued until Launcher returns
1091 InstallShortcutReceiver.enableInstallQueue();
1092
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001094 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001095 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001096 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001097
1098 // We call onHide() aggressively. The custom content callbacks should be able to
1099 // debounce excess onHide calls.
1100 if (mWorkspace.getCustomContentCallbacks() != null) {
1101 mWorkspace.getCustomContentCallbacks().onHide();
1102 }
Romain Guycbb89e42009-06-08 15:52:54 -07001103
Adam Cohen9211d422014-10-07 18:14:53 -07001104 if (mLauncherCallbacks != null) {
1105 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001106 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001107 }
1108
1109 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001110 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1111 // by a onResume or by scrolling otherwise.
1112 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001113
1114 // Custom content is completely hidden
1115 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001116
1117 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1118 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001119
1120 // Indicates whether the user is allowed to scroll away from the custom content.
1121 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001122 }
1123
Adam Cohenc2d6e892014-10-16 09:49:24 -07001124 public interface LauncherOverlay {
1125
1126 /**
1127 * Touch interaction leading to overscroll has begun
1128 */
1129 public void onScrollInteractionBegin();
1130
1131 /**
1132 * Touch interaction related to overscroll has ended
1133 */
1134 public void onScrollInteractionEnd();
1135
1136 /**
1137 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1138 * screen (or in the case of RTL, the rightmost screen).
1139 */
1140 public void onScrollChange(int progress, boolean rtl);
1141
1142 /**
1143 * Screen has stopped scrolling
1144 */
1145 public void onScrollSettled();
1146
1147 /**
1148 * This method can be called by the Launcher in order to force the LauncherOverlay
1149 * to exit fully immersive mode.
1150 */
1151 public void forceExitFullImmersion();
1152 }
1153
Winson Chung0f785722015-04-08 10:27:49 -07001154 public interface LauncherAppsCallbacks {
1155 /**
1156 * Updates launcher to the available space that AllApps can take so as not to overlap with
1157 * any other views.
1158 */
1159 public void onAllAppsBoundsChanged(Rect bounds);
1160
1161 /**
1162 * Called to dismiss all apps if it is showing.
1163 */
1164 public void dismissAllApps();
Sunny Goyal5683f872015-05-29 14:54:40 -07001165
1166 /**
1167 * Sets the search manager to be used for app search.
1168 */
1169 public void setSearchManager(AppSearchManager manager);
Winson Chung0f785722015-04-08 10:27:49 -07001170 }
1171
Jun Mukai8af0cd82015-05-12 12:32:12 -07001172 public interface LauncherSearchCallbacks {
1173 /**
1174 * Called when the search overlay is shown.
1175 */
1176 public void onSearchOverlayOpened();
1177
1178 /**
1179 * Called when the search overlay is dismissed.
1180 */
1181 public void onSearchOverlayClosed();
1182 }
1183
Adam Cohenc2d6e892014-10-16 09:49:24 -07001184 public interface LauncherOverlayCallbacks {
1185 /**
1186 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1187 * however it doesn't modify any state within the launcher.
1188 */
1189 public boolean canEnterFullImmersion();
1190
1191 /**
1192 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1193 * eg. by occupying the full screen and handling all touch events.
1194 *
1195 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1196 * case, Launcher will modify any necessary state and assumes the overlay is
1197 * handling all interaction. If false, the LauncherOverlay should cancel any
1198 *
1199 */
1200 public boolean enterFullImmersion();
1201
1202 /**
1203 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1204 * full control over UI and state.
1205 */
1206 public void exitFullImmersion();
1207 }
1208
1209 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1210
1211 @Override
1212 public boolean canEnterFullImmersion() {
1213 return mState == State.WORKSPACE;
1214 }
1215
1216 @Override
1217 public boolean enterFullImmersion() {
1218 if (mState == State.WORKSPACE) {
1219 // When fully immersed, disregard any touches which fall through.
1220 mDragLayer.setBlockTouch(true);
1221 return true;
1222 }
1223 return false;
1224 }
1225
1226 @Override
1227 public void exitFullImmersion() {
1228 mDragLayer.setBlockTouch(false);
1229 }
1230 }
1231
Jorim Jaggid017f882014-01-14 17:08:48 -08001232 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001233 if (mLauncherCallbacks != null) {
1234 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001235 } else {
1236 // On devices with a locked orientation, we will at least have the allow rotation
1237 // setting.
1238 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001239 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001240 }
1241
Adam Cohen9211d422014-10-07 18:14:53 -07001242 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001243 CustomContentCallbacks callbacks, String description) {
1244 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001245 }
1246
Adam Cohenedb40762013-07-18 16:45:45 -07001247 // The custom content needs to offset its content to account for the QSB
1248 public int getTopOffsetForCustomContent() {
1249 return mWorkspace.getPaddingTop();
1250 }
1251
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001252 @Override
1253 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001255 if (mModel.isCurrentCallbacks(this)) {
1256 mModel.stopLoader();
1257 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001258 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1259
Romain Guy13c2e7b2010-03-10 19:45:00 -08001260 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001261 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001262
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001263 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001264 @Override
1265 public void onWindowFocusChanged(boolean hasFocus) {
1266 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001267 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001268
1269 if (mLauncherCallbacks != null) {
1270 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1271 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001272 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 private boolean acceptFilter() {
1275 final InputMethodManager inputManager = (InputMethodManager)
1276 getSystemService(Context.INPUT_METHOD_SERVICE);
1277 return !inputManager.isFullscreenMode();
1278 }
1279
1280 @Override
1281 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001282 final int uniChar = event.getUnicodeChar();
1283 final boolean handled = super.onKeyDown(keyCode, event);
1284 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1285 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1287 keyCode, event);
1288 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001289 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001290 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001291 // showSearchDialog()
1292 // If there are multiple keystrokes before the search dialog takes focus,
1293 // onSearchRequested() will be called for every keystroke,
1294 // but it is idempotent, so it's fine.
1295 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
1297 }
1298
Joe Onorato8a9625e2010-01-28 15:55:35 -08001299 // Eat the long press event so the keyboard doesn't come up.
1300 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1301 return true;
1302 }
1303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 return handled;
1305 }
1306
Karl Rosaen138a0412009-04-23 19:00:21 -07001307 private String getTypedText() {
1308 return mDefaultKeySsb.toString();
1309 }
1310
1311 private void clearTypedText() {
1312 mDefaultKeySsb.clear();
1313 mDefaultKeySsb.clearSpans();
1314 Selection.setSelection(mDefaultKeySsb, 0);
1315 }
1316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001318 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1319 * State
1320 */
1321 private static State intToState(int stateOrdinal) {
1322 State state = State.WORKSPACE;
1323 final State[] stateValues = State.values();
1324 for (int i = 0; i < stateValues.length; i++) {
1325 if (stateValues[i].ordinal() == stateOrdinal) {
1326 state = stateValues[i];
1327 break;
1328 }
1329 }
1330 return state;
1331 }
1332
1333 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 * Restores the previous state, if it exists.
1335 *
1336 * @param savedState The previous state.
1337 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001338 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 private void restoreState(Bundle savedState) {
1340 if (savedState == null) {
1341 return;
1342 }
1343
Michael Jurka883f55b2010-10-21 15:47:14 -07001344 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001345 if (state == State.APPS || state == State.WIDGETS) {
1346 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001347 }
1348
Adam Cohen21cd0022013-10-09 18:57:02 -07001349 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1350 PagedView.INVALID_RESTORE_PAGE);
1351 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001352 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
1354
Winson Chung3d503fb2011-07-13 17:25:49 -07001355 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001356 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001357
Winson Chung3d503fb2011-07-13 17:25:49 -07001358 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1359 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001360 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001361 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1362 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001363 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1364 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001365 AppWidgetProviderInfo info = savedState.getParcelable(
1366 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001367 mPendingAddWidgetInfo = info == null ?
1368 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1369
Adam Cohen4637b5a2013-11-04 18:21:24 -08001370 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001371 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 mRestoring = true;
1373 }
1374
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001375 mItemIdToViewId = (HashMap<Integer, Integer>)
1376 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
1378
1379 /**
1380 * Finds all the views we need and configure them properly.
1381 */
1382 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001383 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001384
Craig Mautner360310b2012-10-26 15:13:08 -07001385 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001386 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001387 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1388 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001389 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001390 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001391
John Spurlock77e1f472013-09-11 10:09:51 -04001392 mLauncherView.setSystemUiVisibility(
1393 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001394 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395
Winson Chung4c98d922011-05-31 16:50:48 -07001396 // Setup the drag layer
1397 mDragLayer.setup(this, dragController);
1398
Winson Chung3d503fb2011-07-13 17:25:49 -07001399 // Setup the hotseat
1400 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1401 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001402 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001403 }
1404
Jorim Jaggid017f882014-01-14 17:08:48 -08001405 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001406 View widgetButton = findViewById(R.id.widget_button);
1407 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001408 @Override
1409 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001410 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001411 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001412 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001413 }
1414 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001415 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1416
1417 View wallpaperButton = findViewById(R.id.wallpaper_button);
1418 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001419 @Override
1420 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001421 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001422 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001423 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001424 }
1425 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001426 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427
1428 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001429 if (hasSettings()) {
1430 settingsButton.setOnClickListener(new OnClickListener() {
1431 @Override
1432 public void onClick(View arg0) {
1433 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001434 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001435 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001436 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001437 });
1438 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1439 } else {
1440 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001441 }
1442
Adam Cohendbdff6b2013-09-18 19:09:15 -07001443 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001444
Winson Chung4c98d922011-05-31 16:50:48 -07001445 // Setup the workspace
1446 mWorkspace.setHapticFeedbackEnabled(false);
1447 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001448 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001449 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001450
Winson Chungf0ea4d32011-06-06 14:27:16 -07001451 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001452 mSearchDropTargetBar = (SearchDropTargetBar)
1453 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001454
Winson Chungb745afb2015-03-02 11:51:23 -08001455 // Setup Apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001456 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001457 if (isAllAppsSearchOverridden()) {
1458 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001459 }
Winson Chungb745afb2015-03-02 11:51:23 -08001460
Winson Chungf0ea4d32011-06-06 14:27:16 -07001461 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001462 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001463
Winson Chung3d503fb2011-07-13 17:25:49 -07001464 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001465 dragController.setDragScoller(mWorkspace);
1466 dragController.setScrollView(mDragLayer);
1467 dragController.setMoveTarget(mWorkspace);
1468 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001469 if (mSearchDropTargetBar != null) {
1470 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001471 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001472 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001473
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001474 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001475 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001476 mWeightWatcher = new WeightWatcher(this);
1477 mWeightWatcher.setAlpha(0.5f);
1478 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001479 new FrameLayout.LayoutParams(
1480 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001481 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001482 Gravity.BOTTOM)
1483 );
Adam Cohen39a06042013-07-19 14:30:12 -07001484
1485 boolean show = shouldShowWeightWatcher();
1486 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001487 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
1490 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001491 * Sets the all apps button. This method is called from {@link Hotseat}.
1492 */
1493 public void setAllAppsButton(View allAppsButton) {
1494 mAllAppsButton = allAppsButton;
1495 }
1496
1497 public View getAllAppsButton() {
1498 return mAllAppsButton;
1499 }
1500
1501 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 * Creates a view representing a shortcut.
1503 *
1504 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001506 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001507 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
1509
1510 /**
1511 * Creates a view representing a shortcut inflated from the specified resource.
1512 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 * @param parent The group the shortcut belongs to.
1514 * @param info The data structure describing the shortcut.
1515 *
1516 * @return A View inflated from layoutResId.
1517 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001518 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001519 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001520 parent, false);
1521 favorite.applyFromShortcutInfo(info, mIconCache);
1522 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001524 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 return favorite;
1526 }
1527
1528 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 * Add a shortcut to the workspace.
1530 *
1531 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001533 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001534 int cellY) {
1535 int[] cellXY = mTmpAddItemCellCoordinates;
1536 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001538
Sunny Goyal5c97f512015-05-19 16:03:28 -07001539 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001540 if (info == null) {
1541 return;
1542 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001543 final View view = createShortcut(info);
1544
Sunny Goyal5c97f512015-05-19 16:03:28 -07001545 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001546 // First we check if we already know the exact location where we want to add this item.
1547 if (cellX >= 0 && cellY >= 0) {
1548 cellXY[0] = cellX;
1549 cellXY[1] = cellY;
1550 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001551
1552 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001553 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001554 true, null,null)) {
1555 return;
1556 }
1557 DragObject dragObject = new DragObject();
1558 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001559 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1560 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001561 return;
1562 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001563 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001564 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001565 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001566 foundCellSpan = (result != null);
1567 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001568 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001569 }
1570
1571 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001572 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001573 return;
1574 }
1575
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001576 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577
1578 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001579 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001580 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 }
1582 }
1583
Adam Cohen2e6da152015-05-06 11:42:25 -07001584 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1585 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001586 // We want to account for the extra amount of padding that we are adding to the widget
1587 // to ensure that it gets the full amount of space that it has requested
1588 int requiredWidth = minWidth + padding.left + padding.right;
1589 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001590 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001591 }
1592
Adam Cohen2e6da152015-05-06 11:42:25 -07001593 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1594 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001595 }
1596
Adam Cohen2e6da152015-05-06 11:42:25 -07001597 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1598 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001599 }
1600
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001602 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001604 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 */
Adam Cohen091440a2015-03-18 14:16:05 -07001606 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001607 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1608
1609 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001610 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001611 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1612 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001613 }
Romain Guycbb89e42009-06-08 15:52:54 -07001614
Adam Cohen59400422014-03-05 18:07:04 -08001615 if (appWidgetInfo.isCustomWidget) {
1616 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001617 }
1618
Adam Cohen59400422014-03-05 18:07:04 -08001619 LauncherAppWidgetInfo launcherInfo;
1620 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1621 launcherInfo.spanX = info.spanX;
1622 launcherInfo.spanY = info.spanY;
1623 launcherInfo.minSpanX = info.minSpanX;
1624 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001625 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001628 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629
1630 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001631 if (hostView == null) {
1632 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001633 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1634 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001635 } else {
1636 // The AppWidgetHostView has already been inflated and instantiated
1637 launcherInfo.hostView = hostView;
1638 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001640 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001641 launcherInfo.notifyWidgetSizeChanged(this);
1642
Adam Cohen59400422014-03-05 18:07:04 -08001643 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1644 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001645
1646 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001648 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 }
Romain Guycbb89e42009-06-08 15:52:54 -07001650
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1652 @Override
1653 public void onReceive(Context context, Intent intent) {
1654 final String action = intent.getAction();
1655 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1656 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001657 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001658 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001659
Winson Chungc100e8e2011-08-09 16:02:43 -07001660 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001661 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001662 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001663 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001664 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001665 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1667 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001668 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001669 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1670 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001671 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001672 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1673 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1674 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001675 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001676 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1677 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001678 }
1679 }
1680 };
1681
1682 @Override
1683 public void onAttachedToWindow() {
1684 super.onAttachedToWindow();
1685
1686 // Listen for broadcasts related to user-presence
1687 final IntentFilter filter = new IntentFilter();
1688 filter.addAction(Intent.ACTION_SCREEN_OFF);
1689 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001690 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001691 if (ENABLE_DEBUG_INTENTS) {
1692 filter.addAction(DebugIntents.DELETE_DATABASE);
1693 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1694 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001695 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001696 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001697 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001698 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 mVisible = true;
1700 }
1701
Adam Cohen0b395352014-06-09 22:54:36 +00001702 /**
1703 * Sets up transparent navigation and status bars in LMP.
1704 * This method is a no-op for other platform versions.
1705 */
Sunny Goyald0091012015-01-20 15:55:34 -08001706 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001707 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001708 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001709 Window window = getWindow();
1710 window.getAttributes().systemUiVisibility |=
1711 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1712 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1713 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1714 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1715 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1716 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1717 window.setStatusBarColor(Color.TRANSPARENT);
1718 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001719 }
1720 }
1721
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 @Override
1723 public void onDetachedFromWindow() {
1724 super.onDetachedFromWindow();
1725 mVisible = false;
1726
Adam Cohend113e0c2010-11-11 10:48:05 -08001727 if (mAttached) {
1728 unregisterReceiver(mReceiver);
1729 mAttached = false;
1730 }
Winson Chungb745afb2015-03-02 11:51:23 -08001731 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 }
1733
1734 public void onWindowVisibilityChanged(int visibility) {
1735 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001736 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001737 // The following code used to be in onResume, but it turns out onResume is called when
1738 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1739 // is a more appropriate event to handle
1740 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001741 if (!mWorkspaceLoading) {
1742 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001743 // We want to let Launcher draw itself at least once before we force it to build
1744 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001745 // apps is nice and speedy.
1746 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001747 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001748 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001749 if (mStarted) return;
1750 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001751 // We delay the layer building a bit in order to give
1752 // other message processing a time to run. In particular
1753 // this avoids a delay in hiding the IME if it was
1754 // currently shown, because doing that may involve
1755 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001756 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001757 final ViewTreeObserver.OnDrawListener listener = this;
1758 mWorkspace.post(new Runnable() {
1759 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001760 if (mWorkspace != null &&
1761 mWorkspace.getViewTreeObserver() != null) {
1762 mWorkspace.getViewTreeObserver().
1763 removeOnDrawListener(listener);
1764 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001765 }
1766 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001767 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001768 }
1769 });
1770 }
1771 clearTypedText();
1772 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 }
1774
Adam Cohen091440a2015-03-18 14:16:05 -07001775 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 mHandler.removeMessages(ADVANCE_MSG);
1777 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1778 mHandler.sendMessageDelayed(msg, delay);
1779 mAutoAdvanceSentTime = System.currentTimeMillis();
1780 }
1781
Adam Cohen091440a2015-03-18 14:16:05 -07001782 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001783 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1784 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1785 mAutoAdvanceRunning = autoAdvanceRunning;
1786 if (autoAdvanceRunning) {
1787 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1788 sendAdvanceMessage(delay);
1789 } else {
1790 if (!mWidgetsToAdvance.isEmpty()) {
1791 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1792 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1793 }
1794 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001795 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001796 }
1797 }
1798 }
1799
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001800 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1801
Adam Cohended9f8d2010-11-03 13:25:16 -07001802 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001803 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001804 if (msg.what == ADVANCE_MSG) {
1805 int i = 0;
1806 for (View key: mWidgetsToAdvance.keySet()) {
1807 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1808 final int delay = mAdvanceStagger * i;
1809 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001810 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001811 public void run() {
1812 ((Advanceable) v).advance();
1813 }
1814 }, delay);
1815 }
1816 i++;
1817 }
1818 sendAdvanceMessage(mAdvanceInterval);
1819 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001820 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001821 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001822 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001823
1824 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001825 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001826 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1827 if (v instanceof Advanceable) {
1828 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001829 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001830 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001831 }
1832 }
1833
1834 void removeWidgetToAutoAdvance(View hostView) {
1835 if (mWidgetsToAdvance.containsKey(hostView)) {
1836 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001837 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001838 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001839 }
1840
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001842 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 launcherInfo.hostView = null;
1844 }
1845
Hyunyoung Song3f471442015-04-08 19:01:34 -07001846 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001847 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1848 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001849 }
1850
Winson Chunga6945242014-01-08 14:04:34 -08001851 public DragLayer getDragLayer() {
1852 return mDragLayer;
1853 }
1854
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001855 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001856 return mAppsView;
1857 }
1858
Hyunyoung Song3f471442015-04-08 19:01:34 -07001859 public WidgetsContainerView getWidgetsView() {
1860 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001861 }
1862
Winson Chunga6945242014-01-08 14:04:34 -08001863 public Workspace getWorkspace() {
1864 return mWorkspace;
1865 }
1866
1867 public Hotseat getHotseat() {
1868 return mHotseat;
1869 }
1870
Jorim Jaggid017f882014-01-14 17:08:48 -08001871 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001872 return mOverviewPanel;
1873 }
1874
1875 public SearchDropTargetBar getSearchBar() {
1876 return mSearchDropTargetBar;
1877 }
1878
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001879 public LauncherAppWidgetHost getAppWidgetHost() {
1880 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 }
Romain Guycbb89e42009-06-08 15:52:54 -07001882
Winson Chunga9abd0e2010-10-27 17:18:37 -07001883 public LauncherModel getModel() {
1884 return mModel;
1885 }
1886
Winson Chunga6945242014-01-08 14:04:34 -08001887 protected SharedPreferences getSharedPrefs() {
1888 return mSharedPrefs;
1889 }
1890
Adam Cohen2e6da152015-05-06 11:42:25 -07001891 public DeviceProfile getDeviceProfile() {
1892 return mDeviceProfile;
1893 }
1894
Bjorn Bringertc459e522013-06-07 19:36:01 +01001895 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001896 getWindow().closeAllPanels();
1897
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001898 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001899 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001900 }
1901
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 @Override
1903 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001904 long startTime = 0;
1905 if (DEBUG_RESUME_TIME) {
1906 startTime = System.currentTimeMillis();
1907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 super.onNewIntent(intent);
1909
1910 // Close the menu
1911 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001912 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001913 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001914
Adam Cohened307df2013-10-02 09:37:31 -07001915 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1916 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1917 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001918
Adam Cohen6fecd412013-10-02 17:41:50 -07001919 if (mWorkspace == null) {
1920 // Can be cases where mWorkspace is null, this prevents a NPE
1921 return;
1922 }
1923 Folder openFolder = mWorkspace.getOpenFolder();
1924 // In all these cases, only animate if we're already on home
1925 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001926
1927 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1928 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001929 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001930 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001931 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001932 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001933
Adam Cohen6fecd412013-10-02 17:41:50 -07001934 closeFolder();
1935 exitSpringLoadedDragMode();
1936
1937 // If we are already on home, then just animate back to the workspace,
1938 // otherwise, just wait until onResume to set the state back to Workspace
1939 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001940 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001941 } else {
1942 mOnResumeState = State.WORKSPACE;
1943 }
1944
1945 final View v = getWindow().peekDecorView();
1946 if (v != null && v.getWindowToken() != null) {
1947 InputMethodManager imm = (InputMethodManager)getSystemService(
1948 INPUT_METHOD_SERVICE);
1949 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1950 }
1951
Winson Chungb745afb2015-03-02 11:51:23 -08001952 // Reset the apps view
1953 if (!alreadyOnHome && mAppsView != null) {
1954 mAppsView.scrollToTop();
1955 }
1956
Hyunyoung Song3f471442015-04-08 19:01:34 -07001957 // Reset the widgets view
1958 if (!alreadyOnHome && mWidgetsView != null) {
1959 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001960 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001961
Adam Cohen9211d422014-10-07 18:14:53 -07001962 if (mLauncherCallbacks != null) {
1963 mLauncherCallbacks.onHomeIntent();
1964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
Adam Cohened307df2013-10-02 09:37:31 -07001966
Michael Jurka447bf842013-05-15 14:52:15 +02001967 if (DEBUG_RESUME_TIME) {
1968 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970
Adam Cohen9211d422014-10-07 18:14:53 -07001971 if (mLauncherCallbacks != null) {
1972 mLauncherCallbacks.onNewIntent(intent);
1973 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001974 }
1975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001977 public void onRestoreInstanceState(Bundle state) {
1978 super.onRestoreInstanceState(state);
1979 for (int page: mSynchronouslyBoundPages) {
1980 mWorkspace.restoreInstanceStateForChild(page);
1981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
1983
1984 @Override
1985 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001986 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001987 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1988 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001989 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001990 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991
Michael Jurka883f55b2010-10-21 15:47:14 -07001992 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001993 // We close any open folder since it will not be re-opened, and we need to make sure
1994 // this state is reflected.
1995 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001998 mWaitingForResult) {
1999 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002000 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002001 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2002 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002003 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2004 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2005 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002006 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008
Hyunyoung Song3f471442015-04-08 19:01:34 -07002009 // Save the current widgets tray?
2010 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002011 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002012
2013 if (mLauncherCallbacks != null) {
2014 mLauncherCallbacks.onSaveInstanceState(outState);
2015 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 }
2017
2018 @Override
2019 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002021
Winson Chunge7a03942011-08-05 15:05:12 -07002022 // Remove all pending runnables
2023 mHandler.removeMessages(ADVANCE_MSG);
2024 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002025 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002026
Winson Chungcd2b0142011-06-08 16:02:26 -07002027 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002028 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002029
2030 // It's possible to receive onDestroy after a new Launcher activity has
2031 // been created. In this case, don't interfere with the new Launcher.
2032 if (mModel.isCurrentCallbacks(this)) {
2033 mModel.stopLoader();
2034 app.setLauncher(null);
2035 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002038 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002040 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002042 mAppWidgetHost = null;
2043
2044 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045
2046 TextKeyListener.getInstance().release();
2047
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002048 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002049 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002050
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002051 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002052 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002053 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002054 mWorkspace = null;
2055 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002056
Michael Jurka2ecf9952012-06-18 12:52:28 -07002057 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002058
2059 if (mLauncherCallbacks != null) {
2060 mLauncherCallbacks.onDestroy();
2061 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063
Adam Cohena9cf38f2011-05-02 15:36:58 -07002064 public DragController getDragController() {
2065 return mDragController;
2066 }
2067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 @Override
2069 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002070 onStartForResult(requestCode);
2071 super.startActivityForResult(intent, requestCode);
2072 }
2073
2074 @Override
2075 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2076 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2077 onStartForResult(requestCode);
2078 try {
2079 super.startIntentSenderForResult(intent, requestCode,
2080 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2081 } catch (IntentSender.SendIntentException e) {
2082 throw new ActivityNotFoundException();
2083 }
2084 }
2085
2086 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002087 if (requestCode >= 0) {
2088 setWaitingForResult(true);
2089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 }
2091
Winson Chung70d72102011-08-12 11:24:35 -07002092 /**
2093 * Indicates that we want global search for this activity by setting the globalSearch
2094 * argument for {@link #startSearch} to true.
2095 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002097 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002099
Karl Rosaen138a0412009-04-23 19:00:21 -07002100 if (initialQuery == null) {
2101 // Use any text typed in the launcher as the initial query
2102 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 if (appSearchData == null) {
2105 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002106 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
Winson Chungadf0c182012-08-23 14:59:07 -07002108 Rect sourceBounds = new Rect();
2109 if (mSearchDropTargetBar != null) {
2110 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2111 }
Romain Guycbb89e42009-06-08 15:52:54 -07002112
Ian Parkinson047036e2014-09-01 15:40:53 +01002113 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002114 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002115 if (clearTextImmediately) {
2116 clearTypedText();
2117 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002118
2119 // We need to show the workspace after starting the search
2120 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002121 }
2122
Ian Parkinson047036e2014-09-01 15:40:53 +01002123 /**
2124 * Start a text search.
2125 *
2126 * @return {@code true} if the search will start immediately, so any further keypresses
2127 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2128 * to buffer keypresses.
2129 */
2130 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002131 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002132 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2133 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2134 sourceBounds);
2135 }
2136
Michael Jurkaa33411c2012-06-14 16:18:21 -07002137 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002138 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002139 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002140 }
2141
2142 /**
2143 * Starts the global search activity. This code is a copied from SearchManager
2144 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002145 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002146 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002147 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002148 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2149 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2150 if (globalSearchActivity == null) {
2151 Log.w(TAG, "No global search activity found.");
2152 return;
2153 }
2154 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2155 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2156 intent.setComponent(globalSearchActivity);
2157 // Make sure that we have a Bundle to put source in
2158 if (appSearchData == null) {
2159 appSearchData = new Bundle();
2160 } else {
2161 appSearchData = new Bundle(appSearchData);
2162 }
Adam Cohen9211d422014-10-07 18:14:53 -07002163 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002164 if (!appSearchData.containsKey("source")) {
2165 appSearchData.putString("source", getPackageName());
2166 }
2167 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2168 if (!TextUtils.isEmpty(initialQuery)) {
2169 intent.putExtra(SearchManager.QUERY, initialQuery);
2170 }
2171 if (selectInitialQuery) {
2172 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2173 }
2174 intent.setSourceBounds(sourceBounds);
2175 try {
2176 startActivity(intent);
2177 } catch (ActivityNotFoundException ex) {
2178 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 }
2181
Yura4f93ec62014-02-04 14:15:21 +00002182 public boolean isOnCustomContent() {
2183 return mWorkspace.isOnOrMovingToCustomContent();
2184 }
2185
Winson Chung70d72102011-08-12 11:24:35 -07002186 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002187 public boolean onPrepareOptionsMenu(Menu menu) {
2188 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002189 if (!isOnCustomContent()) {
2190 // Close any open folders
2191 closeFolder();
2192 // Stop resizing any widgets
2193 mWorkspace.exitWidgetResizeMode();
2194 if (!mWorkspace.isInOverviewMode()) {
2195 // Show the overview mode
2196 showOverviewMode(true);
2197 } else {
2198 showWorkspace(true);
2199 }
Winson Chunge0298742014-01-17 12:03:00 -08002200 }
Adam Cohen9211d422014-10-07 18:14:53 -07002201 if (mLauncherCallbacks != null) {
2202 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2203 }
2204
Michael Jurkab94f3f82013-09-11 18:08:54 +02002205 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002208 @Override
2209 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002210 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002211 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002212 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002213 }
2214
Joe Onorato9c1289c2009-08-17 11:03:03 -04002215 public boolean isWorkspaceLocked() {
2216 return mWorkspaceLoading || mWaitingForResult;
2217 }
2218
Adam Cohen517a7f52014-03-01 12:12:59 -08002219 public boolean isWorkspaceLoading() {
2220 return mWorkspaceLoading;
2221 }
2222
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002223 private void setWorkspaceLoading(boolean value) {
2224 boolean isLocked = isWorkspaceLocked();
2225 mWorkspaceLoading = value;
2226 if (isLocked != isWorkspaceLocked()) {
2227 onWorkspaceLockedChanged();
2228 }
2229 }
2230
2231 private void setWaitingForResult(boolean value) {
2232 boolean isLocked = isWorkspaceLocked();
2233 mWaitingForResult = value;
2234 if (isLocked != isWorkspaceLocked()) {
2235 onWorkspaceLockedChanged();
2236 }
2237 }
2238
Adam Cohen9211d422014-10-07 18:14:53 -07002239 protected void onWorkspaceLockedChanged() {
2240 if (mLauncherCallbacks != null) {
2241 mLauncherCallbacks.onWorkspaceLockedChanged();
2242 }
2243 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002244
Michael Jurka0280c3b2010-09-17 15:00:07 -07002245 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002248 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2249 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002250 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002251 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002253
Sunny Goyale6b63a32015-01-16 16:14:29 -08002254 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002255 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002256 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2257 }
2258
Sunny Goyale6b63a32015-01-16 16:14:29 -08002259 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002260 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2261 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002262 if (appWidgetInfo.configure != null) {
2263 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002264 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002265
Bjorn Bringert7984c942009-12-09 15:38:25 +00002266 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002267 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2268 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002271 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002272 Runnable onComplete = new Runnable() {
2273 @Override
2274 public void run() {
2275 // Exit spring loaded mode if necessary after adding the widget
2276 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2277 null);
2278 }
2279 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002280 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002281 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002282 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283 }
2284 }
Romain Guycbb89e42009-06-08 15:52:54 -07002285
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002286 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002287 // Close any folders that may be open.
2288 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002289 mWorkspace.moveToCustomContentScreen(animate);
2290 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002291
2292 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2293 int[] cell, int spanX, int spanY) {
2294 switch (info.itemType) {
2295 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2296 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2297 int span[] = new int[2];
2298 span[0] = spanX;
2299 span[1] = spanY;
2300 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2301 container, screenId, cell, span);
2302 break;
2303 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2304 processShortcutFromDrop(info.componentName, container, screenId, cell);
2305 break;
2306 default:
2307 throw new IllegalStateException("Unknown item type: " + info.itemType);
2308 }
2309 }
2310
Adam Cohenfbba09b2011-07-18 21:43:05 -07002311 /**
2312 * Process a shortcut drop.
2313 *
2314 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002315 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002316 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002317 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002318 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2319 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002320 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002321 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002322 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002323 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324
2325 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002326 mPendingAddInfo.cellX = cell[0];
2327 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002329
2330 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2331 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002332 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002333 }
2334
Adam Cohenfbba09b2011-07-18 21:43:05 -07002335 /**
2336 * Process a widget drop.
2337 *
2338 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002339 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002340 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002342 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2343 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002344 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002346 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002347 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002348 mPendingAddInfo.minSpanX = info.minSpanX;
2349 mPendingAddInfo.minSpanY = info.minSpanY;
2350
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002352 mPendingAddInfo.cellX = cell[0];
2353 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002354 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002355 if (span != null) {
2356 mPendingAddInfo.spanX = span[0];
2357 mPendingAddInfo.spanY = span[1];
2358 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002359
Adam Cohened66b2b2012-01-23 17:28:51 -08002360 AppWidgetHostView hostView = info.boundWidget;
2361 int appWidgetId;
2362 if (hostView != null) {
2363 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002364 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002365
2366 // Clear the boundWidget so that it doesn't get destroyed.
2367 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002368 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002369 // In this case, we either need to start an activity to get permission to bind
2370 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002371 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002372 Bundle options = info.bindOptions;
2373
Sunny Goyalffe83f12014-08-14 17:39:34 -07002374 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2375 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002376 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002377 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002378 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002379 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002380 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2381 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2382 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002383 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2384 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002385 // TODO: we need to make sure that this accounts for the options bundle.
2386 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002387 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2388 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002389 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002390 }
2391
Adam Cohendcd297f2013-06-18 13:13:40 -07002392 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002393 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002394 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 folderInfo.title = getText(R.string.folder_name);
2396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002398 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2399 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002400 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401
2402 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002403 FolderIcon newFolder =
2404 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002405 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002406 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002407 // Force measure the new folder icon
2408 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2409 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002410 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 }
Romain Guycbb89e42009-06-08 15:52:54 -07002412
Joe Onorato9c1289c2009-08-17 11:03:03 -04002413 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002414 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002415 }
2416
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002417 /**
2418 * Registers various content observers. The current implementation registers
2419 * only a favorites observer to keep track of the favorites applications.
2420 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002421 private void registerContentObservers() {
2422 ContentResolver resolver = getContentResolver();
2423 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2424 true, mWidgetObserver);
2425 }
2426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 @Override
2428 public boolean dispatchKeyEvent(KeyEvent event) {
2429 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2430 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002432 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002433 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002434 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002435 dumpState();
2436 return true;
2437 }
2438 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002439 }
2440 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2441 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002442 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 return true;
2444 }
2445 }
2446
2447 return super.dispatchKeyEvent(event);
2448 }
2449
Joe Onorato88ec0992009-11-19 13:16:06 -08002450 @Override
2451 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002452 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2453 return;
2454 }
2455
Sunny Goyal45478022015-06-08 16:52:41 -07002456 if (mDragController.isDragging()) {
2457 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002458 return;
2459 }
2460
Winson Chungb745afb2015-03-02 11:51:23 -08002461 if (isAppsViewVisible()) {
2462 showWorkspace(true);
2463 } else if (isWidgetsViewVisible()) {
2464 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002465 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002466 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002467 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002468 Folder openFolder = mWorkspace.getOpenFolder();
2469 if (openFolder.isEditingName()) {
2470 openFolder.dismissEditingName();
2471 } else {
2472 closeFolder();
2473 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002474 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002475 mWorkspace.exitWidgetResizeMode();
2476
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002477 // Back button is a no-op here, but give at least some feedback for the button press
2478 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002479 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002480 }
2481
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002483 * Re-listen when widgets are reset.
2484 */
Adam Cohen091440a2015-03-18 14:16:05 -07002485 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002486 if (mAppWidgetHost != null) {
2487 mAppWidgetHost.startListening();
2488 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002489 }
2490
2491 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 * Launches the intent referred by the clicked shortcut.
2493 *
2494 * @param v The view representing the clicked shortcut.
2495 */
2496 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002497 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2498 // view has detached (it's possible for this to happen if the view is removed mid touch).
2499 if (v.getWindowToken() == null) {
2500 return;
2501 }
2502
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002503 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002504 return;
2505 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002506
Adam Cohen1697b792013-09-17 19:08:21 -07002507 if (v instanceof Workspace) {
2508 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002509 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002510 }
2511 return;
2512 }
2513
2514 if (v instanceof CellLayout) {
2515 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002516 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002517 }
2518 }
2519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002521 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002522 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002524 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002525 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002526 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002527 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002528 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002529 } else if (tag instanceof AppInfo) {
2530 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002531 } else if (tag instanceof LauncherAppWidgetInfo) {
2532 if (v instanceof PendingAppWidgetHostView) {
2533 onClickPendingWidget((PendingAppWidgetHostView) v);
2534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 }
2536 }
2537
Sunny Goyal70660032015-05-14 00:07:08 -07002538 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002539 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002540 return false;
2541 }
2542
Michael Jurkaaf442092010-06-10 17:01:57 -07002543 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002544 * Event handler for the app widget view which has not fully restored.
2545 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002546 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002547 if (mIsSafeModeEnabled) {
2548 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2549 return;
2550 }
2551
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002552 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002553 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002554 int widgetId = info.appWidgetId;
2555 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2556 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002557 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2558 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002559 mPendingAddInfo.copyFrom(info);
2560 mPendingAddWidgetId = widgetId;
2561
Sunny Goyalffe83f12014-08-14 17:39:34 -07002562 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2563 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002564 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002565 } else if (info.installProgress < 0) {
2566 // The install has not been queued
2567 final String packageName = info.providerName.getPackageName();
2568 showBrokenAppInstallDialog(packageName,
2569 new DialogInterface.OnClickListener() {
2570 public void onClick(DialogInterface dialog, int id) {
2571 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2572 }
2573 });
2574 } else {
2575 // Download has started.
2576 final String packageName = info.providerName.getPackageName();
2577 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002578 }
2579 }
2580
2581 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002582 * Event handler for the "grid" button that appears on the home screen, which
2583 * enters all apps mode.
2584 *
2585 * @param v The view that was clicked.
2586 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002587 protected void onClickAllAppsButton(View v) {
2588 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002589 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002590 showWorkspace(true);
2591 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002592 // Try and refresh the set of predicted apps before we enter launcher
2593 showAppsView(true /* animated */, false /* resetListToTop */,
2594 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 }
2596 }
2597
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002598 private void showBrokenAppInstallDialog(final String packageName,
2599 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002600 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002601 .setTitle(R.string.abandoned_promises_title)
2602 .setMessage(R.string.abandoned_promise_explanation)
2603 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2604 .setNeutralButton(R.string.abandoned_clean_this,
2605 new DialogInterface.OnClickListener() {
2606 public void onClick(DialogInterface dialog, int id) {
2607 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2608 mWorkspace.removeAbandonedPromise(packageName, user);
2609 }
2610 })
2611 .create().show();
2612 return;
2613 }
2614
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002615 /**
2616 * Event handler for an app shortcut click.
2617 *
2618 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2619 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002620 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002621 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2622 Object tag = v.getTag();
2623 if (!(tag instanceof ShortcutInfo)) {
2624 throw new IllegalArgumentException("Input must be a Shortcut");
2625 }
2626
2627 // Open shortcut
2628 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002629
2630 if (shortcut.isDisabled != 0) {
2631 int error = R.string.activity_not_available;
2632 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2633 error = R.string.safemode_shortcut_error;
2634 }
2635 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2636 return;
2637 }
2638
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 final Intent intent = shortcut.intent;
2640
2641 // Check for special shortcuts
2642 if (intent.getComponent() != null) {
2643 final String shortcutClass = intent.getComponent().getClassName();
2644
2645 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2646 MemoryDumpActivity.startDump(this);
2647 return;
2648 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2649 toggleShowWeightWatcher();
2650 return;
2651 }
2652 }
2653
Chris Wren40c5ed32014-06-24 18:24:23 -04002654 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002655 if ((v instanceof BubbleTextView)
2656 && shortcut.isPromise()
2657 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002658 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002659 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002660 new DialogInterface.OnClickListener() {
2661 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002662 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 return;
2666 }
2667
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002668 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002669 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002670
2671 if (mLauncherCallbacks != null) {
2672 mLauncherCallbacks.onClickAppShortcut(v);
2673 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 }
2675
Adam Cohen091440a2015-03-18 14:16:05 -07002676 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002678 final ShortcutInfo shortcut;
2679 final Intent intent;
2680 if (tag instanceof ShortcutInfo) {
2681 shortcut = (ShortcutInfo) tag;
2682 intent = shortcut.intent;
2683 int[] pos = new int[2];
2684 v.getLocationOnScreen(pos);
2685 intent.setSourceBounds(new Rect(pos[0], pos[1],
2686 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002687
Sunny Goyal508da152014-08-14 10:53:27 -07002688 } else if (tag instanceof AppInfo) {
2689 shortcut = null;
2690 intent = ((AppInfo) tag).intent;
2691 } else {
2692 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2693 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002694
2695 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002696 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697
2698 if (success && v instanceof BubbleTextView) {
2699 mWaitingForResume = (BubbleTextView) v;
2700 mWaitingForResume.setStayPressed(true);
2701 }
2702 }
2703
2704 /**
2705 * Event handler for a folder icon click.
2706 *
2707 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2708 */
2709 protected void onClickFolderIcon(View v) {
2710 if (LOGD) Log.d(TAG, "onClickFolder");
2711 if (!(v instanceof FolderIcon)){
2712 throw new IllegalArgumentException("Input must be a FolderIcon");
2713 }
2714
2715 FolderIcon folderIcon = (FolderIcon) v;
2716 final FolderInfo info = folderIcon.getFolderInfo();
2717 Folder openFolder = mWorkspace.getFolderForTag(info);
2718
2719 // If the folder info reports that the associated folder is open, then verify that
2720 // it is actually opened. There have been a few instances where this gets out of sync.
2721 if (info.opened && openFolder == null) {
2722 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2723 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2724 info.opened = false;
2725 }
2726
2727 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2728 // Close any open folder
2729 closeFolder();
2730 // Open the requested folder
2731 openFolder(folderIcon);
2732 } else {
2733 // Find the open folder...
2734 int folderScreen;
2735 if (openFolder != null) {
2736 folderScreen = mWorkspace.getPageForView(openFolder);
2737 // .. and close it
2738 closeFolder(openFolder);
2739 if (folderScreen != mWorkspace.getCurrentPage()) {
2740 // Close any folder open on the current screen
2741 closeFolder();
2742 // Pull the folder onto this screen
2743 openFolder(folderIcon);
2744 }
2745 }
2746 }
Adam Cohen9211d422014-10-07 18:14:53 -07002747
2748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.onClickFolderIcon(v);
2750 }
Michael Jurka5130e402011-10-13 04:55:35 -07002751 }
2752
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002753 /**
2754 * Event handler for the (Add) Widgets button that appears after a long press
2755 * on the home screen.
2756 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002757 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002758 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002759 if (mIsSafeModeEnabled) {
2760 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2761 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002762 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002763 if (mLauncherCallbacks != null) {
2764 mLauncherCallbacks.onClickAddWidgetButton(view);
2765 }
Adam Cohen9211d422014-10-07 18:14:53 -07002766 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002767 }
2768
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002769 /**
2770 * Event handler for the wallpaper picker button that appears after a long press
2771 * on the home screen.
2772 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002773 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002774 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002775 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2776 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002777
2778 if (mLauncherCallbacks != null) {
2779 mLauncherCallbacks.onClickWallpaperPicker(v);
2780 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002781 }
2782
2783 /**
2784 * Event handler for a click on the settings button that appears after a long press
2785 * on the home screen.
2786 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002787 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002788 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002789 if (mLauncherCallbacks != null) {
2790 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002791 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002792 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002793 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002794 }
2795
Adam Cohen61f560d2013-09-30 15:58:20 -07002796 public View.OnTouchListener getHapticFeedbackTouchListener() {
2797 if (mHapticFeedbackTouchListener == null) {
2798 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002799 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002800 @Override
2801 public boolean onTouch(View v, MotionEvent event) {
2802 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2803 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2804 }
2805 return false;
2806 }
2807 };
2808 }
2809 return mHapticFeedbackTouchListener;
2810 }
2811
Adam Cohen9211d422014-10-07 18:14:53 -07002812 public void onDragStarted(View view) {
2813 if (isOnCustomContent()) {
2814 // Custom content screen doesn't participate in drag and drop. If on custom
2815 // content screen, move to default.
2816 moveWorkspaceToDefaultScreen();
2817 }
2818
2819 if (mLauncherCallbacks != null) {
2820 mLauncherCallbacks.onDragStarted(view);
2821 }
2822 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002823
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002824 /**
2825 * Called when the user stops interacting with the launcher.
2826 * This implies that the user is now on the homescreen and is not doing housekeeping.
2827 */
Adam Cohen9211d422014-10-07 18:14:53 -07002828 protected void onInteractionEnd() {
2829 if (mLauncherCallbacks != null) {
2830 mLauncherCallbacks.onInteractionEnd();
2831 }
2832 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002833
2834 /**
2835 * Called when the user starts interacting with the launcher.
2836 * The possible interactions are:
2837 * - open all apps
2838 * - reorder an app shortcut, or a widget
2839 * - open the overview mode.
2840 * This is a good time to stop doing things that only make sense
2841 * when the user is on the homescreen and not doing housekeeping.
2842 */
Adam Cohen9211d422014-10-07 18:14:53 -07002843 protected void onInteractionBegin() {
2844 if (mLauncherCallbacks != null) {
2845 mLauncherCallbacks.onInteractionBegin();
2846 }
2847 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002848
Winson Chungcd99cd32015-04-29 11:03:24 -07002849 /** Updates the interaction state. */
2850 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002851 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002852 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002853 boolean fromStateWithOverlay;
2854 boolean toStateWithOverlay;
2855 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2856 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2857 toStateWithOverlay = toState != Workspace.State.NORMAL;
2858 } else {
2859 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2860 fromState != Workspace.State.NORMAL_HIDDEN;
2861 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2862 toState != Workspace.State.NORMAL_HIDDEN;
2863 }
2864 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002865 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002866 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002867 onInteractionEnd();
2868 }
2869 }
2870
Kenny Guyf07af7b2014-07-31 11:39:16 +01002871 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002872 try {
2873 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002874 launcherApps.showAppDetailsForProfile(componentName, user);
2875 } catch (SecurityException e) {
2876 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2877 Log.e(TAG, "Launcher does not have permission to launch settings");
2878 } catch (ActivityNotFoundException e) {
2879 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2880 Log.e(TAG, "Unable to launch settings");
2881 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002882 }
2883
Michael Jurka1e2f4652013-07-08 18:03:46 -07002884 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002885 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2886 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002887 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002888 // System applications cannot be installed. For now, show a toast explaining that.
2889 // We may give them the option of disabling apps this way.
2890 int messageId = R.string.uninstall_system_app_text;
2891 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002892 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002893 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002894 String packageName = componentName.getPackageName();
2895 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002896 Intent intent = new Intent(
2897 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002898 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2899 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002900 if (user != null) {
2901 user.addToIntent(intent, Intent.EXTRA_USER);
2902 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002903 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002904 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002905 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002906 }
2907
Winson Chung8f1eff72015-05-28 17:33:40 -07002908 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002909 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002910 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002911 // Only launch using the new animation if the shortcut has not opted out (this is a
2912 // private contract between launcher and may be ignored in the future).
2913 boolean useLaunchAnimation = (v != null) &&
2914 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002915 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2916 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002917
Kenny Guy1317e2d2014-05-08 18:52:50 +01002918 UserHandleCompat user = null;
2919 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2920 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2921 user = userManager.getUserForSerialNumber(serialNumber);
2922 }
2923
2924 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002925 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002926 ActivityOptions opts = null;
2927 if (sClipRevealMethod != null) {
2928 // TODO: call method directly when Launcher3 can depend on M APIs
2929 int left = 0, top = 0;
2930 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2931 if (v instanceof TextView) {
2932 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002933 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2934 if (icon != null) {
2935 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002936 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002937 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002938 width = bounds.width();
2939 height = bounds.height();
2940 }
2941 }
2942 try {
2943 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2944 left, top, width, height);
2945 } catch (IllegalAccessException e) {
2946 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2947 sClipRevealMethod = null;
2948 } catch (InvocationTargetException e) {
2949 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2950 sClipRevealMethod = null;
2951 }
2952 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002953 if (opts == null && !Utilities.isLmpOrAbove()) {
2954 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002955 v.getMeasuredWidth(), v.getMeasuredHeight());
2956 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002957 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002958 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002959
2960 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2961 // Could be launching some bookkeeping activity
2962 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002963 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002964 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002965 launcherApps.startActivityForProfile(intent.getComponent(), user,
2966 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002967 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002968 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 } catch (SecurityException e) {
2970 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002971 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002972 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002973 "or use the exported attribute for this activity. "
2974 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002976 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002978
Winson Chung8f1eff72015-05-28 17:33:40 -07002979 private boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002980 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002981 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2982 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2983 return false;
2984 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002985 try {
2986 success = startActivity(v, intent, tag);
2987 } catch (ActivityNotFoundException e) {
2988 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2989 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2990 }
2991 return success;
2992 }
2993
Adam Cohen268c4752012-06-06 17:47:33 -07002994 /**
2995 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2996 * in the DragLayer in the exact absolute location of the original FolderIcon.
2997 */
2998 private void copyFolderIconToImage(FolderIcon fi) {
2999 final int width = fi.getMeasuredWidth();
3000 final int height = fi.getMeasuredHeight();
3001
3002 // Lazy load ImageView, Bitmap and Canvas
3003 if (mFolderIconImageView == null) {
3004 mFolderIconImageView = new ImageView(this);
3005 }
3006 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3007 mFolderIconBitmap.getHeight() != height) {
3008 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3009 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3010 }
3011
3012 DragLayer.LayoutParams lp;
3013 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3014 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3015 } else {
3016 lp = new DragLayer.LayoutParams(width, height);
3017 }
3018
Adam Cohen307fe232012-08-16 17:55:58 -07003019 // The layout from which the folder is being opened may be scaled, adjust the starting
3020 // view size by this scale factor.
3021 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003022 lp.customPosition = true;
3023 lp.x = mRectForFolderAnimation.left;
3024 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003025 lp.width = (int) (scale * width);
3026 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003027
3028 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3029 fi.draw(mFolderIconCanvas);
3030 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003031 if (fi.getFolder() != null) {
3032 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3033 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003034 }
Adam Cohen268c4752012-06-06 17:47:33 -07003035 // Just in case this image view is still in the drag layer from a previous animation,
3036 // we remove it and re-add it.
3037 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3038 mDragLayer.removeView(mFolderIconImageView);
3039 }
3040 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003041 if (fi.getFolder() != null) {
3042 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003043 }
Adam Cohen268c4752012-06-06 17:47:33 -07003044 }
3045
Adam Cohen2801caf2011-05-13 20:57:39 -07003046 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003047 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003048 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3049 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3050 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3051
Adam Cohenc51934b2011-07-26 21:07:43 -07003052 FolderInfo info = (FolderInfo) fi.getTag();
3053 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3054 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003055 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3056 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003057 }
3058
Adam Cohen268c4752012-06-06 17:47:33 -07003059 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3060 copyFolderIconToImage(fi);
3061 fi.setVisibility(View.INVISIBLE);
3062
Michael Jurka2ecf9952012-06-18 12:52:28 -07003063 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003064 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003065 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003066 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3067 }
3068 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003069 oa.start();
3070 }
3071
Adam Cohen268c4752012-06-06 17:47:33 -07003072 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003073 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003074 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3075 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3076 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3077
Adam Cohen268c4752012-06-06 17:47:33 -07003078 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003079
Adam Cohen268c4752012-06-06 17:47:33 -07003080 // We remove and re-draw the FolderIcon in-case it has changed
3081 mDragLayer.removeView(mFolderIconImageView);
3082 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003083 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003084 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003085 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003086 oa.addListener(new AnimatorListenerAdapter() {
3087 @Override
3088 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003089 if (cl != null) {
3090 cl.clearFolderLeaveBehind();
3091 // Remove the ImageView copy of the FolderIcon and make the original visible.
3092 mDragLayer.removeView(mFolderIconImageView);
3093 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003094 }
3095 }
3096 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003097 oa.start();
3098 }
3099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003101 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102 * is animated relative to the specified View. If the View is null, no animation
3103 * is played.
3104 *
3105 * @param folderInfo The FolderInfo describing the folder to open.
3106 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003107 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003108 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003109 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3110 if (openFolder != null && openFolder != folder) {
3111 // Close any open folder before opening a folder.
3112 closeFolder();
3113 }
3114
Adam Cohena9cf38f2011-05-02 15:36:58 -07003115 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116
Adam Cohena9cf38f2011-05-02 15:36:58 -07003117 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118
Sunny Goyalf4066152015-04-15 09:42:19 -07003119 // While the folder is open, the position of the icon cannot change.
3120 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3121
Adam Cohen4554ee12011-08-03 16:13:21 -07003122 // Just verify that the folder hasn't already been added to the DragLayer.
3123 // There was a one-off crash where the folder had a parent already.
3124 if (folder.getParent() == null) {
3125 mDragLayer.addView(folder);
3126 mDragController.addDropTarget((DropTarget) folder);
3127 } else {
3128 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3129 folder.getParent() + ").");
3130 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003131 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003132 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003133
3134 // Notify the accessibility manager that this folder "window" has appeared and occluded
3135 // the workspace items
3136 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3137 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003138 }
3139
3140 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003141 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003142 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003143 if (folder.isEditingName()) {
3144 folder.dismissEditingName();
3145 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003146 closeFolder(folder);
3147 }
3148 }
3149
Sunny Goyal83a8f042015-05-19 12:52:12 -07003150 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003151 folder.getInfo().opened = false;
3152
3153 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3154 if (parent != null) {
3155 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3156 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003157 if (fi != null) {
3158 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3159 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003160 }
3161 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003162
3163 // Notify the accessibility manager that this folder "window" has disappeard and no
3164 // longer occludeds the workspace items
3165 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003166 }
3167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003168 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003169 if (!isDraggingEnabled()) return false;
3170 if (isWorkspaceLocked()) return false;
3171 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172
Adam Cohen1697b792013-09-17 19:08:21 -07003173 if (v instanceof Workspace) {
3174 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003175 if (!mWorkspace.isTouchActive()) {
3176 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003177 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3178 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3179 return true;
3180 } else {
3181 return false;
3182 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003183 } else {
3184 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003185 }
Adam Cohen1697b792013-09-17 19:08:21 -07003186 }
3187
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003188 CellLayout.CellInfo longClickCellInfo = null;
3189 View itemUnderLongClick = null;
3190 if (v.getTag() instanceof ItemInfo) {
3191 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003192 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003193 itemUnderLongClick = longClickCellInfo.cell;
3194 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 }
3196
Winson Chung3d503fb2011-07-13 17:25:49 -07003197 // The hotseat touch handling does not go through Workspace, and we always allow long press
3198 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003199 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003200 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003201 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003202 // User long pressed on empty space
3203 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3204 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003205 if (mWorkspace.isInOverviewMode()) {
3206 mWorkspace.startReordering(v);
3207 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003208 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003210 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003211 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3212 mHotseat.getOrderInHotseat(
3213 longClickCellInfo.cellX,
3214 longClickCellInfo.cellY));
3215 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003217 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218 }
3219 }
3220 }
3221 return true;
3222 }
3223
Winson Chung3d503fb2011-07-13 17:25:49 -07003224 boolean isHotseatLayout(View layout) {
3225 return mHotseat != null && layout != null &&
3226 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3227 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003228
Winson Chung3d503fb2011-07-13 17:25:49 -07003229 /**
3230 * Returns the CellLayout of the specified container at the specified screen.
3231 */
Sunny Goyal92820592015-03-02 11:31:35 -08003232 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003233 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3234 if (mHotseat != null) {
3235 return mHotseat.getLayout();
3236 } else {
3237 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003238 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003239 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003240 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003241 }
3242 }
3243
Winson Chungb745afb2015-03-02 11:51:23 -08003244 /**
3245 * For overridden classes.
3246 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003247 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003248 return isAppsViewVisible();
3249 }
3250
3251 public boolean isAppsViewVisible() {
3252 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3253 }
3254
3255 public boolean isWidgetsViewVisible() {
3256 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003257 }
3258
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003259 private void setWorkspaceBackground(int background) {
3260 switch (background) {
3261 case WORKSPACE_BACKGROUND_TRANSPARENT:
3262 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3263 break;
3264 case WORKSPACE_BACKGROUND_BLACK:
3265 getWindow().setBackgroundDrawable(null);
3266 break;
3267 default:
3268 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3269 }
Craig Mautner360310b2012-10-26 15:13:08 -07003270 }
3271
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003272 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003273 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3274 int curflags = getWindow().getAttributes().flags
3275 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3276 if (wpflags != curflags) {
3277 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3278 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003279 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003280 }
3281
Michael Jurkae326f182011-11-21 14:05:46 -08003282 @Override
3283 public void onTrimMemory(int level) {
3284 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003285 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3286 // The widget preview db can result in holding onto over
3287 // 3MB of memory for caching which isn't necessary.
3288 SQLiteDatabase.releaseMemory();
3289
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003290 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003291 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003292 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003293 if (mLauncherCallbacks != null) {
3294 mLauncherCallbacks.onTrimMemory(level);
3295 }
Michael Jurkae326f182011-11-21 14:05:46 -08003296 }
3297
Winson Chungb745afb2015-03-02 11:51:23 -08003298 @Override
3299 public void onStateTransitionHideSearchBar() {
3300 // Hide the search bar
3301 if (mSearchDropTargetBar != null) {
3302 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3303 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003304 }
3305
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003306 public void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003307 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3308 true);
Adam Cohened307df2013-10-02 09:37:31 -07003309 }
3310
Sunny Goyal83a8f042015-05-19 12:52:12 -07003311 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003312 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3313 onCompleteRunnable, true);
3314 }
3315
3316 protected void showWorkspace(int snapToPage, boolean animated) {
3317 showWorkspace(snapToPage, animated, null, true);
3318 }
3319
3320 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3321 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003322 boolean changed = mState != State.WORKSPACE ||
3323 mWorkspace.getState() != Workspace.State.NORMAL;
3324 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003325 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003326 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003327 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003328 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003329
Winson Chungc7d2b602012-05-16 17:02:20 -07003330 // Show the search bar (only animate if we were showing the drop target bar in spring
3331 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003332 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003333 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003334 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003335
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336 // Set focus to the AppsCustomize button
3337 if (mAllAppsButton != null) {
3338 mAllAppsButton.requestFocus();
3339 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003340 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003341
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003342 // Change the state *after* we've called all the transition code
3343 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003344
Winson Chung5fb63472011-02-02 17:03:37 -08003345 // Resume the auto-advance of widgets
3346 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003347 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003348
Winson Chung0f785722015-04-08 10:27:49 -07003349 if (changed) {
3350 // Send an accessibility event to announce the context change
3351 getWindow().getDecorView()
3352 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003353 if (notifyLauncherCallbacks) {
3354 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003355 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003356 mLauncherCallbacks.onAllAppsHidden();
3357 }
3358 }
Winson Chung0f785722015-04-08 10:27:49 -07003359 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003360 }
3361
Adam Cohened307df2013-10-02 09:37:31 -07003362 void showOverviewMode(boolean animated) {
3363 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003364 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003365 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3366 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003367 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003368 }
3369
Winson Chungb745afb2015-03-02 11:51:23 -08003370 /**
3371 * Shows the apps view.
3372 */
Winson Chung4ac30062015-05-08 17:34:17 -07003373 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003374 if (resetListToTop) {
3375 mAppsView.scrollToTop();
3376 }
Winson Chung4ac30062015-05-08 17:34:17 -07003377 if (updatePredictedApps) {
3378 tryAndUpdatePredictedApps();
3379 }
Winson Chungb745afb2015-03-02 11:51:23 -08003380 showAppsOrWidgets(animated, State.APPS);
3381 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003382
Winson Chungb745afb2015-03-02 11:51:23 -08003383 /**
3384 * Shows the widgets view.
3385 */
3386 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003387 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003388 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003389 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003390 }
Winson Chungb745afb2015-03-02 11:51:23 -08003391 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003392
3393 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003394 @Override
3395 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003396 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003397 }
3398 });
Winson Chungb745afb2015-03-02 11:51:23 -08003399 }
3400
3401 /**
3402 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003403 *
3404 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003405 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003406 // TODO: calling method should use the return value so that when {@code false} is returned
3407 // the workspace transition doesn't fall into invalid state.
3408 private boolean showAppsOrWidgets(boolean animated, State toState) {
3409 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3410 mState != State.WIDGETS_SPRING_LOADED) {
3411 return false;
3412 }
3413 if (toState != State.APPS && toState != State.WIDGETS) {
3414 return false;
3415 }
Winson Chungb745afb2015-03-02 11:51:23 -08003416
3417 if (toState == State.APPS) {
3418 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003419 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003420 mLauncherCallbacks.onAllAppsShown();
3421 }
Winson Chungb745afb2015-03-02 11:51:23 -08003422 } else {
3423 mStateTransitionAnimation.startAnimationToWidgets(animated);
3424 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425
Michael Jurkab3e22d92011-10-31 15:58:33 -07003426 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003427 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003428
3429 // Pause the auto-advance of widgets until we are out of AllApps
3430 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003431 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003432 closeFolder();
3433
3434 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003435 getWindow().getDecorView()
3436 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003437 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003438 }
3439
Winson Chungcd99cd32015-04-29 11:03:24 -07003440 /**
3441 * Updates the workspace and interaction state on state change, and return the animation to this
3442 * new state.
3443 */
3444 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3445 boolean animated, HashMap<View, Integer> layerViews) {
3446 Workspace.State fromState = mWorkspace.getState();
3447 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3448 updateInteraction(fromState, toState);
3449 return anim;
3450 }
3451
Hyunyoung Song3f471442015-04-08 19:01:34 -07003452 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003453 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003454 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3455 mState == State.WIDGETS_SPRING_LOADED) {
3456 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003457 }
Winson Chungb745afb2015-03-02 11:51:23 -08003458
3459 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003460 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3461 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003462 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003463 }
Adam Cohen7777d962011-08-18 18:58:38 -07003464
Hyunyoung Song3f471442015-04-08 19:01:34 -07003465 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003466 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003467 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003468
Winson Chungcd99cd32015-04-29 11:03:24 -07003469 if (successfulDrop) {
3470 // We need to trigger all apps hidden to notify search to update itself before the
3471 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003472 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003473 mLauncherCallbacks.onAllAppsHidden();
3474 }
3475 }
3476
Winson Chunge7a03942011-08-05 15:05:12 -07003477 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003478 @Override
3479 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003480 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003481 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3482 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003483 // Before we show workspace, hide all apps again because
3484 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3485 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003486 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003487 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003488 } else {
3489 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003490 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003491 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003492 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003493 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003494
Michael Jurkad3ef3062010-11-23 16:23:58 -08003495 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003496 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003497 showAppsView(true /* animated */, false /* resetListToTop */,
3498 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003499 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003500 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003501 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003502 }
3503
Winson Chung4ac30062015-05-08 17:34:17 -07003504 /**
3505 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3506 * resumed.
3507 */
3508 private void tryAndUpdatePredictedApps() {
3509 if (mLauncherCallbacks != null) {
3510 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3511 if (!apps.isEmpty()) {
3512 mAppsView.setPredictedApps(apps);
3513 }
3514 }
3515 }
3516
Michael Jurkab3e22d92011-10-31 15:58:33 -07003517 void lockAllApps() {
3518 // TODO
3519 }
3520
3521 void unlockAllApps() {
3522 // TODO
3523 }
3524
Sunny Goyal594d76d2014-11-06 10:12:54 -08003525 protected void disableVoiceButtonProxy(boolean disable) {
3526 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003527 }
3528
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003529 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003530 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3531 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003532 }
3533
Adam Cohen24ce0b32014-01-14 16:18:14 -08003534 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003535 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3536 if (searchProvider == null) {
3537 return null;
3538 }
3539
3540 Bundle opts = new Bundle();
3541 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003542 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003543
3544 SharedPreferences sp = getSharedPreferences(
3545 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3546 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003547 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003548 if (!searchProvider.provider.flattenToString().equals(
3549 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003550 || (widgetInfo == null)
3551 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003552 // A valid widget is not already bound.
3553 if (widgetId > -1) {
3554 mAppWidgetHost.deleteAppWidgetId(widgetId);
3555 widgetId = -1;
3556 }
3557
3558 // Try to bind a new widget
3559 widgetId = mAppWidgetHost.allocateAppWidgetId();
3560
3561 if (!AppWidgetManagerCompat.getInstance(this)
3562 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3563 mAppWidgetHost.deleteAppWidgetId(widgetId);
3564 widgetId = -1;
3565 }
3566
3567 sp.edit()
3568 .putInt(QSB_WIDGET_ID, widgetId)
3569 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3570 .commit();
3571 }
3572
3573 if (widgetId != -1) {
3574 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3575 mQsb.updateAppWidgetOptions(opts);
3576 mQsb.setPadding(0, 0, 0, 0);
3577 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003578 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003579 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003580 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003581 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003582 }
3583
Sunny Goyal22235bc2015-04-03 09:23:43 -07003584 private void reinflateQSBIfNecessary() {
3585 if (mQsb instanceof LauncherAppWidgetHostView &&
3586 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3587 mSearchDropTargetBar.removeView(mQsb);
3588 mQsb = null;
3589 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3590 }
3591 }
3592
Michael Jurkad7c28052012-04-27 15:43:36 -07003593 @Override
3594 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003595 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003596 final List<CharSequence> text = event.getText();
3597 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003598 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003599 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003600 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003601 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003602 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003603 } else {
3604 text.add(getString(R.string.all_apps_home_button_label));
3605 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003606 return result;
3607 }
3608
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003609 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003610 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003611 */
Adam Cohen091440a2015-03-18 14:16:05 -07003612 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003613 @Override
3614 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003615 closeSystemDialogs();
3616 }
3617 }
3618
3619 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003620 * Receives notifications whenever the appwidgets are reset.
3621 */
3622 private class AppWidgetResetObserver extends ContentObserver {
3623 public AppWidgetResetObserver() {
3624 super(new Handler());
3625 }
3626
3627 @Override
3628 public void onChange(boolean selfChange) {
3629 onAppWidgetReset();
3630 }
3631 }
3632
3633 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003634 * If the activity is currently paused, signal that we need to run the passed Runnable
3635 * in onResume.
3636 *
3637 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003638 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3639 * wrong when we're not running, and if the activity comes back to what the configuration was
3640 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003641 *
3642 * Implementation of the method from LauncherModel.Callbacks.
3643 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003644 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003645 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003646 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003647 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003648 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003649 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003650 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003651 }
3652 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003653 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003654 return true;
3655 } else {
3656 return false;
3657 }
3658 }
3659
Michael Jurkac402cd92013-05-20 15:49:32 +02003660 private boolean waitUntilResume(Runnable run) {
3661 return waitUntilResume(run, false);
3662 }
3663
Michael Jurka1e2f4652013-07-08 18:03:46 -07003664 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003665 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003666 }
3667
Michael Jurka7607c2f2013-04-03 14:33:19 -07003668 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003669 * If the activity is currently paused, signal that we need to re-run the loader
3670 * in onResume.
3671 *
3672 * This needs to be called from incoming places where resources might have been loaded
3673 * while we are paused. That is becaues the Configuration might be wrong
3674 * when we're not running, and if it comes back to what it was when we
3675 * were paused, we are not restarted.
3676 *
3677 * Implementation of the method from LauncherModel.Callbacks.
3678 *
3679 * @return true if we are currently paused. The caller might be able to
3680 * skip some work in that case since we will come back again.
3681 */
3682 public boolean setLoadOnResume() {
3683 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003684 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003685 mOnResumeNeedsLoad = true;
3686 return true;
3687 } else {
3688 return false;
3689 }
3690 }
3691
3692 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003694 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003696 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003697 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003698 } else {
3699 return SCREEN_COUNT / 2;
3700 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003701 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003702
Joe Onorato9c1289c2009-08-17 11:03:03 -04003703 /**
3704 * Refreshes the shortcuts shown on the workspace.
3705 *
3706 * Implementation of the method from LauncherModel.Callbacks.
3707 */
3708 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003709 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003710
Michael Jurka7607c2f2013-04-03 14:33:19 -07003711 // If we're starting binding all over again, clear any bind calls we'd postponed in
3712 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3713 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003714 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003715
Winson Chungd64d1762013-08-20 14:37:16 -07003716 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003717 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003718 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003719
Michael Jurka05bf6442011-11-30 20:28:53 -08003720 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003721 if (mHotseat != null) {
3722 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003723 }
3724 }
3725
Adam Cohendcd297f2013-06-18 13:13:40 -07003726 @Override
3727 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003728 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003729
Adam Cohen5084cba2013-09-03 12:01:16 -07003730 // If there are no screens, we need to have an empty screen
3731 if (orderedScreenIds.size() == 0) {
3732 mWorkspace.addExtraEmptyScreen();
3733 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003734
3735 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003736 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003737 if (hasCustomContentToLeft()) {
3738 mWorkspace.createCustomContentContainer();
3739 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003740 }
Winson Chung64359a52013-07-08 17:17:08 -07003741 }
3742
3743 @Override
3744 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003745 int count = orderedScreenIds.size();
3746 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003747 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003748 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003749 }
3750
Adam Cohen39a06042013-07-19 14:30:12 -07003751 private boolean shouldShowWeightWatcher() {
3752 String spKey = LauncherAppState.getSharedPreferencesKey();
3753 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003754 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003755
3756 return show;
3757 }
3758
3759 private void toggleShowWeightWatcher() {
3760 String spKey = LauncherAppState.getSharedPreferencesKey();
3761 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3762 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3763
3764 show = !show;
3765
3766 SharedPreferences.Editor editor = sp.edit();
3767 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3768 editor.commit();
3769
3770 if (mWeightWatcher != null) {
3771 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3772 }
3773 }
3774
Winson Chungd64d1762013-08-20 14:37:16 -07003775 public void bindAppsAdded(final ArrayList<Long> newScreens,
3776 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003777 final ArrayList<ItemInfo> addAnimated,
3778 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003779 Runnable r = new Runnable() {
3780 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003781 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003782 }
3783 };
3784 if (waitUntilResume(r)) {
3785 return;
3786 }
3787
Winson Chungd64d1762013-08-20 14:37:16 -07003788 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003789 if (newScreens != null) {
3790 bindAddScreens(newScreens);
3791 }
Winson Chungd64d1762013-08-20 14:37:16 -07003792
3793 // We add the items without animation on non-visible pages, and with
3794 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003795 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003796 bindItems(addNotAnimated, 0,
3797 addNotAnimated.size(), false);
3798 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003799 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003800 bindItems(addAnimated, 0,
3801 addAnimated.size(), true);
3802 }
Winson Chungc58497e2013-09-03 17:48:37 -07003803
Winson Chung87412982013-10-03 18:34:14 -07003804 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003805 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003806
Winson Chungb745afb2015-03-02 11:51:23 -08003807 if (addedApps != null && mAppsView != null) {
3808 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003809 }
Winson Chungd64d1762013-08-20 14:37:16 -07003810 }
3811
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003812 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813 * Bind the items start-end from the list.
3814 *
3815 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003816 */
Winson Chung64359a52013-07-08 17:17:08 -07003817 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3818 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003819 Runnable r = new Runnable() {
3820 public void run() {
3821 bindItems(shortcuts, start, end, forceAnimateIcons);
3822 }
3823 };
3824 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003825 return;
3826 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003827
Winson Chungf0c6ae02012-03-21 16:10:31 -07003828 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003829 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3830 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003831 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003832 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003833 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003834 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003836
3837 // Short circuit if we are loading dock items for a configuration which has no dock
3838 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3839 mHotseat == null) {
3840 continue;
3841 }
3842
Joe Onorato9c1289c2009-08-17 11:03:03 -04003843 switch (item.itemType) {
3844 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3845 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003846 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003847 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003848
Winson Chung64359a52013-07-08 17:17:08 -07003849 /*
3850 * TODO: FIX collision case
3851 */
Winson Chungce376632013-07-11 16:12:41 -07003852 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3853 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3854 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003855 View v = cl.getChildAt(item.cellX, item.cellY);
3856 Object tag = v.getTag();
3857 String desc = "Collision while binding workspace item: " + item
3858 + ". Collides with " + tag;
3859 if (LauncherAppState.isDogfoodBuild()) {
3860 throw (new RuntimeException(desc));
3861 } else {
3862 Log.d(TAG, desc);
3863 }
Winson Chungce376632013-07-11 16:12:41 -07003864 }
Winson Chung64359a52013-07-08 17:17:08 -07003865 }
3866
Adam Cohendcd297f2013-06-18 13:13:40 -07003867 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3868 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003869 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003870 // Animate all the applications up now
3871 shortcut.setAlpha(0f);
3872 shortcut.setScaleX(0f);
3873 shortcut.setScaleY(0f);
3874 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003875 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003876 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003877 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003878 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003879 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003880 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003881 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003882 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3883 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003885 default:
3886 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003888 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003889
Winson Chung997a9232013-07-24 15:33:46 -07003890 if (animateIcons) {
3891 // Animate to the correct page
3892 if (newShortcutsScreenId > -1) {
3893 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003894 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003895 final Runnable startBounceAnimRunnable = new Runnable() {
3896 public void run() {
3897 anim.playTogether(bounceAnims);
3898 anim.start();
3899 }
3900 };
Winson Chung997a9232013-07-24 15:33:46 -07003901 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003902 // We post the animation slightly delayed to prevent slowdowns
3903 // when we are loading right after we return to launcher.
3904 mWorkspace.postDelayed(new Runnable() {
3905 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003906 if (mWorkspace != null) {
3907 mWorkspace.snapToPage(newScreenIndex);
3908 mWorkspace.postDelayed(startBounceAnimRunnable,
3909 NEW_APPS_ANIMATION_DELAY);
3910 }
Winson Chung94d67682013-09-25 16:29:40 -07003911 }
3912 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003913 } else {
3914 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003915 }
3916 }
Winson Chung64359a52013-07-08 17:17:08 -07003917 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003918 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003919 }
3920
Joe Onorato9c1289c2009-08-17 11:03:03 -04003921 /**
3922 * Implementation of the method from LauncherModel.Callbacks.
3923 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003924 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003925 Runnable r = new Runnable() {
3926 public void run() {
3927 bindFolders(folders);
3928 }
3929 };
3930 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003931 return;
3932 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003933 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003934 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935
3936 /**
3937 * Add the views for a widget to the workspace.
3938 *
3939 * Implementation of the method from LauncherModel.Callbacks.
3940 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003941 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003942 Runnable r = new Runnable() {
3943 public void run() {
3944 bindAppWidget(item);
3945 }
3946 };
3947 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003948 return;
3949 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003950
Daniel Sandler843e8602010-06-07 14:59:01 -04003951 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3952 if (DEBUG_WIDGETS) {
3953 Log.d(TAG, "bindAppWidget: " + item);
3954 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003955 final Workspace workspace = mWorkspace;
3956
Adam Cohen59400422014-03-05 18:07:04 -08003957 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003958 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003959
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003960 if (!mIsSafeModeEnabled
3961 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3962 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003963 if (appWidgetInfo == null) {
3964 if (DEBUG_WIDGETS) {
3965 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3966 + " belongs to component " + item.providerName
3967 + ", as the povider is null");
3968 }
3969 LauncherModel.deleteItemFromDatabase(this, item);
3970 return;
3971 }
3972 // Note: This assumes that the id remap broadcast is received before this step.
3973 // If that is not the case, the id remap will be ignored and user may see the
3974 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07003975 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003976 pendingInfo.spanX = item.spanX;
3977 pendingInfo.spanY = item.spanY;
3978 pendingInfo.minSpanX = item.minSpanX;
3979 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003980 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003981 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003982
Sunny Goyalff572272014-07-23 13:58:07 -07003983 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003984 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3985 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003986
3987 // TODO consider showing a permission dialog when the widget is clicked.
3988 if (!success) {
3989 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3990 if (DEBUG_WIDGETS) {
3991 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3992 + " belongs to component " + item.providerName
3993 + ", as the launcher is unable to bing a new widget id");
3994 }
3995 LauncherModel.deleteItemFromDatabase(this, item);
3996 return;
3997 }
3998
3999 item.appWidgetId = newWidgetId;
4000
4001 // If the widget has a configure activity, it is still needs to set it up, otherwise
4002 // the widget is ready to go.
4003 item.restoreStatus = (appWidgetInfo.configure == null)
4004 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4005 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4006
4007 LauncherModel.updateItemInDatabase(this, item);
4008 }
4009
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004010 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004011 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004012 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004013 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4014 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004015 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004016
Sunny Goyal651077b2014-06-30 14:15:31 -07004017 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4018 } else {
4019 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004020 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4021 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004022 view.updateIcon(mIconCache);
4023 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004024 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004025 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004026 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004027
Joe Onorato9c1289c2009-08-17 11:03:03 -04004028 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004029 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030
Adam Cohendcd297f2013-06-18 13:13:40 -07004031 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004032 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004033 if (!item.isCustomWidget()) {
4034 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4035 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004036
Joe Onorato9c1289c2009-08-17 11:03:03 -04004037 workspace.requestLayout();
4038
Daniel Sandler843e8602010-06-07 14:59:01 -04004039 if (DEBUG_WIDGETS) {
4040 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4041 + (SystemClock.uptimeMillis()-start) + "ms");
4042 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004043 }
4044
Sunny Goyalff572272014-07-23 13:58:07 -07004045 /**
4046 * Restores a pending widget.
4047 *
4048 * @param appWidgetId The app widget id
4049 * @param cellInfo The position on screen where to create the widget.
4050 */
4051 private void completeRestoreAppWidget(final int appWidgetId) {
4052 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4053 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4054 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4055 return;
4056 }
4057
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004058 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004059 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4060
4061 mWorkspace.reinflateWidgetsIfNecessary();
4062 LauncherModel.updateItemInDatabase(this, info);
4063 }
4064
Adam Cohen1462de32012-07-24 22:34:36 -07004065 public void onPageBoundSynchronously(int page) {
4066 mSynchronouslyBoundPages.add(page);
4067 }
4068
Joe Onorato9c1289c2009-08-17 11:03:03 -04004069 /**
4070 * Callback saying that there aren't any more items to bind.
4071 *
4072 * Implementation of the method from LauncherModel.Callbacks.
4073 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004074 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004075 Runnable r = new Runnable() {
4076 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004077 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004078 }
4079 };
4080 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004081 return;
4082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004083 if (mSavedState != null) {
4084 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004085 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004086 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004087 mSavedState = null;
4088 }
4089
Adam Cohen1462de32012-07-24 22:34:36 -07004090 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004091
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004092 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004093 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004094
4095 // If we received the result of any pending adds while the loader was running (e.g. the
4096 // widget configuration forced an orientation change), process them now.
4097 if (sPendingAddItem != null) {
4098 final long screenId = completeAdd(sPendingAddItem);
4099
4100 // TODO: this moves the user to the page where the pending item was added. Ideally,
4101 // the screen would be guaranteed to exist after bind, and the page would be set through
4102 // the workspace restore process.
4103 mWorkspace.post(new Runnable() {
4104 @Override
4105 public void run() {
4106 mWorkspace.snapToScreenId(screenId);
4107 }
4108 });
4109 sPendingAddItem = null;
4110 }
4111
Sunny Goyal756adbc2015-04-16 15:20:51 -07004112 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004113
4114 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004115 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004116 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004117 }
4118
Adam Cohen3ed316a2014-07-23 18:21:20 -07004119 private void sendLoadingCompleteBroadcastIfNecessary() {
4120 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4121 String permission =
4122 getResources().getString(R.string.receive_first_load_broadcast_permission);
4123 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4124 sendBroadcast(intent, permission);
4125 SharedPreferences.Editor editor = mSharedPrefs.edit();
4126 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4127 editor.apply();
4128 }
4129 }
4130
Winson Chunga0b7e862013-09-05 16:03:15 -07004131 public boolean isAllAppsButtonRank(int rank) {
4132 if (mHotseat != null) {
4133 return mHotseat.isAllAppsButtonRank(rank);
4134 }
4135 return false;
4136 }
4137
Winson Chunga2413752012-04-03 14:22:34 -07004138 private boolean canRunNewAppsAnimation() {
4139 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4140 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4141 }
4142
Winson Chungc9168342013-06-26 14:54:55 -07004143 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4144 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4145 PropertyValuesHolder.ofFloat("alpha", 1f),
4146 PropertyValuesHolder.ofFloat("scaleX", 1f),
4147 PropertyValuesHolder.ofFloat("scaleY", 1f));
4148 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4149 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004150 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004151 return bounceAnim;
4152 }
4153
Adam Cohen27772732013-11-11 14:00:56 +00004154 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004155 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004156 }
4157
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004158 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004159 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004160 }
4161
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004162 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004163 if (mSearchDropTargetBar == null) {
4164 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004165 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004166 if (mQsb != null) {
4167 mSearchDropTargetBar.removeView(mQsb);
4168 mQsb = null;
4169 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004170 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004171 }
4172
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004173 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004174 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4175 * multiple calls to bind the same list.)
4176 */
4177 @Thunk ArrayList<AppInfo> mTmpAppsList;
4178 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4179 public void run() {
4180 bindAllApplications(mTmpAppsList);
4181 mTmpAppsList = null;
4182 }
4183 };
4184
4185 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004186 * Add the icons for all apps.
4187 *
4188 * Implementation of the method from LauncherModel.Callbacks.
4189 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004190 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004191 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4192 mTmpAppsList = apps;
4193 return;
4194 }
4195
Winson Chungb745afb2015-03-02 11:51:23 -08004196 if (mAppsView != null) {
4197 mAppsView.setApps(apps);
4198 }
Adam Cohen9211d422014-10-07 18:14:53 -07004199 if (mLauncherCallbacks != null) {
4200 mLauncherCallbacks.bindAllApplications(apps);
4201 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 }
4203
4204 /**
4205 * A package was updated.
4206 *
4207 * Implementation of the method from LauncherModel.Callbacks.
4208 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004209 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004210 Runnable r = new Runnable() {
4211 public void run() {
4212 bindAppsUpdated(apps);
4213 }
4214 };
4215 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004216 return;
4217 }
4218
Winson Chungb745afb2015-03-02 11:51:23 -08004219 if (mAppsView != null) {
4220 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004221 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004222 }
4223
Sunny Goyal4390ace2014-10-13 11:33:11 -07004224 @Override
4225 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4226 Runnable r = new Runnable() {
4227 public void run() {
4228 bindWidgetsRestored(widgets);
4229 }
4230 };
4231 if (waitUntilResume(r)) {
4232 return;
4233 }
4234 mWorkspace.widgetsRestored(widgets);
4235 }
4236
Joe Onorato9c1289c2009-08-17 11:03:03 -04004237 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004238 * Some shortcuts were updated in the background.
4239 *
4240 * Implementation of the method from LauncherModel.Callbacks.
4241 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004242 @Override
4243 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4244 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004245 Runnable r = new Runnable() {
4246 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004247 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004248 }
4249 };
4250 if (waitUntilResume(r)) {
4251 return;
4252 }
4253
Sunny Goyal4390ace2014-10-13 11:33:11 -07004254 if (!updated.isEmpty()) {
4255 mWorkspace.updateShortcuts(updated);
4256 }
4257
4258 if (!removed.isEmpty()) {
4259 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4260 for (ShortcutInfo si : removed) {
4261 removedComponents.add(si.getTargetComponent());
4262 }
4263 mWorkspace.removeItemsByComponentName(removedComponents, user);
4264 // Notify the drag controller
4265 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004266 }
4267 }
4268
4269 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004270 * Update the state of a package, typically related to install state.
4271 *
4272 * Implementation of the method from LauncherModel.Callbacks.
4273 */
Sunny Goyale755d462014-07-22 13:48:29 -07004274 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004275 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4276 Runnable r = new Runnable() {
4277 public void run() {
4278 bindRestoreItemsChange(updates);
4279 }
4280 };
4281 if (waitUntilResume(r)) {
4282 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004283 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004284
Sunny Goyal756adbc2015-04-16 15:20:51 -07004285 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004286 }
4287
4288 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004289 * A package was uninstalled. We take both the super set of packageNames
4290 * in addition to specific applications to remove, the reason being that
4291 * this can be called when a package is updated as well. In that scenario,
4292 * we only remove specific components from the workspace, where as
4293 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004294 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004295 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004296 * Implementation of the method from LauncherModel.Callbacks.
4297 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004298 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004299 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004300 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004301 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004302 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004303 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004304 }
Winson Chungd64d1762013-08-20 14:37:16 -07004305 };
4306 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004307 return;
4308 }
4309
Sunny Goyal1a745e82014-10-02 15:58:31 -07004310 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004311 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4312 for (AppInfo info : appInfos) {
4313 removedComponents.add(info.componentName);
4314 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004315 if (!packageNames.isEmpty()) {
4316 mWorkspace.removeItemsByPackageName(packageNames, user);
4317 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004318 if (!removedComponents.isEmpty()) {
4319 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004320 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004321 // Notify the drag controller
4322 mDragController.onAppsRemoved(packageNames, removedComponents);
4323
Sunny Goyal1a745e82014-10-02 15:58:31 -07004324 } else {
4325 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004326 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004327
Winson Chungdf95eb12013-10-16 14:57:07 -07004328 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004329 if (mAppsView != null) {
4330 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004331 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004332 }
Joe Onoratobe386092009-11-17 17:32:16 -08004333
Michael Jurkac402cd92013-05-20 15:49:32 +02004334 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004335 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004336 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004337 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004338 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004339
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004340 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004341 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004342 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004343 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004344 return;
4345 }
4346
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004347 if (mWidgetsView != null && model != null) {
4348 mWidgetsView.addWidgets(model);
4349 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004350 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004351 }
4352
Winson Chung400438b2011-01-16 17:53:48 -08004353 private int mapConfigurationOriActivityInfoOri(int configOri) {
4354 final Display d = getWindowManager().getDefaultDisplay();
4355 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4356 switch (d.getRotation()) {
4357 case Surface.ROTATION_0:
4358 case Surface.ROTATION_180:
4359 // We are currently in the same basic orientation as the natural orientation
4360 naturalOri = configOri;
4361 break;
4362 case Surface.ROTATION_90:
4363 case Surface.ROTATION_270:
4364 // We are currently in the other basic orientation to the natural orientation
4365 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4366 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4367 break;
4368 }
4369
4370 int[] oriMap = {
4371 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4372 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4373 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4374 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4375 };
4376 // Since the map starts at portrait, we need to offset if this device's natural orientation
4377 // is landscape.
4378 int indexOffset = 0;
4379 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4380 indexOffset = 1;
4381 }
4382 return oriMap[(d.getRotation() + indexOffset) % 4];
4383 }
Adam Cohen446e9402011-09-15 18:21:21 -07004384
Winson Chung4b919f82012-05-01 10:44:08 -07004385 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004386 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004387 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4388 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4389 .getConfiguration().orientation));
4390 } else {
4391 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4392 }
Winson Chung4b919f82012-05-01 10:44:08 -07004393 }
4394 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004395
Winson Chung4b919f82012-05-01 10:44:08 -07004396 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004397 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004398 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004399 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004400 } else {
4401 mHandler.postDelayed(new Runnable() {
4402 public void run() {
4403 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4404 }
4405 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004406 }
Winson Chung4b919f82012-05-01 10:44:08 -07004407 }
Winson Chung400438b2011-01-16 17:53:48 -08004408 }
4409
Winson Chunge43a1e72014-01-15 10:33:02 -08004410 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004411 if (mLauncherCallbacks != null) {
4412 return mLauncherCallbacks.isLauncherPreinstalled();
4413 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004414 PackageManager pm = getPackageManager();
4415 try {
4416 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4417 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4418 return true;
4419 } else {
4420 return false;
4421 }
4422 } catch (NameNotFoundException e) {
4423 e.printStackTrace();
4424 return false;
4425 }
4426 }
4427
Adam Cohenea90f832014-05-21 15:03:34 -07004428 /**
4429 * This method indicates whether or not we should suggest default wallpaper dimensions
4430 * when our wallpaper cropper was not yet used to set a wallpaper.
4431 */
4432 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004433 if (mLauncherCallbacks != null) {
4434 return mLauncherCallbacks.overrideWallpaperDimensions();
4435 }
Adam Cohenea90f832014-05-21 15:03:34 -07004436 return true;
4437 }
4438
Adam Cohen432609a2014-03-13 17:03:22 -07004439 /**
4440 * To be overridden by subclasses to indicate that there is an activity to launch
4441 * before showing the standard launcher experience.
4442 */
4443 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004444 if (mLauncherCallbacks != null) {
4445 return mLauncherCallbacks.hasFirstRunActivity();
4446 }
Adam Cohen432609a2014-03-13 17:03:22 -07004447 return false;
4448 }
4449
4450 /**
4451 * To be overridden by subclasses to launch any first run activity
4452 */
4453 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004454 if (mLauncherCallbacks != null) {
4455 return mLauncherCallbacks.getFirstRunActivity();
4456 }
Adam Cohen432609a2014-03-13 17:03:22 -07004457 return null;
4458 }
4459
Winson Chung2826a402015-04-17 16:18:53 -07004460 /**
4461 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004462 */
4463 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004464 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4465 return false;
4466 }
4467
Winson Chung2826a402015-04-17 16:18:53 -07004468 if (mLauncherCallbacks != null) {
4469 return mLauncherCallbacks.overrideAllAppsSearch();
4470 }
4471 return false;
4472 }
4473
Winson Chunga6945242014-01-08 14:04:34 -08004474 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004475 return !ActivityManager.isRunningInTestHarness() &&
4476 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004477 }
4478
Adam Cohen4a9423d2014-03-28 14:22:31 -07004479 protected boolean hasRunFirstRunActivity() {
4480 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4481 }
4482
Adam Cohen432609a2014-03-13 17:03:22 -07004483 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004484 if (shouldRunFirstRunActivity() &&
4485 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004486 Intent firstRunIntent = getFirstRunActivity();
4487 if (firstRunIntent != null) {
4488 startActivity(firstRunIntent);
4489 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004490 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004491 }
4492 }
Adam Cohen432609a2014-03-13 17:03:22 -07004493 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004494 }
4495
4496 private void markFirstRunActivityShown() {
4497 SharedPreferences.Editor editor = mSharedPrefs.edit();
4498 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4499 editor.apply();
4500 }
4501
Adam Cohen432609a2014-03-13 17:03:22 -07004502 /**
4503 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4504 * screen that must be displayed and dismissed.
4505 */
4506 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004507 if (mLauncherCallbacks != null) {
4508 return mLauncherCallbacks.hasDismissableIntroScreen();
4509 }
Adam Cohen432609a2014-03-13 17:03:22 -07004510 return false;
4511 }
4512
4513 /**
4514 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4515 */
4516 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004517 if (mLauncherCallbacks != null) {
4518 return mLauncherCallbacks.getIntroScreen();
4519 }
Adam Cohen432609a2014-03-13 17:03:22 -07004520 return null;
4521 }
4522
4523 /**
4524 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4525 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4526 */
4527 private boolean shouldShowIntroScreen() {
4528 return hasDismissableIntroScreen() &&
4529 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4530 }
4531
4532 protected void showIntroScreen() {
4533 View introScreen = getIntroScreen();
4534 changeWallpaperVisiblity(false);
4535 if (introScreen != null) {
4536 mDragLayer.showOverlayView(introScreen);
4537 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004538 if (mLauncherOverlayContainer != null) {
4539 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4540 }
Adam Cohen432609a2014-03-13 17:03:22 -07004541 }
4542
4543 public void dismissIntroScreen() {
4544 markIntroScreenDismissed();
4545 if (showFirstRunActivity()) {
4546 // We delay hiding the intro view until the first run activity is showing. This
4547 // avoids a blip.
4548 mWorkspace.postDelayed(new Runnable() {
4549 @Override
4550 public void run() {
4551 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004552 if (mLauncherOverlayContainer != null) {
4553 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4554 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004555 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004556 }
4557 }, ACTIVITY_START_DELAY);
4558 } else {
4559 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004560 if (mLauncherOverlayContainer != null) {
4561 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4562 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004563 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004564 }
4565 changeWallpaperVisiblity(true);
4566 }
4567
4568 private void markIntroScreenDismissed() {
4569 SharedPreferences.Editor editor = mSharedPrefs.edit();
4570 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4571 editor.apply();
4572 }
4573
Adam Cohen091440a2015-03-18 14:16:05 -07004574 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004575 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4576 // on the device, then we always show the first run cling experience (or if there is no
4577 // launcher2). Otherwise, we prompt the user upon started for migration
4578 LauncherClings launcherClings = new LauncherClings(this);
4579 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4580 if (mModel.canMigrateFromOldLauncherDb(this)) {
4581 launcherClings.showMigrationCling();
4582 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004583 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004584 }
4585 }
4586 }
4587
Winson Chunga6945242014-01-08 14:04:34 -08004588 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004589 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4590 if (mHotseat != null) mHotseat.setAlpha(1f);
4591 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4592 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004593 }
4594
4595 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004596 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4597 if (mHotseat != null) mHotseat.setAlpha(0f);
4598 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4599 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004600 }
4601
Mathew Inwood72fbec12013-11-19 15:45:07 +00004602 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004603 // Called from search suggestion, not supported in other profiles.
4604 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4605 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4606 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4607 myUser);
4608 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004609 return null;
4610 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004611 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004612 }
4613
4614 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4615 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004616 // Called from search suggestion, not supported in other profiles.
4617 return createShortcutDragInfo(shortcutIntent, caption, icon,
4618 UserHandleCompat.myUserHandle());
4619 }
4620
4621 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4622 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004623 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004624 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004625 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004626 }
4627
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004628 protected void moveWorkspaceToDefaultScreen() {
4629 mWorkspace.moveToDefaultScreen(false);
4630 }
4631
Mathew Inwood72fbec12013-11-19 15:45:07 +00004632 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4633 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004634 mWorkspace.onExternalDragStartedWithItem(dragView);
4635 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004636 }
4637
Anjali Koppalf5d29132014-02-28 13:33:27 -08004638 @Override
4639 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004640 if (mLauncherCallbacks != null) {
4641 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4642 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004643 }
4644
Winson Chung80baf5a2010-08-09 16:03:15 -07004645 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004646 * Returns a FastBitmapDrawable with the icon, accurately sized.
4647 */
4648 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4649 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4650 d.setFilterBitmap(true);
4651 resizeIconDrawable(d);
4652 return d;
4653 }
4654
4655 /**
4656 * Resizes an icon drawable to the correct icon size.
4657 */
4658 public void resizeIconDrawable(Drawable icon) {
4659 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4660 }
4661
4662 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004663 * Prints out out state for debugging.
4664 */
4665 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004666 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004667 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004668 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4669 Log.d(TAG, "mRestoring=" + mRestoring);
4670 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4671 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004672 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004673 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004674 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004675
Daniel Sandler325dc232013-06-05 22:57:57 -04004676 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004677 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004678
4679 @Override
4680 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4681 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004682 synchronized (sDumpLogs) {
4683 writer.println(" ");
4684 writer.println("Debug logs: ");
4685 for (int i = 0; i < sDumpLogs.size(); i++) {
4686 writer.println(" " + sDumpLogs.get(i));
4687 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004688 }
Adam Cohen9211d422014-10-07 18:14:53 -07004689 if (mLauncherCallbacks != null) {
4690 mLauncherCallbacks.dump(prefix, fd, writer, args);
4691 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004692 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004693
4694 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004695 if (DEBUG_DUMP_LOG) {
4696 synchronized (sDumpLogs) {
4697 Log.d(TAG, "");
4698 Log.d(TAG, "*********************");
4699 Log.d(TAG, "Launcher debug logs: ");
4700 for (int i = 0; i < sDumpLogs.size(); i++) {
4701 Log.d(TAG, " " + sDumpLogs.get(i));
4702 }
4703 Log.d(TAG, "*********************");
4704 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004705 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004706 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004707 }
4708
4709 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004710 addDumpLog(tag, log, null, debugLog);
4711 }
4712
4713 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004714 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004715 if (e != null) {
4716 Log.d(tag, log, e);
4717 } else {
4718 Log.d(tag, log);
4719 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004720 }
Winson Chungede41292013-09-19 16:27:36 -07004721 if (DEBUG_DUMP_LOG) {
4722 sDateStamp.setTime(System.currentTimeMillis());
4723 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004724 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4725 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004726 }
Winson Chungede41292013-09-19 16:27:36 -07004727 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004728 }
4729
Adam Cohen59400422014-03-05 18:07:04 -08004730 public static CustomAppWidget getCustomAppWidget(String name) {
4731 return sCustomAppWidgets.get(name);
4732 }
4733
4734 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4735 return sCustomAppWidgets;
4736 }
4737
Winson Chungede41292013-09-19 16:27:36 -07004738 public void dumpLogsToLocalData() {
4739 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004740 new AsyncTask<Void, Void, Void>() {
4741 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004742 boolean success = false;
4743 sDateStamp.setTime(sRunStart);
4744 String FILENAME = sDateStamp.getMonth() + "-"
4745 + sDateStamp.getDay() + "_"
4746 + sDateStamp.getHours() + "-"
4747 + sDateStamp.getMinutes() + "_"
4748 + sDateStamp.getSeconds() + ".txt";
4749
4750 FileOutputStream fos = null;
4751 File outFile = null;
4752 try {
4753 outFile = new File(getFilesDir(), FILENAME);
4754 outFile.createNewFile();
4755 fos = new FileOutputStream(outFile);
4756 } catch (Exception e) {
4757 e.printStackTrace();
4758 }
4759 if (fos != null) {
4760 PrintWriter writer = new PrintWriter(fos);
4761
4762 writer.println(" ");
4763 writer.println("Debug logs: ");
4764 synchronized (sDumpLogs) {
4765 for (int i = 0; i < sDumpLogs.size(); i++) {
4766 writer.println(" " + sDumpLogs.get(i));
4767 }
4768 }
4769 writer.close();
4770 }
4771 try {
4772 if (fos != null) {
4773 fos.close();
4774 success = true;
4775 }
4776 } catch (IOException e) {
4777 e.printStackTrace();
4778 }
Michael Jurka43467462013-11-14 12:03:58 +01004779 return null;
Winson Chungede41292013-09-19 16:27:36 -07004780 }
Michael Jurka43467462013-11-14 12:03:58 +01004781 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004782 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004783 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004784}
Michael Jurka2763be32011-02-24 11:19:57 -08004785
Dan Sandlerd5024042014-01-09 15:01:33 -05004786interface DebugIntents {
4787 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4788 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004789}