blob: 9756727648f706c0d0fde4f424eb216a9e3f703f [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 Goyal5683f872015-05-29 14:54:40 -0700103import com.android.launcher3.allapps.AppSearchManager;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700104import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100105import com.android.launcher3.compat.LauncherActivityInfoCompat;
106import com.android.launcher3.compat.LauncherAppsCompat;
107import com.android.launcher3.compat.UserHandleCompat;
108import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700109import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700110import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700111import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700112import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700113import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700114import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700115
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700120import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800121import java.lang.reflect.InvocationTargetException;
122import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700128import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700129import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000130import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132/**
133 * Default launcher application.
134 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100135public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700136 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800137 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
138 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700139 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700140 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Winson Chung83f59ab2015-05-05 17:21:58 -0700142 // Temporary flag
143 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
144
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700146 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700149 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700150
Dan Sandlerd5024042014-01-09 15:01:33 -0500151 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700159 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700161 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
162 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
163 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
164
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700165 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
166
Mathew Inwood876a8462013-06-14 14:12:41 +0100167 /**
168 * IntentStarter uses request codes starting with this. This must be greater than all activity
169 * request codes used internally.
170 */
171 protected static final int REQUEST_LAST = 100;
172
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800173 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 // To turn on these properties, type
176 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700188 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
189 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
191 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700192 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700194 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700195 // Type: int
196 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
197 // Type: int
198 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
199 // Type: parcelable
200 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000201 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800202 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000203 // Type: int[]
204 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Adam Cohen432609a2014-03-13 17:03:22 -0700206 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800207 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
208
Adam Cohen3ed316a2014-07-23 18:21:20 -0700209 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
210 static final String ACTION_FIRST_LOAD_COMPLETE =
211 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
212
Adam Cohen39a06042013-07-19 14:30:12 -0700213 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700214 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700215
Sunny Goyal594d76d2014-11-06 10:12:54 -0800216 private static final String QSB_WIDGET_ID = "qsb_widget_id";
217 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
218
Winson Chunga6945242014-01-08 14:04:34 -0800219 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
220
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700221 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700222 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
223
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700226
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700227 private boolean mIsSafeModeEnabled;
228
Adam Cohenc2d6e892014-10-16 09:49:24 -0700229 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
230 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700231 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700232
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700234 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
235 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700236 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000238 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
239 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
240
Winson Chunga2413752012-04-03 14:22:34 -0700241 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700242 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
243 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700245
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800246 private final BroadcastReceiver mCloseSystemDialogsReceiver
247 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800248 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private LayoutInflater mInflater;
251
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700253 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800254 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800256 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700257 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700258
Sunny Goyalffe83f12014-08-14 17:39:34 -0700259 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700261
Adam Cohen091440a2015-03-18 14:16:05 -0700262 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800263 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800264 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700265
Michael Jurka0280c3b2010-09-17 15:00:07 -0700266 private int[] mTmpAddItemCellCoordinates = new int[2];
267
Sunny Goyal316490e2015-06-02 09:38:28 -0700268 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800269 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700270
Michael Jurka838a4ca2011-02-07 13:33:06 -0800271 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700272
Winson Chungc51db6a2011-10-05 11:44:49 -0700273 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700274
275 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700276 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700277
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700278 // Main container view and the model for the widget tray screen.
279 @Thunk WidgetsContainerView mWidgetsView;
280 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700281
Winson Chungf0ea4d32011-06-06 14:27:16 -0700282 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800283 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700286 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
287 // scroll issues (because the workspace may not have been measured yet) and extra work.
288 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
289 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
291 private SpannableStringBuilder mDefaultKeySsb = null;
292
Adam Cohen091440a2015-03-18 14:16:05 -0700293 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800295 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 private boolean mRestoring;
297 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700298 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299
Michael Jurka1e2f4652013-07-08 18:03:46 -0700300 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700301 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 private Bundle mSavedInstanceState;
304
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800306 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700307 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800308 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700309 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800310 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400311
Sunny Goyale2df0622015-04-24 11:27:00 -0700312 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700313
Adam Cohen61f560d2013-09-30 15:58:20 -0700314 private View.OnTouchListener mHapticFeedbackTouchListener;
315
Adam Cohended9f8d2010-11-03 13:25:16 -0700316 // Related to the auto-advancing of widgets
317 private final int ADVANCE_MSG = 1;
318 private final int mAdvanceInterval = 20000;
319 private final int mAdvanceStagger = 250;
320 private long mAutoAdvanceSentTime;
321 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700322 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700323 new HashMap<View, AppWidgetProviderInfo>();
324
Winson Chung400438b2011-01-16 17:53:48 -0800325 // Determines how long to wait after a rotation before restoring the screen orientation to
326 // match the sensor state.
327 private final int mRestoreScreenOrientationDelay = 500;
328
Adam Cohen091440a2015-03-18 14:16:05 -0700329 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700330
Adam Cohen1462de32012-07-24 22:34:36 -0700331 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700332 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700333
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700334 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700335 static Date sDateStamp = new Date();
336 static DateFormat sDateFormat =
337 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
338 static long sRunStart = System.currentTimeMillis();
339 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700340
Winson Chung46353de2012-02-16 14:05:10 -0800341 // We only want to get the SharedPreferences once since it does an FS stat each time we get
342 // it from the context.
343 private SharedPreferences mSharedPrefs;
344
Winson Chungf0c6ae02012-03-21 16:10:31 -0700345 // Holds the page that we need to animate to, and the icon views that we need to animate up
346 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700347 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700348 private Bitmap mFolderIconBitmap;
349 private Canvas mFolderIconCanvas;
350 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700351
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700352 private DeviceProfile mDeviceProfile;
353
Winson Chung13eb5272015-05-11 16:30:13 -0700354 // This is set to the view that launched the activity that navigated the user away from
355 // launcher. Since there is no callback for when the activity has finished launching, enable
356 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800357 private BubbleTextView mWaitingForResume;
358
Adam Cohen59400422014-03-05 18:07:04 -0800359 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
360 new HashMap<String, CustomAppWidget>();
361
362 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
363 static {
364 if (ENABLE_CUSTOM_WIDGET_TEST) {
365 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
366 }
367 }
368
Chet Haasea8f996d2015-02-17 12:56:04 -0800369 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
370 private static Method sClipRevealMethod = null;
371 static {
372 Class<?> activityOptionsClass = ActivityOptions.class;
373 try {
374 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
375 View.class, int.class, int.class, int.class, int.class);
376 } catch (Exception e) {
377 // Earlier version
378 }
379 }
380
Adam Cohen091440a2015-03-18 14:16:05 -0700381 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700382 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700383 if (mWorkspace != null) {
384 mWorkspace.buildPageHardwareLayers();
385 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700386 }
387 };
388
Adam Cohendb364c32014-05-20 14:23:40 -0700389 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800390
Adam Cohen091440a2015-03-18 14:16:05 -0700391 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392 int requestCode;
393 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700394 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700395 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800396 int cellX;
397 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700398 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800399 }
400
Daniel Sandlerff02d492013-08-05 02:12:05 -0400401 private Stats mStats;
402
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700403 FocusIndicatorView mFocusHandler;
404
Sunny Goyal316490e2015-06-02 09:38:28 -0700405 @Thunk boolean mRotationEnabled = false;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700406 private boolean mPreferenceObserverRegistered = false;
407
408 final private SharedPreferences.OnSharedPreferenceChangeListener mSettingsObserver =
409 new SharedPreferences.OnSharedPreferenceChangeListener() {
410 @Override
411 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
412 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
413 if (mRotationEnabled = sharedPreferences.getBoolean(
414 Utilities.ALLOW_ROTATION_PREFERENCE_KEY, false)) {
415 unlockScreenOrientation(true);
416 } else {
417 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
418 }
419 }
420 }
421 };
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 @Override
424 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700425 if (DEBUG_STRICT_MODE) {
426 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
427 .detectDiskReads()
428 .detectDiskWrites()
429 .detectNetwork() // or .detectAll() for all detectable problems
430 .penaltyLog()
431 .build());
432 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
433 .detectLeakedSqlLiteObjects()
434 .detectLeakedClosableObjects()
435 .penaltyLog()
436 .penaltyDeath()
437 .build());
438 }
439
Adam Cohen9211d422014-10-07 18:14:53 -0700440 if (mLauncherCallbacks != null) {
441 mLauncherCallbacks.preOnCreate();
442 }
443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400445
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400446 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400447 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700448 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700449
Adam Cohen2e6da152015-05-06 11:42:25 -0700450 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700451 mDeviceProfile = getResources().getConfiguration().orientation
452 == Configuration.ORIENTATION_LANDSCAPE ?
453 app.getInvariantDeviceProfile().landscapeProfile
454 : app.getInvariantDeviceProfile().portraitProfile;
455
456 // TODO: Move this to icon cache.
457 Utilities.setIconSize(mDeviceProfile.iconSizePx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700458
459 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400460 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700461 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700462 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800463 mModel = app.setLauncher(this);
464 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700465
Joe Onorato41a12d22009-10-31 18:30:00 -0400466 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800468 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700469
Daniel Sandlerff02d492013-08-05 02:12:05 -0400470 mStats = new Stats(this);
471
Sunny Goyalffe83f12014-08-14 17:39:34 -0700472 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700473
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700474 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
475 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700476
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700477 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
478 // this also ensures that any synchronous binding below doesn't re-trigger another
479 // LauncherModel load.
480 mPaused = false;
481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200483 android.os.Debug.startMethodTracing(
484 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 }
486
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700488
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700490 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800492 registerContentObservers();
493
Joe Onorato7c312c12009-08-13 21:36:53 -0700494 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700495
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 mSavedState = savedInstanceState;
497 restoreState(mSavedState);
498
499 if (PROFILE_STARTUP) {
500 android.os.Debug.stopMethodTracing();
501 }
502
503 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700504 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700505 // If the user leaves launcher, then we should just load items asynchronously when
506 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700507 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700508 } else {
509 // We only load the page synchronously if the user rotates (or triggers a
510 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700511 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 }
514
515 // For handling default keys
516 mDefaultKeySsb = new SpannableStringBuilder();
517 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800518
519 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
520 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800521
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700522 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
523 // In case we are on a device with locked rotation, we should look at preferences to check
524 // if the user has specifically allowed rotation.
525 if (!mRotationEnabled) {
526 getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
527 Context.MODE_MULTI_PROCESS).registerOnSharedPreferenceChangeListener(
528 mSettingsObserver);
529 mPreferenceObserverRegistered = true;
530 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
531 }
532
533 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
534 // we want the screen to auto-rotate based on the current orientation
Adam Cohenf9426d52012-06-04 17:26:21 -0700535 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700536
Adam Cohen9211d422014-10-07 18:14:53 -0700537 if (mLauncherCallbacks != null) {
538 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700539 if (mLauncherCallbacks.hasLauncherOverlay()) {
540 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700541 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
542 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
543 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700544 mWorkspace.setLauncherOverlay(mLauncherOverlay);
545 }
Adam Cohen9211d422014-10-07 18:14:53 -0700546 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700547
548 if (shouldShowIntroScreen()) {
549 showIntroScreen();
550 } else {
551 showFirstRunActivity();
552 showFirstRunClings();
553 }
Adam Cohen9211d422014-10-07 18:14:53 -0700554 }
555
556 private LauncherCallbacks mLauncherCallbacks;
557
558 public void onPostCreate(Bundle savedInstanceState) {
559 super.onPostCreate(savedInstanceState);
560 if (mLauncherCallbacks != null) {
561 mLauncherCallbacks.onPostCreate(savedInstanceState);
562 }
563 }
564
565 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
566 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700567 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
568 @Override
569 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700570 if (LOGD) {
571 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
572 }
Winson Chung94804152015-05-08 13:06:44 -0700573 mAppsView.setFixedBounds(bounds);
574 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700575 }
576
577 @Override
578 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700579 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
580 // Dismiss All Apps if we aren't already paused/invisible
581 if (!mPaused) {
582 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
583 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
584 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700585 }
Winson Chung0f785722015-04-08 10:27:49 -0700586 }
Sunny Goyal5683f872015-05-29 14:54:40 -0700587
588 @Override
589 public void setSearchManager(AppSearchManager manager) {
590 mAppsView.setSearchManager(manager);
591 }
Winson Chung0f785722015-04-08 10:27:49 -0700592 });
Jun Mukai8af0cd82015-05-12 12:32:12 -0700593 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
594 private boolean mImportanceStored = false;
595 private int mWorkspaceImportanceForAccessibility =
596 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
597 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
598
599 @Override
600 public void onSearchOverlayOpened() {
601 if (mImportanceStored) {
602 return;
603 }
604 // The underlying workspace and hotseat are temporarily suppressed by the search
605 // overlay. So they sholudn't be accessible.
606 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
607 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
608 mWorkspace.setImportantForAccessibility(
609 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
610 mHotseat.setImportantForAccessibility(
611 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
612 mImportanceStored = true;
613 }
614
615 @Override
616 public void onSearchOverlayClosed() {
617 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
618 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
619 mImportanceStored = false;
620 }
621 });
Adam Cohen9211d422014-10-07 18:14:53 -0700622 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700623 }
624
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700625 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700626 public void onLauncherProviderChange() {
627 if (mLauncherCallbacks != null) {
628 mLauncherCallbacks.onLauncherProviderChange();
629 }
630 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700631
Adam Cohen9211d422014-10-07 18:14:53 -0700632 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700633 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700634 if (mLauncherCallbacks != null) {
635 return mLauncherCallbacks.hasCustomContentToLeft();
636 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700637 return false;
638 }
639
Dave Hawkeya8881582013-09-17 15:55:33 -0600640 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500641 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600642 * {@link #addToCustomContentPage}. This will only be invoked if
643 * {@link #hasCustomContentToLeft()} is {@code true}.
644 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500645 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700646 if (mLauncherCallbacks != null) {
647 mLauncherCallbacks.populateCustomContentContainer();
648 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600649 }
650
651 /**
652 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
653 * ensure the custom content page is added or removed if necessary.
654 */
655 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600656 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600657 // Not bound yet, wait for bindScreens to be called.
658 return;
659 }
660
661 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
662 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500663 mWorkspace.createCustomContentContainer();
664 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600665 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
666 mWorkspace.removeCustomContentPage();
667 }
668 }
669
Anton Hanssona2f665f2013-09-26 12:18:32 +0100670 public Stats getStats() {
671 return mStats;
672 }
673
Bjorn Bringertc459e522013-06-07 19:36:01 +0100674 public LayoutInflater getInflater() {
675 return mInflater;
676 }
677
Hyunyoung Song3f471442015-04-08 19:01:34 -0700678 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700679 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
680 // that is subsequently removed from the workspace in startBinding().
681 return !mModel.isLoadingWorkspace();
682 }
683
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800684 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000685 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100686 if (Build.VERSION.SDK_INT >= 17) {
687 return View.generateViewId();
688 } else {
689 // View.generateViewId() is not available. The following fallback logic is a copy
690 // of its implementation.
691 for (;;) {
692 final int result = sNextGeneratedId.get();
693 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
694 int newValue = result + 1;
695 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
696 if (sNextGeneratedId.compareAndSet(result, newValue)) {
697 return result;
698 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000699 }
700 }
701 }
702
703 public int getViewIdForItem(ItemInfo info) {
704 // This cast is safe given the > 2B range for int.
705 int itemId = (int) info.id;
706 if (mItemIdToViewId.containsKey(itemId)) {
707 return mItemIdToViewId.get(itemId);
708 }
709 int viewId = generateViewId();
710 mItemIdToViewId.put(itemId, viewId);
711 return viewId;
712 }
713
714 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700715 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
716 * a configuration step, this allows the proper animations to run after other transitions.
717 */
Adam Cohendb364c32014-05-20 14:23:40 -0700718 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700719 long screenId = args.screenId;
720 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
721 // When the screen id represents an actual screen (as opposed to a rank) we make sure
722 // that the drop page actually exists.
723 screenId = ensurePendingDropLayoutExists(args.screenId);
724 }
Adam Cohendb364c32014-05-20 14:23:40 -0700725
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800726 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800727 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700728 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700729 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700730 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700732 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700733 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700734 case REQUEST_RECONFIGURE_APPWIDGET:
735 completeRestoreAppWidget(args.appWidgetId);
736 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800737 }
Michael Jurka27614d22012-04-02 06:40:22 -0700738 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
739 // if you turned the screen off and then back while in All Apps, Launcher would not
740 // return to the workspace. Clearing mAddInfo.container here fixes this issue
741 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700742 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800743 }
744
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800745 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700746 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700747 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700748 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700749 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800750 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700751
Adam Cohenad4e15c2013-10-17 16:21:35 -0700752 Runnable exitSpringLoaded = new Runnable() {
753 @Override
754 public void run() {
755 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
756 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
757 }
758 };
759
Michael Jurka8b805b12012-04-18 14:23:14 -0700760 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
763 if (resultCode == RESULT_CANCELED) {
764 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700765 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700767 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800768 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700770 }
771 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200772 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
773 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700774 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200775 }
776 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700777 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200778
Adam Cohened66b2b2012-01-23 17:28:51 -0800779 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700780 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700781
Adam Cohendb364c32014-05-20 14:23:40 -0700782 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800783 // We have special handling for widgets
784 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800785 final int appWidgetId;
786 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
787 : -1;
788 if (widgetId < 0) {
789 appWidgetId = pendingAddWidgetId;
790 } else {
791 appWidgetId = widgetId;
792 }
793
Adam Cohenad4e15c2013-10-17 16:21:35 -0700794 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800795 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700796 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
797 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700798 result = RESULT_CANCELED;
799 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700800 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700801 @Override
802 public void run() {
803 exitSpringLoadedDragModeDelayed(false, 0, null);
804 }
805 };
Adam Cohendb364c32014-05-20 14:23:40 -0700806 if (workspaceLocked) {
807 // No need to remove the empty screen if we're mid-binding, as the
808 // the bind will not add the empty screen.
809 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
810 } else {
811 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
812 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
813 }
Winson Chung5aaab772012-04-27 15:24:02 -0700814 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700815 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700816 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
817 // When the screen id represents an actual screen (as opposed to a rank)
818 // we make sure that the drop page actually exists.
819 mPendingAddInfo.screenId =
820 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
821 }
Adam Cohendb364c32014-05-20 14:23:40 -0700822 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
823
824 dropLayout.setDropPending(true);
825 final Runnable onComplete = new Runnable() {
826 @Override
827 public void run() {
828 completeTwoStageWidgetDrop(resultCode, appWidgetId);
829 dropLayout.setDropPending(false);
830 }
831 };
832 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
833 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
834 } else {
835 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
836 mPendingAddInfo);
837 sPendingAddItem = args;
838 }
Winson Chung5aaab772012-04-27 15:24:02 -0700839 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800840 return;
841 }
842
Sunny Goyalff572272014-07-23 13:58:07 -0700843 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
844 if (resultCode == RESULT_OK) {
845 // Update the widget view.
846 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
847 pendingAddWidgetId, mPendingAddInfo);
848 if (workspaceLocked) {
849 sPendingAddItem = args;
850 } else {
851 completeAdd(args);
852 }
853 }
854 // Leave the widget in the pending state if the user canceled the configure.
855 return;
856 }
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 // The pattern used here is that a user PICKs a specific application,
859 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
862 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700863 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700864 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
865 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800866 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700867 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800868 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700869 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700870 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
871 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 }
Adam Cohen00074722013-10-11 17:46:09 -0700873 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700874 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700875 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800878
879 }
880
881 @Override
882 protected void onActivityResult(
883 final int requestCode, final int resultCode, final Intent data) {
884 handleActivityResult(requestCode, resultCode, data);
885 if (mLauncherCallbacks != null) {
886 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
887 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800888 }
889
Adam Cohendb364c32014-05-20 14:23:40 -0700890 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
891 appWidgetId, ItemInfo info) {
892 PendingAddArguments args = new PendingAddArguments();
893 args.requestCode = requestCode;
894 args.intent = data;
895 args.container = info.container;
896 args.screenId = info.screenId;
897 args.cellX = info.cellX;
898 args.cellY = info.cellY;
899 args.appWidgetId = appWidgetId;
900 return args;
901 }
902
903 /**
904 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
905 *
906 * @param screenId the screen id to check
907 * @return the new screen, or screenId if it exists
908 */
909 private long ensurePendingDropLayoutExists(long screenId) {
910 CellLayout dropLayout =
911 (CellLayout) mWorkspace.getScreenWithId(screenId);
912 if (dropLayout == null) {
913 // it's possible that the add screen was removed because it was
914 // empty and a re-bind occurred
915 mWorkspace.addExtraEmptyScreen();
916 return mWorkspace.commitExtraEmptyScreen();
917 } else {
918 return screenId;
919 }
920 }
921
Adam Cohen091440a2015-03-18 14:16:05 -0700922 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700923 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700924 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 Runnable onCompleteRunnable = null;
926 int animationType = 0;
927
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 if (resultCode == RESULT_OK) {
930 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
931 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 mPendingAddWidgetInfo);
933 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 onCompleteRunnable = new Runnable() {
935 @Override
936 public void run() {
937 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700938 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700939 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
940 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 }
942 };
943 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800944 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800945 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800946 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700947 if (mDragLayer.getAnimatedView() != null) {
948 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
949 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
950 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700951 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700952 // The animated view may be null in the case of a rotation during widget configuration
953 onCompleteRunnable.run();
954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 }
956
957 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700958 protected void onStop() {
959 super.onStop();
960 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700961
962 if (mLauncherCallbacks != null) {
963 mLauncherCallbacks.onStop();
964 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700965 }
966
967 @Override
968 protected void onStart() {
969 super.onStart();
970 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700971
972 if (mLauncherCallbacks != null) {
973 mLauncherCallbacks.onStart();
974 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700975 }
976
977 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200979 long startTime = 0;
980 if (DEBUG_RESUME_TIME) {
981 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400982 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200983 }
Adam Cohen9211d422014-10-07 18:14:53 -0700984
985 if (mLauncherCallbacks != null) {
986 mLauncherCallbacks.preOnResume();
987 }
988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700990
Winson Chung4a2afa32012-07-19 14:53:05 -0700991 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700992 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700993 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800994 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700995 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700996 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700997 // view after launching an app, as they may be depending on the UI to be static to
998 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700999 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001000 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001001 } else if (mOnResumeState == State.WIDGETS) {
1002 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001003 }
1004 mOnResumeState = State.NONE;
1005
Winson Chungcd99cd32015-04-29 11:03:24 -07001006 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001007 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1008 // Otherwise, notify the callbacks if we are in all apps mode
1009 if (mState == State.APPS) {
1010 if (mLauncherCallbacks != null) {
1011 mLauncherCallbacks.onAllAppsShown();
1012 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001013 }
1014 }
1015
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001016 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001017 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1018 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001019
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001020 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001021 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001022 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001023 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001025 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001027 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001028 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1029 // execute them here
1030 long startTimeCallbacks = 0;
1031 if (DEBUG_RESUME_TIME) {
1032 startTimeCallbacks = System.currentTimeMillis();
1033 }
1034
Michael Jurka1e2f4652013-07-08 18:03:46 -07001035 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1036 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001038 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001039 if (DEBUG_RESUME_TIME) {
1040 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1041 (System.currentTimeMillis() - startTimeCallbacks));
1042 }
Michael Jurka447bf842013-05-15 14:52:15 +02001043 }
Michael Jurka54554252013-08-01 12:52:23 +02001044 if (mOnResumeCallbacks.size() > 0) {
1045 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1046 mOnResumeCallbacks.get(i).run();
1047 }
1048 mOnResumeCallbacks.clear();
1049 }
Winson Chunge4e50662012-01-23 14:45:13 -08001050
1051 // Reset the pressed state of icons that were locked in the press state while activities
1052 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001053 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001054 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001055 mWaitingForResume.setStayPressed(false);
1056 }
Winson Chung82963d52013-10-09 11:20:57 -07001057
Adam Cohen06dff352012-06-01 17:17:08 -07001058 // It is possible that widgets can receive updates while launcher is not in the foreground.
1059 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1060 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1061 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001062 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001063 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001064
Michael Jurka447bf842013-05-15 14:52:15 +02001065 if (DEBUG_RESUME_TIME) {
1066 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1067 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001068
1069 if (mWorkspace.getCustomContentCallbacks() != null) {
1070 // If we are resuming and the custom content is the current page, we call onShow().
1071 // It is also poassible that onShow will instead be called slightly after first layout
1072 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1073 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001074 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001075 }
1076 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001077 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001078 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001079
Sunny Goyal756adbc2015-04-16 15:20:51 -07001080 if (!isWorkspaceLoading()) {
1081 // Process any items that were added while Launcher was away.
1082 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1083 }
Adam Cohen9211d422014-10-07 18:14:53 -07001084
1085 if (mLauncherCallbacks != null) {
1086 mLauncherCallbacks.onResume();
1087 }
Adam Cohen06dff352012-06-01 17:17:08 -07001088 }
1089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001092 // Ensure that items added to Launcher are queued until Launcher returns
1093 InstallShortcutReceiver.enableInstallQueue();
1094
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001096 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001097 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001098 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001099
1100 // We call onHide() aggressively. The custom content callbacks should be able to
1101 // debounce excess onHide calls.
1102 if (mWorkspace.getCustomContentCallbacks() != null) {
1103 mWorkspace.getCustomContentCallbacks().onHide();
1104 }
Romain Guycbb89e42009-06-08 15:52:54 -07001105
Adam Cohen9211d422014-10-07 18:14:53 -07001106 if (mLauncherCallbacks != null) {
1107 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001108 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001109 }
1110
1111 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001112 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1113 // by a onResume or by scrolling otherwise.
1114 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001115
1116 // Custom content is completely hidden
1117 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001118
1119 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1120 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001121
1122 // Indicates whether the user is allowed to scroll away from the custom content.
1123 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001124 }
1125
Adam Cohenc2d6e892014-10-16 09:49:24 -07001126 public interface LauncherOverlay {
1127
1128 /**
1129 * Touch interaction leading to overscroll has begun
1130 */
1131 public void onScrollInteractionBegin();
1132
1133 /**
1134 * Touch interaction related to overscroll has ended
1135 */
1136 public void onScrollInteractionEnd();
1137
1138 /**
1139 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1140 * screen (or in the case of RTL, the rightmost screen).
1141 */
1142 public void onScrollChange(int progress, boolean rtl);
1143
1144 /**
1145 * Screen has stopped scrolling
1146 */
1147 public void onScrollSettled();
1148
1149 /**
1150 * This method can be called by the Launcher in order to force the LauncherOverlay
1151 * to exit fully immersive mode.
1152 */
1153 public void forceExitFullImmersion();
1154 }
1155
Winson Chung0f785722015-04-08 10:27:49 -07001156 public interface LauncherAppsCallbacks {
1157 /**
1158 * Updates launcher to the available space that AllApps can take so as not to overlap with
1159 * any other views.
1160 */
1161 public void onAllAppsBoundsChanged(Rect bounds);
1162
1163 /**
1164 * Called to dismiss all apps if it is showing.
1165 */
1166 public void dismissAllApps();
Sunny Goyal5683f872015-05-29 14:54:40 -07001167
1168 /**
1169 * Sets the search manager to be used for app search.
1170 */
1171 public void setSearchManager(AppSearchManager manager);
Winson Chung0f785722015-04-08 10:27:49 -07001172 }
1173
Jun Mukai8af0cd82015-05-12 12:32:12 -07001174 public interface LauncherSearchCallbacks {
1175 /**
1176 * Called when the search overlay is shown.
1177 */
1178 public void onSearchOverlayOpened();
1179
1180 /**
1181 * Called when the search overlay is dismissed.
1182 */
1183 public void onSearchOverlayClosed();
1184 }
1185
Adam Cohenc2d6e892014-10-16 09:49:24 -07001186 public interface LauncherOverlayCallbacks {
1187 /**
1188 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1189 * however it doesn't modify any state within the launcher.
1190 */
1191 public boolean canEnterFullImmersion();
1192
1193 /**
1194 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1195 * eg. by occupying the full screen and handling all touch events.
1196 *
1197 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1198 * case, Launcher will modify any necessary state and assumes the overlay is
1199 * handling all interaction. If false, the LauncherOverlay should cancel any
1200 *
1201 */
1202 public boolean enterFullImmersion();
1203
1204 /**
1205 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1206 * full control over UI and state.
1207 */
1208 public void exitFullImmersion();
1209 }
1210
1211 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1212
1213 @Override
1214 public boolean canEnterFullImmersion() {
1215 return mState == State.WORKSPACE;
1216 }
1217
1218 @Override
1219 public boolean enterFullImmersion() {
1220 if (mState == State.WORKSPACE) {
1221 // When fully immersed, disregard any touches which fall through.
1222 mDragLayer.setBlockTouch(true);
1223 return true;
1224 }
1225 return false;
1226 }
1227
1228 @Override
1229 public void exitFullImmersion() {
1230 mDragLayer.setBlockTouch(false);
1231 }
1232 }
1233
Jorim Jaggid017f882014-01-14 17:08:48 -08001234 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001235 if (mLauncherCallbacks != null) {
1236 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001237 } else {
1238 // On devices with a locked orientation, we will at least have the allow rotation
1239 // setting.
1240 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001241 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001242 }
1243
Adam Cohen9211d422014-10-07 18:14:53 -07001244 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001245 CustomContentCallbacks callbacks, String description) {
1246 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001247 }
1248
Adam Cohenedb40762013-07-18 16:45:45 -07001249 // The custom content needs to offset its content to account for the QSB
1250 public int getTopOffsetForCustomContent() {
1251 return mWorkspace.getPaddingTop();
1252 }
1253
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001254 @Override
1255 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001257 if (mModel.isCurrentCallbacks(this)) {
1258 mModel.stopLoader();
1259 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001260 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1261
Romain Guy13c2e7b2010-03-10 19:45:00 -08001262 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001263 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001264
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001265 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001266 @Override
1267 public void onWindowFocusChanged(boolean hasFocus) {
1268 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001269 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001270
1271 if (mLauncherCallbacks != null) {
1272 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1273 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001274 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 private boolean acceptFilter() {
1277 final InputMethodManager inputManager = (InputMethodManager)
1278 getSystemService(Context.INPUT_METHOD_SERVICE);
1279 return !inputManager.isFullscreenMode();
1280 }
1281
1282 @Override
1283 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001284 final int uniChar = event.getUnicodeChar();
1285 final boolean handled = super.onKeyDown(keyCode, event);
1286 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1287 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1289 keyCode, event);
1290 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001291 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001292 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001293 // showSearchDialog()
1294 // If there are multiple keystrokes before the search dialog takes focus,
1295 // onSearchRequested() will be called for every keystroke,
1296 // but it is idempotent, so it's fine.
1297 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
1299 }
1300
Joe Onorato8a9625e2010-01-28 15:55:35 -08001301 // Eat the long press event so the keyboard doesn't come up.
1302 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1303 return true;
1304 }
1305
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 return handled;
1307 }
1308
Karl Rosaen138a0412009-04-23 19:00:21 -07001309 private String getTypedText() {
1310 return mDefaultKeySsb.toString();
1311 }
1312
1313 private void clearTypedText() {
1314 mDefaultKeySsb.clear();
1315 mDefaultKeySsb.clearSpans();
1316 Selection.setSelection(mDefaultKeySsb, 0);
1317 }
1318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001320 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1321 * State
1322 */
1323 private static State intToState(int stateOrdinal) {
1324 State state = State.WORKSPACE;
1325 final State[] stateValues = State.values();
1326 for (int i = 0; i < stateValues.length; i++) {
1327 if (stateValues[i].ordinal() == stateOrdinal) {
1328 state = stateValues[i];
1329 break;
1330 }
1331 }
1332 return state;
1333 }
1334
1335 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 * Restores the previous state, if it exists.
1337 *
1338 * @param savedState The previous state.
1339 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001340 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 private void restoreState(Bundle savedState) {
1342 if (savedState == null) {
1343 return;
1344 }
1345
Michael Jurka883f55b2010-10-21 15:47:14 -07001346 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001347 if (state == State.APPS || state == State.WIDGETS) {
1348 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001349 }
1350
Adam Cohen21cd0022013-10-09 18:57:02 -07001351 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1352 PagedView.INVALID_RESTORE_PAGE);
1353 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001354 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001358 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001359
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1361 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001362 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1364 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001365 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1366 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001367 AppWidgetProviderInfo info = savedState.getParcelable(
1368 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001369 mPendingAddWidgetInfo = info == null ?
1370 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1371
Adam Cohen4637b5a2013-11-04 18:21:24 -08001372 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001373 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 mRestoring = true;
1375 }
1376
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001377 mItemIdToViewId = (HashMap<Integer, Integer>)
1378 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
1380
1381 /**
1382 * Finds all the views we need and configure them properly.
1383 */
1384 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001385 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001386
Craig Mautner360310b2012-10-26 15:13:08 -07001387 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001388 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001389 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1390 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001391 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001392 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001393
John Spurlock77e1f472013-09-11 10:09:51 -04001394 mLauncherView.setSystemUiVisibility(
1395 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001396 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397
Winson Chung4c98d922011-05-31 16:50:48 -07001398 // Setup the drag layer
1399 mDragLayer.setup(this, dragController);
1400
Winson Chung3d503fb2011-07-13 17:25:49 -07001401 // Setup the hotseat
1402 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1403 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001404 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001405 }
1406
Jorim Jaggid017f882014-01-14 17:08:48 -08001407 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001408 View widgetButton = findViewById(R.id.widget_button);
1409 widgetButton.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 onClickAddWidgetButton(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 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1418
1419 View wallpaperButton = findViewById(R.id.wallpaper_button);
1420 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001421 @Override
1422 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001423 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001424 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001425 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001426 }
1427 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001428 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1429
1430 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001431 if (hasSettings()) {
1432 settingsButton.setOnClickListener(new OnClickListener() {
1433 @Override
1434 public void onClick(View arg0) {
1435 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001436 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001437 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001438 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001439 });
1440 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1441 } else {
1442 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001443 }
1444
Adam Cohendbdff6b2013-09-18 19:09:15 -07001445 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001446
Winson Chung4c98d922011-05-31 16:50:48 -07001447 // Setup the workspace
1448 mWorkspace.setHapticFeedbackEnabled(false);
1449 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001450 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001451 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001452
Winson Chungf0ea4d32011-06-06 14:27:16 -07001453 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001454 mSearchDropTargetBar = (SearchDropTargetBar)
1455 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001456
Winson Chungb745afb2015-03-02 11:51:23 -08001457 // Setup Apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001458 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001459 if (isAllAppsSearchOverridden()) {
1460 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001461 }
Winson Chungb745afb2015-03-02 11:51:23 -08001462
Winson Chungf0ea4d32011-06-06 14:27:16 -07001463 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001464 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001465
Winson Chung3d503fb2011-07-13 17:25:49 -07001466 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001467 dragController.setDragScoller(mWorkspace);
1468 dragController.setScrollView(mDragLayer);
1469 dragController.setMoveTarget(mWorkspace);
1470 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001471 if (mSearchDropTargetBar != null) {
1472 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001473 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001474 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001475
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001476 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001477 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001478 mWeightWatcher = new WeightWatcher(this);
1479 mWeightWatcher.setAlpha(0.5f);
1480 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001481 new FrameLayout.LayoutParams(
1482 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001483 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001484 Gravity.BOTTOM)
1485 );
Adam Cohen39a06042013-07-19 14:30:12 -07001486
1487 boolean show = shouldShowWeightWatcher();
1488 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 }
1491
1492 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001493 * Sets the all apps button. This method is called from {@link Hotseat}.
1494 */
1495 public void setAllAppsButton(View allAppsButton) {
1496 mAllAppsButton = allAppsButton;
1497 }
1498
1499 public View getAllAppsButton() {
1500 return mAllAppsButton;
1501 }
1502
1503 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 * Creates a view representing a shortcut.
1505 *
1506 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001508 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001509 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
1511
1512 /**
1513 * Creates a view representing a shortcut inflated from the specified resource.
1514 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 * @param parent The group the shortcut belongs to.
1516 * @param info The data structure describing the shortcut.
1517 *
1518 * @return A View inflated from layoutResId.
1519 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001520 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001521 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001522 parent, false);
1523 favorite.applyFromShortcutInfo(info, mIconCache);
1524 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001526 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 return favorite;
1528 }
1529
1530 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 * Add a shortcut to the workspace.
1532 *
1533 * @param data The intent describing the shortcut.
1534 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001536 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001537 int cellY) {
1538 int[] cellXY = mTmpAddItemCellCoordinates;
1539 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001540 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001541
Sunny Goyal5c97f512015-05-19 16:03:28 -07001542 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001543 if (info == null) {
1544 return;
1545 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001546 final View view = createShortcut(info);
1547
Sunny Goyal5c97f512015-05-19 16:03:28 -07001548 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001549 // First we check if we already know the exact location where we want to add this item.
1550 if (cellX >= 0 && cellY >= 0) {
1551 cellXY[0] = cellX;
1552 cellXY[1] = cellY;
1553 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001554
1555 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001556 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001557 true, null,null)) {
1558 return;
1559 }
1560 DragObject dragObject = new DragObject();
1561 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001562 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1563 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001564 return;
1565 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001566 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001567 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001568 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001569 foundCellSpan = (result != null);
1570 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001571 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001572 }
1573
1574 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001575 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001576 return;
1577 }
1578
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001579 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580
1581 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001582 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001583 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585 }
1586
Adam Cohen2e6da152015-05-06 11:42:25 -07001587 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1588 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001589 // We want to account for the extra amount of padding that we are adding to the widget
1590 // to ensure that it gets the full amount of space that it has requested
1591 int requiredWidth = minWidth + padding.left + padding.right;
1592 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001593 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001594 }
1595
Adam Cohen2e6da152015-05-06 11:42:25 -07001596 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1597 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001598 }
1599
Adam Cohen2e6da152015-05-06 11:42:25 -07001600 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1601 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001602 }
1603
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001605 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001607 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 */
Adam Cohen091440a2015-03-18 14:16:05 -07001609 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001610 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1611
1612 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001613 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001614 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1615 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001616 }
Romain Guycbb89e42009-06-08 15:52:54 -07001617
Adam Cohen59400422014-03-05 18:07:04 -08001618 if (appWidgetInfo.isCustomWidget) {
1619 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001620 }
1621
Adam Cohen59400422014-03-05 18:07:04 -08001622 LauncherAppWidgetInfo launcherInfo;
1623 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1624 launcherInfo.spanX = info.spanX;
1625 launcherInfo.spanY = info.spanY;
1626 launcherInfo.minSpanX = info.minSpanX;
1627 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001628 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001629
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001631 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632
1633 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001634 if (hostView == null) {
1635 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001636 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1637 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001638 } else {
1639 // The AppWidgetHostView has already been inflated and instantiated
1640 launcherInfo.hostView = hostView;
1641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001643 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001644 launcherInfo.notifyWidgetSizeChanged(this);
1645
Adam Cohen59400422014-03-05 18:07:04 -08001646 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1647 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001648
1649 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001651 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 }
Romain Guycbb89e42009-06-08 15:52:54 -07001653
Adam Cohended9f8d2010-11-03 13:25:16 -07001654 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1655 @Override
1656 public void onReceive(Context context, Intent intent) {
1657 final String action = intent.getAction();
1658 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1659 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001660 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001661 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001662
Winson Chungc100e8e2011-08-09 16:02:43 -07001663 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001664 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001665 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001666 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001667 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001668 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001669 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1670 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001671 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001672 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1673 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001674 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001675 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1676 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1677 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001678 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001679 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1680 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001681 }
1682 }
1683 };
1684
1685 @Override
1686 public void onAttachedToWindow() {
1687 super.onAttachedToWindow();
1688
1689 // Listen for broadcasts related to user-presence
1690 final IntentFilter filter = new IntentFilter();
1691 filter.addAction(Intent.ACTION_SCREEN_OFF);
1692 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001693 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001694 if (ENABLE_DEBUG_INTENTS) {
1695 filter.addAction(DebugIntents.DELETE_DATABASE);
1696 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1697 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001699 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001700 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001701 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 mVisible = true;
1703 }
1704
Adam Cohen0b395352014-06-09 22:54:36 +00001705 /**
1706 * Sets up transparent navigation and status bars in LMP.
1707 * This method is a no-op for other platform versions.
1708 */
Sunny Goyald0091012015-01-20 15:55:34 -08001709 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001710 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001711 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001712 Window window = getWindow();
1713 window.getAttributes().systemUiVisibility |=
1714 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1715 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1716 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1717 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1718 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1719 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1720 window.setStatusBarColor(Color.TRANSPARENT);
1721 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001722 }
1723 }
1724
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 @Override
1726 public void onDetachedFromWindow() {
1727 super.onDetachedFromWindow();
1728 mVisible = false;
1729
Adam Cohend113e0c2010-11-11 10:48:05 -08001730 if (mAttached) {
1731 unregisterReceiver(mReceiver);
1732 mAttached = false;
1733 }
Winson Chungb745afb2015-03-02 11:51:23 -08001734 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 }
1736
1737 public void onWindowVisibilityChanged(int visibility) {
1738 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001739 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001740 // The following code used to be in onResume, but it turns out onResume is called when
1741 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1742 // is a more appropriate event to handle
1743 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001744 if (!mWorkspaceLoading) {
1745 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001746 // We want to let Launcher draw itself at least once before we force it to build
1747 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001748 // apps is nice and speedy.
1749 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001750 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001751 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001752 if (mStarted) return;
1753 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001754 // We delay the layer building a bit in order to give
1755 // other message processing a time to run. In particular
1756 // this avoids a delay in hiding the IME if it was
1757 // currently shown, because doing that may involve
1758 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001759 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001760 final ViewTreeObserver.OnDrawListener listener = this;
1761 mWorkspace.post(new Runnable() {
1762 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001763 if (mWorkspace != null &&
1764 mWorkspace.getViewTreeObserver() != null) {
1765 mWorkspace.getViewTreeObserver().
1766 removeOnDrawListener(listener);
1767 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001768 }
1769 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001770 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001771 }
1772 });
1773 }
1774 clearTypedText();
1775 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 }
1777
Adam Cohen091440a2015-03-18 14:16:05 -07001778 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 mHandler.removeMessages(ADVANCE_MSG);
1780 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1781 mHandler.sendMessageDelayed(msg, delay);
1782 mAutoAdvanceSentTime = System.currentTimeMillis();
1783 }
1784
Adam Cohen091440a2015-03-18 14:16:05 -07001785 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001786 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1787 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1788 mAutoAdvanceRunning = autoAdvanceRunning;
1789 if (autoAdvanceRunning) {
1790 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1791 sendAdvanceMessage(delay);
1792 } else {
1793 if (!mWidgetsToAdvance.isEmpty()) {
1794 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1795 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1796 }
1797 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001798 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001799 }
1800 }
1801 }
1802
1803 private final Handler mHandler = new Handler() {
1804 @Override
1805 public void handleMessage(Message msg) {
1806 if (msg.what == ADVANCE_MSG) {
1807 int i = 0;
1808 for (View key: mWidgetsToAdvance.keySet()) {
1809 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1810 final int delay = mAdvanceStagger * i;
1811 if (v instanceof Advanceable) {
1812 postDelayed(new Runnable() {
1813 public void run() {
1814 ((Advanceable) v).advance();
1815 }
1816 }, delay);
1817 }
1818 i++;
1819 }
1820 sendAdvanceMessage(mAdvanceInterval);
1821 }
1822 }
1823 };
1824
1825 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001826 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001827 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1828 if (v instanceof Advanceable) {
1829 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001830 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001831 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001832 }
1833 }
1834
1835 void removeWidgetToAutoAdvance(View hostView) {
1836 if (mWidgetsToAdvance.containsKey(hostView)) {
1837 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001838 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001839 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 }
1841
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 launcherInfo.hostView = null;
1845 }
1846
Hyunyoung Song3f471442015-04-08 19:01:34 -07001847 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001848 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1849 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001850 }
1851
Winson Chunga6945242014-01-08 14:04:34 -08001852 public DragLayer getDragLayer() {
1853 return mDragLayer;
1854 }
1855
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001856 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001857 return mAppsView;
1858 }
1859
Hyunyoung Song3f471442015-04-08 19:01:34 -07001860 public WidgetsContainerView getWidgetsView() {
1861 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001862 }
1863
Winson Chunga6945242014-01-08 14:04:34 -08001864 public Workspace getWorkspace() {
1865 return mWorkspace;
1866 }
1867
1868 public Hotseat getHotseat() {
1869 return mHotseat;
1870 }
1871
Jorim Jaggid017f882014-01-14 17:08:48 -08001872 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001873 return mOverviewPanel;
1874 }
1875
1876 public SearchDropTargetBar getSearchBar() {
1877 return mSearchDropTargetBar;
1878 }
1879
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001880 public LauncherAppWidgetHost getAppWidgetHost() {
1881 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 }
Romain Guycbb89e42009-06-08 15:52:54 -07001883
Winson Chunga9abd0e2010-10-27 17:18:37 -07001884 public LauncherModel getModel() {
1885 return mModel;
1886 }
1887
Winson Chunga6945242014-01-08 14:04:34 -08001888 protected SharedPreferences getSharedPrefs() {
1889 return mSharedPrefs;
1890 }
1891
Adam Cohen2e6da152015-05-06 11:42:25 -07001892 public DeviceProfile getDeviceProfile() {
1893 return mDeviceProfile;
1894 }
1895
Bjorn Bringertc459e522013-06-07 19:36:01 +01001896 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001897 getWindow().closeAllPanels();
1898
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001899 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001900 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001901 }
1902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 @Override
1904 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001905 long startTime = 0;
1906 if (DEBUG_RESUME_TIME) {
1907 startTime = System.currentTimeMillis();
1908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 super.onNewIntent(intent);
1910
1911 // Close the menu
1912 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001913 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001914 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001915
Adam Cohened307df2013-10-02 09:37:31 -07001916 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1917 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1918 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001919
Adam Cohen6fecd412013-10-02 17:41:50 -07001920 if (mWorkspace == null) {
1921 // Can be cases where mWorkspace is null, this prevents a NPE
1922 return;
1923 }
1924 Folder openFolder = mWorkspace.getOpenFolder();
1925 // In all these cases, only animate if we're already on home
1926 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001927
1928 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1929 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001930 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001931 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001933 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001934
Adam Cohen6fecd412013-10-02 17:41:50 -07001935 closeFolder();
1936 exitSpringLoadedDragMode();
1937
1938 // If we are already on home, then just animate back to the workspace,
1939 // otherwise, just wait until onResume to set the state back to Workspace
1940 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001941 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001942 } else {
1943 mOnResumeState = State.WORKSPACE;
1944 }
1945
1946 final View v = getWindow().peekDecorView();
1947 if (v != null && v.getWindowToken() != null) {
1948 InputMethodManager imm = (InputMethodManager)getSystemService(
1949 INPUT_METHOD_SERVICE);
1950 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1951 }
1952
Winson Chungb745afb2015-03-02 11:51:23 -08001953 // Reset the apps view
1954 if (!alreadyOnHome && mAppsView != null) {
1955 mAppsView.scrollToTop();
1956 }
1957
Hyunyoung Song3f471442015-04-08 19:01:34 -07001958 // Reset the widgets view
1959 if (!alreadyOnHome && mWidgetsView != null) {
1960 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001961 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001962
Adam Cohen9211d422014-10-07 18:14:53 -07001963 if (mLauncherCallbacks != null) {
1964 mLauncherCallbacks.onHomeIntent();
1965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
Adam Cohened307df2013-10-02 09:37:31 -07001967
Michael Jurka447bf842013-05-15 14:52:15 +02001968 if (DEBUG_RESUME_TIME) {
1969 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971
Adam Cohen9211d422014-10-07 18:14:53 -07001972 if (mLauncherCallbacks != null) {
1973 mLauncherCallbacks.onNewIntent(intent);
1974 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001975 }
1976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001978 public void onRestoreInstanceState(Bundle state) {
1979 super.onRestoreInstanceState(state);
1980 for (int page: mSynchronouslyBoundPages) {
1981 mWorkspace.restoreInstanceStateForChild(page);
1982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
1984
1985 @Override
1986 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001987 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001988 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1989 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001990 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001991 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
Michael Jurka883f55b2010-10-21 15:47:14 -07001993 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001994 // We close any open folder since it will not be re-opened, and we need to make sure
1995 // this state is reflected.
1996 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997
Adam Cohendcd297f2013-06-18 13:13:40 -07001998 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001999 mWaitingForResult) {
2000 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002001 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002002 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2003 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002004 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2005 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2006 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002007 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
2009
Hyunyoung Song3f471442015-04-08 19:01:34 -07002010 // Save the current widgets tray?
2011 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002012 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002013
2014 if (mLauncherCallbacks != null) {
2015 mLauncherCallbacks.onSaveInstanceState(outState);
2016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
2019 @Override
2020 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002022
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002023 if (mPreferenceObserverRegistered) {
2024 getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
2025 Context.MODE_MULTI_PROCESS).unregisterOnSharedPreferenceChangeListener(
2026 mSettingsObserver);
2027 mPreferenceObserverRegistered = false;
2028 }
2029
Winson Chunge7a03942011-08-05 15:05:12 -07002030 // Remove all pending runnables
2031 mHandler.removeMessages(ADVANCE_MSG);
2032 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002033 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002034
Winson Chungcd2b0142011-06-08 16:02:26 -07002035 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002036 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002037
2038 // It's possible to receive onDestroy after a new Launcher activity has
2039 // been created. In this case, don't interfere with the new Launcher.
2040 if (mModel.isCurrentCallbacks(this)) {
2041 mModel.stopLoader();
2042 app.setLauncher(null);
2043 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002046 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002048 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002050 mAppWidgetHost = null;
2051
2052 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053
2054 TextKeyListener.getInstance().release();
2055
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002056 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002057 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002058
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002059 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002060 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002061 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002062 mWorkspace = null;
2063 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002064
Michael Jurka2ecf9952012-06-18 12:52:28 -07002065 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002066
2067 if (mLauncherCallbacks != null) {
2068 mLauncherCallbacks.onDestroy();
2069 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
2071
Adam Cohena9cf38f2011-05-02 15:36:58 -07002072 public DragController getDragController() {
2073 return mDragController;
2074 }
2075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 @Override
2077 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002078 onStartForResult(requestCode);
2079 super.startActivityForResult(intent, requestCode);
2080 }
2081
2082 @Override
2083 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2084 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2085 onStartForResult(requestCode);
2086 try {
2087 super.startIntentSenderForResult(intent, requestCode,
2088 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2089 } catch (IntentSender.SendIntentException e) {
2090 throw new ActivityNotFoundException();
2091 }
2092 }
2093
2094 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002095 if (requestCode >= 0) {
2096 setWaitingForResult(true);
2097 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 }
2099
Winson Chung70d72102011-08-12 11:24:35 -07002100 /**
2101 * Indicates that we want global search for this activity by setting the globalSearch
2102 * argument for {@link #startSearch} to true.
2103 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002105 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002107
Karl Rosaen138a0412009-04-23 19:00:21 -07002108 if (initialQuery == null) {
2109 // Use any text typed in the launcher as the initial query
2110 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 if (appSearchData == null) {
2113 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002114 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 }
Winson Chungadf0c182012-08-23 14:59:07 -07002116 Rect sourceBounds = new Rect();
2117 if (mSearchDropTargetBar != null) {
2118 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2119 }
Romain Guycbb89e42009-06-08 15:52:54 -07002120
Ian Parkinson047036e2014-09-01 15:40:53 +01002121 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002122 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002123 if (clearTextImmediately) {
2124 clearTypedText();
2125 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002126
2127 // We need to show the workspace after starting the search
2128 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002129 }
2130
Ian Parkinson047036e2014-09-01 15:40:53 +01002131 /**
2132 * Start a text search.
2133 *
2134 * @return {@code true} if the search will start immediately, so any further keypresses
2135 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2136 * to buffer keypresses.
2137 */
2138 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002139 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002140 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2141 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2142 sourceBounds);
2143 }
2144
Michael Jurkaa33411c2012-06-14 16:18:21 -07002145 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002146 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002147 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002148 }
2149
2150 /**
2151 * Starts the global search activity. This code is a copied from SearchManager
2152 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002153 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002154 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002155 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002156 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2157 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2158 if (globalSearchActivity == null) {
2159 Log.w(TAG, "No global search activity found.");
2160 return;
2161 }
2162 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2163 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2164 intent.setComponent(globalSearchActivity);
2165 // Make sure that we have a Bundle to put source in
2166 if (appSearchData == null) {
2167 appSearchData = new Bundle();
2168 } else {
2169 appSearchData = new Bundle(appSearchData);
2170 }
Adam Cohen9211d422014-10-07 18:14:53 -07002171 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002172 if (!appSearchData.containsKey("source")) {
2173 appSearchData.putString("source", getPackageName());
2174 }
2175 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2176 if (!TextUtils.isEmpty(initialQuery)) {
2177 intent.putExtra(SearchManager.QUERY, initialQuery);
2178 }
2179 if (selectInitialQuery) {
2180 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2181 }
2182 intent.setSourceBounds(sourceBounds);
2183 try {
2184 startActivity(intent);
2185 } catch (ActivityNotFoundException ex) {
2186 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 }
2189
Yura4f93ec62014-02-04 14:15:21 +00002190 public boolean isOnCustomContent() {
2191 return mWorkspace.isOnOrMovingToCustomContent();
2192 }
2193
Winson Chung70d72102011-08-12 11:24:35 -07002194 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002195 public boolean onPrepareOptionsMenu(Menu menu) {
2196 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002197 if (!isOnCustomContent()) {
2198 // Close any open folders
2199 closeFolder();
2200 // Stop resizing any widgets
2201 mWorkspace.exitWidgetResizeMode();
2202 if (!mWorkspace.isInOverviewMode()) {
2203 // Show the overview mode
2204 showOverviewMode(true);
2205 } else {
2206 showWorkspace(true);
2207 }
Winson Chunge0298742014-01-17 12:03:00 -08002208 }
Adam Cohen9211d422014-10-07 18:14:53 -07002209 if (mLauncherCallbacks != null) {
2210 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2211 }
2212
Michael Jurkab94f3f82013-09-11 18:08:54 +02002213 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002216 @Override
2217 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002218 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002219 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002220 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002221 }
2222
Joe Onorato9c1289c2009-08-17 11:03:03 -04002223 public boolean isWorkspaceLocked() {
2224 return mWorkspaceLoading || mWaitingForResult;
2225 }
2226
Adam Cohen517a7f52014-03-01 12:12:59 -08002227 public boolean isWorkspaceLoading() {
2228 return mWorkspaceLoading;
2229 }
2230
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002231 private void setWorkspaceLoading(boolean value) {
2232 boolean isLocked = isWorkspaceLocked();
2233 mWorkspaceLoading = value;
2234 if (isLocked != isWorkspaceLocked()) {
2235 onWorkspaceLockedChanged();
2236 }
2237 }
2238
2239 private void setWaitingForResult(boolean value) {
2240 boolean isLocked = isWorkspaceLocked();
2241 mWaitingForResult = value;
2242 if (isLocked != isWorkspaceLocked()) {
2243 onWorkspaceLockedChanged();
2244 }
2245 }
2246
Adam Cohen9211d422014-10-07 18:14:53 -07002247 protected void onWorkspaceLockedChanged() {
2248 if (mLauncherCallbacks != null) {
2249 mLauncherCallbacks.onWorkspaceLockedChanged();
2250 }
2251 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002252
Michael Jurka0280c3b2010-09-17 15:00:07 -07002253 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002254 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002255 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2257 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002258 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002259 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002260 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002261
Sunny Goyale6b63a32015-01-16 16:14:29 -08002262 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002263 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002264 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2265 }
2266
Sunny Goyale6b63a32015-01-16 16:14:29 -08002267 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002268 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2269 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002270 if (appWidgetInfo.configure != null) {
2271 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002272 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002273
Bjorn Bringert7984c942009-12-09 15:38:25 +00002274 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002275 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2276 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002279 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002280 Runnable onComplete = new Runnable() {
2281 @Override
2282 public void run() {
2283 // Exit spring loaded mode if necessary after adding the widget
2284 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2285 null);
2286 }
2287 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002288 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002289 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002290 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 }
2292 }
Romain Guycbb89e42009-06-08 15:52:54 -07002293
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002294 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002295 // Close any folders that may be open.
2296 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002297 mWorkspace.moveToCustomContentScreen(animate);
2298 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002299
2300 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2301 int[] cell, int spanX, int spanY) {
2302 switch (info.itemType) {
2303 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2304 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2305 int span[] = new int[2];
2306 span[0] = spanX;
2307 span[1] = spanY;
2308 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2309 container, screenId, cell, span);
2310 break;
2311 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2312 processShortcutFromDrop(info.componentName, container, screenId, cell);
2313 break;
2314 default:
2315 throw new IllegalStateException("Unknown item type: " + info.itemType);
2316 }
2317 }
2318
Adam Cohenfbba09b2011-07-18 21:43:05 -07002319 /**
2320 * Process a shortcut drop.
2321 *
2322 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002326 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2327 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002328 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002329 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002331 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332
2333 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002334 mPendingAddInfo.cellX = cell[0];
2335 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002337
2338 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2339 createShortcutIntent.setComponent(componentName);
2340 processShortcut(createShortcutIntent);
2341 }
2342
Adam Cohenfbba09b2011-07-18 21:43:05 -07002343 /**
2344 * Process a widget drop.
2345 *
2346 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002347 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002348 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002349 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002350 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2351 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002352 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002354 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002355 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002356 mPendingAddInfo.minSpanX = info.minSpanX;
2357 mPendingAddInfo.minSpanY = info.minSpanY;
2358
Adam Cohenfbba09b2011-07-18 21:43:05 -07002359 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002360 mPendingAddInfo.cellX = cell[0];
2361 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002363 if (span != null) {
2364 mPendingAddInfo.spanX = span[0];
2365 mPendingAddInfo.spanY = span[1];
2366 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002367
Adam Cohened66b2b2012-01-23 17:28:51 -08002368 AppWidgetHostView hostView = info.boundWidget;
2369 int appWidgetId;
2370 if (hostView != null) {
2371 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002372 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002373
2374 // Clear the boundWidget so that it doesn't get destroyed.
2375 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002376 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002377 // In this case, we either need to start an activity to get permission to bind
2378 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002379 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002380 Bundle options = info.bindOptions;
2381
Sunny Goyalffe83f12014-08-14 17:39:34 -07002382 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2383 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002384 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002385 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002386 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002387 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002388 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2389 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2390 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002391 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2392 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002393 // TODO: we need to make sure that this accounts for the options bundle.
2394 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002395 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2396 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002397 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002398 }
2399
Joe Onoratodeb98af2010-02-19 14:59:39 -08002400 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002401 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 }
2403
Winson Chung24ab2f12010-09-16 14:10:47 -07002404 void processWallpaper(Intent intent) {
2405 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2406 }
2407
Adam Cohendcd297f2013-06-18 13:13:40 -07002408 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002409 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002410 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 folderInfo.title = getText(R.string.folder_name);
2412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002414 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2415 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002416 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417
2418 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002419 FolderIcon newFolder =
2420 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002421 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002422 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002423 // Force measure the new folder icon
2424 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2425 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002426 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 }
Romain Guycbb89e42009-06-08 15:52:54 -07002428
Joe Onorato9c1289c2009-08-17 11:03:03 -04002429 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002430 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002431 }
2432
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002433 /**
2434 * Registers various content observers. The current implementation registers
2435 * only a favorites observer to keep track of the favorites applications.
2436 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002437 private void registerContentObservers() {
2438 ContentResolver resolver = getContentResolver();
2439 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2440 true, mWidgetObserver);
2441 }
2442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 @Override
2444 public boolean dispatchKeyEvent(KeyEvent event) {
2445 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2446 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002448 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002449 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002450 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002451 dumpState();
2452 return true;
2453 }
2454 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002455 }
2456 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2457 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002458 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 return true;
2460 }
2461 }
2462
2463 return super.dispatchKeyEvent(event);
2464 }
2465
Joe Onorato88ec0992009-11-19 13:16:06 -08002466 @Override
2467 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002468 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2469 return;
2470 }
2471
Winson Chung3d9490a2015-03-24 17:38:42 -07002472 LauncherAccessibilityDelegate delegate =
2473 LauncherAppState.getInstance().getAccessibilityDelegate();
2474 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002475 return;
2476 }
2477
Winson Chungb745afb2015-03-02 11:51:23 -08002478 if (isAppsViewVisible()) {
2479 showWorkspace(true);
2480 } else if (isWidgetsViewVisible()) {
2481 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002482 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002483 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002484 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002485 Folder openFolder = mWorkspace.getOpenFolder();
2486 if (openFolder.isEditingName()) {
2487 openFolder.dismissEditingName();
2488 } else {
2489 closeFolder();
2490 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002491 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002492 mWorkspace.exitWidgetResizeMode();
2493
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002494 // Back button is a no-op here, but give at least some feedback for the button press
2495 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002496 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002497 }
2498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002500 * Re-listen when widgets are reset.
2501 */
Adam Cohen091440a2015-03-18 14:16:05 -07002502 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002503 if (mAppWidgetHost != null) {
2504 mAppWidgetHost.startListening();
2505 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002506 }
2507
2508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 * Launches the intent referred by the clicked shortcut.
2510 *
2511 * @param v The view representing the clicked shortcut.
2512 */
2513 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002514 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2515 // view has detached (it's possible for this to happen if the view is removed mid touch).
2516 if (v.getWindowToken() == null) {
2517 return;
2518 }
2519
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002520 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002521 return;
2522 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002523
Adam Cohen1697b792013-09-17 19:08:21 -07002524 if (v instanceof Workspace) {
2525 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002526 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002527 }
2528 return;
2529 }
2530
2531 if (v instanceof CellLayout) {
2532 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002533 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002534 }
2535 }
2536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002538 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002539 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002541 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002542 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002543 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002544 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002546 } else if (tag instanceof AppInfo) {
2547 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002548 } else if (tag instanceof LauncherAppWidgetInfo) {
2549 if (v instanceof PendingAppWidgetHostView) {
2550 onClickPendingWidget((PendingAppWidgetHostView) v);
2551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 }
2553 }
2554
Sunny Goyal508da152014-08-14 10:53:27 -07002555 public void onClickPagedViewIcon(View v) {
2556 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002557 if (mLauncherCallbacks != null) {
2558 mLauncherCallbacks.onClickPagedViewIcon(v);
2559 }
Sunny Goyal508da152014-08-14 10:53:27 -07002560 }
2561
Sunny Goyal70660032015-05-14 00:07:08 -07002562 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002563 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002564 return false;
2565 }
2566
Michael Jurkaaf442092010-06-10 17:01:57 -07002567 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002568 * Event handler for the app widget view which has not fully restored.
2569 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002570 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002571 if (mIsSafeModeEnabled) {
2572 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2573 return;
2574 }
2575
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002576 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002577 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002578 int widgetId = info.appWidgetId;
2579 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2580 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002581 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2582 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002583 mPendingAddInfo.copyFrom(info);
2584 mPendingAddWidgetId = widgetId;
2585
Sunny Goyalffe83f12014-08-14 17:39:34 -07002586 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2587 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002588 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002589 } else if (info.installProgress < 0) {
2590 // The install has not been queued
2591 final String packageName = info.providerName.getPackageName();
2592 showBrokenAppInstallDialog(packageName,
2593 new DialogInterface.OnClickListener() {
2594 public void onClick(DialogInterface dialog, int id) {
2595 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2596 }
2597 });
2598 } else {
2599 // Download has started.
2600 final String packageName = info.providerName.getPackageName();
2601 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002602 }
2603 }
2604
2605 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002606 * Event handler for the "grid" button that appears on the home screen, which
2607 * enters all apps mode.
2608 *
2609 * @param v The view that was clicked.
2610 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002611 protected void onClickAllAppsButton(View v) {
2612 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002613 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002614 showWorkspace(true);
2615 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002616 // Try and refresh the set of predicted apps before we enter launcher
2617 showAppsView(true /* animated */, false /* resetListToTop */,
2618 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 }
2620 }
2621
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002622 private void showBrokenAppInstallDialog(final String packageName,
2623 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002624 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002625 .setTitle(R.string.abandoned_promises_title)
2626 .setMessage(R.string.abandoned_promise_explanation)
2627 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2628 .setNeutralButton(R.string.abandoned_clean_this,
2629 new DialogInterface.OnClickListener() {
2630 public void onClick(DialogInterface dialog, int id) {
2631 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2632 mWorkspace.removeAbandonedPromise(packageName, user);
2633 }
2634 })
2635 .create().show();
2636 return;
2637 }
2638
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 /**
2640 * Event handler for an app shortcut click.
2641 *
2642 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2643 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002644 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002645 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2646 Object tag = v.getTag();
2647 if (!(tag instanceof ShortcutInfo)) {
2648 throw new IllegalArgumentException("Input must be a Shortcut");
2649 }
2650
2651 // Open shortcut
2652 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002653
2654 if (shortcut.isDisabled != 0) {
2655 int error = R.string.activity_not_available;
2656 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2657 error = R.string.safemode_shortcut_error;
2658 }
2659 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2660 return;
2661 }
2662
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002663 final Intent intent = shortcut.intent;
2664
2665 // Check for special shortcuts
2666 if (intent.getComponent() != null) {
2667 final String shortcutClass = intent.getComponent().getClassName();
2668
2669 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2670 MemoryDumpActivity.startDump(this);
2671 return;
2672 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2673 toggleShowWeightWatcher();
2674 return;
2675 }
2676 }
2677
Chris Wren40c5ed32014-06-24 18:24:23 -04002678 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002679 if ((v instanceof BubbleTextView)
2680 && shortcut.isPromise()
2681 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002682 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002683 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 new DialogInterface.OnClickListener() {
2685 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002686 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002688 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002689 return;
2690 }
2691
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002692 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002693 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002694
2695 if (mLauncherCallbacks != null) {
2696 mLauncherCallbacks.onClickAppShortcut(v);
2697 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002698 }
2699
Adam Cohen091440a2015-03-18 14:16:05 -07002700 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002701 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002702 final ShortcutInfo shortcut;
2703 final Intent intent;
2704 if (tag instanceof ShortcutInfo) {
2705 shortcut = (ShortcutInfo) tag;
2706 intent = shortcut.intent;
2707 int[] pos = new int[2];
2708 v.getLocationOnScreen(pos);
2709 intent.setSourceBounds(new Rect(pos[0], pos[1],
2710 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002711
Sunny Goyal508da152014-08-14 10:53:27 -07002712 } else if (tag instanceof AppInfo) {
2713 shortcut = null;
2714 intent = ((AppInfo) tag).intent;
2715 } else {
2716 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2717 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002718
2719 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002720 mStats.recordLaunch(intent, shortcut);
2721
2722 if (success && v instanceof BubbleTextView) {
2723 mWaitingForResume = (BubbleTextView) v;
2724 mWaitingForResume.setStayPressed(true);
2725 }
2726 }
2727
2728 /**
2729 * Event handler for a folder icon click.
2730 *
2731 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2732 */
2733 protected void onClickFolderIcon(View v) {
2734 if (LOGD) Log.d(TAG, "onClickFolder");
2735 if (!(v instanceof FolderIcon)){
2736 throw new IllegalArgumentException("Input must be a FolderIcon");
2737 }
2738
2739 FolderIcon folderIcon = (FolderIcon) v;
2740 final FolderInfo info = folderIcon.getFolderInfo();
2741 Folder openFolder = mWorkspace.getFolderForTag(info);
2742
2743 // If the folder info reports that the associated folder is open, then verify that
2744 // it is actually opened. There have been a few instances where this gets out of sync.
2745 if (info.opened && openFolder == null) {
2746 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2747 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2748 info.opened = false;
2749 }
2750
2751 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2752 // Close any open folder
2753 closeFolder();
2754 // Open the requested folder
2755 openFolder(folderIcon);
2756 } else {
2757 // Find the open folder...
2758 int folderScreen;
2759 if (openFolder != null) {
2760 folderScreen = mWorkspace.getPageForView(openFolder);
2761 // .. and close it
2762 closeFolder(openFolder);
2763 if (folderScreen != mWorkspace.getCurrentPage()) {
2764 // Close any folder open on the current screen
2765 closeFolder();
2766 // Pull the folder onto this screen
2767 openFolder(folderIcon);
2768 }
2769 }
2770 }
Adam Cohen9211d422014-10-07 18:14:53 -07002771
2772 if (mLauncherCallbacks != null) {
2773 mLauncherCallbacks.onClickFolderIcon(v);
2774 }
Michael Jurka5130e402011-10-13 04:55:35 -07002775 }
2776
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002777 /**
2778 * Event handler for the (Add) Widgets button that appears after a long press
2779 * on the home screen.
2780 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002781 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002782 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002783 if (mIsSafeModeEnabled) {
2784 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2785 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002786 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002787 if (mLauncherCallbacks != null) {
2788 mLauncherCallbacks.onClickAddWidgetButton(view);
2789 }
Adam Cohen9211d422014-10-07 18:14:53 -07002790 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002791 }
2792
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002793 /**
2794 * Event handler for the wallpaper picker button that appears after a long press
2795 * on the home screen.
2796 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002797 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002798 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002799 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2800 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002801
2802 if (mLauncherCallbacks != null) {
2803 mLauncherCallbacks.onClickWallpaperPicker(v);
2804 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002805 }
2806
2807 /**
2808 * Event handler for a click on the settings button that appears after a long press
2809 * on the home screen.
2810 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002811 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002812 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002813 if (mLauncherCallbacks != null) {
2814 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002815 } else {
2816 showSettingsActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002817 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002818 }
2819
Michael Jurka5130e402011-10-13 04:55:35 -07002820 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002821 // Provide the same haptic feedback that the system offers for virtual keys.
2822 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002823 }
2824
Adam Cohen61f560d2013-09-30 15:58:20 -07002825 public void performHapticFeedbackOnTouchDown(View v) {
2826 // Provide the same haptic feedback that the system offers for virtual keys.
2827 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2828 }
2829
2830 public View.OnTouchListener getHapticFeedbackTouchListener() {
2831 if (mHapticFeedbackTouchListener == null) {
2832 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002833 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002834 @Override
2835 public boolean onTouch(View v, MotionEvent event) {
2836 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2837 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2838 }
2839 return false;
2840 }
2841 };
2842 }
2843 return mHapticFeedbackTouchListener;
2844 }
2845
Adam Cohen9211d422014-10-07 18:14:53 -07002846 public void onDragStarted(View view) {
2847 if (isOnCustomContent()) {
2848 // Custom content screen doesn't participate in drag and drop. If on custom
2849 // content screen, move to default.
2850 moveWorkspaceToDefaultScreen();
2851 }
2852
2853 if (mLauncherCallbacks != null) {
2854 mLauncherCallbacks.onDragStarted(view);
2855 }
2856 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002857
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002858 /**
2859 * Called when the user stops interacting with the launcher.
2860 * This implies that the user is now on the homescreen and is not doing housekeeping.
2861 */
Adam Cohen9211d422014-10-07 18:14:53 -07002862 protected void onInteractionEnd() {
2863 if (mLauncherCallbacks != null) {
2864 mLauncherCallbacks.onInteractionEnd();
2865 }
2866 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002867
2868 /**
2869 * Called when the user starts interacting with the launcher.
2870 * The possible interactions are:
2871 * - open all apps
2872 * - reorder an app shortcut, or a widget
2873 * - open the overview mode.
2874 * This is a good time to stop doing things that only make sense
2875 * when the user is on the homescreen and not doing housekeeping.
2876 */
Adam Cohen9211d422014-10-07 18:14:53 -07002877 protected void onInteractionBegin() {
2878 if (mLauncherCallbacks != null) {
2879 mLauncherCallbacks.onInteractionBegin();
2880 }
2881 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002882
Winson Chungcd99cd32015-04-29 11:03:24 -07002883 /** Updates the interaction state. */
2884 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002885 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002886 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002887 boolean fromStateWithOverlay;
2888 boolean toStateWithOverlay;
2889 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2890 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2891 toStateWithOverlay = toState != Workspace.State.NORMAL;
2892 } else {
2893 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2894 fromState != Workspace.State.NORMAL_HIDDEN;
2895 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2896 toState != Workspace.State.NORMAL_HIDDEN;
2897 }
2898 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002899 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002900 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002901 onInteractionEnd();
2902 }
2903 }
2904
Kenny Guyf07af7b2014-07-31 11:39:16 +01002905 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002906 try {
2907 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002908 launcherApps.showAppDetailsForProfile(componentName, user);
2909 } catch (SecurityException e) {
2910 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2911 Log.e(TAG, "Launcher does not have permission to launch settings");
2912 } catch (ActivityNotFoundException e) {
2913 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2914 Log.e(TAG, "Unable to launch settings");
2915 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002916 }
2917
Michael Jurka1e2f4652013-07-08 18:03:46 -07002918 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002919 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2920 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002921 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002922 // System applications cannot be installed. For now, show a toast explaining that.
2923 // We may give them the option of disabling apps this way.
2924 int messageId = R.string.uninstall_system_app_text;
2925 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002926 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002927 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002928 String packageName = componentName.getPackageName();
2929 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002930 Intent intent = new Intent(
2931 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002932 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2933 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002934 if (user != null) {
2935 user.addToIntent(intent, Intent.EXTRA_USER);
2936 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002937 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002938 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002939 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002940 }
2941
Michael Jurka86a720e2012-05-09 11:23:23 -07002942 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002943 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002945 // Only launch using the new animation if the shortcut has not opted out (this is a
2946 // private contract between launcher and may be ignored in the future).
2947 boolean useLaunchAnimation = (v != null) &&
2948 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002949 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2950 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002951
Kenny Guy1317e2d2014-05-08 18:52:50 +01002952 UserHandleCompat user = null;
2953 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2954 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2955 user = userManager.getUserForSerialNumber(serialNumber);
2956 }
2957
2958 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002959 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002960 ActivityOptions opts = null;
2961 if (sClipRevealMethod != null) {
2962 // TODO: call method directly when Launcher3 can depend on M APIs
2963 int left = 0, top = 0;
2964 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2965 if (v instanceof TextView) {
2966 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002967 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2968 if (icon != null) {
2969 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002970 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002971 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002972 width = bounds.width();
2973 height = bounds.height();
2974 }
2975 }
2976 try {
2977 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2978 left, top, width, height);
2979 } catch (IllegalAccessException e) {
2980 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2981 sClipRevealMethod = null;
2982 } catch (InvocationTargetException e) {
2983 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2984 sClipRevealMethod = null;
2985 }
2986 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002987 if (opts == null && !Utilities.isLmpOrAbove()) {
2988 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002989 v.getMeasuredWidth(), v.getMeasuredHeight());
2990 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002991 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002992 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002993
2994 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2995 // Could be launching some bookkeeping activity
2996 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002997 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002998 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002999 launcherApps.startActivityForProfile(intent.getComponent(), user,
3000 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003001 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003002 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 } catch (SecurityException e) {
3004 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003005 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003007 "or use the exported attribute for this activity. "
3008 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003010 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003011 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003012
Michael Jurka86a720e2012-05-09 11:23:23 -07003013 boolean startActivitySafely(View v, Intent intent, Object tag) {
3014 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003015 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3016 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3017 return false;
3018 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003019 try {
3020 success = startActivity(v, intent, tag);
3021 } catch (ActivityNotFoundException e) {
3022 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3023 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3024 }
3025 return success;
3026 }
3027
Adam Cohen268c4752012-06-06 17:47:33 -07003028 /**
3029 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3030 * in the DragLayer in the exact absolute location of the original FolderIcon.
3031 */
3032 private void copyFolderIconToImage(FolderIcon fi) {
3033 final int width = fi.getMeasuredWidth();
3034 final int height = fi.getMeasuredHeight();
3035
3036 // Lazy load ImageView, Bitmap and Canvas
3037 if (mFolderIconImageView == null) {
3038 mFolderIconImageView = new ImageView(this);
3039 }
3040 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3041 mFolderIconBitmap.getHeight() != height) {
3042 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3043 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3044 }
3045
3046 DragLayer.LayoutParams lp;
3047 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3048 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3049 } else {
3050 lp = new DragLayer.LayoutParams(width, height);
3051 }
3052
Adam Cohen307fe232012-08-16 17:55:58 -07003053 // The layout from which the folder is being opened may be scaled, adjust the starting
3054 // view size by this scale factor.
3055 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003056 lp.customPosition = true;
3057 lp.x = mRectForFolderAnimation.left;
3058 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003059 lp.width = (int) (scale * width);
3060 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003061
3062 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3063 fi.draw(mFolderIconCanvas);
3064 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003065 if (fi.getFolder() != null) {
3066 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3067 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003068 }
Adam Cohen268c4752012-06-06 17:47:33 -07003069 // Just in case this image view is still in the drag layer from a previous animation,
3070 // we remove it and re-add it.
3071 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3072 mDragLayer.removeView(mFolderIconImageView);
3073 }
3074 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003075 if (fi.getFolder() != null) {
3076 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003077 }
Adam Cohen268c4752012-06-06 17:47:33 -07003078 }
3079
Adam Cohen2801caf2011-05-13 20:57:39 -07003080 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003081 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003082 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3083 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3084 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3085
Adam Cohenc51934b2011-07-26 21:07:43 -07003086 FolderInfo info = (FolderInfo) fi.getTag();
3087 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3088 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003089 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3090 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003091 }
3092
Adam Cohen268c4752012-06-06 17:47:33 -07003093 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3094 copyFolderIconToImage(fi);
3095 fi.setVisibility(View.INVISIBLE);
3096
Michael Jurka2ecf9952012-06-18 12:52:28 -07003097 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003098 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003099 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003100 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3101 }
3102 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003103 oa.start();
3104 }
3105
Adam Cohen268c4752012-06-06 17:47:33 -07003106 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003107 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003108 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3109 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3110 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3111
Adam Cohen268c4752012-06-06 17:47:33 -07003112 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003113
Adam Cohen268c4752012-06-06 17:47:33 -07003114 // We remove and re-draw the FolderIcon in-case it has changed
3115 mDragLayer.removeView(mFolderIconImageView);
3116 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003117 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003118 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003119 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003120 oa.addListener(new AnimatorListenerAdapter() {
3121 @Override
3122 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003123 if (cl != null) {
3124 cl.clearFolderLeaveBehind();
3125 // Remove the ImageView copy of the FolderIcon and make the original visible.
3126 mDragLayer.removeView(mFolderIconImageView);
3127 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003128 }
3129 }
3130 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003131 oa.start();
3132 }
3133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003135 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 * is animated relative to the specified View. If the View is null, no animation
3137 * is played.
3138 *
3139 * @param folderInfo The FolderInfo describing the folder to open.
3140 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003141 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003142 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003143 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3144 if (openFolder != null && openFolder != folder) {
3145 // Close any open folder before opening a folder.
3146 closeFolder();
3147 }
3148
Adam Cohena9cf38f2011-05-02 15:36:58 -07003149 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150
Adam Cohena9cf38f2011-05-02 15:36:58 -07003151 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152
Sunny Goyalf4066152015-04-15 09:42:19 -07003153 // While the folder is open, the position of the icon cannot change.
3154 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3155
Adam Cohen4554ee12011-08-03 16:13:21 -07003156 // Just verify that the folder hasn't already been added to the DragLayer.
3157 // There was a one-off crash where the folder had a parent already.
3158 if (folder.getParent() == null) {
3159 mDragLayer.addView(folder);
3160 mDragController.addDropTarget((DropTarget) folder);
3161 } else {
3162 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3163 folder.getParent() + ").");
3164 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003165 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003166 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003167
3168 // Notify the accessibility manager that this folder "window" has appeared and occluded
3169 // the workspace items
3170 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3171 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003172 }
3173
3174 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003175 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003176 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003177 if (folder.isEditingName()) {
3178 folder.dismissEditingName();
3179 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003180 closeFolder(folder);
3181 }
3182 }
3183
Sunny Goyal83a8f042015-05-19 12:52:12 -07003184 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003185 folder.getInfo().opened = false;
3186
3187 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3188 if (parent != null) {
3189 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3190 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003191 if (fi != null) {
3192 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3193 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003194 }
3195 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003196
3197 // Notify the accessibility manager that this folder "window" has disappeard and no
3198 // longer occludeds the workspace items
3199 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003200 }
3201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003203 if (!isDraggingEnabled()) return false;
3204 if (isWorkspaceLocked()) return false;
3205 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206
Adam Cohen1697b792013-09-17 19:08:21 -07003207 if (v instanceof Workspace) {
3208 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003209 if (!mWorkspace.isTouchActive()) {
3210 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003211 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3212 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3213 return true;
3214 } else {
3215 return false;
3216 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003217 } else {
3218 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003219 }
Adam Cohen1697b792013-09-17 19:08:21 -07003220 }
3221
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003222 CellLayout.CellInfo longClickCellInfo = null;
3223 View itemUnderLongClick = null;
3224 if (v.getTag() instanceof ItemInfo) {
3225 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003226 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003227 itemUnderLongClick = longClickCellInfo.cell;
3228 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003229 }
3230
Winson Chung3d503fb2011-07-13 17:25:49 -07003231 // The hotseat touch handling does not go through Workspace, and we always allow long press
3232 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003233 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003234 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003235 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003236 // User long pressed on empty space
3237 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3238 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003239 if (mWorkspace.isInOverviewMode()) {
3240 mWorkspace.startReordering(v);
3241 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003242 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003245 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3246 mHotseat.getOrderInHotseat(
3247 longClickCellInfo.cellX,
3248 longClickCellInfo.cellY));
3249 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003250 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003251 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 }
3253 }
3254 }
3255 return true;
3256 }
3257
Winson Chung3d503fb2011-07-13 17:25:49 -07003258 boolean isHotseatLayout(View layout) {
3259 return mHotseat != null && layout != null &&
3260 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3261 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003262
Winson Chung3d503fb2011-07-13 17:25:49 -07003263 /**
3264 * Returns the CellLayout of the specified container at the specified screen.
3265 */
Sunny Goyal92820592015-03-02 11:31:35 -08003266 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003267 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3268 if (mHotseat != null) {
3269 return mHotseat.getLayout();
3270 } else {
3271 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003272 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003273 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003274 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003275 }
3276 }
3277
Winson Chungb745afb2015-03-02 11:51:23 -08003278 /**
3279 * For overridden classes.
3280 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003281 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003282 return isAppsViewVisible();
3283 }
3284
3285 public boolean isAppsViewVisible() {
3286 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3287 }
3288
3289 public boolean isWidgetsViewVisible() {
3290 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003291 }
3292
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003293 private void setWorkspaceBackground(int background) {
3294 switch (background) {
3295 case WORKSPACE_BACKGROUND_TRANSPARENT:
3296 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3297 break;
3298 case WORKSPACE_BACKGROUND_BLACK:
3299 getWindow().setBackgroundDrawable(null);
3300 break;
3301 default:
3302 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3303 }
Craig Mautner360310b2012-10-26 15:13:08 -07003304 }
3305
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003306 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003307 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3308 int curflags = getWindow().getAttributes().flags
3309 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3310 if (wpflags != curflags) {
3311 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3312 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003313 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003314 }
3315
Michael Jurkae326f182011-11-21 14:05:46 -08003316 @Override
3317 public void onTrimMemory(int level) {
3318 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003319 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3320 // The widget preview db can result in holding onto over
3321 // 3MB of memory for caching which isn't necessary.
3322 SQLiteDatabase.releaseMemory();
3323
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003324 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003325 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003326 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003327 if (mLauncherCallbacks != null) {
3328 mLauncherCallbacks.onTrimMemory(level);
3329 }
Michael Jurkae326f182011-11-21 14:05:46 -08003330 }
3331
Winson Chungb745afb2015-03-02 11:51:23 -08003332 @Override
3333 public void onStateTransitionHideSearchBar() {
3334 // Hide the search bar
3335 if (mSearchDropTargetBar != null) {
3336 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3337 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003338 }
3339
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003340 public void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003341 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3342 true);
Adam Cohened307df2013-10-02 09:37:31 -07003343 }
3344
Sunny Goyal83a8f042015-05-19 12:52:12 -07003345 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003346 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3347 onCompleteRunnable, true);
3348 }
3349
3350 protected void showWorkspace(int snapToPage, boolean animated) {
3351 showWorkspace(snapToPage, animated, null, true);
3352 }
3353
3354 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3355 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003356 boolean changed = mState != State.WORKSPACE ||
3357 mWorkspace.getState() != Workspace.State.NORMAL;
3358 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003359 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003360 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003361 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003362 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003363
Winson Chungc7d2b602012-05-16 17:02:20 -07003364 // Show the search bar (only animate if we were showing the drop target bar in spring
3365 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003366 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003367 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003368 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003369
Michael Jurkab3e22d92011-10-31 15:58:33 -07003370 // Set focus to the AppsCustomize button
3371 if (mAllAppsButton != null) {
3372 mAllAppsButton.requestFocus();
3373 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003374 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003375
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003376 // Change the state *after* we've called all the transition code
3377 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003378
Winson Chung5fb63472011-02-02 17:03:37 -08003379 // Resume the auto-advance of widgets
3380 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003381 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003382
Winson Chung0f785722015-04-08 10:27:49 -07003383 if (changed) {
3384 // Send an accessibility event to announce the context change
3385 getWindow().getDecorView()
3386 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003387 if (notifyLauncherCallbacks) {
3388 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003389 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003390 mLauncherCallbacks.onAllAppsHidden();
3391 }
3392 }
Winson Chung0f785722015-04-08 10:27:49 -07003393 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003394 }
3395
Adam Cohened307df2013-10-02 09:37:31 -07003396 void showOverviewMode(boolean animated) {
3397 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003398 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003399 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3400 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003401 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003402 }
3403
Winson Chungb745afb2015-03-02 11:51:23 -08003404 /**
3405 * Shows the apps view.
3406 */
Winson Chung4ac30062015-05-08 17:34:17 -07003407 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003408 if (resetListToTop) {
3409 mAppsView.scrollToTop();
3410 }
Winson Chung4ac30062015-05-08 17:34:17 -07003411 if (updatePredictedApps) {
3412 tryAndUpdatePredictedApps();
3413 }
Winson Chungb745afb2015-03-02 11:51:23 -08003414 showAppsOrWidgets(animated, State.APPS);
3415 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003416
Winson Chungb745afb2015-03-02 11:51:23 -08003417 /**
3418 * Shows the widgets view.
3419 */
3420 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003421 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003422 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003423 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003424 }
Winson Chungb745afb2015-03-02 11:51:23 -08003425 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003426
3427 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003428 @Override
3429 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003430 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003431 }
3432 });
Winson Chungb745afb2015-03-02 11:51:23 -08003433 }
3434
3435 /**
3436 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003437 *
3438 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003439 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003440 // TODO: calling method should use the return value so that when {@code false} is returned
3441 // the workspace transition doesn't fall into invalid state.
3442 private boolean showAppsOrWidgets(boolean animated, State toState) {
3443 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3444 mState != State.WIDGETS_SPRING_LOADED) {
3445 return false;
3446 }
3447 if (toState != State.APPS && toState != State.WIDGETS) {
3448 return false;
3449 }
Winson Chungb745afb2015-03-02 11:51:23 -08003450
3451 if (toState == State.APPS) {
3452 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003453 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003454 mLauncherCallbacks.onAllAppsShown();
3455 }
Winson Chungb745afb2015-03-02 11:51:23 -08003456 } else {
3457 mStateTransitionAnimation.startAnimationToWidgets(animated);
3458 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003459
Michael Jurkab3e22d92011-10-31 15:58:33 -07003460 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003461 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003462
3463 // Pause the auto-advance of widgets until we are out of AllApps
3464 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003465 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003466 closeFolder();
3467
3468 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003469 getWindow().getDecorView()
3470 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003471 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003472 }
3473
Winson Chungcd99cd32015-04-29 11:03:24 -07003474 /**
3475 * Updates the workspace and interaction state on state change, and return the animation to this
3476 * new state.
3477 */
3478 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3479 boolean animated, HashMap<View, Integer> layerViews) {
3480 Workspace.State fromState = mWorkspace.getState();
3481 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3482 updateInteraction(fromState, toState);
3483 return anim;
3484 }
3485
Hyunyoung Song3f471442015-04-08 19:01:34 -07003486 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003487 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003488 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3489 mState == State.WIDGETS_SPRING_LOADED) {
3490 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003491 }
Winson Chungb745afb2015-03-02 11:51:23 -08003492
3493 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003494 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3495 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003496 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003497 }
Adam Cohen7777d962011-08-18 18:58:38 -07003498
Hyunyoung Song3f471442015-04-08 19:01:34 -07003499 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003500 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003501 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003502
Winson Chungcd99cd32015-04-29 11:03:24 -07003503 if (successfulDrop) {
3504 // We need to trigger all apps hidden to notify search to update itself before the
3505 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003506 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003507 mLauncherCallbacks.onAllAppsHidden();
3508 }
3509 }
3510
Winson Chunge7a03942011-08-05 15:05:12 -07003511 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003512 @Override
3513 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003514 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003515 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3516 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003517 // Before we show workspace, hide all apps again because
3518 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3519 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003520 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003521 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003522 } else {
3523 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003524 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003525 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003526 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003527 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003528
Michael Jurkad3ef3062010-11-23 16:23:58 -08003529 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003530 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003531 showAppsView(true /* animated */, false /* resetListToTop */,
3532 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003533 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003534 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003535 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003536 }
3537
Winson Chung4ac30062015-05-08 17:34:17 -07003538 /**
3539 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3540 * resumed.
3541 */
3542 private void tryAndUpdatePredictedApps() {
3543 if (mLauncherCallbacks != null) {
3544 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3545 if (!apps.isEmpty()) {
3546 mAppsView.setPredictedApps(apps);
3547 }
3548 }
3549 }
3550
Michael Jurkab3e22d92011-10-31 15:58:33 -07003551 void lockAllApps() {
3552 // TODO
3553 }
3554
3555 void unlockAllApps() {
3556 // TODO
3557 }
3558
Sunny Goyal594d76d2014-11-06 10:12:54 -08003559 protected void disableVoiceButtonProxy(boolean disable) {
3560 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003561 }
3562
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003563 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003564 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3565 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003566 }
3567
Adam Cohen24ce0b32014-01-14 16:18:14 -08003568 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003569 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3570 if (searchProvider == null) {
3571 return null;
3572 }
3573
3574 Bundle opts = new Bundle();
3575 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003576 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003577
3578 SharedPreferences sp = getSharedPreferences(
3579 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3580 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003581 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003582 if (!searchProvider.provider.flattenToString().equals(
3583 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003584 || (widgetInfo == null)
3585 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003586 // A valid widget is not already bound.
3587 if (widgetId > -1) {
3588 mAppWidgetHost.deleteAppWidgetId(widgetId);
3589 widgetId = -1;
3590 }
3591
3592 // Try to bind a new widget
3593 widgetId = mAppWidgetHost.allocateAppWidgetId();
3594
3595 if (!AppWidgetManagerCompat.getInstance(this)
3596 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3597 mAppWidgetHost.deleteAppWidgetId(widgetId);
3598 widgetId = -1;
3599 }
3600
3601 sp.edit()
3602 .putInt(QSB_WIDGET_ID, widgetId)
3603 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3604 .commit();
3605 }
3606
3607 if (widgetId != -1) {
3608 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3609 mQsb.updateAppWidgetOptions(opts);
3610 mQsb.setPadding(0, 0, 0, 0);
3611 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003612 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003613 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003614 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003615 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003616 }
3617
Sunny Goyal22235bc2015-04-03 09:23:43 -07003618 private void reinflateQSBIfNecessary() {
3619 if (mQsb instanceof LauncherAppWidgetHostView &&
3620 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3621 mSearchDropTargetBar.removeView(mQsb);
3622 mQsb = null;
3623 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3624 }
3625 }
3626
Michael Jurkad7c28052012-04-27 15:43:36 -07003627 @Override
3628 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003629 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003630 final List<CharSequence> text = event.getText();
3631 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003632 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003633 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003634 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003635 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003636 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003637 } else {
3638 text.add(getString(R.string.all_apps_home_button_label));
3639 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003640 return result;
3641 }
3642
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003643 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003644 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003645 */
Adam Cohen091440a2015-03-18 14:16:05 -07003646 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003647 @Override
3648 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003649 closeSystemDialogs();
3650 }
3651 }
3652
3653 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003654 * Receives notifications whenever the appwidgets are reset.
3655 */
3656 private class AppWidgetResetObserver extends ContentObserver {
3657 public AppWidgetResetObserver() {
3658 super(new Handler());
3659 }
3660
3661 @Override
3662 public void onChange(boolean selfChange) {
3663 onAppWidgetReset();
3664 }
3665 }
3666
3667 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003668 * If the activity is currently paused, signal that we need to run the passed Runnable
3669 * in onResume.
3670 *
3671 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003672 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3673 * wrong when we're not running, and if the activity comes back to what the configuration was
3674 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003675 *
3676 * Implementation of the method from LauncherModel.Callbacks.
3677 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003678 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003679 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003680 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003681 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003682 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003683 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003684 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003685 }
3686 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003687 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003688 return true;
3689 } else {
3690 return false;
3691 }
3692 }
3693
Michael Jurkac402cd92013-05-20 15:49:32 +02003694 private boolean waitUntilResume(Runnable run) {
3695 return waitUntilResume(run, false);
3696 }
3697
Michael Jurka1e2f4652013-07-08 18:03:46 -07003698 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003699 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003700 }
3701
Michael Jurka7607c2f2013-04-03 14:33:19 -07003702 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003703 * If the activity is currently paused, signal that we need to re-run the loader
3704 * in onResume.
3705 *
3706 * This needs to be called from incoming places where resources might have been loaded
3707 * while we are paused. That is becaues the Configuration might be wrong
3708 * when we're not running, and if it comes back to what it was when we
3709 * were paused, we are not restarted.
3710 *
3711 * Implementation of the method from LauncherModel.Callbacks.
3712 *
3713 * @return true if we are currently paused. The caller might be able to
3714 * skip some work in that case since we will come back again.
3715 */
3716 public boolean setLoadOnResume() {
3717 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003718 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003719 mOnResumeNeedsLoad = true;
3720 return true;
3721 } else {
3722 return false;
3723 }
3724 }
3725
3726 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003728 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003730 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003731 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003732 } else {
3733 return SCREEN_COUNT / 2;
3734 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003735 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003736
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 /**
3738 * Refreshes the shortcuts shown on the workspace.
3739 *
3740 * Implementation of the method from LauncherModel.Callbacks.
3741 */
3742 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003743 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003744
Michael Jurka7607c2f2013-04-03 14:33:19 -07003745 // If we're starting binding all over again, clear any bind calls we'd postponed in
3746 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3747 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003748 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003749
Winson Chungd64d1762013-08-20 14:37:16 -07003750 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003751 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003752 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003753
Michael Jurka05bf6442011-11-30 20:28:53 -08003754 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003755 if (mHotseat != null) {
3756 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003757 }
3758 }
3759
Adam Cohendcd297f2013-06-18 13:13:40 -07003760 @Override
3761 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003762 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003763
Adam Cohen5084cba2013-09-03 12:01:16 -07003764 // If there are no screens, we need to have an empty screen
3765 if (orderedScreenIds.size() == 0) {
3766 mWorkspace.addExtraEmptyScreen();
3767 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003768
3769 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003770 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003771 if (hasCustomContentToLeft()) {
3772 mWorkspace.createCustomContentContainer();
3773 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003774 }
Winson Chung64359a52013-07-08 17:17:08 -07003775 }
3776
3777 @Override
3778 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003779 // Log to disk
3780 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3781 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3782 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003783 int count = orderedScreenIds.size();
3784 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003785 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003786 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003787 }
3788
Adam Cohen39a06042013-07-19 14:30:12 -07003789 private boolean shouldShowWeightWatcher() {
3790 String spKey = LauncherAppState.getSharedPreferencesKey();
3791 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003792 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003793
3794 return show;
3795 }
3796
3797 private void toggleShowWeightWatcher() {
3798 String spKey = LauncherAppState.getSharedPreferencesKey();
3799 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3800 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3801
3802 show = !show;
3803
3804 SharedPreferences.Editor editor = sp.edit();
3805 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3806 editor.commit();
3807
3808 if (mWeightWatcher != null) {
3809 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3810 }
3811 }
3812
Winson Chungd64d1762013-08-20 14:37:16 -07003813 public void bindAppsAdded(final ArrayList<Long> newScreens,
3814 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003815 final ArrayList<ItemInfo> addAnimated,
3816 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003817 Runnable r = new Runnable() {
3818 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003819 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003820 }
3821 };
3822 if (waitUntilResume(r)) {
3823 return;
3824 }
3825
Winson Chungd64d1762013-08-20 14:37:16 -07003826 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003827 if (newScreens != null) {
3828 bindAddScreens(newScreens);
3829 }
Winson Chungd64d1762013-08-20 14:37:16 -07003830
3831 // We add the items without animation on non-visible pages, and with
3832 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003833 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003834 bindItems(addNotAnimated, 0,
3835 addNotAnimated.size(), false);
3836 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003837 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003838 bindItems(addAnimated, 0,
3839 addAnimated.size(), true);
3840 }
Winson Chungc58497e2013-09-03 17:48:37 -07003841
Winson Chung87412982013-10-03 18:34:14 -07003842 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003843 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003844
Winson Chungb745afb2015-03-02 11:51:23 -08003845 if (addedApps != null && mAppsView != null) {
3846 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003847 }
Winson Chungd64d1762013-08-20 14:37:16 -07003848 }
3849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003850 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 * Bind the items start-end from the list.
3852 *
3853 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003854 */
Winson Chung64359a52013-07-08 17:17:08 -07003855 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3856 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003857 Runnable r = new Runnable() {
3858 public void run() {
3859 bindItems(shortcuts, start, end, forceAnimateIcons);
3860 }
3861 };
3862 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003863 return;
3864 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003865
Winson Chungf0c6ae02012-03-21 16:10:31 -07003866 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003867 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3868 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003869 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003870 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003871 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003872 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003874
3875 // Short circuit if we are loading dock items for a configuration which has no dock
3876 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3877 mHotseat == null) {
3878 continue;
3879 }
3880
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 switch (item.itemType) {
3882 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3883 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003884 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003885 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003886
Winson Chung64359a52013-07-08 17:17:08 -07003887 /*
3888 * TODO: FIX collision case
3889 */
Winson Chungce376632013-07-11 16:12:41 -07003890 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3891 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3892 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003893 View v = cl.getChildAt(item.cellX, item.cellY);
3894 Object tag = v.getTag();
3895 String desc = "Collision while binding workspace item: " + item
3896 + ". Collides with " + tag;
3897 if (LauncherAppState.isDogfoodBuild()) {
3898 throw (new RuntimeException(desc));
3899 } else {
3900 Log.d(TAG, desc);
3901 }
Winson Chungce376632013-07-11 16:12:41 -07003902 }
Winson Chung64359a52013-07-08 17:17:08 -07003903 }
3904
Adam Cohendcd297f2013-06-18 13:13:40 -07003905 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3906 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003907 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003908 // Animate all the applications up now
3909 shortcut.setAlpha(0f);
3910 shortcut.setScaleX(0f);
3911 shortcut.setScaleY(0f);
3912 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003913 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003914 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003915 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003916 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003917 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003918 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003919 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003920 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3921 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003923 default:
3924 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003926 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003927
Winson Chung997a9232013-07-24 15:33:46 -07003928 if (animateIcons) {
3929 // Animate to the correct page
3930 if (newShortcutsScreenId > -1) {
3931 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003932 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003933 final Runnable startBounceAnimRunnable = new Runnable() {
3934 public void run() {
3935 anim.playTogether(bounceAnims);
3936 anim.start();
3937 }
3938 };
Winson Chung997a9232013-07-24 15:33:46 -07003939 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003940 // We post the animation slightly delayed to prevent slowdowns
3941 // when we are loading right after we return to launcher.
3942 mWorkspace.postDelayed(new Runnable() {
3943 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003944 if (mWorkspace != null) {
3945 mWorkspace.snapToPage(newScreenIndex);
3946 mWorkspace.postDelayed(startBounceAnimRunnable,
3947 NEW_APPS_ANIMATION_DELAY);
3948 }
Winson Chung94d67682013-09-25 16:29:40 -07003949 }
3950 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003951 } else {
3952 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003953 }
3954 }
Winson Chung64359a52013-07-08 17:17:08 -07003955 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003956 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003957 }
3958
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959 /**
3960 * Implementation of the method from LauncherModel.Callbacks.
3961 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003962 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003963 Runnable r = new Runnable() {
3964 public void run() {
3965 bindFolders(folders);
3966 }
3967 };
3968 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003969 return;
3970 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003971 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003972 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973
3974 /**
3975 * Add the views for a widget to the workspace.
3976 *
3977 * Implementation of the method from LauncherModel.Callbacks.
3978 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003979 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003980 Runnable r = new Runnable() {
3981 public void run() {
3982 bindAppWidget(item);
3983 }
3984 };
3985 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003986 return;
3987 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003988
Daniel Sandler843e8602010-06-07 14:59:01 -04003989 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3990 if (DEBUG_WIDGETS) {
3991 Log.d(TAG, "bindAppWidget: " + item);
3992 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993 final Workspace workspace = mWorkspace;
3994
Adam Cohen59400422014-03-05 18:07:04 -08003995 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003996 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003997
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003998 if (!mIsSafeModeEnabled
3999 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4000 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004001 if (appWidgetInfo == null) {
4002 if (DEBUG_WIDGETS) {
4003 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4004 + " belongs to component " + item.providerName
4005 + ", as the povider is null");
4006 }
4007 LauncherModel.deleteItemFromDatabase(this, item);
4008 return;
4009 }
4010 // Note: This assumes that the id remap broadcast is received before this step.
4011 // If that is not the case, the id remap will be ignored and user may see the
4012 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07004013 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004014 pendingInfo.spanX = item.spanX;
4015 pendingInfo.spanY = item.spanY;
4016 pendingInfo.minSpanX = item.minSpanX;
4017 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004018 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004019 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004020
Sunny Goyalff572272014-07-23 13:58:07 -07004021 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004022 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4023 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004024
4025 // TODO consider showing a permission dialog when the widget is clicked.
4026 if (!success) {
4027 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4028 if (DEBUG_WIDGETS) {
4029 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4030 + " belongs to component " + item.providerName
4031 + ", as the launcher is unable to bing a new widget id");
4032 }
4033 LauncherModel.deleteItemFromDatabase(this, item);
4034 return;
4035 }
4036
4037 item.appWidgetId = newWidgetId;
4038
4039 // If the widget has a configure activity, it is still needs to set it up, otherwise
4040 // the widget is ready to go.
4041 item.restoreStatus = (appWidgetInfo.configure == null)
4042 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4043 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4044
4045 LauncherModel.updateItemInDatabase(this, item);
4046 }
4047
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004048 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004049 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004050 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004051 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4052 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004053 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004054
Sunny Goyal651077b2014-06-30 14:15:31 -07004055 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4056 } else {
4057 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004058 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4059 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004060 view.updateIcon(mIconCache);
4061 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004062 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004063 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004067 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068
Adam Cohendcd297f2013-06-18 13:13:40 -07004069 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004071 if (!item.isCustomWidget()) {
4072 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4073 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004074
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075 workspace.requestLayout();
4076
Daniel Sandler843e8602010-06-07 14:59:01 -04004077 if (DEBUG_WIDGETS) {
4078 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4079 + (SystemClock.uptimeMillis()-start) + "ms");
4080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004081 }
4082
Sunny Goyalff572272014-07-23 13:58:07 -07004083 /**
4084 * Restores a pending widget.
4085 *
4086 * @param appWidgetId The app widget id
4087 * @param cellInfo The position on screen where to create the widget.
4088 */
4089 private void completeRestoreAppWidget(final int appWidgetId) {
4090 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4091 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4092 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4093 return;
4094 }
4095
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004096 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004097 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4098
4099 mWorkspace.reinflateWidgetsIfNecessary();
4100 LauncherModel.updateItemInDatabase(this, info);
4101 }
4102
Adam Cohen1462de32012-07-24 22:34:36 -07004103 public void onPageBoundSynchronously(int page) {
4104 mSynchronouslyBoundPages.add(page);
4105 }
4106
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107 /**
4108 * Callback saying that there aren't any more items to bind.
4109 *
4110 * Implementation of the method from LauncherModel.Callbacks.
4111 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004112 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004113 Runnable r = new Runnable() {
4114 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004115 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004116 }
4117 };
4118 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004119 return;
4120 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004121 if (mSavedState != null) {
4122 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004123 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004124 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004125 mSavedState = null;
4126 }
4127
Adam Cohen1462de32012-07-24 22:34:36 -07004128 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004129
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004130 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004131 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004132
4133 // If we received the result of any pending adds while the loader was running (e.g. the
4134 // widget configuration forced an orientation change), process them now.
4135 if (sPendingAddItem != null) {
4136 final long screenId = completeAdd(sPendingAddItem);
4137
4138 // TODO: this moves the user to the page where the pending item was added. Ideally,
4139 // the screen would be guaranteed to exist after bind, and the page would be set through
4140 // the workspace restore process.
4141 mWorkspace.post(new Runnable() {
4142 @Override
4143 public void run() {
4144 mWorkspace.snapToScreenId(screenId);
4145 }
4146 });
4147 sPendingAddItem = null;
4148 }
4149
Sunny Goyal756adbc2015-04-16 15:20:51 -07004150 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004151
4152 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004153 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004154 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004155 }
4156
Adam Cohen3ed316a2014-07-23 18:21:20 -07004157 private void sendLoadingCompleteBroadcastIfNecessary() {
4158 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4159 String permission =
4160 getResources().getString(R.string.receive_first_load_broadcast_permission);
4161 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4162 sendBroadcast(intent, permission);
4163 SharedPreferences.Editor editor = mSharedPrefs.edit();
4164 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4165 editor.apply();
4166 }
4167 }
4168
Winson Chunga0b7e862013-09-05 16:03:15 -07004169 public boolean isAllAppsButtonRank(int rank) {
4170 if (mHotseat != null) {
4171 return mHotseat.isAllAppsButtonRank(rank);
4172 }
4173 return false;
4174 }
4175
Winson Chunga2413752012-04-03 14:22:34 -07004176 private boolean canRunNewAppsAnimation() {
4177 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4178 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4179 }
4180
Winson Chungc9168342013-06-26 14:54:55 -07004181 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4182 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4183 PropertyValuesHolder.ofFloat("alpha", 1f),
4184 PropertyValuesHolder.ofFloat("scaleX", 1f),
4185 PropertyValuesHolder.ofFloat("scaleY", 1f));
4186 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4187 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004188 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004189 return bounceAnim;
4190 }
4191
Adam Cohen27772732013-11-11 14:00:56 +00004192 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004193 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004194 }
4195
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004196 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004197 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004198 }
4199
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004200 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004201 if (mSearchDropTargetBar == null) {
4202 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004203 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004204 if (mQsb != null) {
4205 mSearchDropTargetBar.removeView(mQsb);
4206 mQsb = null;
4207 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004208 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004209 }
4210
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004211 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004212 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4213 * multiple calls to bind the same list.)
4214 */
4215 @Thunk ArrayList<AppInfo> mTmpAppsList;
4216 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4217 public void run() {
4218 bindAllApplications(mTmpAppsList);
4219 mTmpAppsList = null;
4220 }
4221 };
4222
4223 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004224 * Add the icons for all apps.
4225 *
4226 * Implementation of the method from LauncherModel.Callbacks.
4227 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004228 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004229 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4230 mTmpAppsList = apps;
4231 return;
4232 }
4233
Winson Chungb745afb2015-03-02 11:51:23 -08004234 if (mAppsView != null) {
4235 mAppsView.setApps(apps);
4236 }
Adam Cohen9211d422014-10-07 18:14:53 -07004237 if (mLauncherCallbacks != null) {
4238 mLauncherCallbacks.bindAllApplications(apps);
4239 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004240 }
4241
4242 /**
4243 * A package was updated.
4244 *
4245 * Implementation of the method from LauncherModel.Callbacks.
4246 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004247 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004248 Runnable r = new Runnable() {
4249 public void run() {
4250 bindAppsUpdated(apps);
4251 }
4252 };
4253 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004254 return;
4255 }
4256
Winson Chungb745afb2015-03-02 11:51:23 -08004257 if (mAppsView != null) {
4258 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004259 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004260 }
4261
Sunny Goyal4390ace2014-10-13 11:33:11 -07004262 @Override
4263 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4264 Runnable r = new Runnable() {
4265 public void run() {
4266 bindWidgetsRestored(widgets);
4267 }
4268 };
4269 if (waitUntilResume(r)) {
4270 return;
4271 }
4272 mWorkspace.widgetsRestored(widgets);
4273 }
4274
Joe Onorato9c1289c2009-08-17 11:03:03 -04004275 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004276 * Some shortcuts were updated in the background.
4277 *
4278 * Implementation of the method from LauncherModel.Callbacks.
4279 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004280 @Override
4281 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4282 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004283 Runnable r = new Runnable() {
4284 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004285 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004286 }
4287 };
4288 if (waitUntilResume(r)) {
4289 return;
4290 }
4291
Sunny Goyal4390ace2014-10-13 11:33:11 -07004292 if (!updated.isEmpty()) {
4293 mWorkspace.updateShortcuts(updated);
4294 }
4295
4296 if (!removed.isEmpty()) {
4297 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4298 for (ShortcutInfo si : removed) {
4299 removedComponents.add(si.getTargetComponent());
4300 }
4301 mWorkspace.removeItemsByComponentName(removedComponents, user);
4302 // Notify the drag controller
4303 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004304 }
4305 }
4306
4307 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004308 * Update the state of a package, typically related to install state.
4309 *
4310 * Implementation of the method from LauncherModel.Callbacks.
4311 */
Sunny Goyale755d462014-07-22 13:48:29 -07004312 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004313 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4314 Runnable r = new Runnable() {
4315 public void run() {
4316 bindRestoreItemsChange(updates);
4317 }
4318 };
4319 if (waitUntilResume(r)) {
4320 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004321 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004322
Sunny Goyal756adbc2015-04-16 15:20:51 -07004323 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004324 }
4325
4326 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004327 * A package was uninstalled. We take both the super set of packageNames
4328 * in addition to specific applications to remove, the reason being that
4329 * this can be called when a package is updated as well. In that scenario,
4330 * we only remove specific components from the workspace, where as
4331 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004332 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004333 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004334 * Implementation of the method from LauncherModel.Callbacks.
4335 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004336 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004337 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004338 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004339 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004340 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004341 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004342 }
Winson Chungd64d1762013-08-20 14:37:16 -07004343 };
4344 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004345 return;
4346 }
4347
Sunny Goyal1a745e82014-10-02 15:58:31 -07004348 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004349 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4350 for (AppInfo info : appInfos) {
4351 removedComponents.add(info.componentName);
4352 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004353 if (!packageNames.isEmpty()) {
4354 mWorkspace.removeItemsByPackageName(packageNames, user);
4355 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004356 if (!removedComponents.isEmpty()) {
4357 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004358 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004359 // Notify the drag controller
4360 mDragController.onAppsRemoved(packageNames, removedComponents);
4361
Sunny Goyal1a745e82014-10-02 15:58:31 -07004362 } else {
4363 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004364 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004365
Winson Chungdf95eb12013-10-16 14:57:07 -07004366 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004367 if (mAppsView != null) {
4368 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004369 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004370 }
Joe Onoratobe386092009-11-17 17:32:16 -08004371
Michael Jurkac402cd92013-05-20 15:49:32 +02004372 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004373 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004374 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004375 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004376 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004377
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004378 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004379 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004380 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004381 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004382 return;
4383 }
4384
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004385 if (mWidgetsView != null && model != null) {
4386 mWidgetsView.addWidgets(model);
4387 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004388 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004389 }
4390
Winson Chung400438b2011-01-16 17:53:48 -08004391 private int mapConfigurationOriActivityInfoOri(int configOri) {
4392 final Display d = getWindowManager().getDefaultDisplay();
4393 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4394 switch (d.getRotation()) {
4395 case Surface.ROTATION_0:
4396 case Surface.ROTATION_180:
4397 // We are currently in the same basic orientation as the natural orientation
4398 naturalOri = configOri;
4399 break;
4400 case Surface.ROTATION_90:
4401 case Surface.ROTATION_270:
4402 // We are currently in the other basic orientation to the natural orientation
4403 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4404 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4405 break;
4406 }
4407
4408 int[] oriMap = {
4409 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4410 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4411 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4412 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4413 };
4414 // Since the map starts at portrait, we need to offset if this device's natural orientation
4415 // is landscape.
4416 int indexOffset = 0;
4417 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4418 indexOffset = 1;
4419 }
4420 return oriMap[(d.getRotation() + indexOffset) % 4];
4421 }
Adam Cohen446e9402011-09-15 18:21:21 -07004422
Winson Chung4b919f82012-05-01 10:44:08 -07004423 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004424 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004425 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4426 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4427 .getConfiguration().orientation));
4428 } else {
4429 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4430 }
Winson Chung4b919f82012-05-01 10:44:08 -07004431 }
4432 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004433
Winson Chung4b919f82012-05-01 10:44:08 -07004434 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004435 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004436 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004437 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004438 } else {
4439 mHandler.postDelayed(new Runnable() {
4440 public void run() {
4441 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4442 }
4443 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004444 }
Winson Chung4b919f82012-05-01 10:44:08 -07004445 }
Winson Chung400438b2011-01-16 17:53:48 -08004446 }
4447
Winson Chunge43a1e72014-01-15 10:33:02 -08004448 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004449 if (mLauncherCallbacks != null) {
4450 return mLauncherCallbacks.isLauncherPreinstalled();
4451 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004452 PackageManager pm = getPackageManager();
4453 try {
4454 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4455 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4456 return true;
4457 } else {
4458 return false;
4459 }
4460 } catch (NameNotFoundException e) {
4461 e.printStackTrace();
4462 return false;
4463 }
4464 }
4465
Adam Cohenea90f832014-05-21 15:03:34 -07004466 /**
4467 * This method indicates whether or not we should suggest default wallpaper dimensions
4468 * when our wallpaper cropper was not yet used to set a wallpaper.
4469 */
4470 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004471 if (mLauncherCallbacks != null) {
4472 return mLauncherCallbacks.overrideWallpaperDimensions();
4473 }
Adam Cohenea90f832014-05-21 15:03:34 -07004474 return true;
4475 }
4476
Adam Cohen432609a2014-03-13 17:03:22 -07004477 /**
4478 * To be overridden by subclasses to indicate that there is an activity to launch
4479 * before showing the standard launcher experience.
4480 */
4481 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004482 if (mLauncherCallbacks != null) {
4483 return mLauncherCallbacks.hasFirstRunActivity();
4484 }
Adam Cohen432609a2014-03-13 17:03:22 -07004485 return false;
4486 }
4487
4488 /**
4489 * To be overridden by subclasses to launch any first run activity
4490 */
4491 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004492 if (mLauncherCallbacks != null) {
4493 return mLauncherCallbacks.getFirstRunActivity();
4494 }
Adam Cohen432609a2014-03-13 17:03:22 -07004495 return null;
4496 }
4497
Winson Chung2826a402015-04-17 16:18:53 -07004498 /**
4499 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004500 */
4501 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004502 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4503 return false;
4504 }
4505
Winson Chung2826a402015-04-17 16:18:53 -07004506 if (mLauncherCallbacks != null) {
4507 return mLauncherCallbacks.overrideAllAppsSearch();
4508 }
4509 return false;
4510 }
4511
Winson Chunga6945242014-01-08 14:04:34 -08004512 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004513 return !ActivityManager.isRunningInTestHarness() &&
4514 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004515 }
4516
Adam Cohen4a9423d2014-03-28 14:22:31 -07004517 protected boolean hasRunFirstRunActivity() {
4518 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4519 }
4520
Adam Cohen432609a2014-03-13 17:03:22 -07004521 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004522 if (shouldRunFirstRunActivity() &&
4523 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004524 Intent firstRunIntent = getFirstRunActivity();
4525 if (firstRunIntent != null) {
4526 startActivity(firstRunIntent);
4527 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004528 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004529 }
4530 }
Adam Cohen432609a2014-03-13 17:03:22 -07004531 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004532 }
4533
4534 private void markFirstRunActivityShown() {
4535 SharedPreferences.Editor editor = mSharedPrefs.edit();
4536 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4537 editor.apply();
4538 }
4539
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004540 private void showSettingsActivity() {
4541 startActivity(new Intent(this, SettingsActivity.class));
4542 }
4543
Adam Cohen432609a2014-03-13 17:03:22 -07004544 /**
4545 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4546 * screen that must be displayed and dismissed.
4547 */
4548 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004549 if (mLauncherCallbacks != null) {
4550 return mLauncherCallbacks.hasDismissableIntroScreen();
4551 }
Adam Cohen432609a2014-03-13 17:03:22 -07004552 return false;
4553 }
4554
4555 /**
4556 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4557 */
4558 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004559 if (mLauncherCallbacks != null) {
4560 return mLauncherCallbacks.getIntroScreen();
4561 }
Adam Cohen432609a2014-03-13 17:03:22 -07004562 return null;
4563 }
4564
4565 /**
4566 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4567 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4568 */
4569 private boolean shouldShowIntroScreen() {
4570 return hasDismissableIntroScreen() &&
4571 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4572 }
4573
4574 protected void showIntroScreen() {
4575 View introScreen = getIntroScreen();
4576 changeWallpaperVisiblity(false);
4577 if (introScreen != null) {
4578 mDragLayer.showOverlayView(introScreen);
4579 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004580 if (mLauncherOverlayContainer != null) {
4581 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4582 }
Adam Cohen432609a2014-03-13 17:03:22 -07004583 }
4584
4585 public void dismissIntroScreen() {
4586 markIntroScreenDismissed();
4587 if (showFirstRunActivity()) {
4588 // We delay hiding the intro view until the first run activity is showing. This
4589 // avoids a blip.
4590 mWorkspace.postDelayed(new Runnable() {
4591 @Override
4592 public void run() {
4593 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004594 if (mLauncherOverlayContainer != null) {
4595 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4596 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004597 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004598 }
4599 }, ACTIVITY_START_DELAY);
4600 } else {
4601 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004602 if (mLauncherOverlayContainer != null) {
4603 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4604 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004605 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004606 }
4607 changeWallpaperVisiblity(true);
4608 }
4609
4610 private void markIntroScreenDismissed() {
4611 SharedPreferences.Editor editor = mSharedPrefs.edit();
4612 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4613 editor.apply();
4614 }
4615
Adam Cohen091440a2015-03-18 14:16:05 -07004616 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004617 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4618 // on the device, then we always show the first run cling experience (or if there is no
4619 // launcher2). Otherwise, we prompt the user upon started for migration
4620 LauncherClings launcherClings = new LauncherClings(this);
4621 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4622 if (mModel.canMigrateFromOldLauncherDb(this)) {
4623 launcherClings.showMigrationCling();
4624 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004625 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004626 }
4627 }
4628 }
4629
Winson Chunga6945242014-01-08 14:04:34 -08004630 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004631 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4632 if (mHotseat != null) mHotseat.setAlpha(1f);
4633 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4634 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004635 }
4636
4637 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004638 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4639 if (mHotseat != null) mHotseat.setAlpha(0f);
4640 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4641 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004642 }
4643
Mathew Inwood72fbec12013-11-19 15:45:07 +00004644 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004645 // Called from search suggestion, not supported in other profiles.
4646 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4647 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4648 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4649 myUser);
4650 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004651 return null;
4652 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004653 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004654 }
4655
4656 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4657 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004658 // Called from search suggestion, not supported in other profiles.
4659 return createShortcutDragInfo(shortcutIntent, caption, icon,
4660 UserHandleCompat.myUserHandle());
4661 }
4662
4663 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4664 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004665 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004666 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004667 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004668 }
4669
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004670 protected void moveWorkspaceToDefaultScreen() {
4671 mWorkspace.moveToDefaultScreen(false);
4672 }
4673
Mathew Inwood72fbec12013-11-19 15:45:07 +00004674 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4675 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004676 mWorkspace.onExternalDragStartedWithItem(dragView);
4677 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004678 }
4679
Anjali Koppalf5d29132014-02-28 13:33:27 -08004680 @Override
4681 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004682 if (mLauncherCallbacks != null) {
4683 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4684 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004685 }
4686
Winson Chung80baf5a2010-08-09 16:03:15 -07004687 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004688 * Prints out out state for debugging.
4689 */
4690 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004691 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004692 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004693 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4694 Log.d(TAG, "mRestoring=" + mRestoring);
4695 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4696 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004697 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004698 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004699 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004700
Daniel Sandler325dc232013-06-05 22:57:57 -04004701 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004702 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004703
4704 @Override
4705 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4706 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004707 synchronized (sDumpLogs) {
4708 writer.println(" ");
4709 writer.println("Debug logs: ");
4710 for (int i = 0; i < sDumpLogs.size(); i++) {
4711 writer.println(" " + sDumpLogs.get(i));
4712 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004713 }
Adam Cohen9211d422014-10-07 18:14:53 -07004714 if (mLauncherCallbacks != null) {
4715 mLauncherCallbacks.dump(prefix, fd, writer, args);
4716 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004717 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004718
4719 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004720 if (DEBUG_DUMP_LOG) {
4721 synchronized (sDumpLogs) {
4722 Log.d(TAG, "");
4723 Log.d(TAG, "*********************");
4724 Log.d(TAG, "Launcher debug logs: ");
4725 for (int i = 0; i < sDumpLogs.size(); i++) {
4726 Log.d(TAG, " " + sDumpLogs.get(i));
4727 }
4728 Log.d(TAG, "*********************");
4729 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004730 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004731 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004732 }
4733
4734 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004735 addDumpLog(tag, log, null, debugLog);
4736 }
4737
4738 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004739 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004740 if (e != null) {
4741 Log.d(tag, log, e);
4742 } else {
4743 Log.d(tag, log);
4744 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004745 }
Winson Chungede41292013-09-19 16:27:36 -07004746 if (DEBUG_DUMP_LOG) {
4747 sDateStamp.setTime(System.currentTimeMillis());
4748 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004749 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4750 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004751 }
Winson Chungede41292013-09-19 16:27:36 -07004752 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004753 }
4754
Adam Cohen59400422014-03-05 18:07:04 -08004755 public static CustomAppWidget getCustomAppWidget(String name) {
4756 return sCustomAppWidgets.get(name);
4757 }
4758
4759 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4760 return sCustomAppWidgets;
4761 }
4762
Winson Chungede41292013-09-19 16:27:36 -07004763 public void dumpLogsToLocalData() {
4764 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004765 new AsyncTask<Void, Void, Void>() {
4766 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004767 boolean success = false;
4768 sDateStamp.setTime(sRunStart);
4769 String FILENAME = sDateStamp.getMonth() + "-"
4770 + sDateStamp.getDay() + "_"
4771 + sDateStamp.getHours() + "-"
4772 + sDateStamp.getMinutes() + "_"
4773 + sDateStamp.getSeconds() + ".txt";
4774
4775 FileOutputStream fos = null;
4776 File outFile = null;
4777 try {
4778 outFile = new File(getFilesDir(), FILENAME);
4779 outFile.createNewFile();
4780 fos = new FileOutputStream(outFile);
4781 } catch (Exception e) {
4782 e.printStackTrace();
4783 }
4784 if (fos != null) {
4785 PrintWriter writer = new PrintWriter(fos);
4786
4787 writer.println(" ");
4788 writer.println("Debug logs: ");
4789 synchronized (sDumpLogs) {
4790 for (int i = 0; i < sDumpLogs.size(); i++) {
4791 writer.println(" " + sDumpLogs.get(i));
4792 }
4793 }
4794 writer.close();
4795 }
4796 try {
4797 if (fos != null) {
4798 fos.close();
4799 success = true;
4800 }
4801 } catch (IOException e) {
4802 e.printStackTrace();
4803 }
Michael Jurka43467462013-11-14 12:03:58 +01004804 return null;
Winson Chungede41292013-09-19 16:27:36 -07004805 }
Michael Jurka43467462013-11-14 12:03:58 +01004806 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004807 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004809}
Michael Jurka2763be32011-02-24 11:19:57 -08004810
Dan Sandlerd5024042014-01-09 15:01:33 -05004811interface DebugIntents {
4812 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4813 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004814}