blob: 51f091613f96d0114c30f56030c09cb991dc6ff0 [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;
Sunny Goyal83a8f042015-05-19 12:52:12 -0700101import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700102import com.android.launcher3.allapps.AllAppsContainerView;
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
Winson Chung3d503fb2011-07-13 17:25:49 -0700267 private 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
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700404 private boolean mRotationEnabled = false;
405 private boolean mPreferenceObserverRegistered = false;
406
407 final private SharedPreferences.OnSharedPreferenceChangeListener mSettingsObserver =
408 new SharedPreferences.OnSharedPreferenceChangeListener() {
409 @Override
410 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
411 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
412 if (mRotationEnabled = sharedPreferences.getBoolean(
413 Utilities.ALLOW_ROTATION_PREFERENCE_KEY, false)) {
414 unlockScreenOrientation(true);
415 } else {
416 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
417 }
418 }
419 }
420 };
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 @Override
423 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700424 if (DEBUG_STRICT_MODE) {
425 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
426 .detectDiskReads()
427 .detectDiskWrites()
428 .detectNetwork() // or .detectAll() for all detectable problems
429 .penaltyLog()
430 .build());
431 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
432 .detectLeakedSqlLiteObjects()
433 .detectLeakedClosableObjects()
434 .penaltyLog()
435 .penaltyDeath()
436 .build());
437 }
438
Adam Cohen9211d422014-10-07 18:14:53 -0700439 if (mLauncherCallbacks != null) {
440 mLauncherCallbacks.preOnCreate();
441 }
442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400444
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400445 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400446 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700447 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700448
Adam Cohen2e6da152015-05-06 11:42:25 -0700449 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700450 mDeviceProfile = getResources().getConfiguration().orientation
451 == Configuration.ORIENTATION_LANDSCAPE ?
452 app.getInvariantDeviceProfile().landscapeProfile
453 : app.getInvariantDeviceProfile().portraitProfile;
454
455 // TODO: Move this to icon cache.
456 Utilities.setIconSize(mDeviceProfile.iconSizePx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700457
458 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400459 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700460 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700461 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800462 mModel = app.setLauncher(this);
463 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700464
Joe Onorato41a12d22009-10-31 18:30:00 -0400465 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800467 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700468
Daniel Sandlerff02d492013-08-05 02:12:05 -0400469 mStats = new Stats(this);
470
Sunny Goyalffe83f12014-08-14 17:39:34 -0700471 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700472
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700473 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
474 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700475
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700476 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
477 // this also ensures that any synchronous binding below doesn't re-trigger another
478 // LauncherModel load.
479 mPaused = false;
480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200482 android.os.Debug.startMethodTracing(
483 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 }
485
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700487
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700489 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800491 registerContentObservers();
492
Joe Onorato7c312c12009-08-13 21:36:53 -0700493 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700494
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 mSavedState = savedInstanceState;
496 restoreState(mSavedState);
497
498 if (PROFILE_STARTUP) {
499 android.os.Debug.stopMethodTracing();
500 }
501
502 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700503 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700504 // If the user leaves launcher, then we should just load items asynchronously when
505 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700506 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700507 } else {
508 // We only load the page synchronously if the user rotates (or triggers a
509 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700510 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700511 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 }
513
514 // For handling default keys
515 mDefaultKeySsb = new SpannableStringBuilder();
516 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800517
518 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
519 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800520
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700521 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
522 // In case we are on a device with locked rotation, we should look at preferences to check
523 // if the user has specifically allowed rotation.
524 if (!mRotationEnabled) {
525 getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
526 Context.MODE_MULTI_PROCESS).registerOnSharedPreferenceChangeListener(
527 mSettingsObserver);
528 mPreferenceObserverRegistered = true;
529 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
530 }
531
532 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
533 // we want the screen to auto-rotate based on the current orientation
Adam Cohenf9426d52012-06-04 17:26:21 -0700534 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700535
Adam Cohen9211d422014-10-07 18:14:53 -0700536 if (mLauncherCallbacks != null) {
537 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700538 if (mLauncherCallbacks.hasLauncherOverlay()) {
539 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700540 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
541 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
542 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700543 mWorkspace.setLauncherOverlay(mLauncherOverlay);
544 }
Adam Cohen9211d422014-10-07 18:14:53 -0700545 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700546
547 if (shouldShowIntroScreen()) {
548 showIntroScreen();
549 } else {
550 showFirstRunActivity();
551 showFirstRunClings();
552 }
Adam Cohen9211d422014-10-07 18:14:53 -0700553 }
554
555 private LauncherCallbacks mLauncherCallbacks;
556
557 public void onPostCreate(Bundle savedInstanceState) {
558 super.onPostCreate(savedInstanceState);
559 if (mLauncherCallbacks != null) {
560 mLauncherCallbacks.onPostCreate(savedInstanceState);
561 }
562 }
563
564 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
565 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700566 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
567 @Override
568 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700569 if (LOGD) {
570 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
571 }
Winson Chung94804152015-05-08 13:06:44 -0700572 mAppsView.setFixedBounds(bounds);
573 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700574 }
575
576 @Override
577 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700578 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
579 // Dismiss All Apps if we aren't already paused/invisible
580 if (!mPaused) {
581 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
582 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
583 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700584 }
Winson Chung0f785722015-04-08 10:27:49 -0700585 }
586 });
Jun Mukai8af0cd82015-05-12 12:32:12 -0700587 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
588 private boolean mImportanceStored = false;
589 private int mWorkspaceImportanceForAccessibility =
590 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
591 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
592
593 @Override
594 public void onSearchOverlayOpened() {
595 if (mImportanceStored) {
596 return;
597 }
598 // The underlying workspace and hotseat are temporarily suppressed by the search
599 // overlay. So they sholudn't be accessible.
600 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
601 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
602 mWorkspace.setImportantForAccessibility(
603 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
604 mHotseat.setImportantForAccessibility(
605 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
606 mImportanceStored = true;
607 }
608
609 @Override
610 public void onSearchOverlayClosed() {
611 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
612 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
613 mImportanceStored = false;
614 }
615 });
Adam Cohen9211d422014-10-07 18:14:53 -0700616 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700617 }
618
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700619 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700620 public void onLauncherProviderChange() {
621 if (mLauncherCallbacks != null) {
622 mLauncherCallbacks.onLauncherProviderChange();
623 }
624 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700625
Adam Cohen9211d422014-10-07 18:14:53 -0700626 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700627 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700628 if (mLauncherCallbacks != null) {
629 return mLauncherCallbacks.hasCustomContentToLeft();
630 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700631 return false;
632 }
633
Dave Hawkeya8881582013-09-17 15:55:33 -0600634 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500635 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600636 * {@link #addToCustomContentPage}. This will only be invoked if
637 * {@link #hasCustomContentToLeft()} is {@code true}.
638 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500639 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700640 if (mLauncherCallbacks != null) {
641 mLauncherCallbacks.populateCustomContentContainer();
642 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600643 }
644
645 /**
646 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
647 * ensure the custom content page is added or removed if necessary.
648 */
649 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600650 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600651 // Not bound yet, wait for bindScreens to be called.
652 return;
653 }
654
655 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
656 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500657 mWorkspace.createCustomContentContainer();
658 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600659 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
660 mWorkspace.removeCustomContentPage();
661 }
662 }
663
Anton Hanssona2f665f2013-09-26 12:18:32 +0100664 public Stats getStats() {
665 return mStats;
666 }
667
Bjorn Bringertc459e522013-06-07 19:36:01 +0100668 public LayoutInflater getInflater() {
669 return mInflater;
670 }
671
Hyunyoung Song3f471442015-04-08 19:01:34 -0700672 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700673 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
674 // that is subsequently removed from the workspace in startBinding().
675 return !mModel.isLoadingWorkspace();
676 }
677
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800678 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000679 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100680 if (Build.VERSION.SDK_INT >= 17) {
681 return View.generateViewId();
682 } else {
683 // View.generateViewId() is not available. The following fallback logic is a copy
684 // of its implementation.
685 for (;;) {
686 final int result = sNextGeneratedId.get();
687 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
688 int newValue = result + 1;
689 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
690 if (sNextGeneratedId.compareAndSet(result, newValue)) {
691 return result;
692 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000693 }
694 }
695 }
696
697 public int getViewIdForItem(ItemInfo info) {
698 // This cast is safe given the > 2B range for int.
699 int itemId = (int) info.id;
700 if (mItemIdToViewId.containsKey(itemId)) {
701 return mItemIdToViewId.get(itemId);
702 }
703 int viewId = generateViewId();
704 mItemIdToViewId.put(itemId, viewId);
705 return viewId;
706 }
707
708 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700709 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
710 * a configuration step, this allows the proper animations to run after other transitions.
711 */
Adam Cohendb364c32014-05-20 14:23:40 -0700712 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700713 long screenId = args.screenId;
714 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
715 // When the screen id represents an actual screen (as opposed to a rank) we make sure
716 // that the drop page actually exists.
717 screenId = ensurePendingDropLayoutExists(args.screenId);
718 }
Adam Cohendb364c32014-05-20 14:23:40 -0700719
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800720 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800721 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700722 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700723 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700724 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800725 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700726 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700727 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700728 case REQUEST_RECONFIGURE_APPWIDGET:
729 completeRestoreAppWidget(args.appWidgetId);
730 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 }
Michael Jurka27614d22012-04-02 06:40:22 -0700732 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
733 // if you turned the screen off and then back while in All Apps, Launcher would not
734 // return to the workspace. Clearing mAddInfo.container here fixes this issue
735 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700736 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800737 }
738
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800739 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700740 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700741 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700742 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700743 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800744 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700745
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 Runnable exitSpringLoaded = new Runnable() {
747 @Override
748 public void run() {
749 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
750 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
751 }
752 };
753
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
757 if (resultCode == RESULT_CANCELED) {
758 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700759 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700761 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800762 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700764 }
765 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200766 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
767 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700768 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200769 }
770 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200772
Adam Cohened66b2b2012-01-23 17:28:51 -0800773 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700774 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700775
Adam Cohendb364c32014-05-20 14:23:40 -0700776 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800777 // We have special handling for widgets
778 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800779 final int appWidgetId;
780 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
781 : -1;
782 if (widgetId < 0) {
783 appWidgetId = pendingAddWidgetId;
784 } else {
785 appWidgetId = widgetId;
786 }
787
Adam Cohenad4e15c2013-10-17 16:21:35 -0700788 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800789 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700790 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
791 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700792 result = RESULT_CANCELED;
793 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700794 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700795 @Override
796 public void run() {
797 exitSpringLoadedDragModeDelayed(false, 0, null);
798 }
799 };
Adam Cohendb364c32014-05-20 14:23:40 -0700800 if (workspaceLocked) {
801 // No need to remove the empty screen if we're mid-binding, as the
802 // the bind will not add the empty screen.
803 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
804 } else {
805 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
806 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
807 }
Winson Chung5aaab772012-04-27 15:24:02 -0700808 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700809 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700810 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
811 // When the screen id represents an actual screen (as opposed to a rank)
812 // we make sure that the drop page actually exists.
813 mPendingAddInfo.screenId =
814 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
815 }
Adam Cohendb364c32014-05-20 14:23:40 -0700816 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
817
818 dropLayout.setDropPending(true);
819 final Runnable onComplete = new Runnable() {
820 @Override
821 public void run() {
822 completeTwoStageWidgetDrop(resultCode, appWidgetId);
823 dropLayout.setDropPending(false);
824 }
825 };
826 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
827 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
828 } else {
829 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
830 mPendingAddInfo);
831 sPendingAddItem = args;
832 }
Winson Chung5aaab772012-04-27 15:24:02 -0700833 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800834 return;
835 }
836
Sunny Goyalff572272014-07-23 13:58:07 -0700837 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
838 if (resultCode == RESULT_OK) {
839 // Update the widget view.
840 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
841 pendingAddWidgetId, mPendingAddInfo);
842 if (workspaceLocked) {
843 sPendingAddItem = args;
844 } else {
845 completeAdd(args);
846 }
847 }
848 // Leave the widget in the pending state if the user canceled the configure.
849 return;
850 }
851
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 // The pattern used here is that a user PICKs a specific application,
853 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700854
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
856 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700857 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700858 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
859 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800860 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700861 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800862 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700864 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
865 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 }
Adam Cohen00074722013-10-11 17:46:09 -0700867 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700868 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700869 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800872
873 }
874
875 @Override
876 protected void onActivityResult(
877 final int requestCode, final int resultCode, final Intent data) {
878 handleActivityResult(requestCode, resultCode, data);
879 if (mLauncherCallbacks != null) {
880 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
881 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800882 }
883
Adam Cohendb364c32014-05-20 14:23:40 -0700884 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
885 appWidgetId, ItemInfo info) {
886 PendingAddArguments args = new PendingAddArguments();
887 args.requestCode = requestCode;
888 args.intent = data;
889 args.container = info.container;
890 args.screenId = info.screenId;
891 args.cellX = info.cellX;
892 args.cellY = info.cellY;
893 args.appWidgetId = appWidgetId;
894 return args;
895 }
896
897 /**
898 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
899 *
900 * @param screenId the screen id to check
901 * @return the new screen, or screenId if it exists
902 */
903 private long ensurePendingDropLayoutExists(long screenId) {
904 CellLayout dropLayout =
905 (CellLayout) mWorkspace.getScreenWithId(screenId);
906 if (dropLayout == null) {
907 // it's possible that the add screen was removed because it was
908 // empty and a re-bind occurred
909 mWorkspace.addExtraEmptyScreen();
910 return mWorkspace.commitExtraEmptyScreen();
911 } else {
912 return screenId;
913 }
914 }
915
Adam Cohen091440a2015-03-18 14:16:05 -0700916 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700917 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700918 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 Runnable onCompleteRunnable = null;
920 int animationType = 0;
921
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700922 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 if (resultCode == RESULT_OK) {
924 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
925 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700926 mPendingAddWidgetInfo);
927 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 onCompleteRunnable = new Runnable() {
929 @Override
930 public void run() {
931 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700932 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700933 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
934 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800935 }
936 };
937 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800938 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800940 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700941 if (mDragLayer.getAnimatedView() != null) {
942 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
943 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
944 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700945 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700946 // The animated view may be null in the case of a rotation during widget configuration
947 onCompleteRunnable.run();
948 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 }
950
951 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700952 protected void onStop() {
953 super.onStop();
954 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700955
956 if (mLauncherCallbacks != null) {
957 mLauncherCallbacks.onStop();
958 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700959 }
960
961 @Override
962 protected void onStart() {
963 super.onStart();
964 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700965
966 if (mLauncherCallbacks != null) {
967 mLauncherCallbacks.onStart();
968 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700969 }
970
971 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200973 long startTime = 0;
974 if (DEBUG_RESUME_TIME) {
975 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400976 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200977 }
Adam Cohen9211d422014-10-07 18:14:53 -0700978
979 if (mLauncherCallbacks != null) {
980 mLauncherCallbacks.preOnResume();
981 }
982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700984
Winson Chung4a2afa32012-07-19 14:53:05 -0700985 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700986 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700987 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800988 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700989 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700990 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700991 // view after launching an app, as they may be depending on the UI to be static to
992 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700993 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -0700994 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800995 } else if (mOnResumeState == State.WIDGETS) {
996 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700997 }
998 mOnResumeState = State.NONE;
999
Winson Chungcd99cd32015-04-29 11:03:24 -07001000 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001001 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1002 // Otherwise, notify the callbacks if we are in all apps mode
1003 if (mState == State.APPS) {
1004 if (mLauncherCallbacks != null) {
1005 mLauncherCallbacks.onAllAppsShown();
1006 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001007 }
1008 }
1009
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001010 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001011 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1012 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001013
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001014 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001015 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001016 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001017 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001019 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001021 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001022 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1023 // execute them here
1024 long startTimeCallbacks = 0;
1025 if (DEBUG_RESUME_TIME) {
1026 startTimeCallbacks = System.currentTimeMillis();
1027 }
1028
Michael Jurka1e2f4652013-07-08 18:03:46 -07001029 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1030 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001031 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001032 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001033 if (DEBUG_RESUME_TIME) {
1034 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1035 (System.currentTimeMillis() - startTimeCallbacks));
1036 }
Michael Jurka447bf842013-05-15 14:52:15 +02001037 }
Michael Jurka54554252013-08-01 12:52:23 +02001038 if (mOnResumeCallbacks.size() > 0) {
1039 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1040 mOnResumeCallbacks.get(i).run();
1041 }
1042 mOnResumeCallbacks.clear();
1043 }
Winson Chunge4e50662012-01-23 14:45:13 -08001044
1045 // Reset the pressed state of icons that were locked in the press state while activities
1046 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001047 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001048 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001049 mWaitingForResume.setStayPressed(false);
1050 }
Winson Chung82963d52013-10-09 11:20:57 -07001051
Adam Cohen06dff352012-06-01 17:17:08 -07001052 // It is possible that widgets can receive updates while launcher is not in the foreground.
1053 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1054 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1055 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001056 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001057 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001058
Michael Jurka447bf842013-05-15 14:52:15 +02001059 if (DEBUG_RESUME_TIME) {
1060 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1061 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001062
1063 if (mWorkspace.getCustomContentCallbacks() != null) {
1064 // If we are resuming and the custom content is the current page, we call onShow().
1065 // It is also poassible that onShow will instead be called slightly after first layout
1066 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1067 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001068 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001069 }
1070 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001071 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001072 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001073
Sunny Goyal756adbc2015-04-16 15:20:51 -07001074 if (!isWorkspaceLoading()) {
1075 // Process any items that were added while Launcher was away.
1076 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1077 }
Adam Cohen9211d422014-10-07 18:14:53 -07001078
1079 if (mLauncherCallbacks != null) {
1080 mLauncherCallbacks.onResume();
1081 }
Adam Cohen06dff352012-06-01 17:17:08 -07001082 }
1083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001085 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001086 // Ensure that items added to Launcher are queued until Launcher returns
1087 InstallShortcutReceiver.enableInstallQueue();
1088
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001089 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001090 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001091 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001092 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001093
1094 // We call onHide() aggressively. The custom content callbacks should be able to
1095 // debounce excess onHide calls.
1096 if (mWorkspace.getCustomContentCallbacks() != null) {
1097 mWorkspace.getCustomContentCallbacks().onHide();
1098 }
Romain Guycbb89e42009-06-08 15:52:54 -07001099
Adam Cohen9211d422014-10-07 18:14:53 -07001100 if (mLauncherCallbacks != null) {
1101 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001102 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001103 }
1104
1105 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001106 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1107 // by a onResume or by scrolling otherwise.
1108 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001109
1110 // Custom content is completely hidden
1111 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001112
1113 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1114 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001115
1116 // Indicates whether the user is allowed to scroll away from the custom content.
1117 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001118 }
1119
Adam Cohenc2d6e892014-10-16 09:49:24 -07001120 public interface LauncherOverlay {
1121
1122 /**
1123 * Touch interaction leading to overscroll has begun
1124 */
1125 public void onScrollInteractionBegin();
1126
1127 /**
1128 * Touch interaction related to overscroll has ended
1129 */
1130 public void onScrollInteractionEnd();
1131
1132 /**
1133 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1134 * screen (or in the case of RTL, the rightmost screen).
1135 */
1136 public void onScrollChange(int progress, boolean rtl);
1137
1138 /**
1139 * Screen has stopped scrolling
1140 */
1141 public void onScrollSettled();
1142
1143 /**
1144 * This method can be called by the Launcher in order to force the LauncherOverlay
1145 * to exit fully immersive mode.
1146 */
1147 public void forceExitFullImmersion();
1148 }
1149
Winson Chung0f785722015-04-08 10:27:49 -07001150 public interface LauncherAppsCallbacks {
1151 /**
1152 * Updates launcher to the available space that AllApps can take so as not to overlap with
1153 * any other views.
1154 */
1155 public void onAllAppsBoundsChanged(Rect bounds);
1156
1157 /**
1158 * Called to dismiss all apps if it is showing.
1159 */
1160 public void dismissAllApps();
1161 }
1162
Jun Mukai8af0cd82015-05-12 12:32:12 -07001163 public interface LauncherSearchCallbacks {
1164 /**
1165 * Called when the search overlay is shown.
1166 */
1167 public void onSearchOverlayOpened();
1168
1169 /**
1170 * Called when the search overlay is dismissed.
1171 */
1172 public void onSearchOverlayClosed();
1173 }
1174
Adam Cohenc2d6e892014-10-16 09:49:24 -07001175 public interface LauncherOverlayCallbacks {
1176 /**
1177 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1178 * however it doesn't modify any state within the launcher.
1179 */
1180 public boolean canEnterFullImmersion();
1181
1182 /**
1183 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1184 * eg. by occupying the full screen and handling all touch events.
1185 *
1186 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1187 * case, Launcher will modify any necessary state and assumes the overlay is
1188 * handling all interaction. If false, the LauncherOverlay should cancel any
1189 *
1190 */
1191 public boolean enterFullImmersion();
1192
1193 /**
1194 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1195 * full control over UI and state.
1196 */
1197 public void exitFullImmersion();
1198 }
1199
1200 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1201
1202 @Override
1203 public boolean canEnterFullImmersion() {
1204 return mState == State.WORKSPACE;
1205 }
1206
1207 @Override
1208 public boolean enterFullImmersion() {
1209 if (mState == State.WORKSPACE) {
1210 // When fully immersed, disregard any touches which fall through.
1211 mDragLayer.setBlockTouch(true);
1212 return true;
1213 }
1214 return false;
1215 }
1216
1217 @Override
1218 public void exitFullImmersion() {
1219 mDragLayer.setBlockTouch(false);
1220 }
1221 }
1222
Jorim Jaggid017f882014-01-14 17:08:48 -08001223 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001224 if (mLauncherCallbacks != null) {
1225 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001226 } else {
1227 // On devices with a locked orientation, we will at least have the allow rotation
1228 // setting.
1229 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001230 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001231 }
1232
Adam Cohen9211d422014-10-07 18:14:53 -07001233 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001234 CustomContentCallbacks callbacks, String description) {
1235 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001236 }
1237
Adam Cohenedb40762013-07-18 16:45:45 -07001238 // The custom content needs to offset its content to account for the QSB
1239 public int getTopOffsetForCustomContent() {
1240 return mWorkspace.getPaddingTop();
1241 }
1242
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001243 @Override
1244 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001245 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001246 if (mModel.isCurrentCallbacks(this)) {
1247 mModel.stopLoader();
1248 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001249 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1250
Romain Guy13c2e7b2010-03-10 19:45:00 -08001251 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001252 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001253
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001254 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001255 @Override
1256 public void onWindowFocusChanged(boolean hasFocus) {
1257 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001258 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001259
1260 if (mLauncherCallbacks != null) {
1261 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1262 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001263 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 private boolean acceptFilter() {
1266 final InputMethodManager inputManager = (InputMethodManager)
1267 getSystemService(Context.INPUT_METHOD_SERVICE);
1268 return !inputManager.isFullscreenMode();
1269 }
1270
1271 @Override
1272 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001273 final int uniChar = event.getUnicodeChar();
1274 final boolean handled = super.onKeyDown(keyCode, event);
1275 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1276 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1278 keyCode, event);
1279 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001280 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001281 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001282 // showSearchDialog()
1283 // If there are multiple keystrokes before the search dialog takes focus,
1284 // onSearchRequested() will be called for every keystroke,
1285 // but it is idempotent, so it's fine.
1286 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
1288 }
1289
Joe Onorato8a9625e2010-01-28 15:55:35 -08001290 // Eat the long press event so the keyboard doesn't come up.
1291 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1292 return true;
1293 }
1294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 return handled;
1296 }
1297
Karl Rosaen138a0412009-04-23 19:00:21 -07001298 private String getTypedText() {
1299 return mDefaultKeySsb.toString();
1300 }
1301
1302 private void clearTypedText() {
1303 mDefaultKeySsb.clear();
1304 mDefaultKeySsb.clearSpans();
1305 Selection.setSelection(mDefaultKeySsb, 0);
1306 }
1307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001309 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1310 * State
1311 */
1312 private static State intToState(int stateOrdinal) {
1313 State state = State.WORKSPACE;
1314 final State[] stateValues = State.values();
1315 for (int i = 0; i < stateValues.length; i++) {
1316 if (stateValues[i].ordinal() == stateOrdinal) {
1317 state = stateValues[i];
1318 break;
1319 }
1320 }
1321 return state;
1322 }
1323
1324 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 * Restores the previous state, if it exists.
1326 *
1327 * @param savedState The previous state.
1328 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001329 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 private void restoreState(Bundle savedState) {
1331 if (savedState == null) {
1332 return;
1333 }
1334
Michael Jurka883f55b2010-10-21 15:47:14 -07001335 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001336 if (state == State.APPS || state == State.WIDGETS) {
1337 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001338 }
1339
Adam Cohen21cd0022013-10-09 18:57:02 -07001340 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1341 PagedView.INVALID_RESTORE_PAGE);
1342 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001343 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345
Winson Chung3d503fb2011-07-13 17:25:49 -07001346 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001347 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001348
Winson Chung3d503fb2011-07-13 17:25:49 -07001349 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1350 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001351 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001352 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1353 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001354 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1355 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001356 AppWidgetProviderInfo info = savedState.getParcelable(
1357 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001358 mPendingAddWidgetInfo = info == null ?
1359 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1360
Adam Cohen4637b5a2013-11-04 18:21:24 -08001361 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001362 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 mRestoring = true;
1364 }
1365
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001366 mItemIdToViewId = (HashMap<Integer, Integer>)
1367 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 }
1369
1370 /**
1371 * Finds all the views we need and configure them properly.
1372 */
1373 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001374 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001375
Craig Mautner360310b2012-10-26 15:13:08 -07001376 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001377 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001378 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1379 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001380 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001381 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001382
John Spurlock77e1f472013-09-11 10:09:51 -04001383 mLauncherView.setSystemUiVisibility(
1384 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001385 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386
Winson Chung4c98d922011-05-31 16:50:48 -07001387 // Setup the drag layer
1388 mDragLayer.setup(this, dragController);
1389
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 // Setup the hotseat
1391 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1392 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001393 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001394 }
1395
Jorim Jaggid017f882014-01-14 17:08:48 -08001396 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001397 View widgetButton = findViewById(R.id.widget_button);
1398 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001399 @Override
1400 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001401 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001402 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001403 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001404 }
1405 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001406 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1407
1408 View wallpaperButton = findViewById(R.id.wallpaper_button);
1409 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001410 @Override
1411 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001412 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001413 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001414 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001415 }
1416 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001417 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1418
1419 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001420 if (hasSettings()) {
1421 settingsButton.setOnClickListener(new OnClickListener() {
1422 @Override
1423 public void onClick(View arg0) {
1424 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001425 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001426 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001427 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001428 });
1429 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1430 } else {
1431 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001432 }
1433
Adam Cohendbdff6b2013-09-18 19:09:15 -07001434 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001435
Winson Chung4c98d922011-05-31 16:50:48 -07001436 // Setup the workspace
1437 mWorkspace.setHapticFeedbackEnabled(false);
1438 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001439 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001440 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001441
Winson Chungf0ea4d32011-06-06 14:27:16 -07001442 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001443 mSearchDropTargetBar = (SearchDropTargetBar)
1444 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001445
Winson Chungb745afb2015-03-02 11:51:23 -08001446 // Setup Apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001447 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001448 if (isAllAppsSearchOverridden()) {
1449 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001450 }
Winson Chungb745afb2015-03-02 11:51:23 -08001451
Winson Chungf0ea4d32011-06-06 14:27:16 -07001452 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001453 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001454
Winson Chung3d503fb2011-07-13 17:25:49 -07001455 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001456 dragController.setDragScoller(mWorkspace);
1457 dragController.setScrollView(mDragLayer);
1458 dragController.setMoveTarget(mWorkspace);
1459 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001460 if (mSearchDropTargetBar != null) {
1461 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001462 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001463 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001464
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001465 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001466 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001467 mWeightWatcher = new WeightWatcher(this);
1468 mWeightWatcher.setAlpha(0.5f);
1469 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001470 new FrameLayout.LayoutParams(
1471 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001472 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001473 Gravity.BOTTOM)
1474 );
Adam Cohen39a06042013-07-19 14:30:12 -07001475
1476 boolean show = shouldShowWeightWatcher();
1477 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480
1481 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001482 * Sets the all apps button. This method is called from {@link Hotseat}.
1483 */
1484 public void setAllAppsButton(View allAppsButton) {
1485 mAllAppsButton = allAppsButton;
1486 }
1487
1488 public View getAllAppsButton() {
1489 return mAllAppsButton;
1490 }
1491
1492 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 * Creates a view representing a shortcut.
1494 *
1495 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001497 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001498 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
1500
1501 /**
1502 * Creates a view representing a shortcut inflated from the specified resource.
1503 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 * @param parent The group the shortcut belongs to.
1505 * @param info The data structure describing the shortcut.
1506 *
1507 * @return A View inflated from layoutResId.
1508 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001509 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001510 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001511 parent, false);
1512 favorite.applyFromShortcutInfo(info, mIconCache);
1513 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001515 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 return favorite;
1517 }
1518
1519 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 * Add a shortcut to the workspace.
1521 *
1522 * @param data The intent describing the shortcut.
1523 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001525 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001526 int cellY) {
1527 int[] cellXY = mTmpAddItemCellCoordinates;
1528 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001529 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001530
Sunny Goyal5c97f512015-05-19 16:03:28 -07001531 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001532 if (info == null) {
1533 return;
1534 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001535 final View view = createShortcut(info);
1536
Sunny Goyal5c97f512015-05-19 16:03:28 -07001537 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001538 // First we check if we already know the exact location where we want to add this item.
1539 if (cellX >= 0 && cellY >= 0) {
1540 cellXY[0] = cellX;
1541 cellXY[1] = cellY;
1542 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001543
1544 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001545 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001546 true, null,null)) {
1547 return;
1548 }
1549 DragObject dragObject = new DragObject();
1550 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001551 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1552 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001553 return;
1554 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001555 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001556 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001557 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001558 foundCellSpan = (result != null);
1559 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001560 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001561 }
1562
1563 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001564 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001565 return;
1566 }
1567
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001568 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569
1570 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001571 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001572 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574 }
1575
Adam Cohen2e6da152015-05-06 11:42:25 -07001576 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1577 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001578 // We want to account for the extra amount of padding that we are adding to the widget
1579 // to ensure that it gets the full amount of space that it has requested
1580 int requiredWidth = minWidth + padding.left + padding.right;
1581 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001582 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001583 }
1584
Adam Cohen2e6da152015-05-06 11:42:25 -07001585 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1586 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001587 }
1588
Adam Cohen2e6da152015-05-06 11:42:25 -07001589 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1590 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001591 }
1592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001594 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001596 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 */
Adam Cohen091440a2015-03-18 14:16:05 -07001598 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001599 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1600
1601 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001602 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001603 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1604 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001605 }
Romain Guycbb89e42009-06-08 15:52:54 -07001606
Adam Cohen59400422014-03-05 18:07:04 -08001607 if (appWidgetInfo.isCustomWidget) {
1608 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001609 }
1610
Adam Cohen59400422014-03-05 18:07:04 -08001611 LauncherAppWidgetInfo launcherInfo;
1612 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1613 launcherInfo.spanX = info.spanX;
1614 launcherInfo.spanY = info.spanY;
1615 launcherInfo.minSpanX = info.minSpanX;
1616 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001617 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001618
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001620 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621
1622 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001623 if (hostView == null) {
1624 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001625 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1626 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001627 } else {
1628 // The AppWidgetHostView has already been inflated and instantiated
1629 launcherInfo.hostView = hostView;
1630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001632 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001633 launcherInfo.notifyWidgetSizeChanged(this);
1634
Adam Cohen59400422014-03-05 18:07:04 -08001635 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1636 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001637
1638 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001640 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 }
Romain Guycbb89e42009-06-08 15:52:54 -07001642
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1644 @Override
1645 public void onReceive(Context context, Intent intent) {
1646 final String action = intent.getAction();
1647 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1648 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001649 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001650 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001651
Winson Chungc100e8e2011-08-09 16:02:43 -07001652 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001653 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001654 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001655 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001656 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001657 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001658 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1659 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001660 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001661 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1662 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001663 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001664 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1665 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1666 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001667 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001668 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1669 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001670 }
1671 }
1672 };
1673
1674 @Override
1675 public void onAttachedToWindow() {
1676 super.onAttachedToWindow();
1677
1678 // Listen for broadcasts related to user-presence
1679 final IntentFilter filter = new IntentFilter();
1680 filter.addAction(Intent.ACTION_SCREEN_OFF);
1681 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001682 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001683 if (ENABLE_DEBUG_INTENTS) {
1684 filter.addAction(DebugIntents.DELETE_DATABASE);
1685 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1686 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001687 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001688 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001689 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001690 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001691 mVisible = true;
1692 }
1693
Adam Cohen0b395352014-06-09 22:54:36 +00001694 /**
1695 * Sets up transparent navigation and status bars in LMP.
1696 * This method is a no-op for other platform versions.
1697 */
Sunny Goyald0091012015-01-20 15:55:34 -08001698 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001699 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001700 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001701 Window window = getWindow();
1702 window.getAttributes().systemUiVisibility |=
1703 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1704 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1705 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1706 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1707 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1708 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1709 window.setStatusBarColor(Color.TRANSPARENT);
1710 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001711 }
1712 }
1713
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 @Override
1715 public void onDetachedFromWindow() {
1716 super.onDetachedFromWindow();
1717 mVisible = false;
1718
Adam Cohend113e0c2010-11-11 10:48:05 -08001719 if (mAttached) {
1720 unregisterReceiver(mReceiver);
1721 mAttached = false;
1722 }
Winson Chungb745afb2015-03-02 11:51:23 -08001723 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 }
1725
1726 public void onWindowVisibilityChanged(int visibility) {
1727 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001728 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001729 // The following code used to be in onResume, but it turns out onResume is called when
1730 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1731 // is a more appropriate event to handle
1732 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001733 if (!mWorkspaceLoading) {
1734 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001735 // We want to let Launcher draw itself at least once before we force it to build
1736 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001737 // apps is nice and speedy.
1738 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001739 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001740 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001741 if (mStarted) return;
1742 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001743 // We delay the layer building a bit in order to give
1744 // other message processing a time to run. In particular
1745 // this avoids a delay in hiding the IME if it was
1746 // currently shown, because doing that may involve
1747 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001748 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001749 final ViewTreeObserver.OnDrawListener listener = this;
1750 mWorkspace.post(new Runnable() {
1751 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001752 if (mWorkspace != null &&
1753 mWorkspace.getViewTreeObserver() != null) {
1754 mWorkspace.getViewTreeObserver().
1755 removeOnDrawListener(listener);
1756 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001757 }
1758 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001759 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001760 }
1761 });
1762 }
1763 clearTypedText();
1764 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 }
1766
Adam Cohen091440a2015-03-18 14:16:05 -07001767 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 mHandler.removeMessages(ADVANCE_MSG);
1769 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1770 mHandler.sendMessageDelayed(msg, delay);
1771 mAutoAdvanceSentTime = System.currentTimeMillis();
1772 }
1773
Adam Cohen091440a2015-03-18 14:16:05 -07001774 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1776 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1777 mAutoAdvanceRunning = autoAdvanceRunning;
1778 if (autoAdvanceRunning) {
1779 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1780 sendAdvanceMessage(delay);
1781 } else {
1782 if (!mWidgetsToAdvance.isEmpty()) {
1783 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1784 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1785 }
1786 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001787 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001788 }
1789 }
1790 }
1791
1792 private final Handler mHandler = new Handler() {
1793 @Override
1794 public void handleMessage(Message msg) {
1795 if (msg.what == ADVANCE_MSG) {
1796 int i = 0;
1797 for (View key: mWidgetsToAdvance.keySet()) {
1798 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1799 final int delay = mAdvanceStagger * i;
1800 if (v instanceof Advanceable) {
1801 postDelayed(new Runnable() {
1802 public void run() {
1803 ((Advanceable) v).advance();
1804 }
1805 }, delay);
1806 }
1807 i++;
1808 }
1809 sendAdvanceMessage(mAdvanceInterval);
1810 }
1811 }
1812 };
1813
1814 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001815 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001816 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1817 if (v instanceof Advanceable) {
1818 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001819 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001820 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001821 }
1822 }
1823
1824 void removeWidgetToAutoAdvance(View hostView) {
1825 if (mWidgetsToAdvance.containsKey(hostView)) {
1826 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001827 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001828 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001829 }
1830
Joe Onorato9c1289c2009-08-17 11:03:03 -04001831 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001832 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833 launcherInfo.hostView = null;
1834 }
1835
Hyunyoung Song3f471442015-04-08 19:01:34 -07001836 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001837 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1838 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001839 }
1840
Winson Chunga6945242014-01-08 14:04:34 -08001841 public DragLayer getDragLayer() {
1842 return mDragLayer;
1843 }
1844
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001845 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001846 return mAppsView;
1847 }
1848
Hyunyoung Song3f471442015-04-08 19:01:34 -07001849 public WidgetsContainerView getWidgetsView() {
1850 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001851 }
1852
Winson Chunga6945242014-01-08 14:04:34 -08001853 public Workspace getWorkspace() {
1854 return mWorkspace;
1855 }
1856
1857 public Hotseat getHotseat() {
1858 return mHotseat;
1859 }
1860
Jorim Jaggid017f882014-01-14 17:08:48 -08001861 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001862 return mOverviewPanel;
1863 }
1864
1865 public SearchDropTargetBar getSearchBar() {
1866 return mSearchDropTargetBar;
1867 }
1868
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001869 public LauncherAppWidgetHost getAppWidgetHost() {
1870 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
Romain Guycbb89e42009-06-08 15:52:54 -07001872
Winson Chunga9abd0e2010-10-27 17:18:37 -07001873 public LauncherModel getModel() {
1874 return mModel;
1875 }
1876
Winson Chunga6945242014-01-08 14:04:34 -08001877 protected SharedPreferences getSharedPrefs() {
1878 return mSharedPrefs;
1879 }
1880
Adam Cohen2e6da152015-05-06 11:42:25 -07001881 public DeviceProfile getDeviceProfile() {
1882 return mDeviceProfile;
1883 }
1884
Bjorn Bringertc459e522013-06-07 19:36:01 +01001885 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001886 getWindow().closeAllPanels();
1887
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001888 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001889 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001890 }
1891
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 @Override
1893 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001894 long startTime = 0;
1895 if (DEBUG_RESUME_TIME) {
1896 startTime = System.currentTimeMillis();
1897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 super.onNewIntent(intent);
1899
1900 // Close the menu
1901 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001902 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001903 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001904
Adam Cohened307df2013-10-02 09:37:31 -07001905 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1906 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1907 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001908
Adam Cohen6fecd412013-10-02 17:41:50 -07001909 if (mWorkspace == null) {
1910 // Can be cases where mWorkspace is null, this prevents a NPE
1911 return;
1912 }
1913 Folder openFolder = mWorkspace.getOpenFolder();
1914 // In all these cases, only animate if we're already on home
1915 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001916
1917 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1918 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001919 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001920 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001921 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001922 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001923
Adam Cohen6fecd412013-10-02 17:41:50 -07001924 closeFolder();
1925 exitSpringLoadedDragMode();
1926
1927 // If we are already on home, then just animate back to the workspace,
1928 // otherwise, just wait until onResume to set the state back to Workspace
1929 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001930 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001931 } else {
1932 mOnResumeState = State.WORKSPACE;
1933 }
1934
1935 final View v = getWindow().peekDecorView();
1936 if (v != null && v.getWindowToken() != null) {
1937 InputMethodManager imm = (InputMethodManager)getSystemService(
1938 INPUT_METHOD_SERVICE);
1939 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1940 }
1941
Winson Chungb745afb2015-03-02 11:51:23 -08001942 // Reset the apps view
1943 if (!alreadyOnHome && mAppsView != null) {
1944 mAppsView.scrollToTop();
1945 }
1946
Hyunyoung Song3f471442015-04-08 19:01:34 -07001947 // Reset the widgets view
1948 if (!alreadyOnHome && mWidgetsView != null) {
1949 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001950 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001951
Adam Cohen9211d422014-10-07 18:14:53 -07001952 if (mLauncherCallbacks != null) {
1953 mLauncherCallbacks.onHomeIntent();
1954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
Adam Cohened307df2013-10-02 09:37:31 -07001956
Michael Jurka447bf842013-05-15 14:52:15 +02001957 if (DEBUG_RESUME_TIME) {
1958 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960
Adam Cohen9211d422014-10-07 18:14:53 -07001961 if (mLauncherCallbacks != null) {
1962 mLauncherCallbacks.onNewIntent(intent);
1963 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001964 }
1965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001967 public void onRestoreInstanceState(Bundle state) {
1968 super.onRestoreInstanceState(state);
1969 for (int page: mSynchronouslyBoundPages) {
1970 mWorkspace.restoreInstanceStateForChild(page);
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 @Override
1975 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001976 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001977 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1978 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001979 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001980 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981
Michael Jurka883f55b2010-10-21 15:47:14 -07001982 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001983 // We close any open folder since it will not be re-opened, and we need to make sure
1984 // this state is reflected.
1985 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001988 mWaitingForResult) {
1989 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1992 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001993 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1994 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1995 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001996 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
1998
Hyunyoung Song3f471442015-04-08 19:01:34 -07001999 // Save the current widgets tray?
2000 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002001 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002002
2003 if (mLauncherCallbacks != null) {
2004 mLauncherCallbacks.onSaveInstanceState(outState);
2005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 }
2007
2008 @Override
2009 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002011
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002012 if (mPreferenceObserverRegistered) {
2013 getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
2014 Context.MODE_MULTI_PROCESS).unregisterOnSharedPreferenceChangeListener(
2015 mSettingsObserver);
2016 mPreferenceObserverRegistered = false;
2017 }
2018
Winson Chunge7a03942011-08-05 15:05:12 -07002019 // Remove all pending runnables
2020 mHandler.removeMessages(ADVANCE_MSG);
2021 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002022 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002023
Winson Chungcd2b0142011-06-08 16:02:26 -07002024 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002025 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002026
2027 // It's possible to receive onDestroy after a new Launcher activity has
2028 // been created. In this case, don't interfere with the new Launcher.
2029 if (mModel.isCurrentCallbacks(this)) {
2030 mModel.stopLoader();
2031 app.setLauncher(null);
2032 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002035 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002037 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002039 mAppWidgetHost = null;
2040
2041 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042
2043 TextKeyListener.getInstance().release();
2044
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002045 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002046 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002047
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002048 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002049 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002050 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002051 mWorkspace = null;
2052 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002053
Michael Jurka2ecf9952012-06-18 12:52:28 -07002054 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002055
2056 if (mLauncherCallbacks != null) {
2057 mLauncherCallbacks.onDestroy();
2058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
2060
Adam Cohena9cf38f2011-05-02 15:36:58 -07002061 public DragController getDragController() {
2062 return mDragController;
2063 }
2064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 @Override
2066 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002067 onStartForResult(requestCode);
2068 super.startActivityForResult(intent, requestCode);
2069 }
2070
2071 @Override
2072 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2073 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2074 onStartForResult(requestCode);
2075 try {
2076 super.startIntentSenderForResult(intent, requestCode,
2077 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2078 } catch (IntentSender.SendIntentException e) {
2079 throw new ActivityNotFoundException();
2080 }
2081 }
2082
2083 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002084 if (requestCode >= 0) {
2085 setWaitingForResult(true);
2086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088
Winson Chung70d72102011-08-12 11:24:35 -07002089 /**
2090 * Indicates that we want global search for this activity by setting the globalSearch
2091 * argument for {@link #startSearch} to true.
2092 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002094 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002096
Karl Rosaen138a0412009-04-23 19:00:21 -07002097 if (initialQuery == null) {
2098 // Use any text typed in the launcher as the initial query
2099 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 if (appSearchData == null) {
2102 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002103 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 }
Winson Chungadf0c182012-08-23 14:59:07 -07002105 Rect sourceBounds = new Rect();
2106 if (mSearchDropTargetBar != null) {
2107 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2108 }
Romain Guycbb89e42009-06-08 15:52:54 -07002109
Ian Parkinson047036e2014-09-01 15:40:53 +01002110 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002111 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002112 if (clearTextImmediately) {
2113 clearTypedText();
2114 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002115
2116 // We need to show the workspace after starting the search
2117 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002118 }
2119
Ian Parkinson047036e2014-09-01 15:40:53 +01002120 /**
2121 * Start a text search.
2122 *
2123 * @return {@code true} if the search will start immediately, so any further keypresses
2124 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2125 * to buffer keypresses.
2126 */
2127 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002128 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002129 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2130 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2131 sourceBounds);
2132 }
2133
Michael Jurkaa33411c2012-06-14 16:18:21 -07002134 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002135 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002136 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002137 }
2138
2139 /**
2140 * Starts the global search activity. This code is a copied from SearchManager
2141 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002142 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002143 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002144 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002145 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2146 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2147 if (globalSearchActivity == null) {
2148 Log.w(TAG, "No global search activity found.");
2149 return;
2150 }
2151 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2152 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2153 intent.setComponent(globalSearchActivity);
2154 // Make sure that we have a Bundle to put source in
2155 if (appSearchData == null) {
2156 appSearchData = new Bundle();
2157 } else {
2158 appSearchData = new Bundle(appSearchData);
2159 }
Adam Cohen9211d422014-10-07 18:14:53 -07002160 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002161 if (!appSearchData.containsKey("source")) {
2162 appSearchData.putString("source", getPackageName());
2163 }
2164 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2165 if (!TextUtils.isEmpty(initialQuery)) {
2166 intent.putExtra(SearchManager.QUERY, initialQuery);
2167 }
2168 if (selectInitialQuery) {
2169 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2170 }
2171 intent.setSourceBounds(sourceBounds);
2172 try {
2173 startActivity(intent);
2174 } catch (ActivityNotFoundException ex) {
2175 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 }
2178
Yura4f93ec62014-02-04 14:15:21 +00002179 public boolean isOnCustomContent() {
2180 return mWorkspace.isOnOrMovingToCustomContent();
2181 }
2182
Winson Chung70d72102011-08-12 11:24:35 -07002183 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002184 public boolean onPrepareOptionsMenu(Menu menu) {
2185 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002186 if (!isOnCustomContent()) {
2187 // Close any open folders
2188 closeFolder();
2189 // Stop resizing any widgets
2190 mWorkspace.exitWidgetResizeMode();
2191 if (!mWorkspace.isInOverviewMode()) {
2192 // Show the overview mode
2193 showOverviewMode(true);
2194 } else {
2195 showWorkspace(true);
2196 }
Winson Chunge0298742014-01-17 12:03:00 -08002197 }
Adam Cohen9211d422014-10-07 18:14:53 -07002198 if (mLauncherCallbacks != null) {
2199 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2200 }
2201
Michael Jurkab94f3f82013-09-11 18:08:54 +02002202 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002203 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002205 @Override
2206 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002207 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002208 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002209 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002210 }
2211
Joe Onorato9c1289c2009-08-17 11:03:03 -04002212 public boolean isWorkspaceLocked() {
2213 return mWorkspaceLoading || mWaitingForResult;
2214 }
2215
Adam Cohen517a7f52014-03-01 12:12:59 -08002216 public boolean isWorkspaceLoading() {
2217 return mWorkspaceLoading;
2218 }
2219
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002220 private void setWorkspaceLoading(boolean value) {
2221 boolean isLocked = isWorkspaceLocked();
2222 mWorkspaceLoading = value;
2223 if (isLocked != isWorkspaceLocked()) {
2224 onWorkspaceLockedChanged();
2225 }
2226 }
2227
2228 private void setWaitingForResult(boolean value) {
2229 boolean isLocked = isWorkspaceLocked();
2230 mWaitingForResult = value;
2231 if (isLocked != isWorkspaceLocked()) {
2232 onWorkspaceLockedChanged();
2233 }
2234 }
2235
Adam Cohen9211d422014-10-07 18:14:53 -07002236 protected void onWorkspaceLockedChanged() {
2237 if (mLauncherCallbacks != null) {
2238 mLauncherCallbacks.onWorkspaceLockedChanged();
2239 }
2240 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002241
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002243 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002245 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2246 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002247 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002248 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002249 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002250
Sunny Goyale6b63a32015-01-16 16:14:29 -08002251 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002252 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002253 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2254 }
2255
Sunny Goyale6b63a32015-01-16 16:14:29 -08002256 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002257 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2258 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002259 if (appWidgetInfo.configure != null) {
2260 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002261 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002262
Bjorn Bringert7984c942009-12-09 15:38:25 +00002263 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002264 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2265 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002268 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002269 Runnable onComplete = new Runnable() {
2270 @Override
2271 public void run() {
2272 // Exit spring loaded mode if necessary after adding the widget
2273 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2274 null);
2275 }
2276 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002277 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002278 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002279 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 }
2281 }
Romain Guycbb89e42009-06-08 15:52:54 -07002282
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002283 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002284 // Close any folders that may be open.
2285 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002286 mWorkspace.moveToCustomContentScreen(animate);
2287 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002288
2289 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2290 int[] cell, int spanX, int spanY) {
2291 switch (info.itemType) {
2292 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2293 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2294 int span[] = new int[2];
2295 span[0] = spanX;
2296 span[1] = spanY;
2297 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2298 container, screenId, cell, span);
2299 break;
2300 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2301 processShortcutFromDrop(info.componentName, container, screenId, cell);
2302 break;
2303 default:
2304 throw new IllegalStateException("Unknown item type: " + info.itemType);
2305 }
2306 }
2307
Adam Cohenfbba09b2011-07-18 21:43:05 -07002308 /**
2309 * Process a shortcut drop.
2310 *
2311 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002312 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002313 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002315 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2316 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002317 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002318 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002319 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002320 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002321
2322 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002323 mPendingAddInfo.cellX = cell[0];
2324 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002326
2327 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2328 createShortcutIntent.setComponent(componentName);
2329 processShortcut(createShortcutIntent);
2330 }
2331
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 /**
2333 * Process a widget drop.
2334 *
2335 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002336 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002337 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002338 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002339 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2340 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002343 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002344 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002345 mPendingAddInfo.minSpanX = info.minSpanX;
2346 mPendingAddInfo.minSpanY = info.minSpanY;
2347
Adam Cohenfbba09b2011-07-18 21:43:05 -07002348 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002349 mPendingAddInfo.cellX = cell[0];
2350 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002352 if (span != null) {
2353 mPendingAddInfo.spanX = span[0];
2354 mPendingAddInfo.spanY = span[1];
2355 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002356
Adam Cohened66b2b2012-01-23 17:28:51 -08002357 AppWidgetHostView hostView = info.boundWidget;
2358 int appWidgetId;
2359 if (hostView != null) {
2360 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002361 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002362 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002363 // In this case, we either need to start an activity to get permission to bind
2364 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002365 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002366 Bundle options = info.bindOptions;
2367
Sunny Goyalffe83f12014-08-14 17:39:34 -07002368 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2369 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002370 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002371 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002372 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002373 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002374 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2375 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2376 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002377 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2378 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002379 // TODO: we need to make sure that this accounts for the options bundle.
2380 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002381 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2382 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002383 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002384 }
2385
Joe Onoratodeb98af2010-02-19 14:59:39 -08002386 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002387 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 }
2389
Winson Chung24ab2f12010-09-16 14:10:47 -07002390 void processWallpaper(Intent intent) {
2391 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2392 }
2393
Adam Cohendcd297f2013-06-18 13:13:40 -07002394 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002395 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002396 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 folderInfo.title = getText(R.string.folder_name);
2398
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002400 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2401 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002402 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403
2404 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002405 FolderIcon newFolder =
2406 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002407 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002408 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002409 // Force measure the new folder icon
2410 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2411 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002412 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 }
Romain Guycbb89e42009-06-08 15:52:54 -07002414
Joe Onorato9c1289c2009-08-17 11:03:03 -04002415 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002416 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002417 }
2418
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002419 /**
2420 * Registers various content observers. The current implementation registers
2421 * only a favorites observer to keep track of the favorites applications.
2422 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002423 private void registerContentObservers() {
2424 ContentResolver resolver = getContentResolver();
2425 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2426 true, mWidgetObserver);
2427 }
2428
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 @Override
2430 public boolean dispatchKeyEvent(KeyEvent event) {
2431 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2432 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002434 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002435 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002436 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002437 dumpState();
2438 return true;
2439 }
2440 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002441 }
2442 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2443 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002444 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445 return true;
2446 }
2447 }
2448
2449 return super.dispatchKeyEvent(event);
2450 }
2451
Joe Onorato88ec0992009-11-19 13:16:06 -08002452 @Override
2453 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002454 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2455 return;
2456 }
2457
Winson Chung3d9490a2015-03-24 17:38:42 -07002458 LauncherAccessibilityDelegate delegate =
2459 LauncherAppState.getInstance().getAccessibilityDelegate();
2460 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002461 return;
2462 }
2463
Winson Chungb745afb2015-03-02 11:51:23 -08002464 if (isAppsViewVisible()) {
2465 showWorkspace(true);
2466 } else if (isWidgetsViewVisible()) {
2467 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002468 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002469 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002470 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002471 Folder openFolder = mWorkspace.getOpenFolder();
2472 if (openFolder.isEditingName()) {
2473 openFolder.dismissEditingName();
2474 } else {
2475 closeFolder();
2476 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002477 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002478 mWorkspace.exitWidgetResizeMode();
2479
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002480 // Back button is a no-op here, but give at least some feedback for the button press
2481 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002482 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002483 }
2484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002486 * Re-listen when widgets are reset.
2487 */
Adam Cohen091440a2015-03-18 14:16:05 -07002488 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002489 if (mAppWidgetHost != null) {
2490 mAppWidgetHost.startListening();
2491 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002492 }
2493
2494 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 * Launches the intent referred by the clicked shortcut.
2496 *
2497 * @param v The view representing the clicked shortcut.
2498 */
2499 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002500 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2501 // view has detached (it's possible for this to happen if the view is removed mid touch).
2502 if (v.getWindowToken() == null) {
2503 return;
2504 }
2505
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002506 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002507 return;
2508 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002509
Adam Cohen1697b792013-09-17 19:08:21 -07002510 if (v instanceof Workspace) {
2511 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002512 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002513 }
2514 return;
2515 }
2516
2517 if (v instanceof CellLayout) {
2518 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002519 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002520 }
2521 }
2522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002524 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002525 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002527 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002528 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002529 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002530 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002531 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002532 } else if (tag instanceof AppInfo) {
2533 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002534 } else if (tag instanceof LauncherAppWidgetInfo) {
2535 if (v instanceof PendingAppWidgetHostView) {
2536 onClickPendingWidget((PendingAppWidgetHostView) v);
2537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 }
2539 }
2540
Sunny Goyal508da152014-08-14 10:53:27 -07002541 public void onClickPagedViewIcon(View v) {
2542 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002543 if (mLauncherCallbacks != null) {
2544 mLauncherCallbacks.onClickPagedViewIcon(v);
2545 }
Sunny Goyal508da152014-08-14 10:53:27 -07002546 }
2547
Sunny Goyal70660032015-05-14 00:07:08 -07002548 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002549 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002550 return false;
2551 }
2552
Michael Jurkaaf442092010-06-10 17:01:57 -07002553 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002554 * Event handler for the app widget view which has not fully restored.
2555 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002556 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002557 if (mIsSafeModeEnabled) {
2558 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2559 return;
2560 }
2561
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002562 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002563 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002564 int widgetId = info.appWidgetId;
2565 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2566 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002567 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2568 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002569 mPendingAddInfo.copyFrom(info);
2570 mPendingAddWidgetId = widgetId;
2571
Sunny Goyalffe83f12014-08-14 17:39:34 -07002572 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2573 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002574 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 } else if (info.installProgress < 0) {
2576 // The install has not been queued
2577 final String packageName = info.providerName.getPackageName();
2578 showBrokenAppInstallDialog(packageName,
2579 new DialogInterface.OnClickListener() {
2580 public void onClick(DialogInterface dialog, int id) {
2581 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2582 }
2583 });
2584 } else {
2585 // Download has started.
2586 final String packageName = info.providerName.getPackageName();
2587 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002588 }
2589 }
2590
2591 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002592 * Event handler for the "grid" button that appears on the home screen, which
2593 * enters all apps mode.
2594 *
2595 * @param v The view that was clicked.
2596 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002597 protected void onClickAllAppsButton(View v) {
2598 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002599 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002600 showWorkspace(true);
2601 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002602 // Try and refresh the set of predicted apps before we enter launcher
2603 showAppsView(true /* animated */, false /* resetListToTop */,
2604 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002605 }
2606 }
2607
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002608 private void showBrokenAppInstallDialog(final String packageName,
2609 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002610 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002611 .setTitle(R.string.abandoned_promises_title)
2612 .setMessage(R.string.abandoned_promise_explanation)
2613 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2614 .setNeutralButton(R.string.abandoned_clean_this,
2615 new DialogInterface.OnClickListener() {
2616 public void onClick(DialogInterface dialog, int id) {
2617 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2618 mWorkspace.removeAbandonedPromise(packageName, user);
2619 }
2620 })
2621 .create().show();
2622 return;
2623 }
2624
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 /**
2626 * Event handler for an app shortcut click.
2627 *
2628 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2629 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002630 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002631 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2632 Object tag = v.getTag();
2633 if (!(tag instanceof ShortcutInfo)) {
2634 throw new IllegalArgumentException("Input must be a Shortcut");
2635 }
2636
2637 // Open shortcut
2638 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002639
2640 if (shortcut.isDisabled != 0) {
2641 int error = R.string.activity_not_available;
2642 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2643 error = R.string.safemode_shortcut_error;
2644 }
2645 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2646 return;
2647 }
2648
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 final Intent intent = shortcut.intent;
2650
2651 // Check for special shortcuts
2652 if (intent.getComponent() != null) {
2653 final String shortcutClass = intent.getComponent().getClassName();
2654
2655 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2656 MemoryDumpActivity.startDump(this);
2657 return;
2658 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2659 toggleShowWeightWatcher();
2660 return;
2661 }
2662 }
2663
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002665 if ((v instanceof BubbleTextView)
2666 && shortcut.isPromise()
2667 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002668 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002669 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 new DialogInterface.OnClickListener() {
2671 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002672 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 return;
2676 }
2677
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002678 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002679 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002680
2681 if (mLauncherCallbacks != null) {
2682 mLauncherCallbacks.onClickAppShortcut(v);
2683 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 }
2685
Adam Cohen091440a2015-03-18 14:16:05 -07002686 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002688 final ShortcutInfo shortcut;
2689 final Intent intent;
2690 if (tag instanceof ShortcutInfo) {
2691 shortcut = (ShortcutInfo) tag;
2692 intent = shortcut.intent;
2693 int[] pos = new int[2];
2694 v.getLocationOnScreen(pos);
2695 intent.setSourceBounds(new Rect(pos[0], pos[1],
2696 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002697
Sunny Goyal508da152014-08-14 10:53:27 -07002698 } else if (tag instanceof AppInfo) {
2699 shortcut = null;
2700 intent = ((AppInfo) tag).intent;
2701 } else {
2702 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2703 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704
2705 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706 mStats.recordLaunch(intent, shortcut);
2707
2708 if (success && v instanceof BubbleTextView) {
2709 mWaitingForResume = (BubbleTextView) v;
2710 mWaitingForResume.setStayPressed(true);
2711 }
2712 }
2713
2714 /**
2715 * Event handler for a folder icon click.
2716 *
2717 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2718 */
2719 protected void onClickFolderIcon(View v) {
2720 if (LOGD) Log.d(TAG, "onClickFolder");
2721 if (!(v instanceof FolderIcon)){
2722 throw new IllegalArgumentException("Input must be a FolderIcon");
2723 }
2724
2725 FolderIcon folderIcon = (FolderIcon) v;
2726 final FolderInfo info = folderIcon.getFolderInfo();
2727 Folder openFolder = mWorkspace.getFolderForTag(info);
2728
2729 // If the folder info reports that the associated folder is open, then verify that
2730 // it is actually opened. There have been a few instances where this gets out of sync.
2731 if (info.opened && openFolder == null) {
2732 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2733 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2734 info.opened = false;
2735 }
2736
2737 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2738 // Close any open folder
2739 closeFolder();
2740 // Open the requested folder
2741 openFolder(folderIcon);
2742 } else {
2743 // Find the open folder...
2744 int folderScreen;
2745 if (openFolder != null) {
2746 folderScreen = mWorkspace.getPageForView(openFolder);
2747 // .. and close it
2748 closeFolder(openFolder);
2749 if (folderScreen != mWorkspace.getCurrentPage()) {
2750 // Close any folder open on the current screen
2751 closeFolder();
2752 // Pull the folder onto this screen
2753 openFolder(folderIcon);
2754 }
2755 }
2756 }
Adam Cohen9211d422014-10-07 18:14:53 -07002757
2758 if (mLauncherCallbacks != null) {
2759 mLauncherCallbacks.onClickFolderIcon(v);
2760 }
Michael Jurka5130e402011-10-13 04:55:35 -07002761 }
2762
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002763 /**
2764 * Event handler for the (Add) Widgets button that appears after a long press
2765 * on the home screen.
2766 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002767 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002768 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002769 if (mIsSafeModeEnabled) {
2770 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2771 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002772 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002773 if (mLauncherCallbacks != null) {
2774 mLauncherCallbacks.onClickAddWidgetButton(view);
2775 }
Adam Cohen9211d422014-10-07 18:14:53 -07002776 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002777 }
2778
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002779 /**
2780 * Event handler for the wallpaper picker button that appears after a long press
2781 * on the home screen.
2782 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002783 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002784 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002785 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2786 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002787
2788 if (mLauncherCallbacks != null) {
2789 mLauncherCallbacks.onClickWallpaperPicker(v);
2790 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002791 }
2792
2793 /**
2794 * Event handler for a click on the settings button that appears after a long press
2795 * on the home screen.
2796 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002797 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002798 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002799 if (mLauncherCallbacks != null) {
2800 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002801 } else {
2802 showSettingsActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002803 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002804 }
2805
Michael Jurka5130e402011-10-13 04:55:35 -07002806 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002807 // Provide the same haptic feedback that the system offers for virtual keys.
2808 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002809 }
2810
Adam Cohen61f560d2013-09-30 15:58:20 -07002811 public void performHapticFeedbackOnTouchDown(View v) {
2812 // Provide the same haptic feedback that the system offers for virtual keys.
2813 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2814 }
2815
2816 public View.OnTouchListener getHapticFeedbackTouchListener() {
2817 if (mHapticFeedbackTouchListener == null) {
2818 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002819 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002820 @Override
2821 public boolean onTouch(View v, MotionEvent event) {
2822 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2823 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2824 }
2825 return false;
2826 }
2827 };
2828 }
2829 return mHapticFeedbackTouchListener;
2830 }
2831
Adam Cohen9211d422014-10-07 18:14:53 -07002832 public void onDragStarted(View view) {
2833 if (isOnCustomContent()) {
2834 // Custom content screen doesn't participate in drag and drop. If on custom
2835 // content screen, move to default.
2836 moveWorkspaceToDefaultScreen();
2837 }
2838
2839 if (mLauncherCallbacks != null) {
2840 mLauncherCallbacks.onDragStarted(view);
2841 }
2842 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002843
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002844 /**
2845 * Called when the user stops interacting with the launcher.
2846 * This implies that the user is now on the homescreen and is not doing housekeeping.
2847 */
Adam Cohen9211d422014-10-07 18:14:53 -07002848 protected void onInteractionEnd() {
2849 if (mLauncherCallbacks != null) {
2850 mLauncherCallbacks.onInteractionEnd();
2851 }
2852 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002853
2854 /**
2855 * Called when the user starts interacting with the launcher.
2856 * The possible interactions are:
2857 * - open all apps
2858 * - reorder an app shortcut, or a widget
2859 * - open the overview mode.
2860 * This is a good time to stop doing things that only make sense
2861 * when the user is on the homescreen and not doing housekeeping.
2862 */
Adam Cohen9211d422014-10-07 18:14:53 -07002863 protected void onInteractionBegin() {
2864 if (mLauncherCallbacks != null) {
2865 mLauncherCallbacks.onInteractionBegin();
2866 }
2867 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002868
Winson Chungcd99cd32015-04-29 11:03:24 -07002869 /** Updates the interaction state. */
2870 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002871 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002872 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002873 boolean fromStateWithOverlay;
2874 boolean toStateWithOverlay;
2875 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2876 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2877 toStateWithOverlay = toState != Workspace.State.NORMAL;
2878 } else {
2879 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2880 fromState != Workspace.State.NORMAL_HIDDEN;
2881 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2882 toState != Workspace.State.NORMAL_HIDDEN;
2883 }
2884 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002885 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002886 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002887 onInteractionEnd();
2888 }
2889 }
2890
Kenny Guyf07af7b2014-07-31 11:39:16 +01002891 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002892 try {
2893 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002894 launcherApps.showAppDetailsForProfile(componentName, user);
2895 } catch (SecurityException e) {
2896 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2897 Log.e(TAG, "Launcher does not have permission to launch settings");
2898 } catch (ActivityNotFoundException e) {
2899 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2900 Log.e(TAG, "Unable to launch settings");
2901 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002902 }
2903
Michael Jurka1e2f4652013-07-08 18:03:46 -07002904 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002905 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2906 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002907 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002908 // System applications cannot be installed. For now, show a toast explaining that.
2909 // We may give them the option of disabling apps this way.
2910 int messageId = R.string.uninstall_system_app_text;
2911 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002912 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002913 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002914 String packageName = componentName.getPackageName();
2915 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002916 Intent intent = new Intent(
2917 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002918 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2919 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002920 if (user != null) {
2921 user.addToIntent(intent, Intent.EXTRA_USER);
2922 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002923 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002924 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002925 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002926 }
2927
Michael Jurka86a720e2012-05-09 11:23:23 -07002928 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002929 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002930 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002931 // Only launch using the new animation if the shortcut has not opted out (this is a
2932 // private contract between launcher and may be ignored in the future).
2933 boolean useLaunchAnimation = (v != null) &&
2934 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002935 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2936 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002937
Kenny Guy1317e2d2014-05-08 18:52:50 +01002938 UserHandleCompat user = null;
2939 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2940 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2941 user = userManager.getUserForSerialNumber(serialNumber);
2942 }
2943
2944 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002945 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002946 ActivityOptions opts = null;
2947 if (sClipRevealMethod != null) {
2948 // TODO: call method directly when Launcher3 can depend on M APIs
2949 int left = 0, top = 0;
2950 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2951 if (v instanceof TextView) {
2952 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002953 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2954 if (icon != null) {
2955 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002956 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002957 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002958 width = bounds.width();
2959 height = bounds.height();
2960 }
2961 }
2962 try {
2963 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2964 left, top, width, height);
2965 } catch (IllegalAccessException e) {
2966 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2967 sClipRevealMethod = null;
2968 } catch (InvocationTargetException e) {
2969 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2970 sClipRevealMethod = null;
2971 }
2972 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002973 if (opts == null && !Utilities.isLmpOrAbove()) {
2974 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002975 v.getMeasuredWidth(), v.getMeasuredHeight());
2976 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002977 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002978 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002979
2980 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2981 // Could be launching some bookkeeping activity
2982 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002983 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002984 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002985 launcherApps.startActivityForProfile(intent.getComponent(), user,
2986 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002987 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002988 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 } catch (SecurityException e) {
2990 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002991 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002992 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002993 "or use the exported attribute for this activity. "
2994 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002996 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002998
Michael Jurka86a720e2012-05-09 11:23:23 -07002999 boolean startActivitySafely(View v, Intent intent, Object tag) {
3000 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003001 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3002 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3003 return false;
3004 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003005 try {
3006 success = startActivity(v, intent, tag);
3007 } catch (ActivityNotFoundException e) {
3008 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3009 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3010 }
3011 return success;
3012 }
3013
Adam Cohen268c4752012-06-06 17:47:33 -07003014 /**
3015 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3016 * in the DragLayer in the exact absolute location of the original FolderIcon.
3017 */
3018 private void copyFolderIconToImage(FolderIcon fi) {
3019 final int width = fi.getMeasuredWidth();
3020 final int height = fi.getMeasuredHeight();
3021
3022 // Lazy load ImageView, Bitmap and Canvas
3023 if (mFolderIconImageView == null) {
3024 mFolderIconImageView = new ImageView(this);
3025 }
3026 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3027 mFolderIconBitmap.getHeight() != height) {
3028 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3029 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3030 }
3031
3032 DragLayer.LayoutParams lp;
3033 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3034 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3035 } else {
3036 lp = new DragLayer.LayoutParams(width, height);
3037 }
3038
Adam Cohen307fe232012-08-16 17:55:58 -07003039 // The layout from which the folder is being opened may be scaled, adjust the starting
3040 // view size by this scale factor.
3041 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003042 lp.customPosition = true;
3043 lp.x = mRectForFolderAnimation.left;
3044 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003045 lp.width = (int) (scale * width);
3046 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003047
3048 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3049 fi.draw(mFolderIconCanvas);
3050 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003051 if (fi.getFolder() != null) {
3052 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3053 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003054 }
Adam Cohen268c4752012-06-06 17:47:33 -07003055 // Just in case this image view is still in the drag layer from a previous animation,
3056 // we remove it and re-add it.
3057 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3058 mDragLayer.removeView(mFolderIconImageView);
3059 }
3060 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003061 if (fi.getFolder() != null) {
3062 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003063 }
Adam Cohen268c4752012-06-06 17:47:33 -07003064 }
3065
Adam Cohen2801caf2011-05-13 20:57:39 -07003066 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003067 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003068 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3069 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3070 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3071
Adam Cohenc51934b2011-07-26 21:07:43 -07003072 FolderInfo info = (FolderInfo) fi.getTag();
3073 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3074 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003075 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3076 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003077 }
3078
Adam Cohen268c4752012-06-06 17:47:33 -07003079 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3080 copyFolderIconToImage(fi);
3081 fi.setVisibility(View.INVISIBLE);
3082
Michael Jurka2ecf9952012-06-18 12:52:28 -07003083 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003084 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003085 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003086 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3087 }
3088 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003089 oa.start();
3090 }
3091
Adam Cohen268c4752012-06-06 17:47:33 -07003092 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003093 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003094 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3095 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3096 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3097
Adam Cohen268c4752012-06-06 17:47:33 -07003098 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003099
Adam Cohen268c4752012-06-06 17:47:33 -07003100 // We remove and re-draw the FolderIcon in-case it has changed
3101 mDragLayer.removeView(mFolderIconImageView);
3102 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003103 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003104 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003105 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003106 oa.addListener(new AnimatorListenerAdapter() {
3107 @Override
3108 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003109 if (cl != null) {
3110 cl.clearFolderLeaveBehind();
3111 // Remove the ImageView copy of the FolderIcon and make the original visible.
3112 mDragLayer.removeView(mFolderIconImageView);
3113 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003114 }
3115 }
3116 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003117 oa.start();
3118 }
3119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003121 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 * is animated relative to the specified View. If the View is null, no animation
3123 * is played.
3124 *
3125 * @param folderInfo The FolderInfo describing the folder to open.
3126 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003127 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003128 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003129 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3130 if (openFolder != null && openFolder != folder) {
3131 // Close any open folder before opening a folder.
3132 closeFolder();
3133 }
3134
Adam Cohena9cf38f2011-05-02 15:36:58 -07003135 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136
Adam Cohena9cf38f2011-05-02 15:36:58 -07003137 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138
Sunny Goyalf4066152015-04-15 09:42:19 -07003139 // While the folder is open, the position of the icon cannot change.
3140 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3141
Adam Cohen4554ee12011-08-03 16:13:21 -07003142 // Just verify that the folder hasn't already been added to the DragLayer.
3143 // There was a one-off crash where the folder had a parent already.
3144 if (folder.getParent() == null) {
3145 mDragLayer.addView(folder);
3146 mDragController.addDropTarget((DropTarget) folder);
3147 } else {
3148 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3149 folder.getParent() + ").");
3150 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003151 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003152 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003153
3154 // Notify the accessibility manager that this folder "window" has appeared and occluded
3155 // the workspace items
3156 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3157 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003158 }
3159
3160 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003161 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003162 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003163 if (folder.isEditingName()) {
3164 folder.dismissEditingName();
3165 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003166 closeFolder(folder);
3167 }
3168 }
3169
Sunny Goyal83a8f042015-05-19 12:52:12 -07003170 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003171 folder.getInfo().opened = false;
3172
3173 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3174 if (parent != null) {
3175 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3176 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003177 if (fi != null) {
3178 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3179 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003180 }
3181 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003182
3183 // Notify the accessibility manager that this folder "window" has disappeard and no
3184 // longer occludeds the workspace items
3185 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 }
3187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003189 if (!isDraggingEnabled()) return false;
3190 if (isWorkspaceLocked()) return false;
3191 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192
Adam Cohen1697b792013-09-17 19:08:21 -07003193 if (v instanceof Workspace) {
3194 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003195 if (!mWorkspace.isTouchActive()) {
3196 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003197 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3198 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3199 return true;
3200 } else {
3201 return false;
3202 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003203 } else {
3204 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003205 }
Adam Cohen1697b792013-09-17 19:08:21 -07003206 }
3207
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003208 CellLayout.CellInfo longClickCellInfo = null;
3209 View itemUnderLongClick = null;
3210 if (v.getTag() instanceof ItemInfo) {
3211 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003212 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003213 itemUnderLongClick = longClickCellInfo.cell;
3214 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003215 }
3216
Winson Chung3d503fb2011-07-13 17:25:49 -07003217 // The hotseat touch handling does not go through Workspace, and we always allow long press
3218 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003219 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003220 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003221 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003222 // User long pressed on empty space
3223 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3224 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003225 if (mWorkspace.isInOverviewMode()) {
3226 mWorkspace.startReordering(v);
3227 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003228 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003231 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3232 mHotseat.getOrderInHotseat(
3233 longClickCellInfo.cellX,
3234 longClickCellInfo.cellY));
3235 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003236 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003237 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 }
3239 }
3240 }
3241 return true;
3242 }
3243
Winson Chung3d503fb2011-07-13 17:25:49 -07003244 boolean isHotseatLayout(View layout) {
3245 return mHotseat != null && layout != null &&
3246 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3247 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003248
Winson Chung3d503fb2011-07-13 17:25:49 -07003249 /**
3250 * Returns the CellLayout of the specified container at the specified screen.
3251 */
Sunny Goyal92820592015-03-02 11:31:35 -08003252 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003253 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3254 if (mHotseat != null) {
3255 return mHotseat.getLayout();
3256 } else {
3257 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003258 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003259 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003260 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003261 }
3262 }
3263
Winson Chungb745afb2015-03-02 11:51:23 -08003264 /**
3265 * For overridden classes.
3266 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003267 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003268 return isAppsViewVisible();
3269 }
3270
3271 public boolean isAppsViewVisible() {
3272 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3273 }
3274
3275 public boolean isWidgetsViewVisible() {
3276 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003277 }
3278
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003279 private void setWorkspaceBackground(int background) {
3280 switch (background) {
3281 case WORKSPACE_BACKGROUND_TRANSPARENT:
3282 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3283 break;
3284 case WORKSPACE_BACKGROUND_BLACK:
3285 getWindow().setBackgroundDrawable(null);
3286 break;
3287 default:
3288 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3289 }
Craig Mautner360310b2012-10-26 15:13:08 -07003290 }
3291
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003292 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003293 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3294 int curflags = getWindow().getAttributes().flags
3295 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3296 if (wpflags != curflags) {
3297 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3298 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003299 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003300 }
3301
Michael Jurkae326f182011-11-21 14:05:46 -08003302 @Override
3303 public void onTrimMemory(int level) {
3304 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003305 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3306 // The widget preview db can result in holding onto over
3307 // 3MB of memory for caching which isn't necessary.
3308 SQLiteDatabase.releaseMemory();
3309
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003310 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003311 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003312 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003313 if (mLauncherCallbacks != null) {
3314 mLauncherCallbacks.onTrimMemory(level);
3315 }
Michael Jurkae326f182011-11-21 14:05:46 -08003316 }
3317
Winson Chungb745afb2015-03-02 11:51:23 -08003318 @Override
3319 public void onStateTransitionHideSearchBar() {
3320 // Hide the search bar
3321 if (mSearchDropTargetBar != null) {
3322 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3323 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003324 }
3325
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003326 public void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003327 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3328 true);
Adam Cohened307df2013-10-02 09:37:31 -07003329 }
3330
Sunny Goyal83a8f042015-05-19 12:52:12 -07003331 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003332 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3333 onCompleteRunnable, true);
3334 }
3335
3336 protected void showWorkspace(int snapToPage, boolean animated) {
3337 showWorkspace(snapToPage, animated, null, true);
3338 }
3339
3340 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3341 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003342 boolean changed = mState != State.WORKSPACE ||
3343 mWorkspace.getState() != Workspace.State.NORMAL;
3344 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003345 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003346 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003347 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003348 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003349
Winson Chungc7d2b602012-05-16 17:02:20 -07003350 // Show the search bar (only animate if we were showing the drop target bar in spring
3351 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003352 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003353 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003354 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003355
Michael Jurkab3e22d92011-10-31 15:58:33 -07003356 // Set focus to the AppsCustomize button
3357 if (mAllAppsButton != null) {
3358 mAllAppsButton.requestFocus();
3359 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003360 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003361
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003362 // Change the state *after* we've called all the transition code
3363 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003364
Winson Chung5fb63472011-02-02 17:03:37 -08003365 // Resume the auto-advance of widgets
3366 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003367 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003368
Winson Chung0f785722015-04-08 10:27:49 -07003369 if (changed) {
3370 // Send an accessibility event to announce the context change
3371 getWindow().getDecorView()
3372 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003373 if (notifyLauncherCallbacks) {
3374 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003375 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003376 mLauncherCallbacks.onAllAppsHidden();
3377 }
3378 }
Winson Chung0f785722015-04-08 10:27:49 -07003379 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003380 }
3381
Adam Cohened307df2013-10-02 09:37:31 -07003382 void showOverviewMode(boolean animated) {
3383 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003384 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003385 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3386 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003387 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003388 }
3389
Winson Chungb745afb2015-03-02 11:51:23 -08003390 /**
3391 * Shows the apps view.
3392 */
Winson Chung4ac30062015-05-08 17:34:17 -07003393 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003394 if (resetListToTop) {
3395 mAppsView.scrollToTop();
3396 }
Winson Chung4ac30062015-05-08 17:34:17 -07003397 if (updatePredictedApps) {
3398 tryAndUpdatePredictedApps();
3399 }
Winson Chungb745afb2015-03-02 11:51:23 -08003400 showAppsOrWidgets(animated, State.APPS);
3401 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003402
Winson Chungb745afb2015-03-02 11:51:23 -08003403 /**
3404 * Shows the widgets view.
3405 */
3406 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003407 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003408 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003409 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003410 }
Winson Chungb745afb2015-03-02 11:51:23 -08003411 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003412
3413 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003414 @Override
3415 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003416 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003417 }
3418 });
Winson Chungb745afb2015-03-02 11:51:23 -08003419 }
3420
3421 /**
3422 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003423 *
3424 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003425 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003426 // TODO: calling method should use the return value so that when {@code false} is returned
3427 // the workspace transition doesn't fall into invalid state.
3428 private boolean showAppsOrWidgets(boolean animated, State toState) {
3429 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3430 mState != State.WIDGETS_SPRING_LOADED) {
3431 return false;
3432 }
3433 if (toState != State.APPS && toState != State.WIDGETS) {
3434 return false;
3435 }
Winson Chungb745afb2015-03-02 11:51:23 -08003436
3437 if (toState == State.APPS) {
3438 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003439 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003440 mLauncherCallbacks.onAllAppsShown();
3441 }
Winson Chungb745afb2015-03-02 11:51:23 -08003442 } else {
3443 mStateTransitionAnimation.startAnimationToWidgets(animated);
3444 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003445
Michael Jurkab3e22d92011-10-31 15:58:33 -07003446 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003447 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003448
3449 // Pause the auto-advance of widgets until we are out of AllApps
3450 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003451 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003452 closeFolder();
3453
3454 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003455 getWindow().getDecorView()
3456 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003457 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003458 }
3459
Winson Chungcd99cd32015-04-29 11:03:24 -07003460 /**
3461 * Updates the workspace and interaction state on state change, and return the animation to this
3462 * new state.
3463 */
3464 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3465 boolean animated, HashMap<View, Integer> layerViews) {
3466 Workspace.State fromState = mWorkspace.getState();
3467 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3468 updateInteraction(fromState, toState);
3469 return anim;
3470 }
3471
Hyunyoung Song3f471442015-04-08 19:01:34 -07003472 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003473 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003474 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3475 mState == State.WIDGETS_SPRING_LOADED) {
3476 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003477 }
Winson Chungb745afb2015-03-02 11:51:23 -08003478
3479 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003480 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3481 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003482 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003483 }
Adam Cohen7777d962011-08-18 18:58:38 -07003484
Hyunyoung Song3f471442015-04-08 19:01:34 -07003485 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003486 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003487 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003488
Winson Chungcd99cd32015-04-29 11:03:24 -07003489 if (successfulDrop) {
3490 // We need to trigger all apps hidden to notify search to update itself before the
3491 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003492 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003493 mLauncherCallbacks.onAllAppsHidden();
3494 }
3495 }
3496
Winson Chunge7a03942011-08-05 15:05:12 -07003497 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003498 @Override
3499 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003500 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003501 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3502 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003503 // Before we show workspace, hide all apps again because
3504 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3505 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003506 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003507 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003508 } else {
3509 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003510 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003511 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003512 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003513 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003514
Michael Jurkad3ef3062010-11-23 16:23:58 -08003515 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003516 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003517 showAppsView(true /* animated */, false /* resetListToTop */,
3518 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003519 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003520 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003521 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003522 }
3523
Winson Chung4ac30062015-05-08 17:34:17 -07003524 /**
3525 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3526 * resumed.
3527 */
3528 private void tryAndUpdatePredictedApps() {
3529 if (mLauncherCallbacks != null) {
3530 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3531 if (!apps.isEmpty()) {
3532 mAppsView.setPredictedApps(apps);
3533 }
3534 }
3535 }
3536
Michael Jurkab3e22d92011-10-31 15:58:33 -07003537 void lockAllApps() {
3538 // TODO
3539 }
3540
3541 void unlockAllApps() {
3542 // TODO
3543 }
3544
Sunny Goyal594d76d2014-11-06 10:12:54 -08003545 protected void disableVoiceButtonProxy(boolean disable) {
3546 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003547 }
3548
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003549 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003550 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3551 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003552 }
3553
Adam Cohen24ce0b32014-01-14 16:18:14 -08003554 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003555 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3556 if (searchProvider == null) {
3557 return null;
3558 }
3559
3560 Bundle opts = new Bundle();
3561 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003562 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003563
3564 SharedPreferences sp = getSharedPreferences(
3565 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3566 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003567 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003568 if (!searchProvider.provider.flattenToString().equals(
3569 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003570 || (widgetInfo == null)
3571 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003572 // A valid widget is not already bound.
3573 if (widgetId > -1) {
3574 mAppWidgetHost.deleteAppWidgetId(widgetId);
3575 widgetId = -1;
3576 }
3577
3578 // Try to bind a new widget
3579 widgetId = mAppWidgetHost.allocateAppWidgetId();
3580
3581 if (!AppWidgetManagerCompat.getInstance(this)
3582 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3583 mAppWidgetHost.deleteAppWidgetId(widgetId);
3584 widgetId = -1;
3585 }
3586
3587 sp.edit()
3588 .putInt(QSB_WIDGET_ID, widgetId)
3589 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3590 .commit();
3591 }
3592
3593 if (widgetId != -1) {
3594 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3595 mQsb.updateAppWidgetOptions(opts);
3596 mQsb.setPadding(0, 0, 0, 0);
3597 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003598 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003599 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003600 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003601 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003602 }
3603
Sunny Goyal22235bc2015-04-03 09:23:43 -07003604 private void reinflateQSBIfNecessary() {
3605 if (mQsb instanceof LauncherAppWidgetHostView &&
3606 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3607 mSearchDropTargetBar.removeView(mQsb);
3608 mQsb = null;
3609 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3610 }
3611 }
3612
Michael Jurkad7c28052012-04-27 15:43:36 -07003613 @Override
3614 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003615 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003616 final List<CharSequence> text = event.getText();
3617 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003618 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003619 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003620 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003621 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003622 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003623 } else {
3624 text.add(getString(R.string.all_apps_home_button_label));
3625 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003626 return result;
3627 }
3628
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003629 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003630 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003631 */
Adam Cohen091440a2015-03-18 14:16:05 -07003632 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003633 @Override
3634 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003635 closeSystemDialogs();
3636 }
3637 }
3638
3639 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003640 * Receives notifications whenever the appwidgets are reset.
3641 */
3642 private class AppWidgetResetObserver extends ContentObserver {
3643 public AppWidgetResetObserver() {
3644 super(new Handler());
3645 }
3646
3647 @Override
3648 public void onChange(boolean selfChange) {
3649 onAppWidgetReset();
3650 }
3651 }
3652
3653 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003654 * If the activity is currently paused, signal that we need to run the passed Runnable
3655 * in onResume.
3656 *
3657 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003658 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3659 * wrong when we're not running, and if the activity comes back to what the configuration was
3660 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003661 *
3662 * Implementation of the method from LauncherModel.Callbacks.
3663 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003664 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003665 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003666 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003667 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003668 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003669 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003670 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003671 }
3672 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003673 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003674 return true;
3675 } else {
3676 return false;
3677 }
3678 }
3679
Michael Jurkac402cd92013-05-20 15:49:32 +02003680 private boolean waitUntilResume(Runnable run) {
3681 return waitUntilResume(run, false);
3682 }
3683
Michael Jurka1e2f4652013-07-08 18:03:46 -07003684 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003685 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003686 }
3687
Michael Jurka7607c2f2013-04-03 14:33:19 -07003688 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003689 * If the activity is currently paused, signal that we need to re-run the loader
3690 * in onResume.
3691 *
3692 * This needs to be called from incoming places where resources might have been loaded
3693 * while we are paused. That is becaues the Configuration might be wrong
3694 * when we're not running, and if it comes back to what it was when we
3695 * were paused, we are not restarted.
3696 *
3697 * Implementation of the method from LauncherModel.Callbacks.
3698 *
3699 * @return true if we are currently paused. The caller might be able to
3700 * skip some work in that case since we will come back again.
3701 */
3702 public boolean setLoadOnResume() {
3703 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003704 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003705 mOnResumeNeedsLoad = true;
3706 return true;
3707 } else {
3708 return false;
3709 }
3710 }
3711
3712 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003713 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003714 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003716 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003717 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003718 } else {
3719 return SCREEN_COUNT / 2;
3720 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003721 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003722
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 /**
3724 * Refreshes the shortcuts shown on the workspace.
3725 *
3726 * Implementation of the method from LauncherModel.Callbacks.
3727 */
3728 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003729 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003730
Michael Jurka7607c2f2013-04-03 14:33:19 -07003731 // If we're starting binding all over again, clear any bind calls we'd postponed in
3732 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3733 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003734 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003735
Winson Chungd64d1762013-08-20 14:37:16 -07003736 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003737 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003738 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003739
Michael Jurka05bf644e2011-11-30 20:28:53 -08003740 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003741 if (mHotseat != null) {
3742 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003743 }
3744 }
3745
Adam Cohendcd297f2013-06-18 13:13:40 -07003746 @Override
3747 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003748 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003749
Adam Cohen5084cba2013-09-03 12:01:16 -07003750 // If there are no screens, we need to have an empty screen
3751 if (orderedScreenIds.size() == 0) {
3752 mWorkspace.addExtraEmptyScreen();
3753 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003754
3755 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003756 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003757 if (hasCustomContentToLeft()) {
3758 mWorkspace.createCustomContentContainer();
3759 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003760 }
Winson Chung64359a52013-07-08 17:17:08 -07003761 }
3762
3763 @Override
3764 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003765 // Log to disk
3766 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3767 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3768 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003769 int count = orderedScreenIds.size();
3770 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003771 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003772 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003773 }
3774
Adam Cohen39a06042013-07-19 14:30:12 -07003775 private boolean shouldShowWeightWatcher() {
3776 String spKey = LauncherAppState.getSharedPreferencesKey();
3777 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003778 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003779
3780 return show;
3781 }
3782
3783 private void toggleShowWeightWatcher() {
3784 String spKey = LauncherAppState.getSharedPreferencesKey();
3785 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3786 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3787
3788 show = !show;
3789
3790 SharedPreferences.Editor editor = sp.edit();
3791 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3792 editor.commit();
3793
3794 if (mWeightWatcher != null) {
3795 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3796 }
3797 }
3798
Winson Chungd64d1762013-08-20 14:37:16 -07003799 public void bindAppsAdded(final ArrayList<Long> newScreens,
3800 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003801 final ArrayList<ItemInfo> addAnimated,
3802 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003803 Runnable r = new Runnable() {
3804 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003805 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003806 }
3807 };
3808 if (waitUntilResume(r)) {
3809 return;
3810 }
3811
Winson Chungd64d1762013-08-20 14:37:16 -07003812 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003813 if (newScreens != null) {
3814 bindAddScreens(newScreens);
3815 }
Winson Chungd64d1762013-08-20 14:37:16 -07003816
3817 // We add the items without animation on non-visible pages, and with
3818 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003819 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003820 bindItems(addNotAnimated, 0,
3821 addNotAnimated.size(), false);
3822 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003823 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003824 bindItems(addAnimated, 0,
3825 addAnimated.size(), true);
3826 }
Winson Chungc58497e2013-09-03 17:48:37 -07003827
Winson Chung87412982013-10-03 18:34:14 -07003828 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003829 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003830
Winson Chungb745afb2015-03-02 11:51:23 -08003831 if (addedApps != null && mAppsView != null) {
3832 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003833 }
Winson Chungd64d1762013-08-20 14:37:16 -07003834 }
3835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003836 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 * Bind the items start-end from the list.
3838 *
3839 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003840 */
Winson Chung64359a52013-07-08 17:17:08 -07003841 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3842 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003843 Runnable r = new Runnable() {
3844 public void run() {
3845 bindItems(shortcuts, start, end, forceAnimateIcons);
3846 }
3847 };
3848 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003849 return;
3850 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003851
Winson Chungf0c6ae02012-03-21 16:10:31 -07003852 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003853 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3854 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003855 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003856 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003857 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003858 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003859 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003860
3861 // Short circuit if we are loading dock items for a configuration which has no dock
3862 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3863 mHotseat == null) {
3864 continue;
3865 }
3866
Joe Onorato9c1289c2009-08-17 11:03:03 -04003867 switch (item.itemType) {
3868 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3869 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003870 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003871 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003872
Winson Chung64359a52013-07-08 17:17:08 -07003873 /*
3874 * TODO: FIX collision case
3875 */
Winson Chungce376632013-07-11 16:12:41 -07003876 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3877 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3878 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003879 View v = cl.getChildAt(item.cellX, item.cellY);
3880 Object tag = v.getTag();
3881 String desc = "Collision while binding workspace item: " + item
3882 + ". Collides with " + tag;
3883 if (LauncherAppState.isDogfoodBuild()) {
3884 throw (new RuntimeException(desc));
3885 } else {
3886 Log.d(TAG, desc);
3887 }
Winson Chungce376632013-07-11 16:12:41 -07003888 }
Winson Chung64359a52013-07-08 17:17:08 -07003889 }
3890
Adam Cohendcd297f2013-06-18 13:13:40 -07003891 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3892 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003893 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003894 // Animate all the applications up now
3895 shortcut.setAlpha(0f);
3896 shortcut.setScaleX(0f);
3897 shortcut.setScaleY(0f);
3898 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003899 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003902 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003903 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003904 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003905 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003906 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3907 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003909 default:
3910 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003912 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003913
Winson Chung997a9232013-07-24 15:33:46 -07003914 if (animateIcons) {
3915 // Animate to the correct page
3916 if (newShortcutsScreenId > -1) {
3917 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003918 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003919 final Runnable startBounceAnimRunnable = new Runnable() {
3920 public void run() {
3921 anim.playTogether(bounceAnims);
3922 anim.start();
3923 }
3924 };
Winson Chung997a9232013-07-24 15:33:46 -07003925 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003926 // We post the animation slightly delayed to prevent slowdowns
3927 // when we are loading right after we return to launcher.
3928 mWorkspace.postDelayed(new Runnable() {
3929 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003930 if (mWorkspace != null) {
3931 mWorkspace.snapToPage(newScreenIndex);
3932 mWorkspace.postDelayed(startBounceAnimRunnable,
3933 NEW_APPS_ANIMATION_DELAY);
3934 }
Winson Chung94d67682013-09-25 16:29:40 -07003935 }
3936 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003937 } else {
3938 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003939 }
3940 }
Winson Chung64359a52013-07-08 17:17:08 -07003941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003943 }
3944
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945 /**
3946 * Implementation of the method from LauncherModel.Callbacks.
3947 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003948 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003949 Runnable r = new Runnable() {
3950 public void run() {
3951 bindFolders(folders);
3952 }
3953 };
3954 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003955 return;
3956 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003957 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959
3960 /**
3961 * Add the views for a widget to the workspace.
3962 *
3963 * Implementation of the method from LauncherModel.Callbacks.
3964 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003965 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003966 Runnable r = new Runnable() {
3967 public void run() {
3968 bindAppWidget(item);
3969 }
3970 };
3971 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003972 return;
3973 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003974
Daniel Sandler843e8602010-06-07 14:59:01 -04003975 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3976 if (DEBUG_WIDGETS) {
3977 Log.d(TAG, "bindAppWidget: " + item);
3978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003979 final Workspace workspace = mWorkspace;
3980
Adam Cohen59400422014-03-05 18:07:04 -08003981 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003982 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003983
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003984 if (!mIsSafeModeEnabled
3985 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3986 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003987 if (appWidgetInfo == null) {
3988 if (DEBUG_WIDGETS) {
3989 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3990 + " belongs to component " + item.providerName
3991 + ", as the povider is null");
3992 }
3993 LauncherModel.deleteItemFromDatabase(this, item);
3994 return;
3995 }
3996 // Note: This assumes that the id remap broadcast is received before this step.
3997 // If that is not the case, the id remap will be ignored and user may see the
3998 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07003999 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004000 pendingInfo.spanX = item.spanX;
4001 pendingInfo.spanY = item.spanY;
4002 pendingInfo.minSpanX = item.minSpanX;
4003 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004004 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004005 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004006
Sunny Goyalff572272014-07-23 13:58:07 -07004007 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004008 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4009 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004010
4011 // TODO consider showing a permission dialog when the widget is clicked.
4012 if (!success) {
4013 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4014 if (DEBUG_WIDGETS) {
4015 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4016 + " belongs to component " + item.providerName
4017 + ", as the launcher is unable to bing a new widget id");
4018 }
4019 LauncherModel.deleteItemFromDatabase(this, item);
4020 return;
4021 }
4022
4023 item.appWidgetId = newWidgetId;
4024
4025 // If the widget has a configure activity, it is still needs to set it up, otherwise
4026 // the widget is ready to go.
4027 item.restoreStatus = (appWidgetInfo.configure == null)
4028 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4029 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4030
4031 LauncherModel.updateItemInDatabase(this, item);
4032 }
4033
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004034 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004035 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004036 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004037 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4038 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004039 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004040
Sunny Goyal651077b2014-06-30 14:15:31 -07004041 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4042 } else {
4043 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004044 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4045 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004046 view.updateIcon(mIconCache);
4047 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004048 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004049 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004050 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004051
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004053 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004054
Adam Cohendcd297f2013-06-18 13:13:40 -07004055 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004057 if (!item.isCustomWidget()) {
4058 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4059 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004060
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061 workspace.requestLayout();
4062
Daniel Sandler843e8602010-06-07 14:59:01 -04004063 if (DEBUG_WIDGETS) {
4064 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4065 + (SystemClock.uptimeMillis()-start) + "ms");
4066 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004067 }
4068
Sunny Goyalff572272014-07-23 13:58:07 -07004069 /**
4070 * Restores a pending widget.
4071 *
4072 * @param appWidgetId The app widget id
4073 * @param cellInfo The position on screen where to create the widget.
4074 */
4075 private void completeRestoreAppWidget(final int appWidgetId) {
4076 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4077 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4078 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4079 return;
4080 }
4081
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004082 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004083 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4084
4085 mWorkspace.reinflateWidgetsIfNecessary();
4086 LauncherModel.updateItemInDatabase(this, info);
4087 }
4088
Adam Cohen1462de32012-07-24 22:34:36 -07004089 public void onPageBoundSynchronously(int page) {
4090 mSynchronouslyBoundPages.add(page);
4091 }
4092
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093 /**
4094 * Callback saying that there aren't any more items to bind.
4095 *
4096 * Implementation of the method from LauncherModel.Callbacks.
4097 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004098 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004099 Runnable r = new Runnable() {
4100 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004101 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004102 }
4103 };
4104 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004105 return;
4106 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107 if (mSavedState != null) {
4108 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004109 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004110 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004111 mSavedState = null;
4112 }
4113
Adam Cohen1462de32012-07-24 22:34:36 -07004114 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004115
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004116 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004117 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004118
4119 // If we received the result of any pending adds while the loader was running (e.g. the
4120 // widget configuration forced an orientation change), process them now.
4121 if (sPendingAddItem != null) {
4122 final long screenId = completeAdd(sPendingAddItem);
4123
4124 // TODO: this moves the user to the page where the pending item was added. Ideally,
4125 // the screen would be guaranteed to exist after bind, and the page would be set through
4126 // the workspace restore process.
4127 mWorkspace.post(new Runnable() {
4128 @Override
4129 public void run() {
4130 mWorkspace.snapToScreenId(screenId);
4131 }
4132 });
4133 sPendingAddItem = null;
4134 }
4135
Sunny Goyal756adbc2015-04-16 15:20:51 -07004136 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004137
4138 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004139 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004140 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004141 }
4142
Adam Cohen3ed316a2014-07-23 18:21:20 -07004143 private void sendLoadingCompleteBroadcastIfNecessary() {
4144 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4145 String permission =
4146 getResources().getString(R.string.receive_first_load_broadcast_permission);
4147 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4148 sendBroadcast(intent, permission);
4149 SharedPreferences.Editor editor = mSharedPrefs.edit();
4150 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4151 editor.apply();
4152 }
4153 }
4154
Winson Chunga0b7e862013-09-05 16:03:15 -07004155 public boolean isAllAppsButtonRank(int rank) {
4156 if (mHotseat != null) {
4157 return mHotseat.isAllAppsButtonRank(rank);
4158 }
4159 return false;
4160 }
4161
Winson Chunga2413752012-04-03 14:22:34 -07004162 private boolean canRunNewAppsAnimation() {
4163 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4164 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4165 }
4166
Winson Chungc9168342013-06-26 14:54:55 -07004167 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4168 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4169 PropertyValuesHolder.ofFloat("alpha", 1f),
4170 PropertyValuesHolder.ofFloat("scaleX", 1f),
4171 PropertyValuesHolder.ofFloat("scaleY", 1f));
4172 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4173 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004174 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004175 return bounceAnim;
4176 }
4177
Adam Cohen27772732013-11-11 14:00:56 +00004178 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004179 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004180 }
4181
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004182 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004183 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004184 }
4185
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004186 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004187 if (mSearchDropTargetBar == null) {
4188 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004189 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004190 if (mQsb != null) {
4191 mSearchDropTargetBar.removeView(mQsb);
4192 mQsb = null;
4193 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004194 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004195 }
4196
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004197 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004198 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4199 * multiple calls to bind the same list.)
4200 */
4201 @Thunk ArrayList<AppInfo> mTmpAppsList;
4202 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4203 public void run() {
4204 bindAllApplications(mTmpAppsList);
4205 mTmpAppsList = null;
4206 }
4207 };
4208
4209 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004210 * Add the icons for all apps.
4211 *
4212 * Implementation of the method from LauncherModel.Callbacks.
4213 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004214 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004215 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4216 mTmpAppsList = apps;
4217 return;
4218 }
4219
Winson Chungb745afb2015-03-02 11:51:23 -08004220 if (mAppsView != null) {
4221 mAppsView.setApps(apps);
4222 }
Adam Cohen9211d422014-10-07 18:14:53 -07004223 if (mLauncherCallbacks != null) {
4224 mLauncherCallbacks.bindAllApplications(apps);
4225 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004226 }
4227
4228 /**
4229 * A package was updated.
4230 *
4231 * Implementation of the method from LauncherModel.Callbacks.
4232 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004233 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004234 Runnable r = new Runnable() {
4235 public void run() {
4236 bindAppsUpdated(apps);
4237 }
4238 };
4239 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004240 return;
4241 }
4242
Winson Chungb745afb2015-03-02 11:51:23 -08004243 if (mAppsView != null) {
4244 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004245 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004246 }
4247
Sunny Goyal4390ace2014-10-13 11:33:11 -07004248 @Override
4249 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4250 Runnable r = new Runnable() {
4251 public void run() {
4252 bindWidgetsRestored(widgets);
4253 }
4254 };
4255 if (waitUntilResume(r)) {
4256 return;
4257 }
4258 mWorkspace.widgetsRestored(widgets);
4259 }
4260
Joe Onorato9c1289c2009-08-17 11:03:03 -04004261 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004262 * Some shortcuts were updated in the background.
4263 *
4264 * Implementation of the method from LauncherModel.Callbacks.
4265 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004266 @Override
4267 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4268 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004269 Runnable r = new Runnable() {
4270 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004271 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004272 }
4273 };
4274 if (waitUntilResume(r)) {
4275 return;
4276 }
4277
Sunny Goyal4390ace2014-10-13 11:33:11 -07004278 if (!updated.isEmpty()) {
4279 mWorkspace.updateShortcuts(updated);
4280 }
4281
4282 if (!removed.isEmpty()) {
4283 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4284 for (ShortcutInfo si : removed) {
4285 removedComponents.add(si.getTargetComponent());
4286 }
4287 mWorkspace.removeItemsByComponentName(removedComponents, user);
4288 // Notify the drag controller
4289 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004290 }
4291 }
4292
4293 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004294 * Update the state of a package, typically related to install state.
4295 *
4296 * Implementation of the method from LauncherModel.Callbacks.
4297 */
Sunny Goyale755d462014-07-22 13:48:29 -07004298 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004299 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4300 Runnable r = new Runnable() {
4301 public void run() {
4302 bindRestoreItemsChange(updates);
4303 }
4304 };
4305 if (waitUntilResume(r)) {
4306 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004307 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004308
Sunny Goyal756adbc2015-04-16 15:20:51 -07004309 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004310 }
4311
4312 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004313 * A package was uninstalled. We take both the super set of packageNames
4314 * in addition to specific applications to remove, the reason being that
4315 * this can be called when a package is updated as well. In that scenario,
4316 * we only remove specific components from the workspace, where as
4317 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004318 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004319 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004320 * Implementation of the method from LauncherModel.Callbacks.
4321 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004322 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004323 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004324 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004325 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004326 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004327 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004328 }
Winson Chungd64d1762013-08-20 14:37:16 -07004329 };
4330 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004331 return;
4332 }
4333
Sunny Goyal1a745e82014-10-02 15:58:31 -07004334 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004335 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4336 for (AppInfo info : appInfos) {
4337 removedComponents.add(info.componentName);
4338 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004339 if (!packageNames.isEmpty()) {
4340 mWorkspace.removeItemsByPackageName(packageNames, user);
4341 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004342 if (!removedComponents.isEmpty()) {
4343 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004344 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004345 // Notify the drag controller
4346 mDragController.onAppsRemoved(packageNames, removedComponents);
4347
Sunny Goyal1a745e82014-10-02 15:58:31 -07004348 } else {
4349 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004350 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004351
Winson Chungdf95eb12013-10-16 14:57:07 -07004352 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004353 if (mAppsView != null) {
4354 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004356 }
Joe Onoratobe386092009-11-17 17:32:16 -08004357
Michael Jurkac402cd92013-05-20 15:49:32 +02004358 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004359 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004360 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004361 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004362 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004363
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004364 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004365 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004366 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004367 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004368 return;
4369 }
4370
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004371 if (mWidgetsView != null && model != null) {
4372 mWidgetsView.addWidgets(model);
4373 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004374 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004375 }
4376
Winson Chung400438b2011-01-16 17:53:48 -08004377 private int mapConfigurationOriActivityInfoOri(int configOri) {
4378 final Display d = getWindowManager().getDefaultDisplay();
4379 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4380 switch (d.getRotation()) {
4381 case Surface.ROTATION_0:
4382 case Surface.ROTATION_180:
4383 // We are currently in the same basic orientation as the natural orientation
4384 naturalOri = configOri;
4385 break;
4386 case Surface.ROTATION_90:
4387 case Surface.ROTATION_270:
4388 // We are currently in the other basic orientation to the natural orientation
4389 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4390 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4391 break;
4392 }
4393
4394 int[] oriMap = {
4395 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4396 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4397 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4398 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4399 };
4400 // Since the map starts at portrait, we need to offset if this device's natural orientation
4401 // is landscape.
4402 int indexOffset = 0;
4403 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4404 indexOffset = 1;
4405 }
4406 return oriMap[(d.getRotation() + indexOffset) % 4];
4407 }
Adam Cohen446e9402011-09-15 18:21:21 -07004408
Winson Chung4b919f82012-05-01 10:44:08 -07004409 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004410 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004411 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4412 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4413 .getConfiguration().orientation));
4414 } else {
4415 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4416 }
Winson Chung4b919f82012-05-01 10:44:08 -07004417 }
4418 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004419
Winson Chung4b919f82012-05-01 10:44:08 -07004420 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004421 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004422 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004423 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004424 } else {
4425 mHandler.postDelayed(new Runnable() {
4426 public void run() {
4427 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4428 }
4429 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004430 }
Winson Chung4b919f82012-05-01 10:44:08 -07004431 }
Winson Chung400438b2011-01-16 17:53:48 -08004432 }
4433
Winson Chunge43a1e72014-01-15 10:33:02 -08004434 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004435 if (mLauncherCallbacks != null) {
4436 return mLauncherCallbacks.isLauncherPreinstalled();
4437 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004438 PackageManager pm = getPackageManager();
4439 try {
4440 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4441 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4442 return true;
4443 } else {
4444 return false;
4445 }
4446 } catch (NameNotFoundException e) {
4447 e.printStackTrace();
4448 return false;
4449 }
4450 }
4451
Adam Cohenea90f832014-05-21 15:03:34 -07004452 /**
4453 * This method indicates whether or not we should suggest default wallpaper dimensions
4454 * when our wallpaper cropper was not yet used to set a wallpaper.
4455 */
4456 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004457 if (mLauncherCallbacks != null) {
4458 return mLauncherCallbacks.overrideWallpaperDimensions();
4459 }
Adam Cohenea90f832014-05-21 15:03:34 -07004460 return true;
4461 }
4462
Adam Cohen432609a2014-03-13 17:03:22 -07004463 /**
4464 * To be overridden by subclasses to indicate that there is an activity to launch
4465 * before showing the standard launcher experience.
4466 */
4467 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004468 if (mLauncherCallbacks != null) {
4469 return mLauncherCallbacks.hasFirstRunActivity();
4470 }
Adam Cohen432609a2014-03-13 17:03:22 -07004471 return false;
4472 }
4473
4474 /**
4475 * To be overridden by subclasses to launch any first run activity
4476 */
4477 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004478 if (mLauncherCallbacks != null) {
4479 return mLauncherCallbacks.getFirstRunActivity();
4480 }
Adam Cohen432609a2014-03-13 17:03:22 -07004481 return null;
4482 }
4483
Winson Chung2826a402015-04-17 16:18:53 -07004484 /**
4485 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004486 */
4487 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004488 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4489 return false;
4490 }
4491
Winson Chung2826a402015-04-17 16:18:53 -07004492 if (mLauncherCallbacks != null) {
4493 return mLauncherCallbacks.overrideAllAppsSearch();
4494 }
4495 return false;
4496 }
4497
Winson Chunga6945242014-01-08 14:04:34 -08004498 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004499 return !ActivityManager.isRunningInTestHarness() &&
4500 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004501 }
4502
Adam Cohen4a9423d2014-03-28 14:22:31 -07004503 protected boolean hasRunFirstRunActivity() {
4504 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4505 }
4506
Adam Cohen432609a2014-03-13 17:03:22 -07004507 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004508 if (shouldRunFirstRunActivity() &&
4509 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004510 Intent firstRunIntent = getFirstRunActivity();
4511 if (firstRunIntent != null) {
4512 startActivity(firstRunIntent);
4513 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004514 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004515 }
4516 }
Adam Cohen432609a2014-03-13 17:03:22 -07004517 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004518 }
4519
4520 private void markFirstRunActivityShown() {
4521 SharedPreferences.Editor editor = mSharedPrefs.edit();
4522 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4523 editor.apply();
4524 }
4525
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004526 private void showSettingsActivity() {
4527 startActivity(new Intent(this, SettingsActivity.class));
4528 }
4529
Adam Cohen432609a2014-03-13 17:03:22 -07004530 /**
4531 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4532 * screen that must be displayed and dismissed.
4533 */
4534 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004535 if (mLauncherCallbacks != null) {
4536 return mLauncherCallbacks.hasDismissableIntroScreen();
4537 }
Adam Cohen432609a2014-03-13 17:03:22 -07004538 return false;
4539 }
4540
4541 /**
4542 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4543 */
4544 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004545 if (mLauncherCallbacks != null) {
4546 return mLauncherCallbacks.getIntroScreen();
4547 }
Adam Cohen432609a2014-03-13 17:03:22 -07004548 return null;
4549 }
4550
4551 /**
4552 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4553 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4554 */
4555 private boolean shouldShowIntroScreen() {
4556 return hasDismissableIntroScreen() &&
4557 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4558 }
4559
4560 protected void showIntroScreen() {
4561 View introScreen = getIntroScreen();
4562 changeWallpaperVisiblity(false);
4563 if (introScreen != null) {
4564 mDragLayer.showOverlayView(introScreen);
4565 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004566 if (mLauncherOverlayContainer != null) {
4567 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4568 }
Adam Cohen432609a2014-03-13 17:03:22 -07004569 }
4570
4571 public void dismissIntroScreen() {
4572 markIntroScreenDismissed();
4573 if (showFirstRunActivity()) {
4574 // We delay hiding the intro view until the first run activity is showing. This
4575 // avoids a blip.
4576 mWorkspace.postDelayed(new Runnable() {
4577 @Override
4578 public void run() {
4579 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004580 if (mLauncherOverlayContainer != null) {
4581 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4582 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004583 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004584 }
4585 }, ACTIVITY_START_DELAY);
4586 } else {
4587 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004588 if (mLauncherOverlayContainer != null) {
4589 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4590 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004591 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004592 }
4593 changeWallpaperVisiblity(true);
4594 }
4595
4596 private void markIntroScreenDismissed() {
4597 SharedPreferences.Editor editor = mSharedPrefs.edit();
4598 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4599 editor.apply();
4600 }
4601
Adam Cohen091440a2015-03-18 14:16:05 -07004602 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004603 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4604 // on the device, then we always show the first run cling experience (or if there is no
4605 // launcher2). Otherwise, we prompt the user upon started for migration
4606 LauncherClings launcherClings = new LauncherClings(this);
4607 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4608 if (mModel.canMigrateFromOldLauncherDb(this)) {
4609 launcherClings.showMigrationCling();
4610 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004611 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004612 }
4613 }
4614 }
4615
Winson Chunga6945242014-01-08 14:04:34 -08004616 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004617 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4618 if (mHotseat != null) mHotseat.setAlpha(1f);
4619 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4620 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004621 }
4622
4623 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004624 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4625 if (mHotseat != null) mHotseat.setAlpha(0f);
4626 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4627 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004628 }
4629
Mathew Inwood72fbec12013-11-19 15:45:07 +00004630 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004631 // Called from search suggestion, not supported in other profiles.
4632 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4633 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4634 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4635 myUser);
4636 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004637 return null;
4638 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004639 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004640 }
4641
4642 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4643 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004644 // Called from search suggestion, not supported in other profiles.
4645 return createShortcutDragInfo(shortcutIntent, caption, icon,
4646 UserHandleCompat.myUserHandle());
4647 }
4648
4649 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4650 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004651 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004652 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004653 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004654 }
4655
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004656 protected void moveWorkspaceToDefaultScreen() {
4657 mWorkspace.moveToDefaultScreen(false);
4658 }
4659
Mathew Inwood72fbec12013-11-19 15:45:07 +00004660 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4661 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004662 mWorkspace.onExternalDragStartedWithItem(dragView);
4663 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004664 }
4665
Anjali Koppalf5d29132014-02-28 13:33:27 -08004666 @Override
4667 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004668 if (mLauncherCallbacks != null) {
4669 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4670 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004671 }
4672
Winson Chung80baf5a2010-08-09 16:03:15 -07004673 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004674 * Prints out out state for debugging.
4675 */
4676 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004677 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004678 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004679 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4680 Log.d(TAG, "mRestoring=" + mRestoring);
4681 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4682 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004683 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004684 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004685 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004686
Daniel Sandler325dc232013-06-05 22:57:57 -04004687 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004688 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004689
4690 @Override
4691 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4692 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004693 synchronized (sDumpLogs) {
4694 writer.println(" ");
4695 writer.println("Debug logs: ");
4696 for (int i = 0; i < sDumpLogs.size(); i++) {
4697 writer.println(" " + sDumpLogs.get(i));
4698 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004699 }
Adam Cohen9211d422014-10-07 18:14:53 -07004700 if (mLauncherCallbacks != null) {
4701 mLauncherCallbacks.dump(prefix, fd, writer, args);
4702 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004703 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004704
4705 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004706 if (DEBUG_DUMP_LOG) {
4707 synchronized (sDumpLogs) {
4708 Log.d(TAG, "");
4709 Log.d(TAG, "*********************");
4710 Log.d(TAG, "Launcher debug logs: ");
4711 for (int i = 0; i < sDumpLogs.size(); i++) {
4712 Log.d(TAG, " " + sDumpLogs.get(i));
4713 }
4714 Log.d(TAG, "*********************");
4715 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004716 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004717 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004718 }
4719
4720 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004721 addDumpLog(tag, log, null, debugLog);
4722 }
4723
4724 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004725 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004726 if (e != null) {
4727 Log.d(tag, log, e);
4728 } else {
4729 Log.d(tag, log);
4730 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004731 }
Winson Chungede41292013-09-19 16:27:36 -07004732 if (DEBUG_DUMP_LOG) {
4733 sDateStamp.setTime(System.currentTimeMillis());
4734 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004735 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4736 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004737 }
Winson Chungede41292013-09-19 16:27:36 -07004738 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004739 }
4740
Adam Cohen59400422014-03-05 18:07:04 -08004741 public static CustomAppWidget getCustomAppWidget(String name) {
4742 return sCustomAppWidgets.get(name);
4743 }
4744
4745 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4746 return sCustomAppWidgets;
4747 }
4748
Winson Chungede41292013-09-19 16:27:36 -07004749 public void dumpLogsToLocalData() {
4750 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004751 new AsyncTask<Void, Void, Void>() {
4752 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004753 boolean success = false;
4754 sDateStamp.setTime(sRunStart);
4755 String FILENAME = sDateStamp.getMonth() + "-"
4756 + sDateStamp.getDay() + "_"
4757 + sDateStamp.getHours() + "-"
4758 + sDateStamp.getMinutes() + "_"
4759 + sDateStamp.getSeconds() + ".txt";
4760
4761 FileOutputStream fos = null;
4762 File outFile = null;
4763 try {
4764 outFile = new File(getFilesDir(), FILENAME);
4765 outFile.createNewFile();
4766 fos = new FileOutputStream(outFile);
4767 } catch (Exception e) {
4768 e.printStackTrace();
4769 }
4770 if (fos != null) {
4771 PrintWriter writer = new PrintWriter(fos);
4772
4773 writer.println(" ");
4774 writer.println("Debug logs: ");
4775 synchronized (sDumpLogs) {
4776 for (int i = 0; i < sDumpLogs.size(); i++) {
4777 writer.println(" " + sDumpLogs.get(i));
4778 }
4779 }
4780 writer.close();
4781 }
4782 try {
4783 if (fos != null) {
4784 fos.close();
4785 success = true;
4786 }
4787 } catch (IOException e) {
4788 e.printStackTrace();
4789 }
Michael Jurka43467462013-11-14 12:03:58 +01004790 return null;
Winson Chungede41292013-09-19 16:27:36 -07004791 }
Michael Jurka43467462013-11-14 12:03:58 +01004792 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004793 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004794 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004795}
Michael Jurka2763be32011-02-24 11:19:57 -08004796
Dan Sandlerd5024042014-01-09 15:01:33 -05004797interface DebugIntents {
4798 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4799 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004800}