blob: 796de3fa00547489047afa66591520e5b94c4ee4 [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;
401
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700402 FocusIndicatorView mFocusHandler;
403
Sunny Goyal316490e2015-06-02 09:38:28 -0700404 @Thunk boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400405 private boolean mScreenOrientationSettingReceiverRegistered = false;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700406
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400407 final private BroadcastReceiver mScreenOrientationSettingReceiver =
408 new BroadcastReceiver() {
409 @Thunk Runnable mUpdateOrientationRunnable = new Runnable() {
410 public void run() {
411 setOrientation();
412 }
413 };
414
415 @Override
416 public void onReceive(Context context, Intent intent) {
417 mRotationEnabled = intent.getBooleanExtra(
418 Utilities.SCREEN_ROTATION_SETTING_EXTRA, false);
419 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
420 setOrientation();
421 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700422 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400423 };
424
425 @Thunk void setOrientation() {
426 if (mRotationEnabled) {
427 unlockScreenOrientation(true);
428 } else {
429 setRequestedOrientation(
430 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700431 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400432 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700433
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 @Override
435 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700436 if (DEBUG_STRICT_MODE) {
437 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
438 .detectDiskReads()
439 .detectDiskWrites()
440 .detectNetwork() // or .detectAll() for all detectable problems
441 .penaltyLog()
442 .build());
443 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
444 .detectLeakedSqlLiteObjects()
445 .detectLeakedClosableObjects()
446 .penaltyLog()
447 .penaltyDeath()
448 .build());
449 }
450
Adam Cohen9211d422014-10-07 18:14:53 -0700451 if (mLauncherCallbacks != null) {
452 mLauncherCallbacks.preOnCreate();
453 }
454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400456
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400457 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400458 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700459 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700460
Adam Cohen2e6da152015-05-06 11:42:25 -0700461 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700462 mDeviceProfile = getResources().getConfiguration().orientation
463 == Configuration.ORIENTATION_LANDSCAPE ?
464 app.getInvariantDeviceProfile().landscapeProfile
465 : app.getInvariantDeviceProfile().portraitProfile;
466
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400467 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700468 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700469 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800470 mModel = app.setLauncher(this);
471 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700472
Joe Onorato41a12d22009-10-31 18:30:00 -0400473 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800475 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700476
Daniel Sandlerff02d492013-08-05 02:12:05 -0400477 mStats = new Stats(this);
478
Sunny Goyalffe83f12014-08-14 17:39:34 -0700479 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700480
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700481 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
482 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700483
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700484 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
485 // this also ensures that any synchronous binding below doesn't re-trigger another
486 // LauncherModel load.
487 mPaused = false;
488
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200490 android.os.Debug.startMethodTracing(
491 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 }
493
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700495
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700497 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800499 registerContentObservers();
500
Joe Onorato7c312c12009-08-13 21:36:53 -0700501 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700502
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503 mSavedState = savedInstanceState;
504 restoreState(mSavedState);
505
506 if (PROFILE_STARTUP) {
507 android.os.Debug.stopMethodTracing();
508 }
509
510 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700511 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700512 // If the user leaves launcher, then we should just load items asynchronously when
513 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700514 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700515 } else {
516 // We only load the page synchronously if the user rotates (or triggers a
517 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700518 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700519 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 }
521
522 // For handling default keys
523 mDefaultKeySsb = new SpannableStringBuilder();
524 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800525
526 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
527 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800528
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700529 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
530 // In case we are on a device with locked rotation, we should look at preferences to check
531 // if the user has specifically allowed rotation.
532 if (!mRotationEnabled) {
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400533 String updateOrientationBroadcastPermission = getResources().getString(
534 R.string.receive_update_orientation_broadcasts_permission);
535 registerReceiver(mScreenOrientationSettingReceiver,
536 new IntentFilter(Utilities.SCREEN_ROTATION_SETTING_INTENT),
537 updateOrientationBroadcastPermission, null);
538 mScreenOrientationSettingReceiverRegistered = true;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700539 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
540 }
541
542 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
543 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400544 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700545
Adam Cohen9211d422014-10-07 18:14:53 -0700546 if (mLauncherCallbacks != null) {
547 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700548 if (mLauncherCallbacks.hasLauncherOverlay()) {
549 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700550 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
551 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
552 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700553 mWorkspace.setLauncherOverlay(mLauncherOverlay);
554 }
Adam Cohen9211d422014-10-07 18:14:53 -0700555 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700556
557 if (shouldShowIntroScreen()) {
558 showIntroScreen();
559 } else {
560 showFirstRunActivity();
561 showFirstRunClings();
562 }
Adam Cohen9211d422014-10-07 18:14:53 -0700563 }
564
565 private LauncherCallbacks mLauncherCallbacks;
566
567 public void onPostCreate(Bundle savedInstanceState) {
568 super.onPostCreate(savedInstanceState);
569 if (mLauncherCallbacks != null) {
570 mLauncherCallbacks.onPostCreate(savedInstanceState);
571 }
572 }
573
574 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
575 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700576 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
577 @Override
578 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700579 if (LOGD) {
580 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
581 }
Winson Chung94804152015-05-08 13:06:44 -0700582 mAppsView.setFixedBounds(bounds);
583 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700584 }
585
586 @Override
587 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700588 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
589 // Dismiss All Apps if we aren't already paused/invisible
590 if (!mPaused) {
591 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
592 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
593 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700594 }
Winson Chung0f785722015-04-08 10:27:49 -0700595 }
Sunny Goyal5683f872015-05-29 14:54:40 -0700596
597 @Override
598 public void setSearchManager(AppSearchManager manager) {
599 mAppsView.setSearchManager(manager);
600 }
Winson Chung0f785722015-04-08 10:27:49 -0700601 });
Jun Mukai8af0cd82015-05-12 12:32:12 -0700602 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
603 private boolean mImportanceStored = false;
604 private int mWorkspaceImportanceForAccessibility =
605 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
606 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
607
608 @Override
609 public void onSearchOverlayOpened() {
610 if (mImportanceStored) {
611 return;
612 }
613 // The underlying workspace and hotseat are temporarily suppressed by the search
614 // overlay. So they sholudn't be accessible.
615 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
616 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
617 mWorkspace.setImportantForAccessibility(
618 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
619 mHotseat.setImportantForAccessibility(
620 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
621 mImportanceStored = true;
622 }
623
624 @Override
625 public void onSearchOverlayClosed() {
626 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
627 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
628 mImportanceStored = false;
629 }
630 });
Adam Cohen9211d422014-10-07 18:14:53 -0700631 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700632 }
633
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700634 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700635 public void onLauncherProviderChange() {
636 if (mLauncherCallbacks != null) {
637 mLauncherCallbacks.onLauncherProviderChange();
638 }
639 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700640
Adam Cohen9211d422014-10-07 18:14:53 -0700641 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700642 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700643 if (mLauncherCallbacks != null) {
644 return mLauncherCallbacks.hasCustomContentToLeft();
645 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700646 return false;
647 }
648
Dave Hawkeya8881582013-09-17 15:55:33 -0600649 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500650 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600651 * {@link #addToCustomContentPage}. This will only be invoked if
652 * {@link #hasCustomContentToLeft()} is {@code true}.
653 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500654 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700655 if (mLauncherCallbacks != null) {
656 mLauncherCallbacks.populateCustomContentContainer();
657 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600658 }
659
660 /**
661 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
662 * ensure the custom content page is added or removed if necessary.
663 */
664 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600665 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600666 // Not bound yet, wait for bindScreens to be called.
667 return;
668 }
669
670 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
671 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500672 mWorkspace.createCustomContentContainer();
673 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600674 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
675 mWorkspace.removeCustomContentPage();
676 }
677 }
678
Anton Hanssona2f665f2013-09-26 12:18:32 +0100679 public Stats getStats() {
680 return mStats;
681 }
682
Bjorn Bringertc459e522013-06-07 19:36:01 +0100683 public LayoutInflater getInflater() {
684 return mInflater;
685 }
686
Hyunyoung Song3f471442015-04-08 19:01:34 -0700687 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700688 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
689 // that is subsequently removed from the workspace in startBinding().
690 return !mModel.isLoadingWorkspace();
691 }
692
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800693 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000694 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100695 if (Build.VERSION.SDK_INT >= 17) {
696 return View.generateViewId();
697 } else {
698 // View.generateViewId() is not available. The following fallback logic is a copy
699 // of its implementation.
700 for (;;) {
701 final int result = sNextGeneratedId.get();
702 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
703 int newValue = result + 1;
704 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
705 if (sNextGeneratedId.compareAndSet(result, newValue)) {
706 return result;
707 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000708 }
709 }
710 }
711
712 public int getViewIdForItem(ItemInfo info) {
713 // This cast is safe given the > 2B range for int.
714 int itemId = (int) info.id;
715 if (mItemIdToViewId.containsKey(itemId)) {
716 return mItemIdToViewId.get(itemId);
717 }
718 int viewId = generateViewId();
719 mItemIdToViewId.put(itemId, viewId);
720 return viewId;
721 }
722
723 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700724 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
725 * a configuration step, this allows the proper animations to run after other transitions.
726 */
Adam Cohendb364c32014-05-20 14:23:40 -0700727 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700728 long screenId = args.screenId;
729 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
730 // When the screen id represents an actual screen (as opposed to a rank) we make sure
731 // that the drop page actually exists.
732 screenId = ensurePendingDropLayoutExists(args.screenId);
733 }
Adam Cohendb364c32014-05-20 14:23:40 -0700734
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800735 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800736 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700737 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700738 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700739 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700741 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700742 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700743 case REQUEST_RECONFIGURE_APPWIDGET:
744 completeRestoreAppWidget(args.appWidgetId);
745 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800746 }
Michael Jurka27614d22012-04-02 06:40:22 -0700747 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
748 // if you turned the screen off and then back while in All Apps, Launcher would not
749 // return to the workspace. Clearing mAddInfo.container here fixes this issue
750 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700751 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 }
753
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800754 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700756 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700757 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700758 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800759 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700760
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 Runnable exitSpringLoaded = new Runnable() {
762 @Override
763 public void run() {
764 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
765 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
766 }
767 };
768
Michael Jurka8b805b12012-04-18 14:23:14 -0700769 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700770 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
772 if (resultCode == RESULT_CANCELED) {
773 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700774 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700776 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800777 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700779 }
780 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200781 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
782 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700783 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200784 }
785 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700786 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200787
Adam Cohened66b2b2012-01-23 17:28:51 -0800788 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700789 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700790
Adam Cohendb364c32014-05-20 14:23:40 -0700791 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800792 // We have special handling for widgets
793 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800794 final int appWidgetId;
795 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
796 : -1;
797 if (widgetId < 0) {
798 appWidgetId = pendingAddWidgetId;
799 } else {
800 appWidgetId = widgetId;
801 }
802
Adam Cohenad4e15c2013-10-17 16:21:35 -0700803 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800804 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700805 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
806 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700807 result = RESULT_CANCELED;
808 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700809 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700810 @Override
811 public void run() {
812 exitSpringLoadedDragModeDelayed(false, 0, null);
813 }
814 };
Adam Cohendb364c32014-05-20 14:23:40 -0700815 if (workspaceLocked) {
816 // No need to remove the empty screen if we're mid-binding, as the
817 // the bind will not add the empty screen.
818 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
819 } else {
820 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
821 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
822 }
Winson Chung5aaab772012-04-27 15:24:02 -0700823 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700824 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700825 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
826 // When the screen id represents an actual screen (as opposed to a rank)
827 // we make sure that the drop page actually exists.
828 mPendingAddInfo.screenId =
829 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
830 }
Adam Cohendb364c32014-05-20 14:23:40 -0700831 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
832
833 dropLayout.setDropPending(true);
834 final Runnable onComplete = new Runnable() {
835 @Override
836 public void run() {
837 completeTwoStageWidgetDrop(resultCode, appWidgetId);
838 dropLayout.setDropPending(false);
839 }
840 };
841 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
842 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
843 } else {
844 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
845 mPendingAddInfo);
846 sPendingAddItem = args;
847 }
Winson Chung5aaab772012-04-27 15:24:02 -0700848 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800849 return;
850 }
851
Sunny Goyalff572272014-07-23 13:58:07 -0700852 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
853 if (resultCode == RESULT_OK) {
854 // Update the widget view.
855 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
856 pendingAddWidgetId, mPendingAddInfo);
857 if (workspaceLocked) {
858 sPendingAddItem = args;
859 } else {
860 completeAdd(args);
861 }
862 }
863 // Leave the widget in the pending state if the user canceled the configure.
864 return;
865 }
866
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 // The pattern used here is that a user PICKs a specific application,
868 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700869
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
871 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700872 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700873 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
874 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800875 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700876 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800877 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700878 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700879 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
880 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 }
Adam Cohen00074722013-10-11 17:46:09 -0700882 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700883 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700884 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800887
888 }
889
890 @Override
891 protected void onActivityResult(
892 final int requestCode, final int resultCode, final Intent data) {
893 handleActivityResult(requestCode, resultCode, data);
894 if (mLauncherCallbacks != null) {
895 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
896 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 }
898
Adam Cohendb364c32014-05-20 14:23:40 -0700899 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
900 appWidgetId, ItemInfo info) {
901 PendingAddArguments args = new PendingAddArguments();
902 args.requestCode = requestCode;
903 args.intent = data;
904 args.container = info.container;
905 args.screenId = info.screenId;
906 args.cellX = info.cellX;
907 args.cellY = info.cellY;
908 args.appWidgetId = appWidgetId;
909 return args;
910 }
911
912 /**
913 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
914 *
915 * @param screenId the screen id to check
916 * @return the new screen, or screenId if it exists
917 */
918 private long ensurePendingDropLayoutExists(long screenId) {
919 CellLayout dropLayout =
920 (CellLayout) mWorkspace.getScreenWithId(screenId);
921 if (dropLayout == null) {
922 // it's possible that the add screen was removed because it was
923 // empty and a re-bind occurred
924 mWorkspace.addExtraEmptyScreen();
925 return mWorkspace.commitExtraEmptyScreen();
926 } else {
927 return screenId;
928 }
929 }
930
Adam Cohen091440a2015-03-18 14:16:05 -0700931 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700932 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700933 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 Runnable onCompleteRunnable = null;
935 int animationType = 0;
936
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 if (resultCode == RESULT_OK) {
939 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
940 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700941 mPendingAddWidgetInfo);
942 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 onCompleteRunnable = new Runnable() {
944 @Override
945 public void run() {
946 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700947 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700948 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
949 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800950 }
951 };
952 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800953 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800954 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800955 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700956 if (mDragLayer.getAnimatedView() != null) {
957 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
958 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
959 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700960 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700961 // The animated view may be null in the case of a rotation during widget configuration
962 onCompleteRunnable.run();
963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
965
966 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700967 protected void onStop() {
968 super.onStop();
969 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700970
971 if (mLauncherCallbacks != null) {
972 mLauncherCallbacks.onStop();
973 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700974 }
975
976 @Override
977 protected void onStart() {
978 super.onStart();
979 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700980
981 if (mLauncherCallbacks != null) {
982 mLauncherCallbacks.onStart();
983 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700984 }
985
986 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200988 long startTime = 0;
989 if (DEBUG_RESUME_TIME) {
990 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400991 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200992 }
Adam Cohen9211d422014-10-07 18:14:53 -0700993
994 if (mLauncherCallbacks != null) {
995 mLauncherCallbacks.preOnResume();
996 }
997
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700999
Winson Chung4a2afa32012-07-19 14:53:05 -07001000 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001001 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001002 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001003 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001004 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001005 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001006 // view after launching an app, as they may be depending on the UI to be static to
1007 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001008 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001009 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001010 } else if (mOnResumeState == State.WIDGETS) {
1011 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001012 }
1013 mOnResumeState = State.NONE;
1014
Winson Chungcd99cd32015-04-29 11:03:24 -07001015 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001016 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1017 // Otherwise, notify the callbacks if we are in all apps mode
1018 if (mState == State.APPS) {
1019 if (mLauncherCallbacks != null) {
1020 mLauncherCallbacks.onAllAppsShown();
1021 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001022 }
1023 }
1024
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001025 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001026 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1027 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001028
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001029 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001030 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001031 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001032 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001034 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001036 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1038 // execute them here
1039 long startTimeCallbacks = 0;
1040 if (DEBUG_RESUME_TIME) {
1041 startTimeCallbacks = System.currentTimeMillis();
1042 }
1043
Michael Jurka1e2f4652013-07-08 18:03:46 -07001044 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1045 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001046 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001047 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001048 if (DEBUG_RESUME_TIME) {
1049 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1050 (System.currentTimeMillis() - startTimeCallbacks));
1051 }
Michael Jurka447bf842013-05-15 14:52:15 +02001052 }
Michael Jurka54554252013-08-01 12:52:23 +02001053 if (mOnResumeCallbacks.size() > 0) {
1054 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1055 mOnResumeCallbacks.get(i).run();
1056 }
1057 mOnResumeCallbacks.clear();
1058 }
Winson Chunge4e50662012-01-23 14:45:13 -08001059
1060 // Reset the pressed state of icons that were locked in the press state while activities
1061 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001062 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001063 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001064 mWaitingForResume.setStayPressed(false);
1065 }
Winson Chung82963d52013-10-09 11:20:57 -07001066
Adam Cohen06dff352012-06-01 17:17:08 -07001067 // It is possible that widgets can receive updates while launcher is not in the foreground.
1068 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1069 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1070 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001071 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001072 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001073
Michael Jurka447bf842013-05-15 14:52:15 +02001074 if (DEBUG_RESUME_TIME) {
1075 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1076 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001077
1078 if (mWorkspace.getCustomContentCallbacks() != null) {
1079 // If we are resuming and the custom content is the current page, we call onShow().
1080 // It is also poassible that onShow will instead be called slightly after first layout
1081 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1082 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001083 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001084 }
1085 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001086 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001087 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001088
Sunny Goyal756adbc2015-04-16 15:20:51 -07001089 if (!isWorkspaceLoading()) {
1090 // Process any items that were added while Launcher was away.
1091 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1092 }
Adam Cohen9211d422014-10-07 18:14:53 -07001093
1094 if (mLauncherCallbacks != null) {
1095 mLauncherCallbacks.onResume();
1096 }
Adam Cohen06dff352012-06-01 17:17:08 -07001097 }
1098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001100 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001101 // Ensure that items added to Launcher are queued until Launcher returns
1102 InstallShortcutReceiver.enableInstallQueue();
1103
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001104 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001105 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001106 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001107 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001108
1109 // We call onHide() aggressively. The custom content callbacks should be able to
1110 // debounce excess onHide calls.
1111 if (mWorkspace.getCustomContentCallbacks() != null) {
1112 mWorkspace.getCustomContentCallbacks().onHide();
1113 }
Romain Guycbb89e42009-06-08 15:52:54 -07001114
Adam Cohen9211d422014-10-07 18:14:53 -07001115 if (mLauncherCallbacks != null) {
1116 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001117 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001118 }
1119
1120 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001121 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1122 // by a onResume or by scrolling otherwise.
1123 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001124
1125 // Custom content is completely hidden
1126 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001127
1128 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1129 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001130
1131 // Indicates whether the user is allowed to scroll away from the custom content.
1132 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001133 }
1134
Adam Cohenc2d6e892014-10-16 09:49:24 -07001135 public interface LauncherOverlay {
1136
1137 /**
1138 * Touch interaction leading to overscroll has begun
1139 */
1140 public void onScrollInteractionBegin();
1141
1142 /**
1143 * Touch interaction related to overscroll has ended
1144 */
1145 public void onScrollInteractionEnd();
1146
1147 /**
1148 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1149 * screen (or in the case of RTL, the rightmost screen).
1150 */
1151 public void onScrollChange(int progress, boolean rtl);
1152
1153 /**
1154 * Screen has stopped scrolling
1155 */
1156 public void onScrollSettled();
1157
1158 /**
1159 * This method can be called by the Launcher in order to force the LauncherOverlay
1160 * to exit fully immersive mode.
1161 */
1162 public void forceExitFullImmersion();
1163 }
1164
Winson Chung0f785722015-04-08 10:27:49 -07001165 public interface LauncherAppsCallbacks {
1166 /**
1167 * Updates launcher to the available space that AllApps can take so as not to overlap with
1168 * any other views.
1169 */
1170 public void onAllAppsBoundsChanged(Rect bounds);
1171
1172 /**
1173 * Called to dismiss all apps if it is showing.
1174 */
1175 public void dismissAllApps();
Sunny Goyal5683f872015-05-29 14:54:40 -07001176
1177 /**
1178 * Sets the search manager to be used for app search.
1179 */
1180 public void setSearchManager(AppSearchManager manager);
Winson Chung0f785722015-04-08 10:27:49 -07001181 }
1182
Jun Mukai8af0cd82015-05-12 12:32:12 -07001183 public interface LauncherSearchCallbacks {
1184 /**
1185 * Called when the search overlay is shown.
1186 */
1187 public void onSearchOverlayOpened();
1188
1189 /**
1190 * Called when the search overlay is dismissed.
1191 */
1192 public void onSearchOverlayClosed();
1193 }
1194
Adam Cohenc2d6e892014-10-16 09:49:24 -07001195 public interface LauncherOverlayCallbacks {
1196 /**
1197 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1198 * however it doesn't modify any state within the launcher.
1199 */
1200 public boolean canEnterFullImmersion();
1201
1202 /**
1203 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1204 * eg. by occupying the full screen and handling all touch events.
1205 *
1206 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1207 * case, Launcher will modify any necessary state and assumes the overlay is
1208 * handling all interaction. If false, the LauncherOverlay should cancel any
1209 *
1210 */
1211 public boolean enterFullImmersion();
1212
1213 /**
1214 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1215 * full control over UI and state.
1216 */
1217 public void exitFullImmersion();
1218 }
1219
1220 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1221
1222 @Override
1223 public boolean canEnterFullImmersion() {
1224 return mState == State.WORKSPACE;
1225 }
1226
1227 @Override
1228 public boolean enterFullImmersion() {
1229 if (mState == State.WORKSPACE) {
1230 // When fully immersed, disregard any touches which fall through.
1231 mDragLayer.setBlockTouch(true);
1232 return true;
1233 }
1234 return false;
1235 }
1236
1237 @Override
1238 public void exitFullImmersion() {
1239 mDragLayer.setBlockTouch(false);
1240 }
1241 }
1242
Jorim Jaggid017f882014-01-14 17:08:48 -08001243 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001244 if (mLauncherCallbacks != null) {
1245 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001246 } else {
1247 // On devices with a locked orientation, we will at least have the allow rotation
1248 // setting.
1249 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001250 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001251 }
1252
Adam Cohen9211d422014-10-07 18:14:53 -07001253 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001254 CustomContentCallbacks callbacks, String description) {
1255 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001256 }
1257
Adam Cohenedb40762013-07-18 16:45:45 -07001258 // The custom content needs to offset its content to account for the QSB
1259 public int getTopOffsetForCustomContent() {
1260 return mWorkspace.getPaddingTop();
1261 }
1262
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001263 @Override
1264 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001265 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001266 if (mModel.isCurrentCallbacks(this)) {
1267 mModel.stopLoader();
1268 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001269 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1270
Romain Guy13c2e7b2010-03-10 19:45:00 -08001271 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001272 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001273
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001274 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001275 @Override
1276 public void onWindowFocusChanged(boolean hasFocus) {
1277 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001278 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001279
1280 if (mLauncherCallbacks != null) {
1281 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1282 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001283 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 private boolean acceptFilter() {
1286 final InputMethodManager inputManager = (InputMethodManager)
1287 getSystemService(Context.INPUT_METHOD_SERVICE);
1288 return !inputManager.isFullscreenMode();
1289 }
1290
1291 @Override
1292 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001293 final int uniChar = event.getUnicodeChar();
1294 final boolean handled = super.onKeyDown(keyCode, event);
1295 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1296 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1298 keyCode, event);
1299 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001300 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001301 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001302 // showSearchDialog()
1303 // If there are multiple keystrokes before the search dialog takes focus,
1304 // onSearchRequested() will be called for every keystroke,
1305 // but it is idempotent, so it's fine.
1306 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308 }
1309
Joe Onorato8a9625e2010-01-28 15:55:35 -08001310 // Eat the long press event so the keyboard doesn't come up.
1311 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1312 return true;
1313 }
1314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 return handled;
1316 }
1317
Karl Rosaen138a0412009-04-23 19:00:21 -07001318 private String getTypedText() {
1319 return mDefaultKeySsb.toString();
1320 }
1321
1322 private void clearTypedText() {
1323 mDefaultKeySsb.clear();
1324 mDefaultKeySsb.clearSpans();
1325 Selection.setSelection(mDefaultKeySsb, 0);
1326 }
1327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001329 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1330 * State
1331 */
1332 private static State intToState(int stateOrdinal) {
1333 State state = State.WORKSPACE;
1334 final State[] stateValues = State.values();
1335 for (int i = 0; i < stateValues.length; i++) {
1336 if (stateValues[i].ordinal() == stateOrdinal) {
1337 state = stateValues[i];
1338 break;
1339 }
1340 }
1341 return state;
1342 }
1343
1344 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 * Restores the previous state, if it exists.
1346 *
1347 * @param savedState The previous state.
1348 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001349 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 private void restoreState(Bundle savedState) {
1351 if (savedState == null) {
1352 return;
1353 }
1354
Michael Jurka883f55b2010-10-21 15:47:14 -07001355 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001356 if (state == State.APPS || state == State.WIDGETS) {
1357 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001358 }
1359
Adam Cohen21cd0022013-10-09 18:57:02 -07001360 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1361 PagedView.INVALID_RESTORE_PAGE);
1362 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001363 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 }
1365
Winson Chung3d503fb2011-07-13 17:25:49 -07001366 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001367 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1370 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001371 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001372 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1373 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001374 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1375 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001376 AppWidgetProviderInfo info = savedState.getParcelable(
1377 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001378 mPendingAddWidgetInfo = info == null ?
1379 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1380
Adam Cohen4637b5a2013-11-04 18:21:24 -08001381 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001382 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 mRestoring = true;
1384 }
1385
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001386 mItemIdToViewId = (HashMap<Integer, Integer>)
1387 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
1389
1390 /**
1391 * Finds all the views we need and configure them properly.
1392 */
1393 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001394 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001395
Craig Mautner360310b2012-10-26 15:13:08 -07001396 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001397 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001398 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1399 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001400 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001401 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001402
John Spurlock77e1f472013-09-11 10:09:51 -04001403 mLauncherView.setSystemUiVisibility(
1404 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001405 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406
Winson Chung4c98d922011-05-31 16:50:48 -07001407 // Setup the drag layer
1408 mDragLayer.setup(this, dragController);
1409
Winson Chung3d503fb2011-07-13 17:25:49 -07001410 // Setup the hotseat
1411 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1412 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001413 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001414 }
1415
Jorim Jaggid017f882014-01-14 17:08:48 -08001416 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001417 View widgetButton = findViewById(R.id.widget_button);
1418 widgetButton.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 onClickAddWidgetButton(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 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427
1428 View wallpaperButton = findViewById(R.id.wallpaper_button);
1429 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001430 @Override
1431 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001432 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001433 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001434 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001435 }
1436 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001437 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438
1439 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001440 if (hasSettings()) {
1441 settingsButton.setOnClickListener(new OnClickListener() {
1442 @Override
1443 public void onClick(View arg0) {
1444 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001445 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001446 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001447 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001448 });
1449 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1450 } else {
1451 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001452 }
1453
Adam Cohendbdff6b2013-09-18 19:09:15 -07001454 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001455
Winson Chung4c98d922011-05-31 16:50:48 -07001456 // Setup the workspace
1457 mWorkspace.setHapticFeedbackEnabled(false);
1458 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001459 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001460 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001461
Winson Chungf0ea4d32011-06-06 14:27:16 -07001462 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001463 mSearchDropTargetBar = (SearchDropTargetBar)
1464 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001465
Winson Chungb745afb2015-03-02 11:51:23 -08001466 // Setup Apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001467 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001468 if (isAllAppsSearchOverridden()) {
1469 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001470 }
Winson Chungb745afb2015-03-02 11:51:23 -08001471
Winson Chungf0ea4d32011-06-06 14:27:16 -07001472 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001473 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001474
Winson Chung3d503fb2011-07-13 17:25:49 -07001475 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001476 dragController.setDragScoller(mWorkspace);
1477 dragController.setScrollView(mDragLayer);
1478 dragController.setMoveTarget(mWorkspace);
1479 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001480 if (mSearchDropTargetBar != null) {
1481 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001482 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001483 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001484
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001485 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001486 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001487 mWeightWatcher = new WeightWatcher(this);
1488 mWeightWatcher.setAlpha(0.5f);
1489 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001490 new FrameLayout.LayoutParams(
1491 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001492 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001493 Gravity.BOTTOM)
1494 );
Adam Cohen39a06042013-07-19 14:30:12 -07001495
1496 boolean show = shouldShowWeightWatcher();
1497 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
1500
1501 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001502 * Sets the all apps button. This method is called from {@link Hotseat}.
1503 */
1504 public void setAllAppsButton(View allAppsButton) {
1505 mAllAppsButton = allAppsButton;
1506 }
1507
1508 public View getAllAppsButton() {
1509 return mAllAppsButton;
1510 }
1511
1512 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 * Creates a view representing a shortcut.
1514 *
1515 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001517 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001518 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520
1521 /**
1522 * Creates a view representing a shortcut inflated from the specified resource.
1523 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 * @param parent The group the shortcut belongs to.
1525 * @param info The data structure describing the shortcut.
1526 *
1527 * @return A View inflated from layoutResId.
1528 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001529 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001530 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001531 parent, false);
1532 favorite.applyFromShortcutInfo(info, mIconCache);
1533 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001535 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 return favorite;
1537 }
1538
1539 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 * Add a shortcut to the workspace.
1541 *
1542 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 int cellY) {
1546 int[] cellXY = mTmpAddItemCellCoordinates;
1547 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001549
Sunny Goyal5c97f512015-05-19 16:03:28 -07001550 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001551 if (info == null) {
1552 return;
1553 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001554 final View view = createShortcut(info);
1555
Sunny Goyal5c97f512015-05-19 16:03:28 -07001556 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001557 // First we check if we already know the exact location where we want to add this item.
1558 if (cellX >= 0 && cellY >= 0) {
1559 cellXY[0] = cellX;
1560 cellXY[1] = cellY;
1561 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001562
1563 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001564 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001565 true, null,null)) {
1566 return;
1567 }
1568 DragObject dragObject = new DragObject();
1569 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001570 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1571 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001572 return;
1573 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001574 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001575 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001576 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001577 foundCellSpan = (result != null);
1578 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001579 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001580 }
1581
1582 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001583 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001584 return;
1585 }
1586
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001587 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588
1589 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001590 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001591 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
1593 }
1594
Adam Cohen2e6da152015-05-06 11:42:25 -07001595 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1596 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001597 // We want to account for the extra amount of padding that we are adding to the widget
1598 // to ensure that it gets the full amount of space that it has requested
1599 int requiredWidth = minWidth + padding.left + padding.right;
1600 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001601 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001602 }
1603
Adam Cohen2e6da152015-05-06 11:42:25 -07001604 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1605 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001606 }
1607
Adam Cohen2e6da152015-05-06 11:42:25 -07001608 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1609 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001610 }
1611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001613 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001615 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 */
Adam Cohen091440a2015-03-18 14:16:05 -07001617 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001618 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1619
1620 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001621 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001622 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1623 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001624 }
Romain Guycbb89e42009-06-08 15:52:54 -07001625
Adam Cohen59400422014-03-05 18:07:04 -08001626 if (appWidgetInfo.isCustomWidget) {
1627 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001628 }
1629
Adam Cohen59400422014-03-05 18:07:04 -08001630 LauncherAppWidgetInfo launcherInfo;
1631 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1632 launcherInfo.spanX = info.spanX;
1633 launcherInfo.spanY = info.spanY;
1634 launcherInfo.minSpanX = info.minSpanX;
1635 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001636 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001637
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001639 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640
1641 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001642 if (hostView == null) {
1643 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001644 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1645 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001646 } else {
1647 // The AppWidgetHostView has already been inflated and instantiated
1648 launcherInfo.hostView = hostView;
1649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001651 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001652 launcherInfo.notifyWidgetSizeChanged(this);
1653
Adam Cohen59400422014-03-05 18:07:04 -08001654 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1655 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001656
1657 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001659 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 }
Romain Guycbb89e42009-06-08 15:52:54 -07001661
Adam Cohended9f8d2010-11-03 13:25:16 -07001662 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1663 @Override
1664 public void onReceive(Context context, Intent intent) {
1665 final String action = intent.getAction();
1666 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1667 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001668 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001669 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001670
Winson Chungc100e8e2011-08-09 16:02:43 -07001671 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001672 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001673 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001674 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001675 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001676 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001677 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1678 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001679 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001680 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1681 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001682 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001683 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1684 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1685 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001686 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001687 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1688 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 }
1690 }
1691 };
1692
1693 @Override
1694 public void onAttachedToWindow() {
1695 super.onAttachedToWindow();
1696
1697 // Listen for broadcasts related to user-presence
1698 final IntentFilter filter = new IntentFilter();
1699 filter.addAction(Intent.ACTION_SCREEN_OFF);
1700 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001701 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001702 if (ENABLE_DEBUG_INTENTS) {
1703 filter.addAction(DebugIntents.DELETE_DATABASE);
1704 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1705 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001707 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001708 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001709 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 mVisible = true;
1711 }
1712
Adam Cohen0b395352014-06-09 22:54:36 +00001713 /**
1714 * Sets up transparent navigation and status bars in LMP.
1715 * This method is a no-op for other platform versions.
1716 */
Sunny Goyald0091012015-01-20 15:55:34 -08001717 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001718 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001719 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001720 Window window = getWindow();
1721 window.getAttributes().systemUiVisibility |=
1722 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1723 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1724 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1725 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1726 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1727 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1728 window.setStatusBarColor(Color.TRANSPARENT);
1729 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001730 }
1731 }
1732
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 @Override
1734 public void onDetachedFromWindow() {
1735 super.onDetachedFromWindow();
1736 mVisible = false;
1737
Adam Cohend113e0c2010-11-11 10:48:05 -08001738 if (mAttached) {
1739 unregisterReceiver(mReceiver);
1740 mAttached = false;
1741 }
Winson Chungb745afb2015-03-02 11:51:23 -08001742 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 }
1744
1745 public void onWindowVisibilityChanged(int visibility) {
1746 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001747 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001748 // The following code used to be in onResume, but it turns out onResume is called when
1749 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1750 // is a more appropriate event to handle
1751 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001752 if (!mWorkspaceLoading) {
1753 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001754 // We want to let Launcher draw itself at least once before we force it to build
1755 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001756 // apps is nice and speedy.
1757 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001758 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001759 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001760 if (mStarted) return;
1761 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001762 // We delay the layer building a bit in order to give
1763 // other message processing a time to run. In particular
1764 // this avoids a delay in hiding the IME if it was
1765 // currently shown, because doing that may involve
1766 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001767 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001768 final ViewTreeObserver.OnDrawListener listener = this;
1769 mWorkspace.post(new Runnable() {
1770 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001771 if (mWorkspace != null &&
1772 mWorkspace.getViewTreeObserver() != null) {
1773 mWorkspace.getViewTreeObserver().
1774 removeOnDrawListener(listener);
1775 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001776 }
1777 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001778 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001779 }
1780 });
1781 }
1782 clearTypedText();
1783 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001784 }
1785
Adam Cohen091440a2015-03-18 14:16:05 -07001786 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 mHandler.removeMessages(ADVANCE_MSG);
1788 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1789 mHandler.sendMessageDelayed(msg, delay);
1790 mAutoAdvanceSentTime = System.currentTimeMillis();
1791 }
1792
Adam Cohen091440a2015-03-18 14:16:05 -07001793 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001794 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1795 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1796 mAutoAdvanceRunning = autoAdvanceRunning;
1797 if (autoAdvanceRunning) {
1798 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1799 sendAdvanceMessage(delay);
1800 } else {
1801 if (!mWidgetsToAdvance.isEmpty()) {
1802 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1803 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1804 }
1805 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001806 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001807 }
1808 }
1809 }
1810
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001811 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1812
Adam Cohended9f8d2010-11-03 13:25:16 -07001813 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001814 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001815 if (msg.what == ADVANCE_MSG) {
1816 int i = 0;
1817 for (View key: mWidgetsToAdvance.keySet()) {
1818 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1819 final int delay = mAdvanceStagger * i;
1820 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001821 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001822 public void run() {
1823 ((Advanceable) v).advance();
1824 }
1825 }, delay);
1826 }
1827 i++;
1828 }
1829 sendAdvanceMessage(mAdvanceInterval);
1830 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001831 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001832 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001833 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001834
1835 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001836 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001837 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1838 if (v instanceof Advanceable) {
1839 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001840 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001841 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001842 }
1843 }
1844
1845 void removeWidgetToAutoAdvance(View hostView) {
1846 if (mWidgetsToAdvance.containsKey(hostView)) {
1847 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001848 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001849 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001850 }
1851
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001853 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001854 launcherInfo.hostView = null;
1855 }
1856
Hyunyoung Song3f471442015-04-08 19:01:34 -07001857 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001858 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1859 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001860 }
1861
Winson Chunga6945242014-01-08 14:04:34 -08001862 public DragLayer getDragLayer() {
1863 return mDragLayer;
1864 }
1865
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001866 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001867 return mAppsView;
1868 }
1869
Hyunyoung Song3f471442015-04-08 19:01:34 -07001870 public WidgetsContainerView getWidgetsView() {
1871 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001872 }
1873
Winson Chunga6945242014-01-08 14:04:34 -08001874 public Workspace getWorkspace() {
1875 return mWorkspace;
1876 }
1877
1878 public Hotseat getHotseat() {
1879 return mHotseat;
1880 }
1881
Jorim Jaggid017f882014-01-14 17:08:48 -08001882 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001883 return mOverviewPanel;
1884 }
1885
1886 public SearchDropTargetBar getSearchBar() {
1887 return mSearchDropTargetBar;
1888 }
1889
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001890 public LauncherAppWidgetHost getAppWidgetHost() {
1891 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 }
Romain Guycbb89e42009-06-08 15:52:54 -07001893
Winson Chunga9abd0e2010-10-27 17:18:37 -07001894 public LauncherModel getModel() {
1895 return mModel;
1896 }
1897
Winson Chunga6945242014-01-08 14:04:34 -08001898 protected SharedPreferences getSharedPrefs() {
1899 return mSharedPrefs;
1900 }
1901
Adam Cohen2e6da152015-05-06 11:42:25 -07001902 public DeviceProfile getDeviceProfile() {
1903 return mDeviceProfile;
1904 }
1905
Bjorn Bringertc459e522013-06-07 19:36:01 +01001906 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001907 getWindow().closeAllPanels();
1908
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001909 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001910 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001911 }
1912
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 @Override
1914 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001915 long startTime = 0;
1916 if (DEBUG_RESUME_TIME) {
1917 startTime = System.currentTimeMillis();
1918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 super.onNewIntent(intent);
1920
1921 // Close the menu
1922 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001923 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001924 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001925
Adam Cohened307df2013-10-02 09:37:31 -07001926 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1927 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1928 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001929
Adam Cohen6fecd412013-10-02 17:41:50 -07001930 if (mWorkspace == null) {
1931 // Can be cases where mWorkspace is null, this prevents a NPE
1932 return;
1933 }
1934 Folder openFolder = mWorkspace.getOpenFolder();
1935 // In all these cases, only animate if we're already on home
1936 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001937
1938 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1939 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001940 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001941 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001942 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001943 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001944
Adam Cohen6fecd412013-10-02 17:41:50 -07001945 closeFolder();
1946 exitSpringLoadedDragMode();
1947
1948 // If we are already on home, then just animate back to the workspace,
1949 // otherwise, just wait until onResume to set the state back to Workspace
1950 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001951 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001952 } else {
1953 mOnResumeState = State.WORKSPACE;
1954 }
1955
1956 final View v = getWindow().peekDecorView();
1957 if (v != null && v.getWindowToken() != null) {
1958 InputMethodManager imm = (InputMethodManager)getSystemService(
1959 INPUT_METHOD_SERVICE);
1960 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1961 }
1962
Winson Chungb745afb2015-03-02 11:51:23 -08001963 // Reset the apps view
1964 if (!alreadyOnHome && mAppsView != null) {
1965 mAppsView.scrollToTop();
1966 }
1967
Hyunyoung Song3f471442015-04-08 19:01:34 -07001968 // Reset the widgets view
1969 if (!alreadyOnHome && mWidgetsView != null) {
1970 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001971 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001972
Adam Cohen9211d422014-10-07 18:14:53 -07001973 if (mLauncherCallbacks != null) {
1974 mLauncherCallbacks.onHomeIntent();
1975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
Adam Cohened307df2013-10-02 09:37:31 -07001977
Michael Jurka447bf842013-05-15 14:52:15 +02001978 if (DEBUG_RESUME_TIME) {
1979 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981
Adam Cohen9211d422014-10-07 18:14:53 -07001982 if (mLauncherCallbacks != null) {
1983 mLauncherCallbacks.onNewIntent(intent);
1984 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001985 }
1986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001988 public void onRestoreInstanceState(Bundle state) {
1989 super.onRestoreInstanceState(state);
1990 for (int page: mSynchronouslyBoundPages) {
1991 mWorkspace.restoreInstanceStateForChild(page);
1992 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 }
1994
1995 @Override
1996 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001997 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001998 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1999 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002000 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002001 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002
Michael Jurka883f55b2010-10-21 15:47:14 -07002003 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002004 // We close any open folder since it will not be re-opened, and we need to make sure
2005 // this state is reflected.
2006 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007
Adam Cohendcd297f2013-06-18 13:13:40 -07002008 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002009 mWaitingForResult) {
2010 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002011 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002012 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2013 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002014 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2015 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2016 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002017 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 }
2019
Hyunyoung Song3f471442015-04-08 19:01:34 -07002020 // Save the current widgets tray?
2021 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002022 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002023
2024 if (mLauncherCallbacks != null) {
2025 mLauncherCallbacks.onSaveInstanceState(outState);
2026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
2029 @Override
2030 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002032
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04002033 if (mScreenOrientationSettingReceiverRegistered) {
2034 unregisterReceiver(mScreenOrientationSettingReceiver);
2035 mScreenOrientationSettingReceiverRegistered = false;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002036 }
2037
Winson Chunge7a03942011-08-05 15:05:12 -07002038 // Remove all pending runnables
2039 mHandler.removeMessages(ADVANCE_MSG);
2040 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002041 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002042
Winson Chungcd2b0142011-06-08 16:02:26 -07002043 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002044 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002045
2046 // It's possible to receive onDestroy after a new Launcher activity has
2047 // been created. In this case, don't interfere with the new Launcher.
2048 if (mModel.isCurrentCallbacks(this)) {
2049 mModel.stopLoader();
2050 app.setLauncher(null);
2051 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002054 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002056 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002058 mAppWidgetHost = null;
2059
2060 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061
2062 TextKeyListener.getInstance().release();
2063
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002064 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002065 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002066
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002067 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002068 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002069 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002070 mWorkspace = null;
2071 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002072
Michael Jurka2ecf9952012-06-18 12:52:28 -07002073 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002074
2075 if (mLauncherCallbacks != null) {
2076 mLauncherCallbacks.onDestroy();
2077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 }
2079
Adam Cohena9cf38f2011-05-02 15:36:58 -07002080 public DragController getDragController() {
2081 return mDragController;
2082 }
2083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 @Override
2085 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002086 onStartForResult(requestCode);
2087 super.startActivityForResult(intent, requestCode);
2088 }
2089
2090 @Override
2091 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2092 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2093 onStartForResult(requestCode);
2094 try {
2095 super.startIntentSenderForResult(intent, requestCode,
2096 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2097 } catch (IntentSender.SendIntentException e) {
2098 throw new ActivityNotFoundException();
2099 }
2100 }
2101
2102 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002103 if (requestCode >= 0) {
2104 setWaitingForResult(true);
2105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 }
2107
Winson Chung70d72102011-08-12 11:24:35 -07002108 /**
2109 * Indicates that we want global search for this activity by setting the globalSearch
2110 * argument for {@link #startSearch} to true.
2111 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002113 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002115
Karl Rosaen138a0412009-04-23 19:00:21 -07002116 if (initialQuery == null) {
2117 // Use any text typed in the launcher as the initial query
2118 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002119 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 if (appSearchData == null) {
2121 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002122 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 }
Winson Chungadf0c182012-08-23 14:59:07 -07002124 Rect sourceBounds = new Rect();
2125 if (mSearchDropTargetBar != null) {
2126 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2127 }
Romain Guycbb89e42009-06-08 15:52:54 -07002128
Ian Parkinson047036e2014-09-01 15:40:53 +01002129 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002130 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002131 if (clearTextImmediately) {
2132 clearTypedText();
2133 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002134
2135 // We need to show the workspace after starting the search
2136 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002137 }
2138
Ian Parkinson047036e2014-09-01 15:40:53 +01002139 /**
2140 * Start a text search.
2141 *
2142 * @return {@code true} if the search will start immediately, so any further keypresses
2143 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2144 * to buffer keypresses.
2145 */
2146 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002147 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002148 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2149 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2150 sourceBounds);
2151 }
2152
Michael Jurkaa33411c2012-06-14 16:18:21 -07002153 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002154 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002155 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002156 }
2157
2158 /**
2159 * Starts the global search activity. This code is a copied from SearchManager
2160 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002161 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002162 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002163 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002164 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2165 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2166 if (globalSearchActivity == null) {
2167 Log.w(TAG, "No global search activity found.");
2168 return;
2169 }
2170 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2171 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2172 intent.setComponent(globalSearchActivity);
2173 // Make sure that we have a Bundle to put source in
2174 if (appSearchData == null) {
2175 appSearchData = new Bundle();
2176 } else {
2177 appSearchData = new Bundle(appSearchData);
2178 }
Adam Cohen9211d422014-10-07 18:14:53 -07002179 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002180 if (!appSearchData.containsKey("source")) {
2181 appSearchData.putString("source", getPackageName());
2182 }
2183 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2184 if (!TextUtils.isEmpty(initialQuery)) {
2185 intent.putExtra(SearchManager.QUERY, initialQuery);
2186 }
2187 if (selectInitialQuery) {
2188 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2189 }
2190 intent.setSourceBounds(sourceBounds);
2191 try {
2192 startActivity(intent);
2193 } catch (ActivityNotFoundException ex) {
2194 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2195 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 }
2197
Yura4f93ec62014-02-04 14:15:21 +00002198 public boolean isOnCustomContent() {
2199 return mWorkspace.isOnOrMovingToCustomContent();
2200 }
2201
Winson Chung70d72102011-08-12 11:24:35 -07002202 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002203 public boolean onPrepareOptionsMenu(Menu menu) {
2204 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002205 if (!isOnCustomContent()) {
2206 // Close any open folders
2207 closeFolder();
2208 // Stop resizing any widgets
2209 mWorkspace.exitWidgetResizeMode();
2210 if (!mWorkspace.isInOverviewMode()) {
2211 // Show the overview mode
2212 showOverviewMode(true);
2213 } else {
2214 showWorkspace(true);
2215 }
Winson Chunge0298742014-01-17 12:03:00 -08002216 }
Adam Cohen9211d422014-10-07 18:14:53 -07002217 if (mLauncherCallbacks != null) {
2218 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2219 }
2220
Michael Jurkab94f3f82013-09-11 18:08:54 +02002221 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002224 @Override
2225 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002226 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002227 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002228 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002229 }
2230
Joe Onorato9c1289c2009-08-17 11:03:03 -04002231 public boolean isWorkspaceLocked() {
2232 return mWorkspaceLoading || mWaitingForResult;
2233 }
2234
Adam Cohen517a7f52014-03-01 12:12:59 -08002235 public boolean isWorkspaceLoading() {
2236 return mWorkspaceLoading;
2237 }
2238
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002239 private void setWorkspaceLoading(boolean value) {
2240 boolean isLocked = isWorkspaceLocked();
2241 mWorkspaceLoading = value;
2242 if (isLocked != isWorkspaceLocked()) {
2243 onWorkspaceLockedChanged();
2244 }
2245 }
2246
2247 private void setWaitingForResult(boolean value) {
2248 boolean isLocked = isWorkspaceLocked();
2249 mWaitingForResult = value;
2250 if (isLocked != isWorkspaceLocked()) {
2251 onWorkspaceLockedChanged();
2252 }
2253 }
2254
Adam Cohen9211d422014-10-07 18:14:53 -07002255 protected void onWorkspaceLockedChanged() {
2256 if (mLauncherCallbacks != null) {
2257 mLauncherCallbacks.onWorkspaceLockedChanged();
2258 }
2259 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002260
Michael Jurka0280c3b2010-09-17 15:00:07 -07002261 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002264 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2265 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002266 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002267 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002269
Sunny Goyale6b63a32015-01-16 16:14:29 -08002270 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002271 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002272 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2273 }
2274
Sunny Goyale6b63a32015-01-16 16:14:29 -08002275 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002276 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2277 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002278 if (appWidgetInfo.configure != null) {
2279 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002280 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002281
Bjorn Bringert7984c942009-12-09 15:38:25 +00002282 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002283 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2284 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002287 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002288 Runnable onComplete = new Runnable() {
2289 @Override
2290 public void run() {
2291 // Exit spring loaded mode if necessary after adding the widget
2292 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2293 null);
2294 }
2295 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002296 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002297 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002298 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 }
2300 }
Romain Guycbb89e42009-06-08 15:52:54 -07002301
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002302 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002303 // Close any folders that may be open.
2304 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002305 mWorkspace.moveToCustomContentScreen(animate);
2306 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002307
2308 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2309 int[] cell, int spanX, int spanY) {
2310 switch (info.itemType) {
2311 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2312 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2313 int span[] = new int[2];
2314 span[0] = spanX;
2315 span[1] = spanY;
2316 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2317 container, screenId, cell, span);
2318 break;
2319 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2320 processShortcutFromDrop(info.componentName, container, screenId, cell);
2321 break;
2322 default:
2323 throw new IllegalStateException("Unknown item type: " + info.itemType);
2324 }
2325 }
2326
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 /**
2328 * Process a shortcut drop.
2329 *
2330 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002334 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2335 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002336 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002338 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002339 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002340
2341 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 mPendingAddInfo.cellX = cell[0];
2343 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002344 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002345
2346 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2347 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002348 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002349 }
2350
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 /**
2352 * Process a widget drop.
2353 *
2354 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002355 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002356 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002357 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002358 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2359 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002361 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002362 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002363 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002364 mPendingAddInfo.minSpanX = info.minSpanX;
2365 mPendingAddInfo.minSpanY = info.minSpanY;
2366
Adam Cohenfbba09b2011-07-18 21:43:05 -07002367 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002368 mPendingAddInfo.cellX = cell[0];
2369 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002370 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002371 if (span != null) {
2372 mPendingAddInfo.spanX = span[0];
2373 mPendingAddInfo.spanY = span[1];
2374 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002375
Adam Cohened66b2b2012-01-23 17:28:51 -08002376 AppWidgetHostView hostView = info.boundWidget;
2377 int appWidgetId;
2378 if (hostView != null) {
2379 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002380 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002381
2382 // Clear the boundWidget so that it doesn't get destroyed.
2383 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002384 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002385 // In this case, we either need to start an activity to get permission to bind
2386 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002387 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002388 Bundle options = info.bindOptions;
2389
Sunny Goyalffe83f12014-08-14 17:39:34 -07002390 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2391 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002392 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002393 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002394 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002395 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002396 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2397 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2398 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002399 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2400 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002401 // TODO: we need to make sure that this accounts for the options bundle.
2402 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002403 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2404 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002405 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002406 }
2407
Adam Cohendcd297f2013-06-18 13:13:40 -07002408 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002409 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002410 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 folderInfo.title = getText(R.string.folder_name);
2412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002414 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2415 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002416 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417
2418 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002419 FolderIcon newFolder =
2420 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002421 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002422 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002423 // Force measure the new folder icon
2424 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2425 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002426 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 }
Romain Guycbb89e42009-06-08 15:52:54 -07002428
Joe Onorato9c1289c2009-08-17 11:03:03 -04002429 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002430 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002431 }
2432
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002433 /**
2434 * Registers various content observers. The current implementation registers
2435 * only a favorites observer to keep track of the favorites applications.
2436 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002437 private void registerContentObservers() {
2438 ContentResolver resolver = getContentResolver();
2439 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2440 true, mWidgetObserver);
2441 }
2442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 @Override
2444 public boolean dispatchKeyEvent(KeyEvent event) {
2445 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2446 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002448 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002449 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002450 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002451 dumpState();
2452 return true;
2453 }
2454 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002455 }
2456 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2457 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002458 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 return true;
2460 }
2461 }
2462
2463 return super.dispatchKeyEvent(event);
2464 }
2465
Joe Onorato88ec0992009-11-19 13:16:06 -08002466 @Override
2467 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002468 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2469 return;
2470 }
2471
Sunny Goyal45478022015-06-08 16:52:41 -07002472 if (mDragController.isDragging()) {
2473 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002474 return;
2475 }
2476
Winson Chungb745afb2015-03-02 11:51:23 -08002477 if (isAppsViewVisible()) {
2478 showWorkspace(true);
2479 } else if (isWidgetsViewVisible()) {
2480 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002481 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002482 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002483 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002484 Folder openFolder = mWorkspace.getOpenFolder();
2485 if (openFolder.isEditingName()) {
2486 openFolder.dismissEditingName();
2487 } else {
2488 closeFolder();
2489 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002490 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002491 mWorkspace.exitWidgetResizeMode();
2492
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002493 // Back button is a no-op here, but give at least some feedback for the button press
2494 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002495 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002496 }
2497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002499 * Re-listen when widgets are reset.
2500 */
Adam Cohen091440a2015-03-18 14:16:05 -07002501 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002502 if (mAppWidgetHost != null) {
2503 mAppWidgetHost.startListening();
2504 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002505 }
2506
2507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 * Launches the intent referred by the clicked shortcut.
2509 *
2510 * @param v The view representing the clicked shortcut.
2511 */
2512 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002513 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2514 // view has detached (it's possible for this to happen if the view is removed mid touch).
2515 if (v.getWindowToken() == null) {
2516 return;
2517 }
2518
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002519 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002520 return;
2521 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002522
Adam Cohen1697b792013-09-17 19:08:21 -07002523 if (v instanceof Workspace) {
2524 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002525 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002526 }
2527 return;
2528 }
2529
2530 if (v instanceof CellLayout) {
2531 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002532 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 }
2534 }
2535
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002537 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002540 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002541 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002542 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002543 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002544 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002545 } else if (tag instanceof AppInfo) {
2546 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002547 } else if (tag instanceof LauncherAppWidgetInfo) {
2548 if (v instanceof PendingAppWidgetHostView) {
2549 onClickPendingWidget((PendingAppWidgetHostView) v);
2550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 }
2552 }
2553
Sunny Goyal70660032015-05-14 00:07:08 -07002554 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002555 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002556 return false;
2557 }
2558
Michael Jurkaaf442092010-06-10 17:01:57 -07002559 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002560 * Event handler for the app widget view which has not fully restored.
2561 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002562 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002563 if (mIsSafeModeEnabled) {
2564 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2565 return;
2566 }
2567
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002568 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002569 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002570 int widgetId = info.appWidgetId;
2571 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2572 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002573 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2574 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002575 mPendingAddInfo.copyFrom(info);
2576 mPendingAddWidgetId = widgetId;
2577
Sunny Goyalffe83f12014-08-14 17:39:34 -07002578 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2579 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002580 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002581 } else if (info.installProgress < 0) {
2582 // The install has not been queued
2583 final String packageName = info.providerName.getPackageName();
2584 showBrokenAppInstallDialog(packageName,
2585 new DialogInterface.OnClickListener() {
2586 public void onClick(DialogInterface dialog, int id) {
2587 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2588 }
2589 });
2590 } else {
2591 // Download has started.
2592 final String packageName = info.providerName.getPackageName();
2593 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002594 }
2595 }
2596
2597 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002598 * Event handler for the "grid" button that appears on the home screen, which
2599 * enters all apps mode.
2600 *
2601 * @param v The view that was clicked.
2602 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 protected void onClickAllAppsButton(View v) {
2604 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002605 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002606 showWorkspace(true);
2607 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002608 // Try and refresh the set of predicted apps before we enter launcher
2609 showAppsView(true /* animated */, false /* resetListToTop */,
2610 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002611 }
2612 }
2613
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002614 private void showBrokenAppInstallDialog(final String packageName,
2615 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002616 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002617 .setTitle(R.string.abandoned_promises_title)
2618 .setMessage(R.string.abandoned_promise_explanation)
2619 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2620 .setNeutralButton(R.string.abandoned_clean_this,
2621 new DialogInterface.OnClickListener() {
2622 public void onClick(DialogInterface dialog, int id) {
2623 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2624 mWorkspace.removeAbandonedPromise(packageName, user);
2625 }
2626 })
2627 .create().show();
2628 return;
2629 }
2630
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002631 /**
2632 * Event handler for an app shortcut click.
2633 *
2634 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2635 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002636 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002637 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2638 Object tag = v.getTag();
2639 if (!(tag instanceof ShortcutInfo)) {
2640 throw new IllegalArgumentException("Input must be a Shortcut");
2641 }
2642
2643 // Open shortcut
2644 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002645
2646 if (shortcut.isDisabled != 0) {
2647 int error = R.string.activity_not_available;
2648 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2649 error = R.string.safemode_shortcut_error;
2650 }
2651 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2652 return;
2653 }
2654
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002655 final Intent intent = shortcut.intent;
2656
2657 // Check for special shortcuts
2658 if (intent.getComponent() != null) {
2659 final String shortcutClass = intent.getComponent().getClassName();
2660
2661 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2662 MemoryDumpActivity.startDump(this);
2663 return;
2664 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2665 toggleShowWeightWatcher();
2666 return;
2667 }
2668 }
2669
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002671 if ((v instanceof BubbleTextView)
2672 && shortcut.isPromise()
2673 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002674 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002675 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 new DialogInterface.OnClickListener() {
2677 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002678 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002679 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002680 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 return;
2682 }
2683
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002684 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002685 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002686
2687 if (mLauncherCallbacks != null) {
2688 mLauncherCallbacks.onClickAppShortcut(v);
2689 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002690 }
2691
Adam Cohen091440a2015-03-18 14:16:05 -07002692 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002693 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002694 final ShortcutInfo shortcut;
2695 final Intent intent;
2696 if (tag instanceof ShortcutInfo) {
2697 shortcut = (ShortcutInfo) tag;
2698 intent = shortcut.intent;
2699 int[] pos = new int[2];
2700 v.getLocationOnScreen(pos);
2701 intent.setSourceBounds(new Rect(pos[0], pos[1],
2702 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002703
Sunny Goyal508da152014-08-14 10:53:27 -07002704 } else if (tag instanceof AppInfo) {
2705 shortcut = null;
2706 intent = ((AppInfo) tag).intent;
2707 } else {
2708 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2709 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002710
2711 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002712 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713
2714 if (success && v instanceof BubbleTextView) {
2715 mWaitingForResume = (BubbleTextView) v;
2716 mWaitingForResume.setStayPressed(true);
2717 }
2718 }
2719
2720 /**
2721 * Event handler for a folder icon click.
2722 *
2723 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2724 */
2725 protected void onClickFolderIcon(View v) {
2726 if (LOGD) Log.d(TAG, "onClickFolder");
2727 if (!(v instanceof FolderIcon)){
2728 throw new IllegalArgumentException("Input must be a FolderIcon");
2729 }
2730
2731 FolderIcon folderIcon = (FolderIcon) v;
2732 final FolderInfo info = folderIcon.getFolderInfo();
2733 Folder openFolder = mWorkspace.getFolderForTag(info);
2734
2735 // If the folder info reports that the associated folder is open, then verify that
2736 // it is actually opened. There have been a few instances where this gets out of sync.
2737 if (info.opened && openFolder == null) {
2738 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2739 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2740 info.opened = false;
2741 }
2742
2743 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2744 // Close any open folder
2745 closeFolder();
2746 // Open the requested folder
2747 openFolder(folderIcon);
2748 } else {
2749 // Find the open folder...
2750 int folderScreen;
2751 if (openFolder != null) {
2752 folderScreen = mWorkspace.getPageForView(openFolder);
2753 // .. and close it
2754 closeFolder(openFolder);
2755 if (folderScreen != mWorkspace.getCurrentPage()) {
2756 // Close any folder open on the current screen
2757 closeFolder();
2758 // Pull the folder onto this screen
2759 openFolder(folderIcon);
2760 }
2761 }
2762 }
Adam Cohen9211d422014-10-07 18:14:53 -07002763
2764 if (mLauncherCallbacks != null) {
2765 mLauncherCallbacks.onClickFolderIcon(v);
2766 }
Michael Jurka5130e402011-10-13 04:55:35 -07002767 }
2768
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002769 /**
2770 * Event handler for the (Add) Widgets button that appears after a long press
2771 * on the home screen.
2772 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002773 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002774 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002775 if (mIsSafeModeEnabled) {
2776 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2777 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002778 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002779 if (mLauncherCallbacks != null) {
2780 mLauncherCallbacks.onClickAddWidgetButton(view);
2781 }
Adam Cohen9211d422014-10-07 18:14:53 -07002782 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002783 }
2784
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002785 /**
2786 * Event handler for the wallpaper picker button that appears after a long press
2787 * on the home screen.
2788 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002789 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002790 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002791 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2792 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002793
2794 if (mLauncherCallbacks != null) {
2795 mLauncherCallbacks.onClickWallpaperPicker(v);
2796 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002797 }
2798
2799 /**
2800 * Event handler for a click on the settings button that appears after a long press
2801 * on the home screen.
2802 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002803 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002804 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002805 if (mLauncherCallbacks != null) {
2806 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002807 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002808 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002809 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002810 }
2811
Adam Cohen61f560d2013-09-30 15:58:20 -07002812 public View.OnTouchListener getHapticFeedbackTouchListener() {
2813 if (mHapticFeedbackTouchListener == null) {
2814 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002815 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002816 @Override
2817 public boolean onTouch(View v, MotionEvent event) {
2818 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2819 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2820 }
2821 return false;
2822 }
2823 };
2824 }
2825 return mHapticFeedbackTouchListener;
2826 }
2827
Adam Cohen9211d422014-10-07 18:14:53 -07002828 public void onDragStarted(View view) {
2829 if (isOnCustomContent()) {
2830 // Custom content screen doesn't participate in drag and drop. If on custom
2831 // content screen, move to default.
2832 moveWorkspaceToDefaultScreen();
2833 }
2834
2835 if (mLauncherCallbacks != null) {
2836 mLauncherCallbacks.onDragStarted(view);
2837 }
2838 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002839
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002840 /**
2841 * Called when the user stops interacting with the launcher.
2842 * This implies that the user is now on the homescreen and is not doing housekeeping.
2843 */
Adam Cohen9211d422014-10-07 18:14:53 -07002844 protected void onInteractionEnd() {
2845 if (mLauncherCallbacks != null) {
2846 mLauncherCallbacks.onInteractionEnd();
2847 }
2848 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002849
2850 /**
2851 * Called when the user starts interacting with the launcher.
2852 * The possible interactions are:
2853 * - open all apps
2854 * - reorder an app shortcut, or a widget
2855 * - open the overview mode.
2856 * This is a good time to stop doing things that only make sense
2857 * when the user is on the homescreen and not doing housekeeping.
2858 */
Adam Cohen9211d422014-10-07 18:14:53 -07002859 protected void onInteractionBegin() {
2860 if (mLauncherCallbacks != null) {
2861 mLauncherCallbacks.onInteractionBegin();
2862 }
2863 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002864
Winson Chungcd99cd32015-04-29 11:03:24 -07002865 /** Updates the interaction state. */
2866 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002867 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002868 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002869 boolean fromStateWithOverlay;
2870 boolean toStateWithOverlay;
2871 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2872 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2873 toStateWithOverlay = toState != Workspace.State.NORMAL;
2874 } else {
2875 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2876 fromState != Workspace.State.NORMAL_HIDDEN;
2877 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2878 toState != Workspace.State.NORMAL_HIDDEN;
2879 }
2880 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002881 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002882 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002883 onInteractionEnd();
2884 }
2885 }
2886
Kenny Guyf07af7b2014-07-31 11:39:16 +01002887 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002888 try {
2889 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002890 launcherApps.showAppDetailsForProfile(componentName, user);
2891 } catch (SecurityException e) {
2892 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2893 Log.e(TAG, "Launcher does not have permission to launch settings");
2894 } catch (ActivityNotFoundException e) {
2895 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2896 Log.e(TAG, "Unable to launch settings");
2897 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002898 }
2899
Michael Jurka1e2f4652013-07-08 18:03:46 -07002900 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002901 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2902 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002903 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002904 // System applications cannot be installed. For now, show a toast explaining that.
2905 // We may give them the option of disabling apps this way.
2906 int messageId = R.string.uninstall_system_app_text;
2907 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002908 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002909 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002910 String packageName = componentName.getPackageName();
2911 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002912 Intent intent = new Intent(
2913 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002914 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2915 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002916 if (user != null) {
2917 user.addToIntent(intent, Intent.EXTRA_USER);
2918 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002919 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002920 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002921 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002922 }
2923
Winson Chung8f1eff72015-05-28 17:33:40 -07002924 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002925 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002927 // Only launch using the new animation if the shortcut has not opted out (this is a
2928 // private contract between launcher and may be ignored in the future).
2929 boolean useLaunchAnimation = (v != null) &&
2930 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002931 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2932 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002933
Kenny Guy1317e2d2014-05-08 18:52:50 +01002934 UserHandleCompat user = null;
2935 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2936 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2937 user = userManager.getUserForSerialNumber(serialNumber);
2938 }
2939
2940 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002941 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002942 ActivityOptions opts = null;
2943 if (sClipRevealMethod != null) {
2944 // TODO: call method directly when Launcher3 can depend on M APIs
2945 int left = 0, top = 0;
2946 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2947 if (v instanceof TextView) {
2948 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002949 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2950 if (icon != null) {
2951 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002952 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002953 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002954 width = bounds.width();
2955 height = bounds.height();
2956 }
2957 }
2958 try {
2959 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2960 left, top, width, height);
2961 } catch (IllegalAccessException e) {
2962 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2963 sClipRevealMethod = null;
2964 } catch (InvocationTargetException e) {
2965 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2966 sClipRevealMethod = null;
2967 }
2968 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002969 if (opts == null && !Utilities.isLmpOrAbove()) {
2970 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002971 v.getMeasuredWidth(), v.getMeasuredHeight());
2972 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002973 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002974 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002975
2976 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2977 // Could be launching some bookkeeping activity
2978 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002979 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002980 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002981 launcherApps.startActivityForProfile(intent.getComponent(), user,
2982 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002983 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002984 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 } catch (SecurityException e) {
2986 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002987 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002988 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002989 "or use the exported attribute for this activity. "
2990 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002991 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002992 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002993 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002994
Winson Chung8f1eff72015-05-28 17:33:40 -07002995 private boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002996 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002997 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2998 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2999 return false;
3000 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003001 try {
3002 success = startActivity(v, intent, tag);
3003 } catch (ActivityNotFoundException e) {
3004 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3005 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3006 }
3007 return success;
3008 }
3009
Adam Cohen268c4752012-06-06 17:47:33 -07003010 /**
3011 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3012 * in the DragLayer in the exact absolute location of the original FolderIcon.
3013 */
3014 private void copyFolderIconToImage(FolderIcon fi) {
3015 final int width = fi.getMeasuredWidth();
3016 final int height = fi.getMeasuredHeight();
3017
3018 // Lazy load ImageView, Bitmap and Canvas
3019 if (mFolderIconImageView == null) {
3020 mFolderIconImageView = new ImageView(this);
3021 }
3022 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3023 mFolderIconBitmap.getHeight() != height) {
3024 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3025 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3026 }
3027
3028 DragLayer.LayoutParams lp;
3029 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3030 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3031 } else {
3032 lp = new DragLayer.LayoutParams(width, height);
3033 }
3034
Adam Cohen307fe232012-08-16 17:55:58 -07003035 // The layout from which the folder is being opened may be scaled, adjust the starting
3036 // view size by this scale factor.
3037 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003038 lp.customPosition = true;
3039 lp.x = mRectForFolderAnimation.left;
3040 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003041 lp.width = (int) (scale * width);
3042 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003043
3044 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3045 fi.draw(mFolderIconCanvas);
3046 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003047 if (fi.getFolder() != null) {
3048 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3049 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003050 }
Adam Cohen268c4752012-06-06 17:47:33 -07003051 // Just in case this image view is still in the drag layer from a previous animation,
3052 // we remove it and re-add it.
3053 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3054 mDragLayer.removeView(mFolderIconImageView);
3055 }
3056 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003057 if (fi.getFolder() != null) {
3058 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003059 }
Adam Cohen268c4752012-06-06 17:47:33 -07003060 }
3061
Adam Cohen2801caf2011-05-13 20:57:39 -07003062 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003063 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003064 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3065 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3066 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3067
Adam Cohenc51934b2011-07-26 21:07:43 -07003068 FolderInfo info = (FolderInfo) fi.getTag();
3069 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3070 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003071 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3072 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003073 }
3074
Adam Cohen268c4752012-06-06 17:47:33 -07003075 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3076 copyFolderIconToImage(fi);
3077 fi.setVisibility(View.INVISIBLE);
3078
Michael Jurka2ecf9952012-06-18 12:52:28 -07003079 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003080 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003081 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003082 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3083 }
3084 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003085 oa.start();
3086 }
3087
Adam Cohen268c4752012-06-06 17:47:33 -07003088 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003089 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003090 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3091 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3092 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3093
Adam Cohen268c4752012-06-06 17:47:33 -07003094 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003095
Adam Cohen268c4752012-06-06 17:47:33 -07003096 // We remove and re-draw the FolderIcon in-case it has changed
3097 mDragLayer.removeView(mFolderIconImageView);
3098 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003099 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003100 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003101 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003102 oa.addListener(new AnimatorListenerAdapter() {
3103 @Override
3104 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003105 if (cl != null) {
3106 cl.clearFolderLeaveBehind();
3107 // Remove the ImageView copy of the FolderIcon and make the original visible.
3108 mDragLayer.removeView(mFolderIconImageView);
3109 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003110 }
3111 }
3112 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003113 oa.start();
3114 }
3115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003117 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118 * is animated relative to the specified View. If the View is null, no animation
3119 * is played.
3120 *
3121 * @param folderInfo The FolderInfo describing the folder to open.
3122 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003123 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003124 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003125 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3126 if (openFolder != null && openFolder != folder) {
3127 // Close any open folder before opening a folder.
3128 closeFolder();
3129 }
3130
Adam Cohena9cf38f2011-05-02 15:36:58 -07003131 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003132
Adam Cohena9cf38f2011-05-02 15:36:58 -07003133 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134
Sunny Goyalf4066152015-04-15 09:42:19 -07003135 // While the folder is open, the position of the icon cannot change.
3136 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3137
Adam Cohen4554ee12011-08-03 16:13:21 -07003138 // Just verify that the folder hasn't already been added to the DragLayer.
3139 // There was a one-off crash where the folder had a parent already.
3140 if (folder.getParent() == null) {
3141 mDragLayer.addView(folder);
3142 mDragController.addDropTarget((DropTarget) folder);
3143 } else {
3144 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3145 folder.getParent() + ").");
3146 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003147 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003148 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003149
3150 // Notify the accessibility manager that this folder "window" has appeared and occluded
3151 // the workspace items
3152 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3153 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003154 }
3155
3156 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003157 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003158 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003159 if (folder.isEditingName()) {
3160 folder.dismissEditingName();
3161 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003162 closeFolder(folder);
3163 }
3164 }
3165
Sunny Goyal83a8f042015-05-19 12:52:12 -07003166 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003167 folder.getInfo().opened = false;
3168
3169 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3170 if (parent != null) {
3171 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3172 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003173 if (fi != null) {
3174 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3175 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003176 }
3177 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003178
3179 // Notify the accessibility manager that this folder "window" has disappeard and no
3180 // longer occludeds the workspace items
3181 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003182 }
3183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003185 if (!isDraggingEnabled()) return false;
3186 if (isWorkspaceLocked()) return false;
3187 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188
Adam Cohen1697b792013-09-17 19:08:21 -07003189 if (v instanceof Workspace) {
3190 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003191 if (!mWorkspace.isTouchActive()) {
3192 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003193 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3194 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3195 return true;
3196 } else {
3197 return false;
3198 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003199 } else {
3200 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003201 }
Adam Cohen1697b792013-09-17 19:08:21 -07003202 }
3203
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003204 CellLayout.CellInfo longClickCellInfo = null;
3205 View itemUnderLongClick = null;
3206 if (v.getTag() instanceof ItemInfo) {
3207 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003208 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003209 itemUnderLongClick = longClickCellInfo.cell;
3210 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211 }
3212
Winson Chung3d503fb2011-07-13 17:25:49 -07003213 // The hotseat touch handling does not go through Workspace, and we always allow long press
3214 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003215 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003216 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003217 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003218 // User long pressed on empty space
3219 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3220 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003221 if (mWorkspace.isInOverviewMode()) {
3222 mWorkspace.startReordering(v);
3223 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003224 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003226 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003227 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3228 mHotseat.getOrderInHotseat(
3229 longClickCellInfo.cellX,
3230 longClickCellInfo.cellY));
3231 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003233 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003234 }
3235 }
3236 }
3237 return true;
3238 }
3239
Winson Chung3d503fb2011-07-13 17:25:49 -07003240 boolean isHotseatLayout(View layout) {
3241 return mHotseat != null && layout != null &&
3242 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3243 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003244
Winson Chung3d503fb2011-07-13 17:25:49 -07003245 /**
3246 * Returns the CellLayout of the specified container at the specified screen.
3247 */
Sunny Goyal92820592015-03-02 11:31:35 -08003248 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003249 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3250 if (mHotseat != null) {
3251 return mHotseat.getLayout();
3252 } else {
3253 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003254 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003255 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003256 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003257 }
3258 }
3259
Winson Chungb745afb2015-03-02 11:51:23 -08003260 /**
3261 * For overridden classes.
3262 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003263 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003264 return isAppsViewVisible();
3265 }
3266
3267 public boolean isAppsViewVisible() {
3268 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3269 }
3270
3271 public boolean isWidgetsViewVisible() {
3272 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003273 }
3274
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003275 private void setWorkspaceBackground(int background) {
3276 switch (background) {
3277 case WORKSPACE_BACKGROUND_TRANSPARENT:
3278 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3279 break;
3280 case WORKSPACE_BACKGROUND_BLACK:
3281 getWindow().setBackgroundDrawable(null);
3282 break;
3283 default:
3284 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3285 }
Craig Mautner360310b2012-10-26 15:13:08 -07003286 }
3287
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003288 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003289 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3290 int curflags = getWindow().getAttributes().flags
3291 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3292 if (wpflags != curflags) {
3293 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3294 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003295 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003296 }
3297
Michael Jurkae326f182011-11-21 14:05:46 -08003298 @Override
3299 public void onTrimMemory(int level) {
3300 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003301 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3302 // The widget preview db can result in holding onto over
3303 // 3MB of memory for caching which isn't necessary.
3304 SQLiteDatabase.releaseMemory();
3305
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003306 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003307 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003308 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003309 if (mLauncherCallbacks != null) {
3310 mLauncherCallbacks.onTrimMemory(level);
3311 }
Michael Jurkae326f182011-11-21 14:05:46 -08003312 }
3313
Winson Chungb745afb2015-03-02 11:51:23 -08003314 @Override
3315 public void onStateTransitionHideSearchBar() {
3316 // Hide the search bar
3317 if (mSearchDropTargetBar != null) {
3318 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3319 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003320 }
3321
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003322 public void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003323 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3324 true);
Adam Cohened307df2013-10-02 09:37:31 -07003325 }
3326
Sunny Goyal83a8f042015-05-19 12:52:12 -07003327 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003328 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3329 onCompleteRunnable, true);
3330 }
3331
3332 protected void showWorkspace(int snapToPage, boolean animated) {
3333 showWorkspace(snapToPage, animated, null, true);
3334 }
3335
3336 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3337 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003338 boolean changed = mState != State.WORKSPACE ||
3339 mWorkspace.getState() != Workspace.State.NORMAL;
3340 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003341 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003342 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003343 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003344 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003345
Winson Chungc7d2b602012-05-16 17:02:20 -07003346 // Show the search bar (only animate if we were showing the drop target bar in spring
3347 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003348 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003349 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003350 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003351
Michael Jurkab3e22d92011-10-31 15:58:33 -07003352 // Set focus to the AppsCustomize button
3353 if (mAllAppsButton != null) {
3354 mAllAppsButton.requestFocus();
3355 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003356 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003357
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003358 // Change the state *after* we've called all the transition code
3359 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003360
Winson Chung5fb63472011-02-02 17:03:37 -08003361 // Resume the auto-advance of widgets
3362 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003363 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003364
Winson Chung0f785722015-04-08 10:27:49 -07003365 if (changed) {
3366 // Send an accessibility event to announce the context change
3367 getWindow().getDecorView()
3368 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003369 if (notifyLauncherCallbacks) {
3370 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003371 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003372 mLauncherCallbacks.onAllAppsHidden();
3373 }
3374 }
Winson Chung0f785722015-04-08 10:27:49 -07003375 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003376 }
3377
Adam Cohened307df2013-10-02 09:37:31 -07003378 void showOverviewMode(boolean animated) {
3379 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003380 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003381 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3382 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003383 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003384 }
3385
Winson Chungb745afb2015-03-02 11:51:23 -08003386 /**
3387 * Shows the apps view.
3388 */
Winson Chung4ac30062015-05-08 17:34:17 -07003389 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003390 if (resetListToTop) {
3391 mAppsView.scrollToTop();
3392 }
Winson Chung4ac30062015-05-08 17:34:17 -07003393 if (updatePredictedApps) {
3394 tryAndUpdatePredictedApps();
3395 }
Winson Chungb745afb2015-03-02 11:51:23 -08003396 showAppsOrWidgets(animated, State.APPS);
3397 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003398
Winson Chungb745afb2015-03-02 11:51:23 -08003399 /**
3400 * Shows the widgets view.
3401 */
3402 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003403 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003404 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003405 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003406 }
Winson Chungb745afb2015-03-02 11:51:23 -08003407 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003408
3409 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003410 @Override
3411 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003412 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003413 }
3414 });
Winson Chungb745afb2015-03-02 11:51:23 -08003415 }
3416
3417 /**
3418 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003419 *
3420 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003421 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003422 // TODO: calling method should use the return value so that when {@code false} is returned
3423 // the workspace transition doesn't fall into invalid state.
3424 private boolean showAppsOrWidgets(boolean animated, State toState) {
3425 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3426 mState != State.WIDGETS_SPRING_LOADED) {
3427 return false;
3428 }
3429 if (toState != State.APPS && toState != State.WIDGETS) {
3430 return false;
3431 }
Winson Chungb745afb2015-03-02 11:51:23 -08003432
3433 if (toState == State.APPS) {
3434 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003435 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003436 mLauncherCallbacks.onAllAppsShown();
3437 }
Winson Chungb745afb2015-03-02 11:51:23 -08003438 } else {
3439 mStateTransitionAnimation.startAnimationToWidgets(animated);
3440 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003441
Michael Jurkab3e22d92011-10-31 15:58:33 -07003442 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003443 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003444
3445 // Pause the auto-advance of widgets until we are out of AllApps
3446 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003447 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003448 closeFolder();
3449
3450 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003451 getWindow().getDecorView()
3452 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003453 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454 }
3455
Winson Chungcd99cd32015-04-29 11:03:24 -07003456 /**
3457 * Updates the workspace and interaction state on state change, and return the animation to this
3458 * new state.
3459 */
3460 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3461 boolean animated, HashMap<View, Integer> layerViews) {
3462 Workspace.State fromState = mWorkspace.getState();
3463 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3464 updateInteraction(fromState, toState);
3465 return anim;
3466 }
3467
Hyunyoung Song3f471442015-04-08 19:01:34 -07003468 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003469 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003470 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3471 mState == State.WIDGETS_SPRING_LOADED) {
3472 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003473 }
Winson Chungb745afb2015-03-02 11:51:23 -08003474
3475 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003476 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3477 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003478 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003479 }
Adam Cohen7777d962011-08-18 18:58:38 -07003480
Hyunyoung Song3f471442015-04-08 19:01:34 -07003481 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003482 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003483 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003484
Winson Chungcd99cd32015-04-29 11:03:24 -07003485 if (successfulDrop) {
3486 // We need to trigger all apps hidden to notify search to update itself before the
3487 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003488 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003489 mLauncherCallbacks.onAllAppsHidden();
3490 }
3491 }
3492
Winson Chunge7a03942011-08-05 15:05:12 -07003493 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003494 @Override
3495 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003496 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003497 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3498 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003499 // Before we show workspace, hide all apps again because
3500 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3501 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003502 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003503 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003504 } else {
3505 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003506 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003507 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003508 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003509 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003510
Michael Jurkad3ef3062010-11-23 16:23:58 -08003511 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003512 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003513 showAppsView(true /* animated */, false /* resetListToTop */,
3514 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003515 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003516 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003517 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003518 }
3519
Winson Chung4ac30062015-05-08 17:34:17 -07003520 /**
3521 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3522 * resumed.
3523 */
3524 private void tryAndUpdatePredictedApps() {
3525 if (mLauncherCallbacks != null) {
3526 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3527 if (!apps.isEmpty()) {
3528 mAppsView.setPredictedApps(apps);
3529 }
3530 }
3531 }
3532
Michael Jurkab3e22d92011-10-31 15:58:33 -07003533 void lockAllApps() {
3534 // TODO
3535 }
3536
3537 void unlockAllApps() {
3538 // TODO
3539 }
3540
Sunny Goyal594d76d2014-11-06 10:12:54 -08003541 protected void disableVoiceButtonProxy(boolean disable) {
3542 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003543 }
3544
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003545 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003546 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3547 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003548 }
3549
Adam Cohen24ce0b32014-01-14 16:18:14 -08003550 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003551 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3552 if (searchProvider == null) {
3553 return null;
3554 }
3555
3556 Bundle opts = new Bundle();
3557 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003558 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003559
3560 SharedPreferences sp = getSharedPreferences(
3561 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3562 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003563 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003564 if (!searchProvider.provider.flattenToString().equals(
3565 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003566 || (widgetInfo == null)
3567 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003568 // A valid widget is not already bound.
3569 if (widgetId > -1) {
3570 mAppWidgetHost.deleteAppWidgetId(widgetId);
3571 widgetId = -1;
3572 }
3573
3574 // Try to bind a new widget
3575 widgetId = mAppWidgetHost.allocateAppWidgetId();
3576
3577 if (!AppWidgetManagerCompat.getInstance(this)
3578 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3579 mAppWidgetHost.deleteAppWidgetId(widgetId);
3580 widgetId = -1;
3581 }
3582
3583 sp.edit()
3584 .putInt(QSB_WIDGET_ID, widgetId)
3585 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3586 .commit();
3587 }
3588
3589 if (widgetId != -1) {
3590 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3591 mQsb.updateAppWidgetOptions(opts);
3592 mQsb.setPadding(0, 0, 0, 0);
3593 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003594 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003595 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003596 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003597 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003598 }
3599
Sunny Goyal22235bc2015-04-03 09:23:43 -07003600 private void reinflateQSBIfNecessary() {
3601 if (mQsb instanceof LauncherAppWidgetHostView &&
3602 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3603 mSearchDropTargetBar.removeView(mQsb);
3604 mQsb = null;
3605 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3606 }
3607 }
3608
Michael Jurkad7c28052012-04-27 15:43:36 -07003609 @Override
3610 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003611 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003612 final List<CharSequence> text = event.getText();
3613 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003614 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003615 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003616 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003617 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003618 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003619 } else {
3620 text.add(getString(R.string.all_apps_home_button_label));
3621 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003622 return result;
3623 }
3624
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003625 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003626 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003627 */
Adam Cohen091440a2015-03-18 14:16:05 -07003628 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003629 @Override
3630 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003631 closeSystemDialogs();
3632 }
3633 }
3634
3635 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003636 * Receives notifications whenever the appwidgets are reset.
3637 */
3638 private class AppWidgetResetObserver extends ContentObserver {
3639 public AppWidgetResetObserver() {
3640 super(new Handler());
3641 }
3642
3643 @Override
3644 public void onChange(boolean selfChange) {
3645 onAppWidgetReset();
3646 }
3647 }
3648
3649 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003650 * If the activity is currently paused, signal that we need to run the passed Runnable
3651 * in onResume.
3652 *
3653 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003654 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3655 * wrong when we're not running, and if the activity comes back to what the configuration was
3656 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003657 *
3658 * Implementation of the method from LauncherModel.Callbacks.
3659 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003660 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003661 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003662 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003663 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003664 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003665 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003666 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003667 }
3668 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003669 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003670 return true;
3671 } else {
3672 return false;
3673 }
3674 }
3675
Michael Jurkac402cd92013-05-20 15:49:32 +02003676 private boolean waitUntilResume(Runnable run) {
3677 return waitUntilResume(run, false);
3678 }
3679
Michael Jurka1e2f4652013-07-08 18:03:46 -07003680 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003681 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003682 }
3683
Michael Jurka7607c2f2013-04-03 14:33:19 -07003684 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003685 * If the activity is currently paused, signal that we need to re-run the loader
3686 * in onResume.
3687 *
3688 * This needs to be called from incoming places where resources might have been loaded
3689 * while we are paused. That is becaues the Configuration might be wrong
3690 * when we're not running, and if it comes back to what it was when we
3691 * were paused, we are not restarted.
3692 *
3693 * Implementation of the method from LauncherModel.Callbacks.
3694 *
3695 * @return true if we are currently paused. The caller might be able to
3696 * skip some work in that case since we will come back again.
3697 */
3698 public boolean setLoadOnResume() {
3699 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003700 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003701 mOnResumeNeedsLoad = true;
3702 return true;
3703 } else {
3704 return false;
3705 }
3706 }
3707
3708 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003709 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003710 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003711 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003712 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003713 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003714 } else {
3715 return SCREEN_COUNT / 2;
3716 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003717 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003718
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 /**
3720 * Refreshes the shortcuts shown on the workspace.
3721 *
3722 * Implementation of the method from LauncherModel.Callbacks.
3723 */
3724 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003725 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003726
Michael Jurka7607c2f2013-04-03 14:33:19 -07003727 // If we're starting binding all over again, clear any bind calls we'd postponed in
3728 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3729 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003730 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003731
Winson Chungd64d1762013-08-20 14:37:16 -07003732 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003733 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003734 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003735
Michael Jurka05bf6442011-11-30 20:28:53 -08003736 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003737 if (mHotseat != null) {
3738 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003739 }
3740 }
3741
Adam Cohendcd297f2013-06-18 13:13:40 -07003742 @Override
3743 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003744 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003745
Adam Cohen5084cba2013-09-03 12:01:16 -07003746 // If there are no screens, we need to have an empty screen
3747 if (orderedScreenIds.size() == 0) {
3748 mWorkspace.addExtraEmptyScreen();
3749 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003750
3751 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003752 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003753 if (hasCustomContentToLeft()) {
3754 mWorkspace.createCustomContentContainer();
3755 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003756 }
Winson Chung64359a52013-07-08 17:17:08 -07003757 }
3758
3759 @Override
3760 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003761 int count = orderedScreenIds.size();
3762 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003763 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003764 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003765 }
3766
Adam Cohen39a06042013-07-19 14:30:12 -07003767 private boolean shouldShowWeightWatcher() {
3768 String spKey = LauncherAppState.getSharedPreferencesKey();
3769 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003770 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003771
3772 return show;
3773 }
3774
3775 private void toggleShowWeightWatcher() {
3776 String spKey = LauncherAppState.getSharedPreferencesKey();
3777 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3778 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3779
3780 show = !show;
3781
3782 SharedPreferences.Editor editor = sp.edit();
3783 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3784 editor.commit();
3785
3786 if (mWeightWatcher != null) {
3787 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3788 }
3789 }
3790
Winson Chungd64d1762013-08-20 14:37:16 -07003791 public void bindAppsAdded(final ArrayList<Long> newScreens,
3792 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003793 final ArrayList<ItemInfo> addAnimated,
3794 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003795 Runnable r = new Runnable() {
3796 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003797 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003798 }
3799 };
3800 if (waitUntilResume(r)) {
3801 return;
3802 }
3803
Winson Chungd64d1762013-08-20 14:37:16 -07003804 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003805 if (newScreens != null) {
3806 bindAddScreens(newScreens);
3807 }
Winson Chungd64d1762013-08-20 14:37:16 -07003808
3809 // We add the items without animation on non-visible pages, and with
3810 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003811 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003812 bindItems(addNotAnimated, 0,
3813 addNotAnimated.size(), false);
3814 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003815 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003816 bindItems(addAnimated, 0,
3817 addAnimated.size(), true);
3818 }
Winson Chungc58497e2013-09-03 17:48:37 -07003819
Winson Chung87412982013-10-03 18:34:14 -07003820 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003821 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003822
Winson Chungb745afb2015-03-02 11:51:23 -08003823 if (addedApps != null && mAppsView != null) {
3824 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003825 }
Winson Chungd64d1762013-08-20 14:37:16 -07003826 }
3827
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003828 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003829 * Bind the items start-end from the list.
3830 *
3831 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003832 */
Winson Chung64359a52013-07-08 17:17:08 -07003833 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3834 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003835 Runnable r = new Runnable() {
3836 public void run() {
3837 bindItems(shortcuts, start, end, forceAnimateIcons);
3838 }
3839 };
3840 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003841 return;
3842 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003843
Winson Chungf0c6ae02012-03-21 16:10:31 -07003844 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003845 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3846 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003847 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003848 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003849 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003850 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003852
3853 // Short circuit if we are loading dock items for a configuration which has no dock
3854 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3855 mHotseat == null) {
3856 continue;
3857 }
3858
Joe Onorato9c1289c2009-08-17 11:03:03 -04003859 switch (item.itemType) {
3860 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3861 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003862 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003863 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003864
Winson Chung64359a52013-07-08 17:17:08 -07003865 /*
3866 * TODO: FIX collision case
3867 */
Winson Chungce376632013-07-11 16:12:41 -07003868 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3869 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3870 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003871 View v = cl.getChildAt(item.cellX, item.cellY);
3872 Object tag = v.getTag();
3873 String desc = "Collision while binding workspace item: " + item
3874 + ". Collides with " + tag;
3875 if (LauncherAppState.isDogfoodBuild()) {
3876 throw (new RuntimeException(desc));
3877 } else {
3878 Log.d(TAG, desc);
3879 }
Winson Chungce376632013-07-11 16:12:41 -07003880 }
Winson Chung64359a52013-07-08 17:17:08 -07003881 }
3882
Adam Cohendcd297f2013-06-18 13:13:40 -07003883 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3884 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003885 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003886 // Animate all the applications up now
3887 shortcut.setAlpha(0f);
3888 shortcut.setScaleX(0f);
3889 shortcut.setScaleY(0f);
3890 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003891 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003892 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003893 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003894 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003895 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003896 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003897 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003898 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3899 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003901 default:
3902 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003904 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003905
Winson Chung997a9232013-07-24 15:33:46 -07003906 if (animateIcons) {
3907 // Animate to the correct page
3908 if (newShortcutsScreenId > -1) {
3909 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003910 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003911 final Runnable startBounceAnimRunnable = new Runnable() {
3912 public void run() {
3913 anim.playTogether(bounceAnims);
3914 anim.start();
3915 }
3916 };
Winson Chung997a9232013-07-24 15:33:46 -07003917 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003918 // We post the animation slightly delayed to prevent slowdowns
3919 // when we are loading right after we return to launcher.
3920 mWorkspace.postDelayed(new Runnable() {
3921 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003922 if (mWorkspace != null) {
3923 mWorkspace.snapToPage(newScreenIndex);
3924 mWorkspace.postDelayed(startBounceAnimRunnable,
3925 NEW_APPS_ANIMATION_DELAY);
3926 }
Winson Chung94d67682013-09-25 16:29:40 -07003927 }
3928 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003929 } else {
3930 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003931 }
3932 }
Winson Chung64359a52013-07-08 17:17:08 -07003933 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003934 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003935 }
3936
Joe Onorato9c1289c2009-08-17 11:03:03 -04003937 /**
3938 * Implementation of the method from LauncherModel.Callbacks.
3939 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003940 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003941 Runnable r = new Runnable() {
3942 public void run() {
3943 bindFolders(folders);
3944 }
3945 };
3946 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003947 return;
3948 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003949 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003950 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951
3952 /**
3953 * Add the views for a widget to the workspace.
3954 *
3955 * Implementation of the method from LauncherModel.Callbacks.
3956 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003957 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003958 Runnable r = new Runnable() {
3959 public void run() {
3960 bindAppWidget(item);
3961 }
3962 };
3963 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003964 return;
3965 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003966
Daniel Sandler843e8602010-06-07 14:59:01 -04003967 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3968 if (DEBUG_WIDGETS) {
3969 Log.d(TAG, "bindAppWidget: " + item);
3970 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003971 final Workspace workspace = mWorkspace;
3972
Adam Cohen59400422014-03-05 18:07:04 -08003973 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003974 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003975
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003976 if (!mIsSafeModeEnabled
3977 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3978 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003979 if (appWidgetInfo == null) {
3980 if (DEBUG_WIDGETS) {
3981 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3982 + " belongs to component " + item.providerName
3983 + ", as the povider is null");
3984 }
3985 LauncherModel.deleteItemFromDatabase(this, item);
3986 return;
3987 }
3988 // Note: This assumes that the id remap broadcast is received before this step.
3989 // If that is not the case, the id remap will be ignored and user may see the
3990 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07003991 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003992 pendingInfo.spanX = item.spanX;
3993 pendingInfo.spanY = item.spanY;
3994 pendingInfo.minSpanX = item.minSpanX;
3995 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003996 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003997 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003998
Sunny Goyalff572272014-07-23 13:58:07 -07003999 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004000 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4001 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004002
4003 // TODO consider showing a permission dialog when the widget is clicked.
4004 if (!success) {
4005 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4006 if (DEBUG_WIDGETS) {
4007 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4008 + " belongs to component " + item.providerName
4009 + ", as the launcher is unable to bing a new widget id");
4010 }
4011 LauncherModel.deleteItemFromDatabase(this, item);
4012 return;
4013 }
4014
4015 item.appWidgetId = newWidgetId;
4016
4017 // If the widget has a configure activity, it is still needs to set it up, otherwise
4018 // the widget is ready to go.
4019 item.restoreStatus = (appWidgetInfo.configure == null)
4020 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4021 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4022
4023 LauncherModel.updateItemInDatabase(this, item);
4024 }
4025
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004026 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004027 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004028 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004029 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4030 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004031 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004032
Sunny Goyal651077b2014-06-30 14:15:31 -07004033 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4034 } else {
4035 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004036 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4037 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004038 view.updateIcon(mIconCache);
4039 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004040 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004041 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004042 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004043
Joe Onorato9c1289c2009-08-17 11:03:03 -04004044 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004045 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004046
Adam Cohendcd297f2013-06-18 13:13:40 -07004047 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004048 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004049 if (!item.isCustomWidget()) {
4050 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4051 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004052
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053 workspace.requestLayout();
4054
Daniel Sandler843e8602010-06-07 14:59:01 -04004055 if (DEBUG_WIDGETS) {
4056 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4057 + (SystemClock.uptimeMillis()-start) + "ms");
4058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004059 }
4060
Sunny Goyalff572272014-07-23 13:58:07 -07004061 /**
4062 * Restores a pending widget.
4063 *
4064 * @param appWidgetId The app widget id
4065 * @param cellInfo The position on screen where to create the widget.
4066 */
4067 private void completeRestoreAppWidget(final int appWidgetId) {
4068 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4069 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4070 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4071 return;
4072 }
4073
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004074 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004075 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4076
4077 mWorkspace.reinflateWidgetsIfNecessary();
4078 LauncherModel.updateItemInDatabase(this, info);
4079 }
4080
Adam Cohen1462de32012-07-24 22:34:36 -07004081 public void onPageBoundSynchronously(int page) {
4082 mSynchronouslyBoundPages.add(page);
4083 }
4084
Joe Onorato9c1289c2009-08-17 11:03:03 -04004085 /**
4086 * Callback saying that there aren't any more items to bind.
4087 *
4088 * Implementation of the method from LauncherModel.Callbacks.
4089 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004090 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004091 Runnable r = new Runnable() {
4092 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004093 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004094 }
4095 };
4096 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004097 return;
4098 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004099 if (mSavedState != null) {
4100 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004101 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004102 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004103 mSavedState = null;
4104 }
4105
Adam Cohen1462de32012-07-24 22:34:36 -07004106 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004108 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004109 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004110
4111 // If we received the result of any pending adds while the loader was running (e.g. the
4112 // widget configuration forced an orientation change), process them now.
4113 if (sPendingAddItem != null) {
4114 final long screenId = completeAdd(sPendingAddItem);
4115
4116 // TODO: this moves the user to the page where the pending item was added. Ideally,
4117 // the screen would be guaranteed to exist after bind, and the page would be set through
4118 // the workspace restore process.
4119 mWorkspace.post(new Runnable() {
4120 @Override
4121 public void run() {
4122 mWorkspace.snapToScreenId(screenId);
4123 }
4124 });
4125 sPendingAddItem = null;
4126 }
4127
Sunny Goyal756adbc2015-04-16 15:20:51 -07004128 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004129
4130 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004131 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004132 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004133 }
4134
Adam Cohen3ed316a2014-07-23 18:21:20 -07004135 private void sendLoadingCompleteBroadcastIfNecessary() {
4136 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4137 String permission =
4138 getResources().getString(R.string.receive_first_load_broadcast_permission);
4139 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4140 sendBroadcast(intent, permission);
4141 SharedPreferences.Editor editor = mSharedPrefs.edit();
4142 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4143 editor.apply();
4144 }
4145 }
4146
Winson Chunga0b7e862013-09-05 16:03:15 -07004147 public boolean isAllAppsButtonRank(int rank) {
4148 if (mHotseat != null) {
4149 return mHotseat.isAllAppsButtonRank(rank);
4150 }
4151 return false;
4152 }
4153
Winson Chunga2413752012-04-03 14:22:34 -07004154 private boolean canRunNewAppsAnimation() {
4155 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4156 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4157 }
4158
Winson Chungc9168342013-06-26 14:54:55 -07004159 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4160 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4161 PropertyValuesHolder.ofFloat("alpha", 1f),
4162 PropertyValuesHolder.ofFloat("scaleX", 1f),
4163 PropertyValuesHolder.ofFloat("scaleY", 1f));
4164 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4165 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004166 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004167 return bounceAnim;
4168 }
4169
Adam Cohen27772732013-11-11 14:00:56 +00004170 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004171 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004172 }
4173
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004174 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004175 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004176 }
4177
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004178 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004179 if (mSearchDropTargetBar == null) {
4180 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004181 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004182 if (mQsb != null) {
4183 mSearchDropTargetBar.removeView(mQsb);
4184 mQsb = null;
4185 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004186 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004187 }
4188
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004189 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004190 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4191 * multiple calls to bind the same list.)
4192 */
4193 @Thunk ArrayList<AppInfo> mTmpAppsList;
4194 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4195 public void run() {
4196 bindAllApplications(mTmpAppsList);
4197 mTmpAppsList = null;
4198 }
4199 };
4200
4201 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 * Add the icons for all apps.
4203 *
4204 * Implementation of the method from LauncherModel.Callbacks.
4205 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004206 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004207 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4208 mTmpAppsList = apps;
4209 return;
4210 }
4211
Winson Chungb745afb2015-03-02 11:51:23 -08004212 if (mAppsView != null) {
4213 mAppsView.setApps(apps);
4214 }
Adam Cohen9211d422014-10-07 18:14:53 -07004215 if (mLauncherCallbacks != null) {
4216 mLauncherCallbacks.bindAllApplications(apps);
4217 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004218 }
4219
4220 /**
4221 * A package was updated.
4222 *
4223 * Implementation of the method from LauncherModel.Callbacks.
4224 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004225 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004226 Runnable r = new Runnable() {
4227 public void run() {
4228 bindAppsUpdated(apps);
4229 }
4230 };
4231 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004232 return;
4233 }
4234
Winson Chungb745afb2015-03-02 11:51:23 -08004235 if (mAppsView != null) {
4236 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004237 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004238 }
4239
Sunny Goyal4390ace2014-10-13 11:33:11 -07004240 @Override
4241 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4242 Runnable r = new Runnable() {
4243 public void run() {
4244 bindWidgetsRestored(widgets);
4245 }
4246 };
4247 if (waitUntilResume(r)) {
4248 return;
4249 }
4250 mWorkspace.widgetsRestored(widgets);
4251 }
4252
Joe Onorato9c1289c2009-08-17 11:03:03 -04004253 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004254 * Some shortcuts were updated in the background.
4255 *
4256 * Implementation of the method from LauncherModel.Callbacks.
4257 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004258 @Override
4259 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4260 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004261 Runnable r = new Runnable() {
4262 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004263 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004264 }
4265 };
4266 if (waitUntilResume(r)) {
4267 return;
4268 }
4269
Sunny Goyal4390ace2014-10-13 11:33:11 -07004270 if (!updated.isEmpty()) {
4271 mWorkspace.updateShortcuts(updated);
4272 }
4273
4274 if (!removed.isEmpty()) {
4275 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4276 for (ShortcutInfo si : removed) {
4277 removedComponents.add(si.getTargetComponent());
4278 }
4279 mWorkspace.removeItemsByComponentName(removedComponents, user);
4280 // Notify the drag controller
4281 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004282 }
4283 }
4284
4285 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004286 * Update the state of a package, typically related to install state.
4287 *
4288 * Implementation of the method from LauncherModel.Callbacks.
4289 */
Sunny Goyale755d462014-07-22 13:48:29 -07004290 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004291 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4292 Runnable r = new Runnable() {
4293 public void run() {
4294 bindRestoreItemsChange(updates);
4295 }
4296 };
4297 if (waitUntilResume(r)) {
4298 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004299 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004300
Sunny Goyal756adbc2015-04-16 15:20:51 -07004301 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004302 }
4303
4304 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004305 * A package was uninstalled. We take both the super set of packageNames
4306 * in addition to specific applications to remove, the reason being that
4307 * this can be called when a package is updated as well. In that scenario,
4308 * we only remove specific components from the workspace, where as
4309 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004310 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004311 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004312 * Implementation of the method from LauncherModel.Callbacks.
4313 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004314 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004315 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004316 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004317 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004318 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004319 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004320 }
Winson Chungd64d1762013-08-20 14:37:16 -07004321 };
4322 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004323 return;
4324 }
4325
Sunny Goyal1a745e82014-10-02 15:58:31 -07004326 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004327 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4328 for (AppInfo info : appInfos) {
4329 removedComponents.add(info.componentName);
4330 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004331 if (!packageNames.isEmpty()) {
4332 mWorkspace.removeItemsByPackageName(packageNames, user);
4333 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004334 if (!removedComponents.isEmpty()) {
4335 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004336 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004337 // Notify the drag controller
4338 mDragController.onAppsRemoved(packageNames, removedComponents);
4339
Sunny Goyal1a745e82014-10-02 15:58:31 -07004340 } else {
4341 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004342 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004343
Winson Chungdf95eb12013-10-16 14:57:07 -07004344 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004345 if (mAppsView != null) {
4346 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004347 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004348 }
Joe Onoratobe386092009-11-17 17:32:16 -08004349
Michael Jurkac402cd92013-05-20 15:49:32 +02004350 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004351 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004352 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004353 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004354 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004355
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004356 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004357 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004358 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004359 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004360 return;
4361 }
4362
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004363 if (mWidgetsView != null && model != null) {
4364 mWidgetsView.addWidgets(model);
4365 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004366 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004367 }
4368
Winson Chung400438b2011-01-16 17:53:48 -08004369 private int mapConfigurationOriActivityInfoOri(int configOri) {
4370 final Display d = getWindowManager().getDefaultDisplay();
4371 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4372 switch (d.getRotation()) {
4373 case Surface.ROTATION_0:
4374 case Surface.ROTATION_180:
4375 // We are currently in the same basic orientation as the natural orientation
4376 naturalOri = configOri;
4377 break;
4378 case Surface.ROTATION_90:
4379 case Surface.ROTATION_270:
4380 // We are currently in the other basic orientation to the natural orientation
4381 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4382 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4383 break;
4384 }
4385
4386 int[] oriMap = {
4387 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4388 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4389 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4390 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4391 };
4392 // Since the map starts at portrait, we need to offset if this device's natural orientation
4393 // is landscape.
4394 int indexOffset = 0;
4395 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4396 indexOffset = 1;
4397 }
4398 return oriMap[(d.getRotation() + indexOffset) % 4];
4399 }
Adam Cohen446e9402011-09-15 18:21:21 -07004400
Winson Chung4b919f82012-05-01 10:44:08 -07004401 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004402 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004403 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4404 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4405 .getConfiguration().orientation));
4406 } else {
4407 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4408 }
Winson Chung4b919f82012-05-01 10:44:08 -07004409 }
4410 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004411
Winson Chung4b919f82012-05-01 10:44:08 -07004412 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004413 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004414 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004415 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004416 } else {
4417 mHandler.postDelayed(new Runnable() {
4418 public void run() {
4419 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4420 }
4421 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004422 }
Winson Chung4b919f82012-05-01 10:44:08 -07004423 }
Winson Chung400438b2011-01-16 17:53:48 -08004424 }
4425
Winson Chunge43a1e72014-01-15 10:33:02 -08004426 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004427 if (mLauncherCallbacks != null) {
4428 return mLauncherCallbacks.isLauncherPreinstalled();
4429 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004430 PackageManager pm = getPackageManager();
4431 try {
4432 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4433 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4434 return true;
4435 } else {
4436 return false;
4437 }
4438 } catch (NameNotFoundException e) {
4439 e.printStackTrace();
4440 return false;
4441 }
4442 }
4443
Adam Cohenea90f832014-05-21 15:03:34 -07004444 /**
4445 * This method indicates whether or not we should suggest default wallpaper dimensions
4446 * when our wallpaper cropper was not yet used to set a wallpaper.
4447 */
4448 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004449 if (mLauncherCallbacks != null) {
4450 return mLauncherCallbacks.overrideWallpaperDimensions();
4451 }
Adam Cohenea90f832014-05-21 15:03:34 -07004452 return true;
4453 }
4454
Adam Cohen432609a2014-03-13 17:03:22 -07004455 /**
4456 * To be overridden by subclasses to indicate that there is an activity to launch
4457 * before showing the standard launcher experience.
4458 */
4459 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004460 if (mLauncherCallbacks != null) {
4461 return mLauncherCallbacks.hasFirstRunActivity();
4462 }
Adam Cohen432609a2014-03-13 17:03:22 -07004463 return false;
4464 }
4465
4466 /**
4467 * To be overridden by subclasses to launch any first run activity
4468 */
4469 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004470 if (mLauncherCallbacks != null) {
4471 return mLauncherCallbacks.getFirstRunActivity();
4472 }
Adam Cohen432609a2014-03-13 17:03:22 -07004473 return null;
4474 }
4475
Winson Chung2826a402015-04-17 16:18:53 -07004476 /**
4477 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004478 */
4479 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004480 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4481 return false;
4482 }
4483
Winson Chung2826a402015-04-17 16:18:53 -07004484 if (mLauncherCallbacks != null) {
4485 return mLauncherCallbacks.overrideAllAppsSearch();
4486 }
4487 return false;
4488 }
4489
Winson Chunga6945242014-01-08 14:04:34 -08004490 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004491 return !ActivityManager.isRunningInTestHarness() &&
4492 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004493 }
4494
Adam Cohen4a9423d2014-03-28 14:22:31 -07004495 protected boolean hasRunFirstRunActivity() {
4496 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4497 }
4498
Adam Cohen432609a2014-03-13 17:03:22 -07004499 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004500 if (shouldRunFirstRunActivity() &&
4501 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004502 Intent firstRunIntent = getFirstRunActivity();
4503 if (firstRunIntent != null) {
4504 startActivity(firstRunIntent);
4505 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004506 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004507 }
4508 }
Adam Cohen432609a2014-03-13 17:03:22 -07004509 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004510 }
4511
4512 private void markFirstRunActivityShown() {
4513 SharedPreferences.Editor editor = mSharedPrefs.edit();
4514 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4515 editor.apply();
4516 }
4517
Adam Cohen432609a2014-03-13 17:03:22 -07004518 /**
4519 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4520 * screen that must be displayed and dismissed.
4521 */
4522 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004523 if (mLauncherCallbacks != null) {
4524 return mLauncherCallbacks.hasDismissableIntroScreen();
4525 }
Adam Cohen432609a2014-03-13 17:03:22 -07004526 return false;
4527 }
4528
4529 /**
4530 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4531 */
4532 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004533 if (mLauncherCallbacks != null) {
4534 return mLauncherCallbacks.getIntroScreen();
4535 }
Adam Cohen432609a2014-03-13 17:03:22 -07004536 return null;
4537 }
4538
4539 /**
4540 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4541 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4542 */
4543 private boolean shouldShowIntroScreen() {
4544 return hasDismissableIntroScreen() &&
4545 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4546 }
4547
4548 protected void showIntroScreen() {
4549 View introScreen = getIntroScreen();
4550 changeWallpaperVisiblity(false);
4551 if (introScreen != null) {
4552 mDragLayer.showOverlayView(introScreen);
4553 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004554 if (mLauncherOverlayContainer != null) {
4555 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4556 }
Adam Cohen432609a2014-03-13 17:03:22 -07004557 }
4558
4559 public void dismissIntroScreen() {
4560 markIntroScreenDismissed();
4561 if (showFirstRunActivity()) {
4562 // We delay hiding the intro view until the first run activity is showing. This
4563 // avoids a blip.
4564 mWorkspace.postDelayed(new Runnable() {
4565 @Override
4566 public void run() {
4567 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004568 if (mLauncherOverlayContainer != null) {
4569 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4570 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004571 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004572 }
4573 }, ACTIVITY_START_DELAY);
4574 } else {
4575 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004576 if (mLauncherOverlayContainer != null) {
4577 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4578 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004579 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004580 }
4581 changeWallpaperVisiblity(true);
4582 }
4583
4584 private void markIntroScreenDismissed() {
4585 SharedPreferences.Editor editor = mSharedPrefs.edit();
4586 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4587 editor.apply();
4588 }
4589
Adam Cohen091440a2015-03-18 14:16:05 -07004590 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004591 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4592 // on the device, then we always show the first run cling experience (or if there is no
4593 // launcher2). Otherwise, we prompt the user upon started for migration
4594 LauncherClings launcherClings = new LauncherClings(this);
4595 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4596 if (mModel.canMigrateFromOldLauncherDb(this)) {
4597 launcherClings.showMigrationCling();
4598 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004599 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004600 }
4601 }
4602 }
4603
Winson Chunga6945242014-01-08 14:04:34 -08004604 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004605 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4606 if (mHotseat != null) mHotseat.setAlpha(1f);
4607 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4608 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004609 }
4610
4611 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004612 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4613 if (mHotseat != null) mHotseat.setAlpha(0f);
4614 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4615 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004616 }
4617
Mathew Inwood72fbec12013-11-19 15:45:07 +00004618 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004619 // Called from search suggestion, not supported in other profiles.
4620 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4621 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4622 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4623 myUser);
4624 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004625 return null;
4626 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004627 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004628 }
4629
4630 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4631 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004632 // Called from search suggestion, not supported in other profiles.
4633 return createShortcutDragInfo(shortcutIntent, caption, icon,
4634 UserHandleCompat.myUserHandle());
4635 }
4636
4637 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4638 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004639 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004640 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004641 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004642 }
4643
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004644 protected void moveWorkspaceToDefaultScreen() {
4645 mWorkspace.moveToDefaultScreen(false);
4646 }
4647
Mathew Inwood72fbec12013-11-19 15:45:07 +00004648 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4649 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004650 mWorkspace.onExternalDragStartedWithItem(dragView);
4651 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004652 }
4653
Anjali Koppalf5d29132014-02-28 13:33:27 -08004654 @Override
4655 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004656 if (mLauncherCallbacks != null) {
4657 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4658 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004659 }
4660
Winson Chung80baf5a2010-08-09 16:03:15 -07004661 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004662 * Returns a FastBitmapDrawable with the icon, accurately sized.
4663 */
4664 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4665 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4666 d.setFilterBitmap(true);
4667 resizeIconDrawable(d);
4668 return d;
4669 }
4670
4671 /**
4672 * Resizes an icon drawable to the correct icon size.
4673 */
4674 public void resizeIconDrawable(Drawable icon) {
4675 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4676 }
4677
4678 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004679 * Prints out out state for debugging.
4680 */
4681 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004682 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004683 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004684 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4685 Log.d(TAG, "mRestoring=" + mRestoring);
4686 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4687 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004688 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004689 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004690 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004691
Daniel Sandler325dc232013-06-05 22:57:57 -04004692 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004693 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004694
4695 @Override
4696 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4697 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004698 synchronized (sDumpLogs) {
4699 writer.println(" ");
4700 writer.println("Debug logs: ");
4701 for (int i = 0; i < sDumpLogs.size(); i++) {
4702 writer.println(" " + sDumpLogs.get(i));
4703 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004704 }
Adam Cohen9211d422014-10-07 18:14:53 -07004705 if (mLauncherCallbacks != null) {
4706 mLauncherCallbacks.dump(prefix, fd, writer, args);
4707 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004708 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004709
4710 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004711 if (DEBUG_DUMP_LOG) {
4712 synchronized (sDumpLogs) {
4713 Log.d(TAG, "");
4714 Log.d(TAG, "*********************");
4715 Log.d(TAG, "Launcher debug logs: ");
4716 for (int i = 0; i < sDumpLogs.size(); i++) {
4717 Log.d(TAG, " " + sDumpLogs.get(i));
4718 }
4719 Log.d(TAG, "*********************");
4720 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004721 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004722 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004723 }
4724
4725 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004726 addDumpLog(tag, log, null, debugLog);
4727 }
4728
4729 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004730 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004731 if (e != null) {
4732 Log.d(tag, log, e);
4733 } else {
4734 Log.d(tag, log);
4735 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004736 }
Winson Chungede41292013-09-19 16:27:36 -07004737 if (DEBUG_DUMP_LOG) {
4738 sDateStamp.setTime(System.currentTimeMillis());
4739 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004740 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4741 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004742 }
Winson Chungede41292013-09-19 16:27:36 -07004743 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004744 }
4745
Adam Cohen59400422014-03-05 18:07:04 -08004746 public static CustomAppWidget getCustomAppWidget(String name) {
4747 return sCustomAppWidgets.get(name);
4748 }
4749
4750 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4751 return sCustomAppWidgets;
4752 }
4753
Winson Chungede41292013-09-19 16:27:36 -07004754 public void dumpLogsToLocalData() {
4755 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004756 new AsyncTask<Void, Void, Void>() {
4757 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004758 boolean success = false;
4759 sDateStamp.setTime(sRunStart);
4760 String FILENAME = sDateStamp.getMonth() + "-"
4761 + sDateStamp.getDay() + "_"
4762 + sDateStamp.getHours() + "-"
4763 + sDateStamp.getMinutes() + "_"
4764 + sDateStamp.getSeconds() + ".txt";
4765
4766 FileOutputStream fos = null;
4767 File outFile = null;
4768 try {
4769 outFile = new File(getFilesDir(), FILENAME);
4770 outFile.createNewFile();
4771 fos = new FileOutputStream(outFile);
4772 } catch (Exception e) {
4773 e.printStackTrace();
4774 }
4775 if (fos != null) {
4776 PrintWriter writer = new PrintWriter(fos);
4777
4778 writer.println(" ");
4779 writer.println("Debug logs: ");
4780 synchronized (sDumpLogs) {
4781 for (int i = 0; i < sDumpLogs.size(); i++) {
4782 writer.println(" " + sDumpLogs.get(i));
4783 }
4784 }
4785 writer.close();
4786 }
4787 try {
4788 if (fos != null) {
4789 fos.close();
4790 success = true;
4791 }
4792 } catch (IOException e) {
4793 e.printStackTrace();
4794 }
Michael Jurka43467462013-11-14 12:03:58 +01004795 return null;
Winson Chungede41292013-09-19 16:27:36 -07004796 }
Michael Jurka43467462013-11-14 12:03:58 +01004797 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004798 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004800}
Michael Jurka2763be32011-02-24 11:19:57 -08004801
Dan Sandlerd5024042014-01-09 15:01:33 -05004802interface DebugIntents {
4803 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4804 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004805}