blob: 335a77bcfe8830402cf59c397b4da748aa90693b [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070086import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070088import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070091import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Advanceable;
94import android.widget.FrameLayout;
95import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080096import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070097import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070098
Sunny Goyal651077b2014-06-30 14:15:31 -070099import com.android.launcher3.DropTarget.DragObject;
100import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700101import com.android.launcher3.allapps.AllAppsContainerView;
Winson Chungef7f8742015-06-04 17:18:17 -0700102import com.android.launcher3.allapps.AllAppsSearchBarController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700103import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100104import com.android.launcher3.compat.LauncherActivityInfoCompat;
105import com.android.launcher3.compat.LauncherAppsCompat;
106import com.android.launcher3.compat.UserHandleCompat;
107import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700108import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700109import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700110import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700111import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700112import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700113import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700114
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700116import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800120import java.lang.reflect.InvocationTargetException;
121import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700124import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
137 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700138 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700139 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700142 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700143 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700145 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700146
Dan Sandlerd5024042014-01-09 15:01:33 -0500147 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700152 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Michael Jurka8b805b12012-04-18 14:23:14 -0700154 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700155 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700156
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700157 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
158 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
159 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
160
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700161 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
162
Mathew Inwood876a8462013-06-14 14:12:41 +0100163 /**
164 * IntentStarter uses request codes starting with this. This must be greater than all activity
165 * request codes used internally.
166 */
167 protected static final int REQUEST_LAST = 100;
168
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800169 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Michael Jurka0a457bf2012-11-19 14:05:05 -0800171 // To turn on these properties, type
172 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800173 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Winson Chung2672ff92012-05-04 16:22:30 -0700175 // The Intent extra that defines whether to ignore the launch animation
176 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400177 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
180 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700181 // Type: int
182 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
185 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
187 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700188 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700190 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700191 // Type: int
192 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
193 // Type: int
194 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
195 // Type: parcelable
196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800198 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000199 // Type: int[]
200 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Adam Cohen432609a2014-03-13 17:03:22 -0700202 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800203 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
204
Adam Cohen3ed316a2014-07-23 18:21:20 -0700205 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
206 static final String ACTION_FIRST_LOAD_COMPLETE =
207 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
208
Adam Cohen39a06042013-07-19 14:30:12 -0700209 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700210 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700211
Sunny Goyal594d76d2014-11-06 10:12:54 -0800212 private static final String QSB_WIDGET_ID = "qsb_widget_id";
213 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
214
Winson Chunga6945242014-01-08 14:04:34 -0800215 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
216
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700217 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700218 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
219
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700222
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700223 private boolean mIsSafeModeEnabled;
224
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
226 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700227 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700230 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
231 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700232 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000234 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
235 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
236
Winson Chunga2413752012-04-03 14:22:34 -0700237 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700238 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
239 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700240 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700241
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800242 private final BroadcastReceiver mCloseSystemDialogsReceiver
243 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800244 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private LayoutInflater mInflater;
247
Adam Cohen091440a2015-03-18 14:16:05 -0700248 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700249 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800250 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700251 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800252 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700253 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700254
Sunny Goyalffe83f12014-08-14 17:39:34 -0700255 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700256 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700257
Adam Cohen091440a2015-03-18 14:16:05 -0700258 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800259 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800260 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700261
Michael Jurka0280c3b2010-09-17 15:00:07 -0700262 private int[] mTmpAddItemCellCoordinates = new int[2];
263
Sunny Goyal316490e2015-06-02 09:38:28 -0700264 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800265 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700266
Michael Jurka838a4ca2011-02-07 13:33:06 -0800267 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700268
Winson Chungc51db6a2011-10-05 11:44:49 -0700269 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700270
271 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700272 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700273
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700274 // Main container view and the model for the widget tray screen.
275 @Thunk WidgetsContainerView mWidgetsView;
276 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700277
Winson Chungf0ea4d32011-06-06 14:27:16 -0700278 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800279 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700282 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
283 // scroll issues (because the workspace may not have been measured yet) and extra work.
284 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
285 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286
287 private SpannableStringBuilder mDefaultKeySsb = null;
288
Adam Cohen091440a2015-03-18 14:16:05 -0700289 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800291 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 private boolean mRestoring;
293 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700294 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295
Michael Jurka1e2f4652013-07-08 18:03:46 -0700296 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700297 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
298
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 private Bundle mSavedInstanceState;
300
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800302 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700303 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700305 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800306 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307
Sunny Goyale2df0622015-04-24 11:27:00 -0700308 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700309
Adam Cohen61f560d2013-09-30 15:58:20 -0700310 private View.OnTouchListener mHapticFeedbackTouchListener;
311
Adam Cohended9f8d2010-11-03 13:25:16 -0700312 // Related to the auto-advancing of widgets
313 private final int ADVANCE_MSG = 1;
314 private final int mAdvanceInterval = 20000;
315 private final int mAdvanceStagger = 250;
316 private long mAutoAdvanceSentTime;
317 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700318 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700319 new HashMap<View, AppWidgetProviderInfo>();
320
Winson Chung400438b2011-01-16 17:53:48 -0800321 // Determines how long to wait after a rotation before restoring the screen orientation to
322 // match the sensor state.
323 private final int mRestoreScreenOrientationDelay = 500;
324
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700326
Adam Cohen1462de32012-07-24 22:34:36 -0700327 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700328 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700329
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700330 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700331 static Date sDateStamp = new Date();
332 static DateFormat sDateFormat =
333 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
334 static long sRunStart = System.currentTimeMillis();
335 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700336
Winson Chung46353de2012-02-16 14:05:10 -0800337 // We only want to get the SharedPreferences once since it does an FS stat each time we get
338 // it from the context.
339 private SharedPreferences mSharedPrefs;
340
Winson Chungf0c6ae02012-03-21 16:10:31 -0700341 // Holds the page that we need to animate to, and the icon views that we need to animate up
342 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700343 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700344 private Bitmap mFolderIconBitmap;
345 private Canvas mFolderIconCanvas;
346 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700347
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700348 private DeviceProfile mDeviceProfile;
349
Winson Chung13eb5272015-05-11 16:30:13 -0700350 // This is set to the view that launched the activity that navigated the user away from
351 // launcher. Since there is no callback for when the activity has finished launching, enable
352 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800353 private BubbleTextView mWaitingForResume;
354
Adam Cohen59400422014-03-05 18:07:04 -0800355 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
356 new HashMap<String, CustomAppWidget>();
357
358 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
359 static {
360 if (ENABLE_CUSTOM_WIDGET_TEST) {
361 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
362 }
363 }
364
Chet Haasea8f996d2015-02-17 12:56:04 -0800365 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
366 private static Method sClipRevealMethod = null;
367 static {
368 Class<?> activityOptionsClass = ActivityOptions.class;
369 try {
370 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
371 View.class, int.class, int.class, int.class, int.class);
372 } catch (Exception e) {
373 // Earlier version
374 }
375 }
376
Adam Cohen091440a2015-03-18 14:16:05 -0700377 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700378 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700379 if (mWorkspace != null) {
380 mWorkspace.buildPageHardwareLayers();
381 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700382 }
383 };
384
Adam Cohendb364c32014-05-20 14:23:40 -0700385 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800386
Adam Cohen091440a2015-03-18 14:16:05 -0700387 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800388 int requestCode;
389 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700390 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700391 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392 int cellX;
393 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700394 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800395 }
396
Daniel Sandlerff02d492013-08-05 02:12:05 -0400397 private Stats mStats;
398
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700399 FocusIndicatorView mFocusHandler;
400
Sunny Goyal316490e2015-06-02 09:38:28 -0700401 @Thunk boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400402 private boolean mScreenOrientationSettingReceiverRegistered = false;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700403
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400404 final private BroadcastReceiver mScreenOrientationSettingReceiver =
405 new BroadcastReceiver() {
406 @Thunk Runnable mUpdateOrientationRunnable = new Runnable() {
407 public void run() {
408 setOrientation();
409 }
410 };
411
412 @Override
413 public void onReceive(Context context, Intent intent) {
414 mRotationEnabled = intent.getBooleanExtra(
415 Utilities.SCREEN_ROTATION_SETTING_EXTRA, false);
416 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
417 setOrientation();
418 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700419 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400420 };
421
422 @Thunk void setOrientation() {
423 if (mRotationEnabled) {
424 unlockScreenOrientation(true);
425 } else {
426 setRequestedOrientation(
427 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700428 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400429 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 @Override
432 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700433 if (DEBUG_STRICT_MODE) {
434 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
435 .detectDiskReads()
436 .detectDiskWrites()
437 .detectNetwork() // or .detectAll() for all detectable problems
438 .penaltyLog()
439 .build());
440 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
441 .detectLeakedSqlLiteObjects()
442 .detectLeakedClosableObjects()
443 .penaltyLog()
444 .penaltyDeath()
445 .build());
446 }
447
Adam Cohen9211d422014-10-07 18:14:53 -0700448 if (mLauncherCallbacks != null) {
449 mLauncherCallbacks.preOnCreate();
450 }
451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400453
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400454 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400455 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700456 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700457
Adam Cohen2e6da152015-05-06 11:42:25 -0700458 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700459 mDeviceProfile = getResources().getConfiguration().orientation
460 == Configuration.ORIENTATION_LANDSCAPE ?
461 app.getInvariantDeviceProfile().landscapeProfile
462 : app.getInvariantDeviceProfile().portraitProfile;
463
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400464 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700465 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700466 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800467 mModel = app.setLauncher(this);
468 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700469
Joe Onorato41a12d22009-10-31 18:30:00 -0400470 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800472 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700473
Daniel Sandlerff02d492013-08-05 02:12:05 -0400474 mStats = new Stats(this);
475
Sunny Goyalffe83f12014-08-14 17:39:34 -0700476 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700477
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700478 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
479 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700480
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700481 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
482 // this also ensures that any synchronous binding below doesn't re-trigger another
483 // LauncherModel load.
484 mPaused = false;
485
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200487 android.os.Debug.startMethodTracing(
488 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 }
490
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700492
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700494 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800496 registerContentObservers();
497
Joe Onorato7c312c12009-08-13 21:36:53 -0700498 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700499
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 mSavedState = savedInstanceState;
501 restoreState(mSavedState);
502
503 if (PROFILE_STARTUP) {
504 android.os.Debug.stopMethodTracing();
505 }
506
507 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700508 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700509 // If the user leaves launcher, then we should just load items asynchronously when
510 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700511 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700512 } else {
513 // We only load the page synchronously if the user rotates (or triggers a
514 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700515 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700516 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 }
518
519 // For handling default keys
520 mDefaultKeySsb = new SpannableStringBuilder();
521 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800522
523 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
524 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800525
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700526 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
527 // In case we are on a device with locked rotation, we should look at preferences to check
528 // if the user has specifically allowed rotation.
529 if (!mRotationEnabled) {
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400530 String updateOrientationBroadcastPermission = getResources().getString(
531 R.string.receive_update_orientation_broadcasts_permission);
532 registerReceiver(mScreenOrientationSettingReceiver,
533 new IntentFilter(Utilities.SCREEN_ROTATION_SETTING_INTENT),
534 updateOrientationBroadcastPermission, null);
535 mScreenOrientationSettingReceiverRegistered = true;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700536 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
537 }
538
539 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
540 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400541 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700542
Adam Cohen9211d422014-10-07 18:14:53 -0700543 if (mLauncherCallbacks != null) {
544 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700545 if (mLauncherCallbacks.hasLauncherOverlay()) {
546 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700547 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
548 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
549 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700550 mWorkspace.setLauncherOverlay(mLauncherOverlay);
551 }
Adam Cohen9211d422014-10-07 18:14:53 -0700552 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700553
554 if (shouldShowIntroScreen()) {
555 showIntroScreen();
556 } else {
557 showFirstRunActivity();
558 showFirstRunClings();
559 }
Adam Cohen9211d422014-10-07 18:14:53 -0700560 }
561
562 private LauncherCallbacks mLauncherCallbacks;
563
564 public void onPostCreate(Bundle savedInstanceState) {
565 super.onPostCreate(savedInstanceState);
566 if (mLauncherCallbacks != null) {
567 mLauncherCallbacks.onPostCreate(savedInstanceState);
568 }
569 }
570
571 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
572 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700573 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
574 private boolean mImportanceStored = false;
575 private int mWorkspaceImportanceForAccessibility =
576 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
577 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
578
579 @Override
580 public void onSearchOverlayOpened() {
581 if (mImportanceStored) {
582 return;
583 }
584 // The underlying workspace and hotseat are temporarily suppressed by the search
585 // overlay. So they sholudn't be accessible.
586 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
587 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
588 mWorkspace.setImportantForAccessibility(
589 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
590 mHotseat.setImportantForAccessibility(
591 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
592 mImportanceStored = true;
593 }
594
595 @Override
596 public void onSearchOverlayClosed() {
597 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
598 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
599 mImportanceStored = false;
600 }
601 });
Adam Cohen9211d422014-10-07 18:14:53 -0700602 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700603 }
604
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700605 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700606 public void onLauncherProviderChange() {
607 if (mLauncherCallbacks != null) {
608 mLauncherCallbacks.onLauncherProviderChange();
609 }
610 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700611
Winson Chungef7f8742015-06-04 17:18:17 -0700612 /**
613 * Updates the bounds of all the overlays to match the new fixed bounds.
614 */
615 public void updateOverlayBounds(Rect newBounds) {
616 mAppsView.setSearchBarBounds(newBounds);
617 mWidgetsView.setSearchBarBounds(newBounds);
618 }
619
Adam Cohen9211d422014-10-07 18:14:53 -0700620 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700621 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700622 if (mLauncherCallbacks != null) {
623 return mLauncherCallbacks.hasCustomContentToLeft();
624 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700625 return false;
626 }
627
Dave Hawkeya8881582013-09-17 15:55:33 -0600628 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500629 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600630 * {@link #addToCustomContentPage}. This will only be invoked if
631 * {@link #hasCustomContentToLeft()} is {@code true}.
632 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500633 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700634 if (mLauncherCallbacks != null) {
635 mLauncherCallbacks.populateCustomContentContainer();
636 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600637 }
638
639 /**
640 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
641 * ensure the custom content page is added or removed if necessary.
642 */
643 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600644 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600645 // Not bound yet, wait for bindScreens to be called.
646 return;
647 }
648
649 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
650 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500651 mWorkspace.createCustomContentContainer();
652 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600653 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
654 mWorkspace.removeCustomContentPage();
655 }
656 }
657
Anton Hanssona2f665f2013-09-26 12:18:32 +0100658 public Stats getStats() {
659 return mStats;
660 }
661
Bjorn Bringertc459e522013-06-07 19:36:01 +0100662 public LayoutInflater getInflater() {
663 return mInflater;
664 }
665
Hyunyoung Song3f471442015-04-08 19:01:34 -0700666 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700667 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
668 // that is subsequently removed from the workspace in startBinding().
669 return !mModel.isLoadingWorkspace();
670 }
671
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800672 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000673 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100674 if (Build.VERSION.SDK_INT >= 17) {
675 return View.generateViewId();
676 } else {
677 // View.generateViewId() is not available. The following fallback logic is a copy
678 // of its implementation.
679 for (;;) {
680 final int result = sNextGeneratedId.get();
681 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
682 int newValue = result + 1;
683 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
684 if (sNextGeneratedId.compareAndSet(result, newValue)) {
685 return result;
686 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000687 }
688 }
689 }
690
691 public int getViewIdForItem(ItemInfo info) {
692 // This cast is safe given the > 2B range for int.
693 int itemId = (int) info.id;
694 if (mItemIdToViewId.containsKey(itemId)) {
695 return mItemIdToViewId.get(itemId);
696 }
697 int viewId = generateViewId();
698 mItemIdToViewId.put(itemId, viewId);
699 return viewId;
700 }
701
702 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700703 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
704 * a configuration step, this allows the proper animations to run after other transitions.
705 */
Adam Cohendb364c32014-05-20 14:23:40 -0700706 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700707 long screenId = args.screenId;
708 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
709 // When the screen id represents an actual screen (as opposed to a rank) we make sure
710 // that the drop page actually exists.
711 screenId = ensurePendingDropLayoutExists(args.screenId);
712 }
Adam Cohendb364c32014-05-20 14:23:40 -0700713
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800715 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700716 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700717 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700718 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800719 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700720 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700721 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700722 case REQUEST_RECONFIGURE_APPWIDGET:
723 completeRestoreAppWidget(args.appWidgetId);
724 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800725 }
Michael Jurka27614d22012-04-02 06:40:22 -0700726 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
727 // if you turned the screen off and then back while in All Apps, Launcher would not
728 // return to the workspace. Clearing mAddInfo.container here fixes this issue
729 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700730 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 }
732
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800733 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700735 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700736 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700737 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800738 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700739
Adam Cohenad4e15c2013-10-17 16:21:35 -0700740 Runnable exitSpringLoaded = new Runnable() {
741 @Override
742 public void run() {
743 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
744 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
745 }
746 };
747
Michael Jurka8b805b12012-04-18 14:23:14 -0700748 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700749 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
751 if (resultCode == RESULT_CANCELED) {
752 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700753 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700754 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800756 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 }
759 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200760 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
761 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700762 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200763 }
764 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200766
Adam Cohened66b2b2012-01-23 17:28:51 -0800767 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700768 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700769
Adam Cohendb364c32014-05-20 14:23:40 -0700770 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800771 // We have special handling for widgets
772 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800773 final int appWidgetId;
774 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
775 : -1;
776 if (widgetId < 0) {
777 appWidgetId = pendingAddWidgetId;
778 } else {
779 appWidgetId = widgetId;
780 }
781
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800783 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700784 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
785 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 result = RESULT_CANCELED;
787 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700788 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 @Override
790 public void run() {
791 exitSpringLoadedDragModeDelayed(false, 0, null);
792 }
793 };
Adam Cohendb364c32014-05-20 14:23:40 -0700794 if (workspaceLocked) {
795 // No need to remove the empty screen if we're mid-binding, as the
796 // the bind will not add the empty screen.
797 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
798 } else {
799 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
800 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
801 }
Winson Chung5aaab772012-04-27 15:24:02 -0700802 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700803 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700804 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
805 // When the screen id represents an actual screen (as opposed to a rank)
806 // we make sure that the drop page actually exists.
807 mPendingAddInfo.screenId =
808 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
809 }
Adam Cohendb364c32014-05-20 14:23:40 -0700810 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
811
812 dropLayout.setDropPending(true);
813 final Runnable onComplete = new Runnable() {
814 @Override
815 public void run() {
816 completeTwoStageWidgetDrop(resultCode, appWidgetId);
817 dropLayout.setDropPending(false);
818 }
819 };
820 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
821 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
822 } else {
823 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
824 mPendingAddInfo);
825 sPendingAddItem = args;
826 }
Winson Chung5aaab772012-04-27 15:24:02 -0700827 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800828 return;
829 }
830
Sunny Goyalff572272014-07-23 13:58:07 -0700831 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
832 if (resultCode == RESULT_OK) {
833 // Update the widget view.
834 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
835 pendingAddWidgetId, mPendingAddInfo);
836 if (workspaceLocked) {
837 sPendingAddItem = args;
838 } else {
839 completeAdd(args);
840 }
841 }
842 // Leave the widget in the pending state if the user canceled the configure.
843 return;
844 }
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 // The pattern used here is that a user PICKs a specific application,
847 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
850 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700851 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700852 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
853 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800854 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700855 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800856 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700857 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700858 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
859 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
Adam Cohen00074722013-10-11 17:46:09 -0700861 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700862 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800866
867 }
868
869 @Override
870 protected void onActivityResult(
871 final int requestCode, final int resultCode, final Intent data) {
872 handleActivityResult(requestCode, resultCode, data);
873 if (mLauncherCallbacks != null) {
874 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
875 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 }
877
Adam Cohendb364c32014-05-20 14:23:40 -0700878 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
879 appWidgetId, ItemInfo info) {
880 PendingAddArguments args = new PendingAddArguments();
881 args.requestCode = requestCode;
882 args.intent = data;
883 args.container = info.container;
884 args.screenId = info.screenId;
885 args.cellX = info.cellX;
886 args.cellY = info.cellY;
887 args.appWidgetId = appWidgetId;
888 return args;
889 }
890
891 /**
892 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
893 *
894 * @param screenId the screen id to check
895 * @return the new screen, or screenId if it exists
896 */
897 private long ensurePendingDropLayoutExists(long screenId) {
898 CellLayout dropLayout =
899 (CellLayout) mWorkspace.getScreenWithId(screenId);
900 if (dropLayout == null) {
901 // it's possible that the add screen was removed because it was
902 // empty and a re-bind occurred
903 mWorkspace.addExtraEmptyScreen();
904 return mWorkspace.commitExtraEmptyScreen();
905 } else {
906 return screenId;
907 }
908 }
909
Adam Cohen091440a2015-03-18 14:16:05 -0700910 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700911 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700912 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 Runnable onCompleteRunnable = null;
914 int animationType = 0;
915
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 if (resultCode == RESULT_OK) {
918 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
919 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 mPendingAddWidgetInfo);
921 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 onCompleteRunnable = new Runnable() {
923 @Override
924 public void run() {
925 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700926 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700927 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
928 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 }
930 };
931 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800932 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 if (mDragLayer.getAnimatedView() != null) {
936 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
937 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
938 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700939 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700940 // The animated view may be null in the case of a rotation during widget configuration
941 onCompleteRunnable.run();
942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
945 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700946 protected void onStop() {
947 super.onStop();
948 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700949
950 if (mLauncherCallbacks != null) {
951 mLauncherCallbacks.onStop();
952 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 }
954
955 @Override
956 protected void onStart() {
957 super.onStart();
958 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700959
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onStart();
962 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700963 }
964
965 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200967 long startTime = 0;
968 if (DEBUG_RESUME_TIME) {
969 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400970 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200971 }
Adam Cohen9211d422014-10-07 18:14:53 -0700972
973 if (mLauncherCallbacks != null) {
974 mLauncherCallbacks.preOnResume();
975 }
976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700978
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700980 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700981 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800982 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700983 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700984 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700985 // view after launching an app, as they may be depending on the UI to be static to
986 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700987 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -0700988 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800989 } else if (mOnResumeState == State.WIDGETS) {
990 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700991 }
992 mOnResumeState = State.NONE;
993
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700994 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700995 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
996 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700997
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800998 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700999 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001000 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001001 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001003 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001005 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001006 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1007 // execute them here
1008 long startTimeCallbacks = 0;
1009 if (DEBUG_RESUME_TIME) {
1010 startTimeCallbacks = System.currentTimeMillis();
1011 }
1012
Michael Jurka1e2f4652013-07-08 18:03:46 -07001013 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1014 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001016 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001017 if (DEBUG_RESUME_TIME) {
1018 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1019 (System.currentTimeMillis() - startTimeCallbacks));
1020 }
Michael Jurka447bf842013-05-15 14:52:15 +02001021 }
Michael Jurka54554252013-08-01 12:52:23 +02001022 if (mOnResumeCallbacks.size() > 0) {
1023 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1024 mOnResumeCallbacks.get(i).run();
1025 }
1026 mOnResumeCallbacks.clear();
1027 }
Winson Chunge4e50662012-01-23 14:45:13 -08001028
1029 // Reset the pressed state of icons that were locked in the press state while activities
1030 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001031 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001032 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001033 mWaitingForResume.setStayPressed(false);
1034 }
Winson Chung82963d52013-10-09 11:20:57 -07001035
Adam Cohen06dff352012-06-01 17:17:08 -07001036 // It is possible that widgets can receive updates while launcher is not in the foreground.
1037 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1038 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1039 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001040 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001041 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001042
Michael Jurka447bf842013-05-15 14:52:15 +02001043 if (DEBUG_RESUME_TIME) {
1044 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1045 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001046
1047 if (mWorkspace.getCustomContentCallbacks() != null) {
1048 // If we are resuming and the custom content is the current page, we call onShow().
1049 // It is also poassible that onShow will instead be called slightly after first layout
1050 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1051 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001052 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001053 }
1054 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001055 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001056 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001057
Sunny Goyal756adbc2015-04-16 15:20:51 -07001058 if (!isWorkspaceLoading()) {
1059 // Process any items that were added while Launcher was away.
1060 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1061 }
Adam Cohen9211d422014-10-07 18:14:53 -07001062
1063 if (mLauncherCallbacks != null) {
1064 mLauncherCallbacks.onResume();
1065 }
Adam Cohen06dff352012-06-01 17:17:08 -07001066 }
1067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001069 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001070 // Ensure that items added to Launcher are queued until Launcher returns
1071 InstallShortcutReceiver.enableInstallQueue();
1072
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001074 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001075 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001076 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001077
1078 // We call onHide() aggressively. The custom content callbacks should be able to
1079 // debounce excess onHide calls.
1080 if (mWorkspace.getCustomContentCallbacks() != null) {
1081 mWorkspace.getCustomContentCallbacks().onHide();
1082 }
Romain Guycbb89e42009-06-08 15:52:54 -07001083
Adam Cohen9211d422014-10-07 18:14:53 -07001084 if (mLauncherCallbacks != null) {
1085 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001086 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001087 }
1088
1089 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001090 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1091 // by a onResume or by scrolling otherwise.
1092 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001093
1094 // Custom content is completely hidden
1095 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001096
1097 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1098 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001099
1100 // Indicates whether the user is allowed to scroll away from the custom content.
1101 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001102 }
1103
Adam Cohenc2d6e892014-10-16 09:49:24 -07001104 public interface LauncherOverlay {
1105
1106 /**
1107 * Touch interaction leading to overscroll has begun
1108 */
1109 public void onScrollInteractionBegin();
1110
1111 /**
1112 * Touch interaction related to overscroll has ended
1113 */
1114 public void onScrollInteractionEnd();
1115
1116 /**
1117 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1118 * screen (or in the case of RTL, the rightmost screen).
1119 */
1120 public void onScrollChange(int progress, boolean rtl);
1121
1122 /**
1123 * Screen has stopped scrolling
1124 */
1125 public void onScrollSettled();
1126
1127 /**
1128 * This method can be called by the Launcher in order to force the LauncherOverlay
1129 * to exit fully immersive mode.
1130 */
1131 public void forceExitFullImmersion();
1132 }
1133
Winson Chung0f785722015-04-08 10:27:49 -07001134 public interface LauncherAppsCallbacks {
1135 /**
1136 * Updates launcher to the available space that AllApps can take so as not to overlap with
1137 * any other views.
1138 */
Winson Chungef7f8742015-06-04 17:18:17 -07001139 @Deprecated
Winson Chung0f785722015-04-08 10:27:49 -07001140 public void onAllAppsBoundsChanged(Rect bounds);
1141
1142 /**
1143 * Called to dismiss all apps if it is showing.
1144 */
Winson Chungef7f8742015-06-04 17:18:17 -07001145 @Deprecated
Winson Chung0f785722015-04-08 10:27:49 -07001146 public void dismissAllApps();
Sunny Goyal5683f872015-05-29 14:54:40 -07001147
1148 /**
1149 * Sets the search manager to be used for app search.
1150 */
Winson Chungef7f8742015-06-04 17:18:17 -07001151 @Deprecated
1152 public void setSearchManager(Object manager);
Winson Chung0f785722015-04-08 10:27:49 -07001153 }
1154
Jun Mukai8af0cd82015-05-12 12:32:12 -07001155 public interface LauncherSearchCallbacks {
1156 /**
1157 * Called when the search overlay is shown.
1158 */
1159 public void onSearchOverlayOpened();
1160
1161 /**
1162 * Called when the search overlay is dismissed.
1163 */
1164 public void onSearchOverlayClosed();
1165 }
1166
Adam Cohenc2d6e892014-10-16 09:49:24 -07001167 public interface LauncherOverlayCallbacks {
1168 /**
1169 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1170 * however it doesn't modify any state within the launcher.
1171 */
1172 public boolean canEnterFullImmersion();
1173
1174 /**
1175 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1176 * eg. by occupying the full screen and handling all touch events.
1177 *
1178 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1179 * case, Launcher will modify any necessary state and assumes the overlay is
1180 * handling all interaction. If false, the LauncherOverlay should cancel any
1181 *
1182 */
1183 public boolean enterFullImmersion();
1184
1185 /**
1186 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1187 * full control over UI and state.
1188 */
1189 public void exitFullImmersion();
1190 }
1191
1192 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1193
1194 @Override
1195 public boolean canEnterFullImmersion() {
1196 return mState == State.WORKSPACE;
1197 }
1198
1199 @Override
1200 public boolean enterFullImmersion() {
1201 if (mState == State.WORKSPACE) {
1202 // When fully immersed, disregard any touches which fall through.
1203 mDragLayer.setBlockTouch(true);
1204 return true;
1205 }
1206 return false;
1207 }
1208
1209 @Override
1210 public void exitFullImmersion() {
1211 mDragLayer.setBlockTouch(false);
1212 }
1213 }
1214
Jorim Jaggid017f882014-01-14 17:08:48 -08001215 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001216 if (mLauncherCallbacks != null) {
1217 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001218 } else {
1219 // On devices with a locked orientation, we will at least have the allow rotation
1220 // setting.
1221 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001222 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001223 }
1224
Adam Cohen9211d422014-10-07 18:14:53 -07001225 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001226 CustomContentCallbacks callbacks, String description) {
1227 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001228 }
1229
Adam Cohenedb40762013-07-18 16:45:45 -07001230 // The custom content needs to offset its content to account for the QSB
1231 public int getTopOffsetForCustomContent() {
1232 return mWorkspace.getPaddingTop();
1233 }
1234
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001235 @Override
1236 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001238 if (mModel.isCurrentCallbacks(this)) {
1239 mModel.stopLoader();
1240 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001241 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1242
Romain Guy13c2e7b2010-03-10 19:45:00 -08001243 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001244 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001245
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001246 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001247 @Override
1248 public void onWindowFocusChanged(boolean hasFocus) {
1249 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001250 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001251
1252 if (mLauncherCallbacks != null) {
1253 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1254 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001255 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 private boolean acceptFilter() {
1258 final InputMethodManager inputManager = (InputMethodManager)
1259 getSystemService(Context.INPUT_METHOD_SERVICE);
1260 return !inputManager.isFullscreenMode();
1261 }
1262
1263 @Override
1264 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001265 final int uniChar = event.getUnicodeChar();
1266 final boolean handled = super.onKeyDown(keyCode, event);
1267 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1268 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1270 keyCode, event);
1271 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001272 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001273 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001274 // showSearchDialog()
1275 // If there are multiple keystrokes before the search dialog takes focus,
1276 // onSearchRequested() will be called for every keystroke,
1277 // but it is idempotent, so it's fine.
1278 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 }
1280 }
1281
Joe Onorato8a9625e2010-01-28 15:55:35 -08001282 // Eat the long press event so the keyboard doesn't come up.
1283 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1284 return true;
1285 }
1286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 return handled;
1288 }
1289
Karl Rosaen138a0412009-04-23 19:00:21 -07001290 private String getTypedText() {
1291 return mDefaultKeySsb.toString();
1292 }
1293
1294 private void clearTypedText() {
1295 mDefaultKeySsb.clear();
1296 mDefaultKeySsb.clearSpans();
1297 Selection.setSelection(mDefaultKeySsb, 0);
1298 }
1299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001301 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1302 * State
1303 */
1304 private static State intToState(int stateOrdinal) {
1305 State state = State.WORKSPACE;
1306 final State[] stateValues = State.values();
1307 for (int i = 0; i < stateValues.length; i++) {
1308 if (stateValues[i].ordinal() == stateOrdinal) {
1309 state = stateValues[i];
1310 break;
1311 }
1312 }
1313 return state;
1314 }
1315
1316 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 * Restores the previous state, if it exists.
1318 *
1319 * @param savedState The previous state.
1320 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001321 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 private void restoreState(Bundle savedState) {
1323 if (savedState == null) {
1324 return;
1325 }
1326
Michael Jurka883f55b2010-10-21 15:47:14 -07001327 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001328 if (state == State.APPS || state == State.WIDGETS) {
1329 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001330 }
1331
Adam Cohen21cd0022013-10-09 18:57:02 -07001332 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1333 PagedView.INVALID_RESTORE_PAGE);
1334 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001335 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337
Winson Chung3d503fb2011-07-13 17:25:49 -07001338 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001339 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001340
Winson Chung3d503fb2011-07-13 17:25:49 -07001341 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1342 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001343 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001344 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1345 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001346 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1347 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001348 AppWidgetProviderInfo info = savedState.getParcelable(
1349 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001350 mPendingAddWidgetInfo = info == null ?
1351 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1352
Adam Cohen4637b5a2013-11-04 18:21:24 -08001353 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001354 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 mRestoring = true;
1356 }
1357
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001358 mItemIdToViewId = (HashMap<Integer, Integer>)
1359 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361
1362 /**
1363 * Finds all the views we need and configure them properly.
1364 */
1365 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001366 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001367
Craig Mautner360310b2012-10-26 15:13:08 -07001368 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001369 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001370 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1371 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001372 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001373 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001374
John Spurlock77e1f472013-09-11 10:09:51 -04001375 mLauncherView.setSystemUiVisibility(
1376 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001377 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378
Winson Chung4c98d922011-05-31 16:50:48 -07001379 // Setup the drag layer
1380 mDragLayer.setup(this, dragController);
1381
Winson Chung3d503fb2011-07-13 17:25:49 -07001382 // Setup the hotseat
1383 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1384 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001385 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001386 }
1387
Jorim Jaggid017f882014-01-14 17:08:48 -08001388 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001389 View widgetButton = findViewById(R.id.widget_button);
1390 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001391 @Override
1392 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001393 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001394 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001395 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001396 }
1397 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001398 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1399
1400 View wallpaperButton = findViewById(R.id.wallpaper_button);
1401 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001402 @Override
1403 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001404 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001405 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001406 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001407 }
1408 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001409 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1410
1411 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001412 if (hasSettings()) {
1413 settingsButton.setOnClickListener(new OnClickListener() {
1414 @Override
1415 public void onClick(View arg0) {
1416 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001417 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001418 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001419 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001420 });
1421 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1422 } else {
1423 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001424 }
1425
Adam Cohendbdff6b2013-09-18 19:09:15 -07001426 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001427
Winson Chung4c98d922011-05-31 16:50:48 -07001428 // Setup the workspace
1429 mWorkspace.setHapticFeedbackEnabled(false);
1430 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001431 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001432 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001433
Winson Chungf0ea4d32011-06-06 14:27:16 -07001434 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001435 mSearchDropTargetBar = (SearchDropTargetBar)
1436 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001437
Winson Chungef7f8742015-06-04 17:18:17 -07001438 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001439 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001440 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001441 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1442 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1443 } else {
1444 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1445 }
Craig Mautner360310b2012-10-26 15:13:08 -07001446
Winson Chung3d503fb2011-07-13 17:25:49 -07001447 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001448 dragController.setDragScoller(mWorkspace);
1449 dragController.setScrollView(mDragLayer);
1450 dragController.setMoveTarget(mWorkspace);
1451 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001452 if (mSearchDropTargetBar != null) {
1453 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001454 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001455 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001456
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001457 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001458 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001459 mWeightWatcher = new WeightWatcher(this);
1460 mWeightWatcher.setAlpha(0.5f);
1461 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001462 new FrameLayout.LayoutParams(
1463 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001464 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001465 Gravity.BOTTOM)
1466 );
Adam Cohen39a06042013-07-19 14:30:12 -07001467
1468 boolean show = shouldShowWeightWatcher();
1469 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472
1473 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001474 * Sets the all apps button. This method is called from {@link Hotseat}.
1475 */
1476 public void setAllAppsButton(View allAppsButton) {
1477 mAllAppsButton = allAppsButton;
1478 }
1479
1480 public View getAllAppsButton() {
1481 return mAllAppsButton;
1482 }
1483
1484 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * Creates a view representing a shortcut.
1486 *
1487 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001489 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001490 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492
1493 /**
1494 * Creates a view representing a shortcut inflated from the specified resource.
1495 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 * @param parent The group the shortcut belongs to.
1497 * @param info The data structure describing the shortcut.
1498 *
1499 * @return A View inflated from layoutResId.
1500 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001501 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001502 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001503 parent, false);
1504 favorite.applyFromShortcutInfo(info, mIconCache);
1505 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001507 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 return favorite;
1509 }
1510
1511 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 * Add a shortcut to the workspace.
1513 *
1514 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001517 int cellY) {
1518 int[] cellXY = mTmpAddItemCellCoordinates;
1519 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001520 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001521
Sunny Goyal5c97f512015-05-19 16:03:28 -07001522 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001523 if (info == null) {
1524 return;
1525 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 final View view = createShortcut(info);
1527
Sunny Goyal5c97f512015-05-19 16:03:28 -07001528 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001529 // First we check if we already know the exact location where we want to add this item.
1530 if (cellX >= 0 && cellY >= 0) {
1531 cellXY[0] = cellX;
1532 cellXY[1] = cellY;
1533 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001534
1535 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001536 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001537 true, null,null)) {
1538 return;
1539 }
1540 DragObject dragObject = new DragObject();
1541 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001542 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1543 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001544 return;
1545 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001546 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001547 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001548 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001549 foundCellSpan = (result != null);
1550 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001551 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001552 }
1553
1554 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001555 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 return;
1557 }
1558
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001559 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
1561 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001562 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001563 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
1565 }
1566
Adam Cohen2e6da152015-05-06 11:42:25 -07001567 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1568 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001569 // We want to account for the extra amount of padding that we are adding to the widget
1570 // to ensure that it gets the full amount of space that it has requested
1571 int requiredWidth = minWidth + padding.left + padding.right;
1572 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001573 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001574 }
1575
Adam Cohen2e6da152015-05-06 11:42:25 -07001576 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1577 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001578 }
1579
Adam Cohen2e6da152015-05-06 11:42:25 -07001580 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1581 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001582 }
1583
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001585 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001587 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 */
Adam Cohen091440a2015-03-18 14:16:05 -07001589 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001590 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1591
1592 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001593 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001594 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1595 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001596 }
Romain Guycbb89e42009-06-08 15:52:54 -07001597
Adam Cohen59400422014-03-05 18:07:04 -08001598 if (appWidgetInfo.isCustomWidget) {
1599 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001600 }
1601
Adam Cohen59400422014-03-05 18:07:04 -08001602 LauncherAppWidgetInfo launcherInfo;
1603 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1604 launcherInfo.spanX = info.spanX;
1605 launcherInfo.spanY = info.spanY;
1606 launcherInfo.minSpanX = info.minSpanX;
1607 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001608 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001609
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001611 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
1613 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001614 if (hostView == null) {
1615 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001616 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1617 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001618 } else {
1619 // The AppWidgetHostView has already been inflated and instantiated
1620 launcherInfo.hostView = hostView;
1621 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001623 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001624 launcherInfo.notifyWidgetSizeChanged(this);
1625
Adam Cohen59400422014-03-05 18:07:04 -08001626 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1627 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001628
1629 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001631 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 }
Romain Guycbb89e42009-06-08 15:52:54 -07001633
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1635 @Override
1636 public void onReceive(Context context, Intent intent) {
1637 final String action = intent.getAction();
1638 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1639 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001640 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001641 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001642
Winson Chungc100e8e2011-08-09 16:02:43 -07001643 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001644 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001645 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001646 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001647 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001648 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1650 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001651 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001652 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1653 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001654 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001655 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1656 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1657 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001658 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001659 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1660 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001661 }
1662 }
1663 };
1664
1665 @Override
1666 public void onAttachedToWindow() {
1667 super.onAttachedToWindow();
1668
1669 // Listen for broadcasts related to user-presence
1670 final IntentFilter filter = new IntentFilter();
1671 filter.addAction(Intent.ACTION_SCREEN_OFF);
1672 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001673 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001674 if (ENABLE_DEBUG_INTENTS) {
1675 filter.addAction(DebugIntents.DELETE_DATABASE);
1676 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1677 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001678 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001679 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001680 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001681 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 mVisible = true;
1683 }
1684
Adam Cohen0b395352014-06-09 22:54:36 +00001685 /**
1686 * Sets up transparent navigation and status bars in LMP.
1687 * This method is a no-op for other platform versions.
1688 */
Sunny Goyald0091012015-01-20 15:55:34 -08001689 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001690 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001691 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001692 Window window = getWindow();
1693 window.getAttributes().systemUiVisibility |=
1694 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1695 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1696 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1697 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1698 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1699 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1700 window.setStatusBarColor(Color.TRANSPARENT);
1701 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001702 }
1703 }
1704
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 @Override
1706 public void onDetachedFromWindow() {
1707 super.onDetachedFromWindow();
1708 mVisible = false;
1709
Adam Cohend113e0c2010-11-11 10:48:05 -08001710 if (mAttached) {
1711 unregisterReceiver(mReceiver);
1712 mAttached = false;
1713 }
Winson Chungb745afb2015-03-02 11:51:23 -08001714 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 }
1716
1717 public void onWindowVisibilityChanged(int visibility) {
1718 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001719 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001720 // The following code used to be in onResume, but it turns out onResume is called when
1721 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1722 // is a more appropriate event to handle
1723 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001724 if (!mWorkspaceLoading) {
1725 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001726 // We want to let Launcher draw itself at least once before we force it to build
1727 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001728 // apps is nice and speedy.
1729 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001730 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001731 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001732 if (mStarted) return;
1733 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001734 // We delay the layer building a bit in order to give
1735 // other message processing a time to run. In particular
1736 // this avoids a delay in hiding the IME if it was
1737 // currently shown, because doing that may involve
1738 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001739 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001740 final ViewTreeObserver.OnDrawListener listener = this;
1741 mWorkspace.post(new Runnable() {
1742 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001743 if (mWorkspace != null &&
1744 mWorkspace.getViewTreeObserver() != null) {
1745 mWorkspace.getViewTreeObserver().
1746 removeOnDrawListener(listener);
1747 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001748 }
1749 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001750 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001751 }
1752 });
1753 }
1754 clearTypedText();
1755 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 }
1757
Adam Cohen091440a2015-03-18 14:16:05 -07001758 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 mHandler.removeMessages(ADVANCE_MSG);
1760 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1761 mHandler.sendMessageDelayed(msg, delay);
1762 mAutoAdvanceSentTime = System.currentTimeMillis();
1763 }
1764
Adam Cohen091440a2015-03-18 14:16:05 -07001765 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1767 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1768 mAutoAdvanceRunning = autoAdvanceRunning;
1769 if (autoAdvanceRunning) {
1770 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1771 sendAdvanceMessage(delay);
1772 } else {
1773 if (!mWidgetsToAdvance.isEmpty()) {
1774 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1775 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1776 }
1777 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001778 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 }
1780 }
1781 }
1782
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001783 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1784
Adam Cohended9f8d2010-11-03 13:25:16 -07001785 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001786 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 if (msg.what == ADVANCE_MSG) {
1788 int i = 0;
1789 for (View key: mWidgetsToAdvance.keySet()) {
1790 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1791 final int delay = mAdvanceStagger * i;
1792 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001793 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001794 public void run() {
1795 ((Advanceable) v).advance();
1796 }
1797 }, delay);
1798 }
1799 i++;
1800 }
1801 sendAdvanceMessage(mAdvanceInterval);
1802 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001803 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001804 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001805 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001806
1807 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001808 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001809 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1810 if (v instanceof Advanceable) {
1811 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001812 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001813 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001814 }
1815 }
1816
1817 void removeWidgetToAutoAdvance(View hostView) {
1818 if (mWidgetsToAdvance.containsKey(hostView)) {
1819 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001820 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001821 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001822 }
1823
Joe Onorato9c1289c2009-08-17 11:03:03 -04001824 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001825 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001826 launcherInfo.hostView = null;
1827 }
1828
Hyunyoung Song3f471442015-04-08 19:01:34 -07001829 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001830 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1831 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001832 }
1833
Winson Chunga6945242014-01-08 14:04:34 -08001834 public DragLayer getDragLayer() {
1835 return mDragLayer;
1836 }
1837
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001838 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001839 return mAppsView;
1840 }
1841
Hyunyoung Song3f471442015-04-08 19:01:34 -07001842 public WidgetsContainerView getWidgetsView() {
1843 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001844 }
1845
Winson Chunga6945242014-01-08 14:04:34 -08001846 public Workspace getWorkspace() {
1847 return mWorkspace;
1848 }
1849
1850 public Hotseat getHotseat() {
1851 return mHotseat;
1852 }
1853
Jorim Jaggid017f882014-01-14 17:08:48 -08001854 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001855 return mOverviewPanel;
1856 }
1857
1858 public SearchDropTargetBar getSearchBar() {
1859 return mSearchDropTargetBar;
1860 }
1861
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001862 public LauncherAppWidgetHost getAppWidgetHost() {
1863 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 }
Romain Guycbb89e42009-06-08 15:52:54 -07001865
Winson Chunga9abd0e2010-10-27 17:18:37 -07001866 public LauncherModel getModel() {
1867 return mModel;
1868 }
1869
Winson Chunga6945242014-01-08 14:04:34 -08001870 protected SharedPreferences getSharedPrefs() {
1871 return mSharedPrefs;
1872 }
1873
Adam Cohen2e6da152015-05-06 11:42:25 -07001874 public DeviceProfile getDeviceProfile() {
1875 return mDeviceProfile;
1876 }
1877
Bjorn Bringertc459e522013-06-07 19:36:01 +01001878 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001879 getWindow().closeAllPanels();
1880
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001881 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001882 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001883 }
1884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 @Override
1886 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001887 long startTime = 0;
1888 if (DEBUG_RESUME_TIME) {
1889 startTime = System.currentTimeMillis();
1890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 super.onNewIntent(intent);
1892
1893 // Close the menu
1894 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001895 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001896 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001897
Adam Cohened307df2013-10-02 09:37:31 -07001898 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1899 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1900 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001901
Adam Cohen6fecd412013-10-02 17:41:50 -07001902 if (mWorkspace == null) {
1903 // Can be cases where mWorkspace is null, this prevents a NPE
1904 return;
1905 }
1906 Folder openFolder = mWorkspace.getOpenFolder();
1907 // In all these cases, only animate if we're already on home
1908 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001909
1910 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1911 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001912 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001913 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001914 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001915 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001916
Adam Cohen6fecd412013-10-02 17:41:50 -07001917 closeFolder();
1918 exitSpringLoadedDragMode();
1919
1920 // If we are already on home, then just animate back to the workspace,
1921 // otherwise, just wait until onResume to set the state back to Workspace
1922 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001923 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001924 } else {
1925 mOnResumeState = State.WORKSPACE;
1926 }
1927
1928 final View v = getWindow().peekDecorView();
1929 if (v != null && v.getWindowToken() != null) {
1930 InputMethodManager imm = (InputMethodManager)getSystemService(
1931 INPUT_METHOD_SERVICE);
1932 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1933 }
1934
Winson Chungb745afb2015-03-02 11:51:23 -08001935 // Reset the apps view
1936 if (!alreadyOnHome && mAppsView != null) {
1937 mAppsView.scrollToTop();
1938 }
1939
Hyunyoung Song3f471442015-04-08 19:01:34 -07001940 // Reset the widgets view
1941 if (!alreadyOnHome && mWidgetsView != null) {
1942 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001943 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001944
Adam Cohen9211d422014-10-07 18:14:53 -07001945 if (mLauncherCallbacks != null) {
1946 mLauncherCallbacks.onHomeIntent();
1947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
Adam Cohened307df2013-10-02 09:37:31 -07001949
Michael Jurka447bf842013-05-15 14:52:15 +02001950 if (DEBUG_RESUME_TIME) {
1951 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953
Adam Cohen9211d422014-10-07 18:14:53 -07001954 if (mLauncherCallbacks != null) {
1955 mLauncherCallbacks.onNewIntent(intent);
1956 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001957 }
1958
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001960 public void onRestoreInstanceState(Bundle state) {
1961 super.onRestoreInstanceState(state);
1962 for (int page: mSynchronouslyBoundPages) {
1963 mWorkspace.restoreInstanceStateForChild(page);
1964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
1966
1967 @Override
1968 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001969 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001970 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1971 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001972 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001973 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974
Michael Jurka883f55b2010-10-21 15:47:14 -07001975 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001976 // We close any open folder since it will not be re-opened, and we need to make sure
1977 // this state is reflected.
1978 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001981 mWaitingForResult) {
1982 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001983 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001984 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1985 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001986 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1987 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1988 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001989 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
1991
Hyunyoung Song3f471442015-04-08 19:01:34 -07001992 // Save the current widgets tray?
1993 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001994 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001995
1996 if (mLauncherCallbacks != null) {
1997 mLauncherCallbacks.onSaveInstanceState(outState);
1998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
2000
2001 @Override
2002 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002004
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04002005 if (mScreenOrientationSettingReceiverRegistered) {
2006 unregisterReceiver(mScreenOrientationSettingReceiver);
2007 mScreenOrientationSettingReceiverRegistered = false;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002008 }
2009
Winson Chunge7a03942011-08-05 15:05:12 -07002010 // Remove all pending runnables
2011 mHandler.removeMessages(ADVANCE_MSG);
2012 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002013 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002014
Winson Chungcd2b0142011-06-08 16:02:26 -07002015 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002016 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002017
2018 // It's possible to receive onDestroy after a new Launcher activity has
2019 // been created. In this case, don't interfere with the new Launcher.
2020 if (mModel.isCurrentCallbacks(this)) {
2021 mModel.stopLoader();
2022 app.setLauncher(null);
2023 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002026 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002028 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002030 mAppWidgetHost = null;
2031
2032 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033
2034 TextKeyListener.getInstance().release();
2035
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002036 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002037 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002038
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002039 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002040 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002041 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002042 mWorkspace = null;
2043 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002044
Michael Jurka2ecf9952012-06-18 12:52:28 -07002045 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002046
2047 if (mLauncherCallbacks != null) {
2048 mLauncherCallbacks.onDestroy();
2049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 }
2051
Adam Cohena9cf38f2011-05-02 15:36:58 -07002052 public DragController getDragController() {
2053 return mDragController;
2054 }
2055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 @Override
2057 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002058 onStartForResult(requestCode);
2059 super.startActivityForResult(intent, requestCode);
2060 }
2061
2062 @Override
2063 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2064 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2065 onStartForResult(requestCode);
2066 try {
2067 super.startIntentSenderForResult(intent, requestCode,
2068 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2069 } catch (IntentSender.SendIntentException e) {
2070 throw new ActivityNotFoundException();
2071 }
2072 }
2073
2074 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002075 if (requestCode >= 0) {
2076 setWaitingForResult(true);
2077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 }
2079
Winson Chung70d72102011-08-12 11:24:35 -07002080 /**
2081 * Indicates that we want global search for this activity by setting the globalSearch
2082 * argument for {@link #startSearch} to true.
2083 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002085 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002087
Karl Rosaen138a0412009-04-23 19:00:21 -07002088 if (initialQuery == null) {
2089 // Use any text typed in the launcher as the initial query
2090 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002091 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 if (appSearchData == null) {
2093 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002094 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 }
Winson Chungadf0c182012-08-23 14:59:07 -07002096 Rect sourceBounds = new Rect();
2097 if (mSearchDropTargetBar != null) {
2098 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2099 }
Romain Guycbb89e42009-06-08 15:52:54 -07002100
Ian Parkinson047036e2014-09-01 15:40:53 +01002101 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002102 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002103 if (clearTextImmediately) {
2104 clearTypedText();
2105 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002106
2107 // We need to show the workspace after starting the search
2108 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002109 }
2110
Ian Parkinson047036e2014-09-01 15:40:53 +01002111 /**
2112 * Start a text search.
2113 *
2114 * @return {@code true} if the search will start immediately, so any further keypresses
2115 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2116 * to buffer keypresses.
2117 */
2118 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002119 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002120 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2121 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2122 sourceBounds);
2123 }
2124
Michael Jurkaa33411c2012-06-14 16:18:21 -07002125 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002126 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002127 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002128 }
2129
2130 /**
2131 * Starts the global search activity. This code is a copied from SearchManager
2132 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002133 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002134 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002135 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002136 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2137 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2138 if (globalSearchActivity == null) {
2139 Log.w(TAG, "No global search activity found.");
2140 return;
2141 }
2142 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2143 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2144 intent.setComponent(globalSearchActivity);
2145 // Make sure that we have a Bundle to put source in
2146 if (appSearchData == null) {
2147 appSearchData = new Bundle();
2148 } else {
2149 appSearchData = new Bundle(appSearchData);
2150 }
Adam Cohen9211d422014-10-07 18:14:53 -07002151 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002152 if (!appSearchData.containsKey("source")) {
2153 appSearchData.putString("source", getPackageName());
2154 }
2155 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2156 if (!TextUtils.isEmpty(initialQuery)) {
2157 intent.putExtra(SearchManager.QUERY, initialQuery);
2158 }
2159 if (selectInitialQuery) {
2160 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2161 }
2162 intent.setSourceBounds(sourceBounds);
2163 try {
2164 startActivity(intent);
2165 } catch (ActivityNotFoundException ex) {
2166 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 }
2169
Yura4f93ec62014-02-04 14:15:21 +00002170 public boolean isOnCustomContent() {
2171 return mWorkspace.isOnOrMovingToCustomContent();
2172 }
2173
Winson Chung70d72102011-08-12 11:24:35 -07002174 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002175 public boolean onPrepareOptionsMenu(Menu menu) {
2176 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002177 if (!isOnCustomContent()) {
2178 // Close any open folders
2179 closeFolder();
2180 // Stop resizing any widgets
2181 mWorkspace.exitWidgetResizeMode();
2182 if (!mWorkspace.isInOverviewMode()) {
2183 // Show the overview mode
2184 showOverviewMode(true);
2185 } else {
2186 showWorkspace(true);
2187 }
Winson Chunge0298742014-01-17 12:03:00 -08002188 }
Adam Cohen9211d422014-10-07 18:14:53 -07002189 if (mLauncherCallbacks != null) {
2190 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2191 }
2192
Michael Jurkab94f3f82013-09-11 18:08:54 +02002193 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002194 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002196 @Override
2197 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002198 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002199 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002200 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002201 }
2202
Joe Onorato9c1289c2009-08-17 11:03:03 -04002203 public boolean isWorkspaceLocked() {
2204 return mWorkspaceLoading || mWaitingForResult;
2205 }
2206
Adam Cohen517a7f52014-03-01 12:12:59 -08002207 public boolean isWorkspaceLoading() {
2208 return mWorkspaceLoading;
2209 }
2210
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002211 private void setWorkspaceLoading(boolean value) {
2212 boolean isLocked = isWorkspaceLocked();
2213 mWorkspaceLoading = value;
2214 if (isLocked != isWorkspaceLocked()) {
2215 onWorkspaceLockedChanged();
2216 }
2217 }
2218
2219 private void setWaitingForResult(boolean value) {
2220 boolean isLocked = isWorkspaceLocked();
2221 mWaitingForResult = value;
2222 if (isLocked != isWorkspaceLocked()) {
2223 onWorkspaceLockedChanged();
2224 }
2225 }
2226
Adam Cohen9211d422014-10-07 18:14:53 -07002227 protected void onWorkspaceLockedChanged() {
2228 if (mLauncherCallbacks != null) {
2229 mLauncherCallbacks.onWorkspaceLockedChanged();
2230 }
2231 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002232
Michael Jurka0280c3b2010-09-17 15:00:07 -07002233 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002234 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002235 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002236 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2237 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002238 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002239 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002240 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002241
Sunny Goyale6b63a32015-01-16 16:14:29 -08002242 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002243 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002244 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2245 }
2246
Sunny Goyale6b63a32015-01-16 16:14:29 -08002247 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002248 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2249 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002250 if (appWidgetInfo.configure != null) {
2251 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002252 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002253
Bjorn Bringert7984c942009-12-09 15:38:25 +00002254 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002255 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2256 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002259 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002260 Runnable onComplete = new Runnable() {
2261 @Override
2262 public void run() {
2263 // Exit spring loaded mode if necessary after adding the widget
2264 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2265 null);
2266 }
2267 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002268 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002269 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002270 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 }
2272 }
Romain Guycbb89e42009-06-08 15:52:54 -07002273
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002274 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002275 // Close any folders that may be open.
2276 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002277 mWorkspace.moveToCustomContentScreen(animate);
2278 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002279
2280 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2281 int[] cell, int spanX, int spanY) {
2282 switch (info.itemType) {
2283 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2284 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2285 int span[] = new int[2];
2286 span[0] = spanX;
2287 span[1] = spanY;
2288 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2289 container, screenId, cell, span);
2290 break;
2291 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2292 processShortcutFromDrop(info.componentName, container, screenId, cell);
2293 break;
2294 default:
2295 throw new IllegalStateException("Unknown item type: " + info.itemType);
2296 }
2297 }
2298
Adam Cohenfbba09b2011-07-18 21:43:05 -07002299 /**
2300 * Process a shortcut drop.
2301 *
2302 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002303 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002304 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002306 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2307 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002308 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002309 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002310 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002311 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002312
2313 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002314 mPendingAddInfo.cellX = cell[0];
2315 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002316 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002317
2318 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2319 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002320 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002321 }
2322
Adam Cohenfbba09b2011-07-18 21:43:05 -07002323 /**
2324 * Process a widget drop.
2325 *
2326 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002327 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002330 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2331 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002335 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002336 mPendingAddInfo.minSpanX = info.minSpanX;
2337 mPendingAddInfo.minSpanY = info.minSpanY;
2338
Adam Cohenfbba09b2011-07-18 21:43:05 -07002339 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 mPendingAddInfo.cellX = cell[0];
2341 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002343 if (span != null) {
2344 mPendingAddInfo.spanX = span[0];
2345 mPendingAddInfo.spanY = span[1];
2346 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002347
Adam Cohened66b2b2012-01-23 17:28:51 -08002348 AppWidgetHostView hostView = info.boundWidget;
2349 int appWidgetId;
2350 if (hostView != null) {
2351 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002352 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002353
2354 // Clear the boundWidget so that it doesn't get destroyed.
2355 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002356 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002357 // In this case, we either need to start an activity to get permission to bind
2358 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002359 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002360 Bundle options = info.bindOptions;
2361
Sunny Goyalffe83f12014-08-14 17:39:34 -07002362 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2363 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002364 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002365 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002366 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002367 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002368 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2369 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2370 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002371 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2372 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002373 // TODO: we need to make sure that this accounts for the options bundle.
2374 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002375 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2376 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002377 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002378 }
2379
Adam Cohendcd297f2013-06-18 13:13:40 -07002380 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002381 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002382 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 folderInfo.title = getText(R.string.folder_name);
2384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002386 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2387 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002388 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389
2390 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002391 FolderIcon newFolder =
2392 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002393 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002394 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002395 // Force measure the new folder icon
2396 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2397 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002398 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 }
Romain Guycbb89e42009-06-08 15:52:54 -07002400
Joe Onorato9c1289c2009-08-17 11:03:03 -04002401 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002402 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002403 }
2404
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002405 /**
2406 * Registers various content observers. The current implementation registers
2407 * only a favorites observer to keep track of the favorites applications.
2408 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002409 private void registerContentObservers() {
2410 ContentResolver resolver = getContentResolver();
2411 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2412 true, mWidgetObserver);
2413 }
2414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 @Override
2416 public boolean dispatchKeyEvent(KeyEvent event) {
2417 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2418 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002420 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002421 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002422 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002423 dumpState();
2424 return true;
2425 }
2426 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002427 }
2428 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2429 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002430 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 return true;
2432 }
2433 }
2434
2435 return super.dispatchKeyEvent(event);
2436 }
2437
Joe Onorato88ec0992009-11-19 13:16:06 -08002438 @Override
2439 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002440 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2441 return;
2442 }
2443
Sunny Goyal45478022015-06-08 16:52:41 -07002444 if (mDragController.isDragging()) {
2445 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002446 return;
2447 }
2448
Winson Chungb745afb2015-03-02 11:51:23 -08002449 if (isAppsViewVisible()) {
2450 showWorkspace(true);
2451 } else if (isWidgetsViewVisible()) {
2452 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002453 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002454 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002455 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002456 Folder openFolder = mWorkspace.getOpenFolder();
2457 if (openFolder.isEditingName()) {
2458 openFolder.dismissEditingName();
2459 } else {
2460 closeFolder();
2461 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002462 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002463 mWorkspace.exitWidgetResizeMode();
2464
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002465 // Back button is a no-op here, but give at least some feedback for the button press
2466 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002467 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002468 }
2469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002471 * Re-listen when widgets are reset.
2472 */
Adam Cohen091440a2015-03-18 14:16:05 -07002473 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002474 if (mAppWidgetHost != null) {
2475 mAppWidgetHost.startListening();
2476 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002477 }
2478
2479 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 * Launches the intent referred by the clicked shortcut.
2481 *
2482 * @param v The view representing the clicked shortcut.
2483 */
2484 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002485 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2486 // view has detached (it's possible for this to happen if the view is removed mid touch).
2487 if (v.getWindowToken() == null) {
2488 return;
2489 }
2490
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002491 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002492 return;
2493 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002494
Adam Cohen1697b792013-09-17 19:08:21 -07002495 if (v instanceof Workspace) {
2496 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002497 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002498 }
2499 return;
2500 }
2501
2502 if (v instanceof CellLayout) {
2503 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002504 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002505 }
2506 }
2507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002509 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002510 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002512 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002514 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002515 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002516 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002517 } else if (tag instanceof AppInfo) {
2518 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002519 } else if (tag instanceof LauncherAppWidgetInfo) {
2520 if (v instanceof PendingAppWidgetHostView) {
2521 onClickPendingWidget((PendingAppWidgetHostView) v);
2522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 }
2524 }
2525
Sunny Goyal70660032015-05-14 00:07:08 -07002526 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002527 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002528 return false;
2529 }
2530
Michael Jurkaaf442092010-06-10 17:01:57 -07002531 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002532 * Event handler for the app widget view which has not fully restored.
2533 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002534 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002535 if (mIsSafeModeEnabled) {
2536 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2537 return;
2538 }
2539
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002540 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002541 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002542 int widgetId = info.appWidgetId;
2543 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2544 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002545 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2546 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002547 mPendingAddInfo.copyFrom(info);
2548 mPendingAddWidgetId = widgetId;
2549
Sunny Goyalffe83f12014-08-14 17:39:34 -07002550 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2551 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002552 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002553 } else if (info.installProgress < 0) {
2554 // The install has not been queued
2555 final String packageName = info.providerName.getPackageName();
2556 showBrokenAppInstallDialog(packageName,
2557 new DialogInterface.OnClickListener() {
2558 public void onClick(DialogInterface dialog, int id) {
2559 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2560 }
2561 });
2562 } else {
2563 // Download has started.
2564 final String packageName = info.providerName.getPackageName();
2565 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002566 }
2567 }
2568
2569 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002570 * Event handler for the "grid" button that appears on the home screen, which
2571 * enters all apps mode.
2572 *
2573 * @param v The view that was clicked.
2574 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002575 protected void onClickAllAppsButton(View v) {
2576 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002577 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002578 showWorkspace(true);
2579 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002580 // Try and refresh the set of predicted apps before we enter launcher
2581 showAppsView(true /* animated */, false /* resetListToTop */,
2582 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002583 }
2584 }
2585
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002586 private void showBrokenAppInstallDialog(final String packageName,
2587 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002588 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002589 .setTitle(R.string.abandoned_promises_title)
2590 .setMessage(R.string.abandoned_promise_explanation)
2591 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2592 .setNeutralButton(R.string.abandoned_clean_this,
2593 new DialogInterface.OnClickListener() {
2594 public void onClick(DialogInterface dialog, int id) {
2595 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2596 mWorkspace.removeAbandonedPromise(packageName, user);
2597 }
2598 })
2599 .create().show();
2600 return;
2601 }
2602
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 /**
2604 * Event handler for an app shortcut click.
2605 *
2606 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2607 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002608 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002609 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2610 Object tag = v.getTag();
2611 if (!(tag instanceof ShortcutInfo)) {
2612 throw new IllegalArgumentException("Input must be a Shortcut");
2613 }
2614
2615 // Open shortcut
2616 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002617
2618 if (shortcut.isDisabled != 0) {
2619 int error = R.string.activity_not_available;
2620 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2621 error = R.string.safemode_shortcut_error;
2622 }
2623 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2624 return;
2625 }
2626
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 final Intent intent = shortcut.intent;
2628
2629 // Check for special shortcuts
2630 if (intent.getComponent() != null) {
2631 final String shortcutClass = intent.getComponent().getClassName();
2632
2633 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2634 MemoryDumpActivity.startDump(this);
2635 return;
2636 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2637 toggleShowWeightWatcher();
2638 return;
2639 }
2640 }
2641
Chris Wren40c5ed32014-06-24 18:24:23 -04002642 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002643 if ((v instanceof BubbleTextView)
2644 && shortcut.isPromise()
2645 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002646 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002647 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 new DialogInterface.OnClickListener() {
2649 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002650 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002652 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002653 return;
2654 }
2655
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002656 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002657 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002658
2659 if (mLauncherCallbacks != null) {
2660 mLauncherCallbacks.onClickAppShortcut(v);
2661 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002662 }
2663
Adam Cohen091440a2015-03-18 14:16:05 -07002664 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002666 final ShortcutInfo shortcut;
2667 final Intent intent;
2668 if (tag instanceof ShortcutInfo) {
2669 shortcut = (ShortcutInfo) tag;
2670 intent = shortcut.intent;
2671 int[] pos = new int[2];
2672 v.getLocationOnScreen(pos);
2673 intent.setSourceBounds(new Rect(pos[0], pos[1],
2674 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002675
Sunny Goyal508da152014-08-14 10:53:27 -07002676 } else if (tag instanceof AppInfo) {
2677 shortcut = null;
2678 intent = ((AppInfo) tag).intent;
2679 } else {
2680 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2681 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002682
2683 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002684 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002685
2686 if (success && v instanceof BubbleTextView) {
2687 mWaitingForResume = (BubbleTextView) v;
2688 mWaitingForResume.setStayPressed(true);
2689 }
2690 }
2691
2692 /**
2693 * Event handler for a folder icon click.
2694 *
2695 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2696 */
2697 protected void onClickFolderIcon(View v) {
2698 if (LOGD) Log.d(TAG, "onClickFolder");
2699 if (!(v instanceof FolderIcon)){
2700 throw new IllegalArgumentException("Input must be a FolderIcon");
2701 }
2702
2703 FolderIcon folderIcon = (FolderIcon) v;
2704 final FolderInfo info = folderIcon.getFolderInfo();
2705 Folder openFolder = mWorkspace.getFolderForTag(info);
2706
2707 // If the folder info reports that the associated folder is open, then verify that
2708 // it is actually opened. There have been a few instances where this gets out of sync.
2709 if (info.opened && openFolder == null) {
2710 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2711 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2712 info.opened = false;
2713 }
2714
2715 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2716 // Close any open folder
2717 closeFolder();
2718 // Open the requested folder
2719 openFolder(folderIcon);
2720 } else {
2721 // Find the open folder...
2722 int folderScreen;
2723 if (openFolder != null) {
2724 folderScreen = mWorkspace.getPageForView(openFolder);
2725 // .. and close it
2726 closeFolder(openFolder);
2727 if (folderScreen != mWorkspace.getCurrentPage()) {
2728 // Close any folder open on the current screen
2729 closeFolder();
2730 // Pull the folder onto this screen
2731 openFolder(folderIcon);
2732 }
2733 }
2734 }
Adam Cohen9211d422014-10-07 18:14:53 -07002735
2736 if (mLauncherCallbacks != null) {
2737 mLauncherCallbacks.onClickFolderIcon(v);
2738 }
Michael Jurka5130e402011-10-13 04:55:35 -07002739 }
2740
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002741 /**
2742 * Event handler for the (Add) Widgets button that appears after a long press
2743 * on the home screen.
2744 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002745 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002746 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002747 if (mIsSafeModeEnabled) {
2748 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2749 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002750 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002751 if (mLauncherCallbacks != null) {
2752 mLauncherCallbacks.onClickAddWidgetButton(view);
2753 }
Adam Cohen9211d422014-10-07 18:14:53 -07002754 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002755 }
2756
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002757 /**
2758 * Event handler for the wallpaper picker button that appears after a long press
2759 * on the home screen.
2760 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002761 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002762 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002763 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2764 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002765
2766 if (mLauncherCallbacks != null) {
2767 mLauncherCallbacks.onClickWallpaperPicker(v);
2768 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002769 }
2770
2771 /**
2772 * Event handler for a click on the settings button that appears after a long press
2773 * on the home screen.
2774 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002775 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002776 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002777 if (mLauncherCallbacks != null) {
2778 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002779 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002780 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002781 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002782 }
2783
Adam Cohen61f560d2013-09-30 15:58:20 -07002784 public View.OnTouchListener getHapticFeedbackTouchListener() {
2785 if (mHapticFeedbackTouchListener == null) {
2786 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002787 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002788 @Override
2789 public boolean onTouch(View v, MotionEvent event) {
2790 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2791 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2792 }
2793 return false;
2794 }
2795 };
2796 }
2797 return mHapticFeedbackTouchListener;
2798 }
2799
Adam Cohen9211d422014-10-07 18:14:53 -07002800 public void onDragStarted(View view) {
2801 if (isOnCustomContent()) {
2802 // Custom content screen doesn't participate in drag and drop. If on custom
2803 // content screen, move to default.
2804 moveWorkspaceToDefaultScreen();
2805 }
2806
2807 if (mLauncherCallbacks != null) {
2808 mLauncherCallbacks.onDragStarted(view);
2809 }
2810 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002811
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002812 /**
2813 * Called when the user stops interacting with the launcher.
2814 * This implies that the user is now on the homescreen and is not doing housekeeping.
2815 */
Adam Cohen9211d422014-10-07 18:14:53 -07002816 protected void onInteractionEnd() {
2817 if (mLauncherCallbacks != null) {
2818 mLauncherCallbacks.onInteractionEnd();
2819 }
2820 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002821
2822 /**
2823 * Called when the user starts interacting with the launcher.
2824 * The possible interactions are:
2825 * - open all apps
2826 * - reorder an app shortcut, or a widget
2827 * - open the overview mode.
2828 * This is a good time to stop doing things that only make sense
2829 * when the user is on the homescreen and not doing housekeeping.
2830 */
Adam Cohen9211d422014-10-07 18:14:53 -07002831 protected void onInteractionBegin() {
2832 if (mLauncherCallbacks != null) {
2833 mLauncherCallbacks.onInteractionBegin();
2834 }
2835 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002836
Winson Chungcd99cd32015-04-29 11:03:24 -07002837 /** Updates the interaction state. */
2838 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002839 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002840 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002841 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2842 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002843 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002844 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002845 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002846 onInteractionEnd();
2847 }
2848 }
2849
Kenny Guyf07af7b2014-07-31 11:39:16 +01002850 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002851 try {
2852 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002853 launcherApps.showAppDetailsForProfile(componentName, user);
2854 } catch (SecurityException e) {
2855 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2856 Log.e(TAG, "Launcher does not have permission to launch settings");
2857 } catch (ActivityNotFoundException e) {
2858 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2859 Log.e(TAG, "Unable to launch settings");
2860 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002861 }
2862
Michael Jurka1e2f4652013-07-08 18:03:46 -07002863 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002864 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2865 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002866 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002867 // System applications cannot be installed. For now, show a toast explaining that.
2868 // We may give them the option of disabling apps this way.
2869 int messageId = R.string.uninstall_system_app_text;
2870 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002871 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002872 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002873 String packageName = componentName.getPackageName();
2874 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002875 Intent intent = new Intent(
2876 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002877 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2878 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002879 if (user != null) {
2880 user.addToIntent(intent, Intent.EXTRA_USER);
2881 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002882 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002883 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002884 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002885 }
2886
Winson Chung8f1eff72015-05-28 17:33:40 -07002887 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002888 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002890 // Only launch using the new animation if the shortcut has not opted out (this is a
2891 // private contract between launcher and may be ignored in the future).
2892 boolean useLaunchAnimation = (v != null) &&
2893 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002894 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2895 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002896
Kenny Guy1317e2d2014-05-08 18:52:50 +01002897 UserHandleCompat user = null;
2898 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2899 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2900 user = userManager.getUserForSerialNumber(serialNumber);
2901 }
2902
2903 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002904 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002905 ActivityOptions opts = null;
2906 if (sClipRevealMethod != null) {
2907 // TODO: call method directly when Launcher3 can depend on M APIs
2908 int left = 0, top = 0;
2909 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2910 if (v instanceof TextView) {
2911 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002912 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2913 if (icon != null) {
2914 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002915 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002916 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002917 width = bounds.width();
2918 height = bounds.height();
2919 }
2920 }
2921 try {
2922 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2923 left, top, width, height);
2924 } catch (IllegalAccessException e) {
2925 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2926 sClipRevealMethod = null;
2927 } catch (InvocationTargetException e) {
2928 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2929 sClipRevealMethod = null;
2930 }
2931 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002932 if (opts == null && !Utilities.isLmpOrAbove()) {
2933 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002934 v.getMeasuredWidth(), v.getMeasuredHeight());
2935 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002936 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002937 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002938
2939 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2940 // Could be launching some bookkeeping activity
2941 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002942 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002943 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002944 launcherApps.startActivityForProfile(intent.getComponent(), user,
2945 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002946 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002947 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 } catch (SecurityException e) {
2949 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002950 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002951 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002952 "or use the exported attribute for this activity. "
2953 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002955 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002957
Winson Chung8f1eff72015-05-28 17:33:40 -07002958 private boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002959 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002960 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2961 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2962 return false;
2963 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002964 try {
2965 success = startActivity(v, intent, tag);
2966 } catch (ActivityNotFoundException e) {
2967 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2968 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2969 }
2970 return success;
2971 }
2972
Adam Cohen268c4752012-06-06 17:47:33 -07002973 /**
2974 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2975 * in the DragLayer in the exact absolute location of the original FolderIcon.
2976 */
2977 private void copyFolderIconToImage(FolderIcon fi) {
2978 final int width = fi.getMeasuredWidth();
2979 final int height = fi.getMeasuredHeight();
2980
2981 // Lazy load ImageView, Bitmap and Canvas
2982 if (mFolderIconImageView == null) {
2983 mFolderIconImageView = new ImageView(this);
2984 }
2985 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2986 mFolderIconBitmap.getHeight() != height) {
2987 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2988 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2989 }
2990
2991 DragLayer.LayoutParams lp;
2992 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2993 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2994 } else {
2995 lp = new DragLayer.LayoutParams(width, height);
2996 }
2997
Adam Cohen307fe232012-08-16 17:55:58 -07002998 // The layout from which the folder is being opened may be scaled, adjust the starting
2999 // view size by this scale factor.
3000 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003001 lp.customPosition = true;
3002 lp.x = mRectForFolderAnimation.left;
3003 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003004 lp.width = (int) (scale * width);
3005 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003006
3007 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3008 fi.draw(mFolderIconCanvas);
3009 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003010 if (fi.getFolder() != null) {
3011 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3012 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003013 }
Adam Cohen268c4752012-06-06 17:47:33 -07003014 // Just in case this image view is still in the drag layer from a previous animation,
3015 // we remove it and re-add it.
3016 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3017 mDragLayer.removeView(mFolderIconImageView);
3018 }
3019 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003020 if (fi.getFolder() != null) {
3021 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003022 }
Adam Cohen268c4752012-06-06 17:47:33 -07003023 }
3024
Adam Cohen2801caf2011-05-13 20:57:39 -07003025 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003026 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003027 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3028 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3029 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3030
Adam Cohenc51934b2011-07-26 21:07:43 -07003031 FolderInfo info = (FolderInfo) fi.getTag();
3032 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3033 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003034 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3035 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003036 }
3037
Adam Cohen268c4752012-06-06 17:47:33 -07003038 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3039 copyFolderIconToImage(fi);
3040 fi.setVisibility(View.INVISIBLE);
3041
Michael Jurka2ecf9952012-06-18 12:52:28 -07003042 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003043 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003044 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003045 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3046 }
3047 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003048 oa.start();
3049 }
3050
Adam Cohen268c4752012-06-06 17:47:33 -07003051 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003052 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003053 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3054 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3055 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3056
Adam Cohen268c4752012-06-06 17:47:33 -07003057 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003058
Adam Cohen268c4752012-06-06 17:47:33 -07003059 // We remove and re-draw the FolderIcon in-case it has changed
3060 mDragLayer.removeView(mFolderIconImageView);
3061 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003062 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003063 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003064 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003065 oa.addListener(new AnimatorListenerAdapter() {
3066 @Override
3067 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003068 if (cl != null) {
3069 cl.clearFolderLeaveBehind();
3070 // Remove the ImageView copy of the FolderIcon and make the original visible.
3071 mDragLayer.removeView(mFolderIconImageView);
3072 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003073 }
3074 }
3075 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003076 oa.start();
3077 }
3078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003079 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003080 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003081 * is animated relative to the specified View. If the View is null, no animation
3082 * is played.
3083 *
3084 * @param folderInfo The FolderInfo describing the folder to open.
3085 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003086 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003087 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003088 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3089 if (openFolder != null && openFolder != folder) {
3090 // Close any open folder before opening a folder.
3091 closeFolder();
3092 }
3093
Adam Cohena9cf38f2011-05-02 15:36:58 -07003094 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095
Adam Cohena9cf38f2011-05-02 15:36:58 -07003096 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097
Sunny Goyalf4066152015-04-15 09:42:19 -07003098 // While the folder is open, the position of the icon cannot change.
3099 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3100
Adam Cohen4554ee12011-08-03 16:13:21 -07003101 // Just verify that the folder hasn't already been added to the DragLayer.
3102 // There was a one-off crash where the folder had a parent already.
3103 if (folder.getParent() == null) {
3104 mDragLayer.addView(folder);
3105 mDragController.addDropTarget((DropTarget) folder);
3106 } else {
3107 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3108 folder.getParent() + ").");
3109 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003110 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003111 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003112
3113 // Notify the accessibility manager that this folder "window" has appeared and occluded
3114 // the workspace items
3115 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3116 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003117 }
3118
3119 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003120 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003121 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003122 if (folder.isEditingName()) {
3123 folder.dismissEditingName();
3124 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003125 closeFolder(folder);
3126 }
3127 }
3128
Sunny Goyal83a8f042015-05-19 12:52:12 -07003129 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003130 folder.getInfo().opened = false;
3131
3132 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3133 if (parent != null) {
3134 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3135 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003136 if (fi != null) {
3137 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3138 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003139 }
3140 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003141
3142 // Notify the accessibility manager that this folder "window" has disappeard and no
3143 // longer occludeds the workspace items
3144 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 }
3146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003148 if (!isDraggingEnabled()) return false;
3149 if (isWorkspaceLocked()) return false;
3150 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151
Adam Cohen1697b792013-09-17 19:08:21 -07003152 if (v instanceof Workspace) {
3153 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003154 if (!mWorkspace.isTouchActive()) {
3155 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003156 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3157 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3158 return true;
3159 } else {
3160 return false;
3161 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003162 } else {
3163 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003164 }
Adam Cohen1697b792013-09-17 19:08:21 -07003165 }
3166
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003167 CellLayout.CellInfo longClickCellInfo = null;
3168 View itemUnderLongClick = null;
3169 if (v.getTag() instanceof ItemInfo) {
3170 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003171 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003172 itemUnderLongClick = longClickCellInfo.cell;
3173 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 }
3175
Winson Chung3d503fb2011-07-13 17:25:49 -07003176 // The hotseat touch handling does not go through Workspace, and we always allow long press
3177 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003178 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003179 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003180 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003181 // User long pressed on empty space
3182 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3183 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003184 if (mWorkspace.isInOverviewMode()) {
3185 mWorkspace.startReordering(v);
3186 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003187 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003189 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003190 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3191 mHotseat.getOrderInHotseat(
3192 longClickCellInfo.cellX,
3193 longClickCellInfo.cellY));
3194 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003196 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 }
3198 }
3199 }
3200 return true;
3201 }
3202
Winson Chung3d503fb2011-07-13 17:25:49 -07003203 boolean isHotseatLayout(View layout) {
3204 return mHotseat != null && layout != null &&
3205 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3206 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003207
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 /**
3209 * Returns the CellLayout of the specified container at the specified screen.
3210 */
Sunny Goyal92820592015-03-02 11:31:35 -08003211 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003212 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3213 if (mHotseat != null) {
3214 return mHotseat.getLayout();
3215 } else {
3216 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003217 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003218 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003219 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003220 }
3221 }
3222
Winson Chungb745afb2015-03-02 11:51:23 -08003223 /**
3224 * For overridden classes.
3225 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003226 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003227 return isAppsViewVisible();
3228 }
3229
3230 public boolean isAppsViewVisible() {
3231 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3232 }
3233
3234 public boolean isWidgetsViewVisible() {
3235 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003236 }
3237
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003238 private void setWorkspaceBackground(int background) {
3239 switch (background) {
3240 case WORKSPACE_BACKGROUND_TRANSPARENT:
3241 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3242 break;
3243 case WORKSPACE_BACKGROUND_BLACK:
3244 getWindow().setBackgroundDrawable(null);
3245 break;
3246 default:
3247 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3248 }
Craig Mautner360310b2012-10-26 15:13:08 -07003249 }
3250
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003251 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003252 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3253 int curflags = getWindow().getAttributes().flags
3254 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3255 if (wpflags != curflags) {
3256 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3257 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003258 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003259 }
3260
Michael Jurkae326f182011-11-21 14:05:46 -08003261 @Override
3262 public void onTrimMemory(int level) {
3263 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003264 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3265 // The widget preview db can result in holding onto over
3266 // 3MB of memory for caching which isn't necessary.
3267 SQLiteDatabase.releaseMemory();
3268
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003269 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003270 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003271 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003272 if (mLauncherCallbacks != null) {
3273 mLauncherCallbacks.onTrimMemory(level);
3274 }
Michael Jurkae326f182011-11-21 14:05:46 -08003275 }
3276
Winson Chungb745afb2015-03-02 11:51:23 -08003277 @Override
3278 public void onStateTransitionHideSearchBar() {
3279 // Hide the search bar
3280 if (mSearchDropTargetBar != null) {
3281 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3282 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003283 }
3284
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003285 public void showWorkspace(boolean animated) {
Winson Chungef7f8742015-06-04 17:18:17 -07003286 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003287 }
3288
Sunny Goyal83a8f042015-05-19 12:52:12 -07003289 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003290 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
Winson Chungef7f8742015-06-04 17:18:17 -07003291 onCompleteRunnable);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003292 }
3293
3294 protected void showWorkspace(int snapToPage, boolean animated) {
Winson Chungef7f8742015-06-04 17:18:17 -07003295 showWorkspace(snapToPage, animated, null);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003296 }
3297
Winson Chungef7f8742015-06-04 17:18:17 -07003298 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003299 boolean changed = mState != State.WORKSPACE ||
3300 mWorkspace.getState() != Workspace.State.NORMAL;
3301 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003302 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003303 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003304 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003305 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003306
Winson Chungc7d2b602012-05-16 17:02:20 -07003307 // Show the search bar (only animate if we were showing the drop target bar in spring
3308 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003309 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003310 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003311 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003312
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313 // Set focus to the AppsCustomize button
3314 if (mAllAppsButton != null) {
3315 mAllAppsButton.requestFocus();
3316 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003317 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003318
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003319 // Change the state *after* we've called all the transition code
3320 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003321
Winson Chung5fb63472011-02-02 17:03:37 -08003322 // Resume the auto-advance of widgets
3323 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003324 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003325
Winson Chung0f785722015-04-08 10:27:49 -07003326 if (changed) {
3327 // Send an accessibility event to announce the context change
3328 getWindow().getDecorView()
3329 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003330 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003331 }
3332
Adam Cohened307df2013-10-02 09:37:31 -07003333 void showOverviewMode(boolean animated) {
3334 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003335 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003336 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3337 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003338 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003339 }
3340
Winson Chungb745afb2015-03-02 11:51:23 -08003341 /**
3342 * Shows the apps view.
3343 */
Winson Chung4ac30062015-05-08 17:34:17 -07003344 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003345 if (resetListToTop) {
3346 mAppsView.scrollToTop();
3347 }
Winson Chung4ac30062015-05-08 17:34:17 -07003348 if (updatePredictedApps) {
3349 tryAndUpdatePredictedApps();
3350 }
Winson Chungb745afb2015-03-02 11:51:23 -08003351 showAppsOrWidgets(animated, State.APPS);
3352 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003353
Winson Chungb745afb2015-03-02 11:51:23 -08003354 /**
3355 * Shows the widgets view.
3356 */
3357 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003358 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003359 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003360 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003361 }
Winson Chungb745afb2015-03-02 11:51:23 -08003362 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003363
3364 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003365 @Override
3366 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003367 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003368 }
3369 });
Winson Chungb745afb2015-03-02 11:51:23 -08003370 }
3371
3372 /**
3373 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003374 *
3375 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003376 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003377 // TODO: calling method should use the return value so that when {@code false} is returned
3378 // the workspace transition doesn't fall into invalid state.
3379 private boolean showAppsOrWidgets(boolean animated, State toState) {
3380 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3381 mState != State.WIDGETS_SPRING_LOADED) {
3382 return false;
3383 }
3384 if (toState != State.APPS && toState != State.WIDGETS) {
3385 return false;
3386 }
Winson Chungb745afb2015-03-02 11:51:23 -08003387
3388 if (toState == State.APPS) {
Winson Chungef7f8742015-06-04 17:18:17 -07003389 mStateTransitionAnimation.startAnimationToAllApps(mState, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003390 } else {
3391 mStateTransitionAnimation.startAnimationToWidgets(animated);
3392 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003393
Michael Jurkab3e22d92011-10-31 15:58:33 -07003394 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003395 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003396
3397 // Pause the auto-advance of widgets until we are out of AllApps
3398 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003399 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003400 closeFolder();
3401
3402 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003403 getWindow().getDecorView()
3404 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003405 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003406 }
3407
Winson Chungcd99cd32015-04-29 11:03:24 -07003408 /**
3409 * Updates the workspace and interaction state on state change, and return the animation to this
3410 * new state.
3411 */
3412 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
Winson Chungef7f8742015-06-04 17:18:17 -07003413 boolean animated, boolean hasOverlaySearchBar, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003414 Workspace.State fromState = mWorkspace.getState();
Winson Chungef7f8742015-06-04 17:18:17 -07003415 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated,
3416 hasOverlaySearchBar, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003417 updateInteraction(fromState, toState);
3418 return anim;
3419 }
3420
Hyunyoung Song3f471442015-04-08 19:01:34 -07003421 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003422 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003423 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3424 mState == State.WIDGETS_SPRING_LOADED) {
3425 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003426 }
Winson Chungb745afb2015-03-02 11:51:23 -08003427
3428 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003429 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3430 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003431 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003432 }
Adam Cohen7777d962011-08-18 18:58:38 -07003433
Hyunyoung Song3f471442015-04-08 19:01:34 -07003434 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003435 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003436 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003437
Winson Chunge7a03942011-08-05 15:05:12 -07003438 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003439 @Override
3440 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003441 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003442 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3443 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003444 // Before we show workspace, hide all apps again because
3445 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3446 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003447 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003448 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003449 } else {
3450 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003451 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003452 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003453 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003454 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003455
Michael Jurkad3ef3062010-11-23 16:23:58 -08003456 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003457 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003458 showAppsView(true /* animated */, false /* resetListToTop */,
3459 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003460 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003461 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003462 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003463 }
3464
Winson Chung4ac30062015-05-08 17:34:17 -07003465 /**
3466 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3467 * resumed.
3468 */
3469 private void tryAndUpdatePredictedApps() {
3470 if (mLauncherCallbacks != null) {
3471 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3472 if (!apps.isEmpty()) {
3473 mAppsView.setPredictedApps(apps);
3474 }
3475 }
3476 }
3477
Michael Jurkab3e22d92011-10-31 15:58:33 -07003478 void lockAllApps() {
3479 // TODO
3480 }
3481
3482 void unlockAllApps() {
3483 // TODO
3484 }
3485
Sunny Goyal594d76d2014-11-06 10:12:54 -08003486 protected void disableVoiceButtonProxy(boolean disable) {
3487 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003488 }
3489
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003490 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003491 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3492 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003493 }
3494
Adam Cohen24ce0b32014-01-14 16:18:14 -08003495 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003496 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3497 if (searchProvider == null) {
3498 return null;
3499 }
3500
3501 Bundle opts = new Bundle();
3502 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003503 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003504
3505 SharedPreferences sp = getSharedPreferences(
3506 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3507 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003508 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003509 if (!searchProvider.provider.flattenToString().equals(
3510 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003511 || (widgetInfo == null)
3512 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003513 // A valid widget is not already bound.
3514 if (widgetId > -1) {
3515 mAppWidgetHost.deleteAppWidgetId(widgetId);
3516 widgetId = -1;
3517 }
3518
3519 // Try to bind a new widget
3520 widgetId = mAppWidgetHost.allocateAppWidgetId();
3521
3522 if (!AppWidgetManagerCompat.getInstance(this)
3523 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3524 mAppWidgetHost.deleteAppWidgetId(widgetId);
3525 widgetId = -1;
3526 }
3527
3528 sp.edit()
3529 .putInt(QSB_WIDGET_ID, widgetId)
3530 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3531 .commit();
3532 }
3533
3534 if (widgetId != -1) {
3535 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3536 mQsb.updateAppWidgetOptions(opts);
3537 mQsb.setPadding(0, 0, 0, 0);
3538 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003539 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003540 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003541 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003542 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003543 }
3544
Sunny Goyal22235bc2015-04-03 09:23:43 -07003545 private void reinflateQSBIfNecessary() {
3546 if (mQsb instanceof LauncherAppWidgetHostView &&
3547 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3548 mSearchDropTargetBar.removeView(mQsb);
3549 mQsb = null;
3550 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3551 }
3552 }
3553
Michael Jurkad7c28052012-04-27 15:43:36 -07003554 @Override
3555 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003556 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003557 final List<CharSequence> text = event.getText();
3558 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003559 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003560 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003561 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003562 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003563 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003564 } else {
3565 text.add(getString(R.string.all_apps_home_button_label));
3566 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003567 return result;
3568 }
3569
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003570 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003571 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003572 */
Adam Cohen091440a2015-03-18 14:16:05 -07003573 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003574 @Override
3575 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003576 closeSystemDialogs();
3577 }
3578 }
3579
3580 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003581 * Receives notifications whenever the appwidgets are reset.
3582 */
3583 private class AppWidgetResetObserver extends ContentObserver {
3584 public AppWidgetResetObserver() {
3585 super(new Handler());
3586 }
3587
3588 @Override
3589 public void onChange(boolean selfChange) {
3590 onAppWidgetReset();
3591 }
3592 }
3593
3594 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003595 * If the activity is currently paused, signal that we need to run the passed Runnable
3596 * in onResume.
3597 *
3598 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003599 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3600 * wrong when we're not running, and if the activity comes back to what the configuration was
3601 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003602 *
3603 * Implementation of the method from LauncherModel.Callbacks.
3604 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003605 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003606 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003607 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003608 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003609 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003610 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003611 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003612 }
3613 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003614 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003615 return true;
3616 } else {
3617 return false;
3618 }
3619 }
3620
Michael Jurkac402cd92013-05-20 15:49:32 +02003621 private boolean waitUntilResume(Runnable run) {
3622 return waitUntilResume(run, false);
3623 }
3624
Michael Jurka1e2f4652013-07-08 18:03:46 -07003625 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003626 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003627 }
3628
Michael Jurka7607c2f2013-04-03 14:33:19 -07003629 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003630 * If the activity is currently paused, signal that we need to re-run the loader
3631 * in onResume.
3632 *
3633 * This needs to be called from incoming places where resources might have been loaded
3634 * while we are paused. That is becaues the Configuration might be wrong
3635 * when we're not running, and if it comes back to what it was when we
3636 * were paused, we are not restarted.
3637 *
3638 * Implementation of the method from LauncherModel.Callbacks.
3639 *
3640 * @return true if we are currently paused. The caller might be able to
3641 * skip some work in that case since we will come back again.
3642 */
3643 public boolean setLoadOnResume() {
3644 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003645 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003646 mOnResumeNeedsLoad = true;
3647 return true;
3648 } else {
3649 return false;
3650 }
3651 }
3652
3653 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003655 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003657 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003658 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003659 } else {
3660 return SCREEN_COUNT / 2;
3661 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003662 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003663
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 /**
3665 * Refreshes the shortcuts shown on the workspace.
3666 *
3667 * Implementation of the method from LauncherModel.Callbacks.
3668 */
3669 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003670 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003671
Michael Jurka7607c2f2013-04-03 14:33:19 -07003672 // If we're starting binding all over again, clear any bind calls we'd postponed in
3673 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3674 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003675 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003676
Winson Chungd64d1762013-08-20 14:37:16 -07003677 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003678 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003679 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003680
Michael Jurka05bf644e2011-11-30 20:28:53 -08003681 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003682 if (mHotseat != null) {
3683 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003684 }
3685 }
3686
Adam Cohendcd297f2013-06-18 13:13:40 -07003687 @Override
3688 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003689 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003690
Adam Cohen5084cba2013-09-03 12:01:16 -07003691 // If there are no screens, we need to have an empty screen
3692 if (orderedScreenIds.size() == 0) {
3693 mWorkspace.addExtraEmptyScreen();
3694 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003695
3696 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003697 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003698 if (hasCustomContentToLeft()) {
3699 mWorkspace.createCustomContentContainer();
3700 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003701 }
Winson Chung64359a52013-07-08 17:17:08 -07003702 }
3703
3704 @Override
3705 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003706 int count = orderedScreenIds.size();
3707 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003708 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003709 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003710 }
3711
Adam Cohen39a06042013-07-19 14:30:12 -07003712 private boolean shouldShowWeightWatcher() {
3713 String spKey = LauncherAppState.getSharedPreferencesKey();
3714 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003715 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003716
3717 return show;
3718 }
3719
3720 private void toggleShowWeightWatcher() {
3721 String spKey = LauncherAppState.getSharedPreferencesKey();
3722 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3723 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3724
3725 show = !show;
3726
3727 SharedPreferences.Editor editor = sp.edit();
3728 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3729 editor.commit();
3730
3731 if (mWeightWatcher != null) {
3732 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3733 }
3734 }
3735
Winson Chungd64d1762013-08-20 14:37:16 -07003736 public void bindAppsAdded(final ArrayList<Long> newScreens,
3737 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003738 final ArrayList<ItemInfo> addAnimated,
3739 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003740 Runnable r = new Runnable() {
3741 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003742 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003743 }
3744 };
3745 if (waitUntilResume(r)) {
3746 return;
3747 }
3748
Winson Chungd64d1762013-08-20 14:37:16 -07003749 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003750 if (newScreens != null) {
3751 bindAddScreens(newScreens);
3752 }
Winson Chungd64d1762013-08-20 14:37:16 -07003753
3754 // We add the items without animation on non-visible pages, and with
3755 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003756 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003757 bindItems(addNotAnimated, 0,
3758 addNotAnimated.size(), false);
3759 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003760 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003761 bindItems(addAnimated, 0,
3762 addAnimated.size(), true);
3763 }
Winson Chungc58497e2013-09-03 17:48:37 -07003764
Winson Chung87412982013-10-03 18:34:14 -07003765 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003766 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003767
Winson Chungb745afb2015-03-02 11:51:23 -08003768 if (addedApps != null && mAppsView != null) {
3769 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003770 }
Winson Chungd64d1762013-08-20 14:37:16 -07003771 }
3772
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003773 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003774 * Bind the items start-end from the list.
3775 *
3776 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003777 */
Winson Chung64359a52013-07-08 17:17:08 -07003778 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3779 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003780 Runnable r = new Runnable() {
3781 public void run() {
3782 bindItems(shortcuts, start, end, forceAnimateIcons);
3783 }
3784 };
3785 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003786 return;
3787 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003788
Winson Chungf0c6ae02012-03-21 16:10:31 -07003789 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003790 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3791 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003792 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003793 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003794 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003795 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003797
3798 // Short circuit if we are loading dock items for a configuration which has no dock
3799 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3800 mHotseat == null) {
3801 continue;
3802 }
3803
Joe Onorato9c1289c2009-08-17 11:03:03 -04003804 switch (item.itemType) {
3805 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3806 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003807 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003808 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003809
Winson Chung64359a52013-07-08 17:17:08 -07003810 /*
3811 * TODO: FIX collision case
3812 */
Winson Chungce376632013-07-11 16:12:41 -07003813 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3814 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3815 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003816 View v = cl.getChildAt(item.cellX, item.cellY);
3817 Object tag = v.getTag();
3818 String desc = "Collision while binding workspace item: " + item
3819 + ". Collides with " + tag;
3820 if (LauncherAppState.isDogfoodBuild()) {
3821 throw (new RuntimeException(desc));
3822 } else {
3823 Log.d(TAG, desc);
3824 }
Winson Chungce376632013-07-11 16:12:41 -07003825 }
Winson Chung64359a52013-07-08 17:17:08 -07003826 }
3827
Adam Cohendcd297f2013-06-18 13:13:40 -07003828 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3829 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003830 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003831 // Animate all the applications up now
3832 shortcut.setAlpha(0f);
3833 shortcut.setScaleX(0f);
3834 shortcut.setScaleY(0f);
3835 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003836 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003838 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003839 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003840 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003841 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003842 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003843 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3844 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003845 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003846 default:
3847 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003849 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003850
Winson Chung997a9232013-07-24 15:33:46 -07003851 if (animateIcons) {
3852 // Animate to the correct page
3853 if (newShortcutsScreenId > -1) {
3854 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003855 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003856 final Runnable startBounceAnimRunnable = new Runnable() {
3857 public void run() {
3858 anim.playTogether(bounceAnims);
3859 anim.start();
3860 }
3861 };
Winson Chung997a9232013-07-24 15:33:46 -07003862 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003863 // We post the animation slightly delayed to prevent slowdowns
3864 // when we are loading right after we return to launcher.
3865 mWorkspace.postDelayed(new Runnable() {
3866 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003867 if (mWorkspace != null) {
3868 mWorkspace.snapToPage(newScreenIndex);
3869 mWorkspace.postDelayed(startBounceAnimRunnable,
3870 NEW_APPS_ANIMATION_DELAY);
3871 }
Winson Chung94d67682013-09-25 16:29:40 -07003872 }
3873 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003874 } else {
3875 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003876 }
3877 }
Winson Chung64359a52013-07-08 17:17:08 -07003878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003880 }
3881
Joe Onorato9c1289c2009-08-17 11:03:03 -04003882 /**
3883 * Implementation of the method from LauncherModel.Callbacks.
3884 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003885 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003886 Runnable r = new Runnable() {
3887 public void run() {
3888 bindFolders(folders);
3889 }
3890 };
3891 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003892 return;
3893 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003894 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896
3897 /**
3898 * Add the views for a widget to the workspace.
3899 *
3900 * Implementation of the method from LauncherModel.Callbacks.
3901 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003902 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003903 Runnable r = new Runnable() {
3904 public void run() {
3905 bindAppWidget(item);
3906 }
3907 };
3908 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003909 return;
3910 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003911
Daniel Sandler843e8602010-06-07 14:59:01 -04003912 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3913 if (DEBUG_WIDGETS) {
3914 Log.d(TAG, "bindAppWidget: " + item);
3915 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003916 final Workspace workspace = mWorkspace;
3917
Adam Cohen59400422014-03-05 18:07:04 -08003918 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003919 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003920
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003921 if (!mIsSafeModeEnabled
3922 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3923 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003924 if (appWidgetInfo == null) {
3925 if (DEBUG_WIDGETS) {
3926 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3927 + " belongs to component " + item.providerName
3928 + ", as the povider is null");
3929 }
3930 LauncherModel.deleteItemFromDatabase(this, item);
3931 return;
3932 }
3933 // Note: This assumes that the id remap broadcast is received before this step.
3934 // If that is not the case, the id remap will be ignored and user may see the
3935 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07003936 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003937 pendingInfo.spanX = item.spanX;
3938 pendingInfo.spanY = item.spanY;
3939 pendingInfo.minSpanX = item.minSpanX;
3940 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003941 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003942 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003943
Sunny Goyalff572272014-07-23 13:58:07 -07003944 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003945 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3946 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003947
3948 // TODO consider showing a permission dialog when the widget is clicked.
3949 if (!success) {
3950 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3951 if (DEBUG_WIDGETS) {
3952 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3953 + " belongs to component " + item.providerName
3954 + ", as the launcher is unable to bing a new widget id");
3955 }
3956 LauncherModel.deleteItemFromDatabase(this, item);
3957 return;
3958 }
3959
3960 item.appWidgetId = newWidgetId;
3961
3962 // If the widget has a configure activity, it is still needs to set it up, otherwise
3963 // the widget is ready to go.
3964 item.restoreStatus = (appWidgetInfo.configure == null)
3965 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3966 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3967
3968 LauncherModel.updateItemInDatabase(this, item);
3969 }
3970
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003971 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003972 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003973 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003974 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3975 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003976 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003977
Sunny Goyal651077b2014-06-30 14:15:31 -07003978 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3979 } else {
3980 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003981 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3982 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003983 view.updateIcon(mIconCache);
3984 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003985 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003986 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003988
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003990 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003991
Adam Cohendcd297f2013-06-18 13:13:40 -07003992 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003994 if (!item.isCustomWidget()) {
3995 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3996 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003997
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 workspace.requestLayout();
3999
Daniel Sandler843e8602010-06-07 14:59:01 -04004000 if (DEBUG_WIDGETS) {
4001 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4002 + (SystemClock.uptimeMillis()-start) + "ms");
4003 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 }
4005
Sunny Goyalff572272014-07-23 13:58:07 -07004006 /**
4007 * Restores a pending widget.
4008 *
4009 * @param appWidgetId The app widget id
4010 * @param cellInfo The position on screen where to create the widget.
4011 */
4012 private void completeRestoreAppWidget(final int appWidgetId) {
4013 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4014 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4015 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4016 return;
4017 }
4018
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004019 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004020 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4021
4022 mWorkspace.reinflateWidgetsIfNecessary();
4023 LauncherModel.updateItemInDatabase(this, info);
4024 }
4025
Adam Cohen1462de32012-07-24 22:34:36 -07004026 public void onPageBoundSynchronously(int page) {
4027 mSynchronouslyBoundPages.add(page);
4028 }
4029
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030 /**
4031 * Callback saying that there aren't any more items to bind.
4032 *
4033 * Implementation of the method from LauncherModel.Callbacks.
4034 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004035 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004036 Runnable r = new Runnable() {
4037 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004038 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004039 }
4040 };
4041 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004042 return;
4043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004044 if (mSavedState != null) {
4045 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004046 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004047 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004048 mSavedState = null;
4049 }
4050
Adam Cohen1462de32012-07-24 22:34:36 -07004051 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004053 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004054 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004055
4056 // If we received the result of any pending adds while the loader was running (e.g. the
4057 // widget configuration forced an orientation change), process them now.
4058 if (sPendingAddItem != null) {
4059 final long screenId = completeAdd(sPendingAddItem);
4060
4061 // TODO: this moves the user to the page where the pending item was added. Ideally,
4062 // the screen would be guaranteed to exist after bind, and the page would be set through
4063 // the workspace restore process.
4064 mWorkspace.post(new Runnable() {
4065 @Override
4066 public void run() {
4067 mWorkspace.snapToScreenId(screenId);
4068 }
4069 });
4070 sPendingAddItem = null;
4071 }
4072
Sunny Goyal756adbc2015-04-16 15:20:51 -07004073 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004074
4075 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004076 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004077 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004078 }
4079
Adam Cohen3ed316a2014-07-23 18:21:20 -07004080 private void sendLoadingCompleteBroadcastIfNecessary() {
4081 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4082 String permission =
4083 getResources().getString(R.string.receive_first_load_broadcast_permission);
4084 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4085 sendBroadcast(intent, permission);
4086 SharedPreferences.Editor editor = mSharedPrefs.edit();
4087 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4088 editor.apply();
4089 }
4090 }
4091
Winson Chunga0b7e862013-09-05 16:03:15 -07004092 public boolean isAllAppsButtonRank(int rank) {
4093 if (mHotseat != null) {
4094 return mHotseat.isAllAppsButtonRank(rank);
4095 }
4096 return false;
4097 }
4098
Winson Chunga2413752012-04-03 14:22:34 -07004099 private boolean canRunNewAppsAnimation() {
4100 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4101 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4102 }
4103
Winson Chungc9168342013-06-26 14:54:55 -07004104 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4105 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4106 PropertyValuesHolder.ofFloat("alpha", 1f),
4107 PropertyValuesHolder.ofFloat("scaleX", 1f),
4108 PropertyValuesHolder.ofFloat("scaleY", 1f));
4109 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4110 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004111 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004112 return bounceAnim;
4113 }
4114
Adam Cohen27772732013-11-11 14:00:56 +00004115 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004116 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004117 }
4118
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004119 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004120 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004121 }
4122
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004123 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004124 if (mSearchDropTargetBar == null) {
4125 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004126 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004127 if (mQsb != null) {
4128 mSearchDropTargetBar.removeView(mQsb);
4129 mQsb = null;
4130 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004131 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004132 }
4133
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004134 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004135 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4136 * multiple calls to bind the same list.)
4137 */
4138 @Thunk ArrayList<AppInfo> mTmpAppsList;
4139 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4140 public void run() {
4141 bindAllApplications(mTmpAppsList);
4142 mTmpAppsList = null;
4143 }
4144 };
4145
4146 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004147 * Add the icons for all apps.
4148 *
4149 * Implementation of the method from LauncherModel.Callbacks.
4150 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004151 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004152 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4153 mTmpAppsList = apps;
4154 return;
4155 }
4156
Winson Chungb745afb2015-03-02 11:51:23 -08004157 if (mAppsView != null) {
4158 mAppsView.setApps(apps);
4159 }
Adam Cohen9211d422014-10-07 18:14:53 -07004160 if (mLauncherCallbacks != null) {
4161 mLauncherCallbacks.bindAllApplications(apps);
4162 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004163 }
4164
4165 /**
4166 * A package was updated.
4167 *
4168 * Implementation of the method from LauncherModel.Callbacks.
4169 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004170 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004171 Runnable r = new Runnable() {
4172 public void run() {
4173 bindAppsUpdated(apps);
4174 }
4175 };
4176 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004177 return;
4178 }
4179
Winson Chungb745afb2015-03-02 11:51:23 -08004180 if (mAppsView != null) {
4181 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004182 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004183 }
4184
Sunny Goyal4390ace2014-10-13 11:33:11 -07004185 @Override
4186 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4187 Runnable r = new Runnable() {
4188 public void run() {
4189 bindWidgetsRestored(widgets);
4190 }
4191 };
4192 if (waitUntilResume(r)) {
4193 return;
4194 }
4195 mWorkspace.widgetsRestored(widgets);
4196 }
4197
Joe Onorato9c1289c2009-08-17 11:03:03 -04004198 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004199 * Some shortcuts were updated in the background.
4200 *
4201 * Implementation of the method from LauncherModel.Callbacks.
4202 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004203 @Override
4204 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4205 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004206 Runnable r = new Runnable() {
4207 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004208 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004209 }
4210 };
4211 if (waitUntilResume(r)) {
4212 return;
4213 }
4214
Sunny Goyal4390ace2014-10-13 11:33:11 -07004215 if (!updated.isEmpty()) {
4216 mWorkspace.updateShortcuts(updated);
4217 }
4218
4219 if (!removed.isEmpty()) {
4220 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4221 for (ShortcutInfo si : removed) {
4222 removedComponents.add(si.getTargetComponent());
4223 }
4224 mWorkspace.removeItemsByComponentName(removedComponents, user);
4225 // Notify the drag controller
4226 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004227 }
4228 }
4229
4230 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004231 * Update the state of a package, typically related to install state.
4232 *
4233 * Implementation of the method from LauncherModel.Callbacks.
4234 */
Sunny Goyale755d462014-07-22 13:48:29 -07004235 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004236 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4237 Runnable r = new Runnable() {
4238 public void run() {
4239 bindRestoreItemsChange(updates);
4240 }
4241 };
4242 if (waitUntilResume(r)) {
4243 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004244 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004245
Sunny Goyal756adbc2015-04-16 15:20:51 -07004246 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004247 }
4248
4249 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004250 * A package was uninstalled. We take both the super set of packageNames
4251 * in addition to specific applications to remove, the reason being that
4252 * this can be called when a package is updated as well. In that scenario,
4253 * we only remove specific components from the workspace, where as
4254 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004255 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004256 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004257 * Implementation of the method from LauncherModel.Callbacks.
4258 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004259 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004260 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004261 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004262 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004263 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004264 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004265 }
Winson Chungd64d1762013-08-20 14:37:16 -07004266 };
4267 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004268 return;
4269 }
4270
Sunny Goyal1a745e82014-10-02 15:58:31 -07004271 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004272 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4273 for (AppInfo info : appInfos) {
4274 removedComponents.add(info.componentName);
4275 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004276 if (!packageNames.isEmpty()) {
4277 mWorkspace.removeItemsByPackageName(packageNames, user);
4278 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004279 if (!removedComponents.isEmpty()) {
4280 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004281 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004282 // Notify the drag controller
4283 mDragController.onAppsRemoved(packageNames, removedComponents);
4284
Sunny Goyal1a745e82014-10-02 15:58:31 -07004285 } else {
4286 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004287 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004288
Winson Chungdf95eb12013-10-16 14:57:07 -07004289 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004290 if (mAppsView != null) {
4291 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004292 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004293 }
Joe Onoratobe386092009-11-17 17:32:16 -08004294
Michael Jurkac402cd92013-05-20 15:49:32 +02004295 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004296 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004297 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004298 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004299 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004300
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004301 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004302 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004303 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004304 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004305 return;
4306 }
4307
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004308 if (mWidgetsView != null && model != null) {
4309 mWidgetsView.addWidgets(model);
4310 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004311 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004312 }
4313
Winson Chung400438b2011-01-16 17:53:48 -08004314 private int mapConfigurationOriActivityInfoOri(int configOri) {
4315 final Display d = getWindowManager().getDefaultDisplay();
4316 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4317 switch (d.getRotation()) {
4318 case Surface.ROTATION_0:
4319 case Surface.ROTATION_180:
4320 // We are currently in the same basic orientation as the natural orientation
4321 naturalOri = configOri;
4322 break;
4323 case Surface.ROTATION_90:
4324 case Surface.ROTATION_270:
4325 // We are currently in the other basic orientation to the natural orientation
4326 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4327 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4328 break;
4329 }
4330
4331 int[] oriMap = {
4332 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4333 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4334 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4335 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4336 };
4337 // Since the map starts at portrait, we need to offset if this device's natural orientation
4338 // is landscape.
4339 int indexOffset = 0;
4340 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4341 indexOffset = 1;
4342 }
4343 return oriMap[(d.getRotation() + indexOffset) % 4];
4344 }
Adam Cohen446e9402011-09-15 18:21:21 -07004345
Winson Chung4b919f82012-05-01 10:44:08 -07004346 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004347 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004348 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4349 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4350 .getConfiguration().orientation));
4351 } else {
4352 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4353 }
Winson Chung4b919f82012-05-01 10:44:08 -07004354 }
4355 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004356
Winson Chung4b919f82012-05-01 10:44:08 -07004357 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004358 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004359 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004360 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004361 } else {
4362 mHandler.postDelayed(new Runnable() {
4363 public void run() {
4364 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4365 }
4366 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004367 }
Winson Chung4b919f82012-05-01 10:44:08 -07004368 }
Winson Chung400438b2011-01-16 17:53:48 -08004369 }
4370
Winson Chunge43a1e72014-01-15 10:33:02 -08004371 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004372 if (mLauncherCallbacks != null) {
4373 return mLauncherCallbacks.isLauncherPreinstalled();
4374 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004375 PackageManager pm = getPackageManager();
4376 try {
4377 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4378 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4379 return true;
4380 } else {
4381 return false;
4382 }
4383 } catch (NameNotFoundException e) {
4384 e.printStackTrace();
4385 return false;
4386 }
4387 }
4388
Adam Cohenea90f832014-05-21 15:03:34 -07004389 /**
4390 * This method indicates whether or not we should suggest default wallpaper dimensions
4391 * when our wallpaper cropper was not yet used to set a wallpaper.
4392 */
4393 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004394 if (mLauncherCallbacks != null) {
4395 return mLauncherCallbacks.overrideWallpaperDimensions();
4396 }
Adam Cohenea90f832014-05-21 15:03:34 -07004397 return true;
4398 }
4399
Adam Cohen432609a2014-03-13 17:03:22 -07004400 /**
4401 * To be overridden by subclasses to indicate that there is an activity to launch
4402 * before showing the standard launcher experience.
4403 */
4404 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004405 if (mLauncherCallbacks != null) {
4406 return mLauncherCallbacks.hasFirstRunActivity();
4407 }
Adam Cohen432609a2014-03-13 17:03:22 -07004408 return false;
4409 }
4410
4411 /**
4412 * To be overridden by subclasses to launch any first run activity
4413 */
4414 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004415 if (mLauncherCallbacks != null) {
4416 return mLauncherCallbacks.getFirstRunActivity();
4417 }
Adam Cohen432609a2014-03-13 17:03:22 -07004418 return null;
4419 }
4420
Winson Chunga6945242014-01-08 14:04:34 -08004421 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004422 return !ActivityManager.isRunningInTestHarness() &&
4423 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004424 }
4425
Adam Cohen4a9423d2014-03-28 14:22:31 -07004426 protected boolean hasRunFirstRunActivity() {
4427 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4428 }
4429
Adam Cohen432609a2014-03-13 17:03:22 -07004430 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004431 if (shouldRunFirstRunActivity() &&
4432 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004433 Intent firstRunIntent = getFirstRunActivity();
4434 if (firstRunIntent != null) {
4435 startActivity(firstRunIntent);
4436 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004437 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004438 }
4439 }
Adam Cohen432609a2014-03-13 17:03:22 -07004440 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004441 }
4442
4443 private void markFirstRunActivityShown() {
4444 SharedPreferences.Editor editor = mSharedPrefs.edit();
4445 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4446 editor.apply();
4447 }
4448
Adam Cohen432609a2014-03-13 17:03:22 -07004449 /**
4450 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4451 * screen that must be displayed and dismissed.
4452 */
4453 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004454 if (mLauncherCallbacks != null) {
4455 return mLauncherCallbacks.hasDismissableIntroScreen();
4456 }
Adam Cohen432609a2014-03-13 17:03:22 -07004457 return false;
4458 }
4459
4460 /**
4461 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4462 */
4463 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004464 if (mLauncherCallbacks != null) {
4465 return mLauncherCallbacks.getIntroScreen();
4466 }
Adam Cohen432609a2014-03-13 17:03:22 -07004467 return null;
4468 }
4469
4470 /**
4471 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4472 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4473 */
4474 private boolean shouldShowIntroScreen() {
4475 return hasDismissableIntroScreen() &&
4476 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4477 }
4478
4479 protected void showIntroScreen() {
4480 View introScreen = getIntroScreen();
4481 changeWallpaperVisiblity(false);
4482 if (introScreen != null) {
4483 mDragLayer.showOverlayView(introScreen);
4484 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004485 if (mLauncherOverlayContainer != null) {
4486 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4487 }
Adam Cohen432609a2014-03-13 17:03:22 -07004488 }
4489
4490 public void dismissIntroScreen() {
4491 markIntroScreenDismissed();
4492 if (showFirstRunActivity()) {
4493 // We delay hiding the intro view until the first run activity is showing. This
4494 // avoids a blip.
4495 mWorkspace.postDelayed(new Runnable() {
4496 @Override
4497 public void run() {
4498 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004499 if (mLauncherOverlayContainer != null) {
4500 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4501 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004502 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004503 }
4504 }, ACTIVITY_START_DELAY);
4505 } else {
4506 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004507 if (mLauncherOverlayContainer != null) {
4508 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4509 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004510 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004511 }
4512 changeWallpaperVisiblity(true);
4513 }
4514
4515 private void markIntroScreenDismissed() {
4516 SharedPreferences.Editor editor = mSharedPrefs.edit();
4517 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4518 editor.apply();
4519 }
4520
Adam Cohen091440a2015-03-18 14:16:05 -07004521 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004522 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4523 // on the device, then we always show the first run cling experience (or if there is no
4524 // launcher2). Otherwise, we prompt the user upon started for migration
4525 LauncherClings launcherClings = new LauncherClings(this);
4526 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4527 if (mModel.canMigrateFromOldLauncherDb(this)) {
4528 launcherClings.showMigrationCling();
4529 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004530 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004531 }
4532 }
4533 }
4534
Winson Chunga6945242014-01-08 14:04:34 -08004535 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004536 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4537 if (mHotseat != null) mHotseat.setAlpha(1f);
4538 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4539 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004540 }
4541
4542 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004543 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4544 if (mHotseat != null) mHotseat.setAlpha(0f);
4545 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4546 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004547 }
4548
Mathew Inwood72fbec12013-11-19 15:45:07 +00004549 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004550 // Called from search suggestion, not supported in other profiles.
4551 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4552 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4553 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4554 myUser);
4555 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004556 return null;
4557 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004558 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004559 }
4560
4561 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4562 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004563 // Called from search suggestion, not supported in other profiles.
4564 return createShortcutDragInfo(shortcutIntent, caption, icon,
4565 UserHandleCompat.myUserHandle());
4566 }
4567
4568 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4569 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004570 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004571 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004572 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004573 }
4574
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004575 protected void moveWorkspaceToDefaultScreen() {
4576 mWorkspace.moveToDefaultScreen(false);
4577 }
4578
Mathew Inwood72fbec12013-11-19 15:45:07 +00004579 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4580 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004581 mWorkspace.onExternalDragStartedWithItem(dragView);
4582 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004583 }
4584
Anjali Koppalf5d29132014-02-28 13:33:27 -08004585 @Override
4586 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004587 if (mLauncherCallbacks != null) {
4588 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4589 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004590 }
4591
Winson Chung80baf5a2010-08-09 16:03:15 -07004592 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004593 * Returns a FastBitmapDrawable with the icon, accurately sized.
4594 */
4595 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4596 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4597 d.setFilterBitmap(true);
4598 resizeIconDrawable(d);
4599 return d;
4600 }
4601
4602 /**
4603 * Resizes an icon drawable to the correct icon size.
4604 */
4605 public void resizeIconDrawable(Drawable icon) {
4606 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4607 }
4608
4609 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004610 * Prints out out state for debugging.
4611 */
4612 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004613 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004614 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004615 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4616 Log.d(TAG, "mRestoring=" + mRestoring);
4617 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4618 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004619 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004620 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004621 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004622
Daniel Sandler325dc232013-06-05 22:57:57 -04004623 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004624 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004625
4626 @Override
4627 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4628 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004629 synchronized (sDumpLogs) {
4630 writer.println(" ");
4631 writer.println("Debug logs: ");
4632 for (int i = 0; i < sDumpLogs.size(); i++) {
4633 writer.println(" " + sDumpLogs.get(i));
4634 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004635 }
Adam Cohen9211d422014-10-07 18:14:53 -07004636 if (mLauncherCallbacks != null) {
4637 mLauncherCallbacks.dump(prefix, fd, writer, args);
4638 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004639 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004640
4641 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004642 if (DEBUG_DUMP_LOG) {
4643 synchronized (sDumpLogs) {
4644 Log.d(TAG, "");
4645 Log.d(TAG, "*********************");
4646 Log.d(TAG, "Launcher debug logs: ");
4647 for (int i = 0; i < sDumpLogs.size(); i++) {
4648 Log.d(TAG, " " + sDumpLogs.get(i));
4649 }
4650 Log.d(TAG, "*********************");
4651 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004652 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004653 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004654 }
4655
4656 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004657 addDumpLog(tag, log, null, debugLog);
4658 }
4659
4660 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004661 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004662 if (e != null) {
4663 Log.d(tag, log, e);
4664 } else {
4665 Log.d(tag, log);
4666 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004667 }
Winson Chungede41292013-09-19 16:27:36 -07004668 if (DEBUG_DUMP_LOG) {
4669 sDateStamp.setTime(System.currentTimeMillis());
4670 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004671 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4672 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004673 }
Winson Chungede41292013-09-19 16:27:36 -07004674 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004675 }
4676
Adam Cohen59400422014-03-05 18:07:04 -08004677 public static CustomAppWidget getCustomAppWidget(String name) {
4678 return sCustomAppWidgets.get(name);
4679 }
4680
4681 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4682 return sCustomAppWidgets;
4683 }
4684
Winson Chungede41292013-09-19 16:27:36 -07004685 public void dumpLogsToLocalData() {
4686 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004687 new AsyncTask<Void, Void, Void>() {
4688 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004689 boolean success = false;
4690 sDateStamp.setTime(sRunStart);
4691 String FILENAME = sDateStamp.getMonth() + "-"
4692 + sDateStamp.getDay() + "_"
4693 + sDateStamp.getHours() + "-"
4694 + sDateStamp.getMinutes() + "_"
4695 + sDateStamp.getSeconds() + ".txt";
4696
4697 FileOutputStream fos = null;
4698 File outFile = null;
4699 try {
4700 outFile = new File(getFilesDir(), FILENAME);
4701 outFile.createNewFile();
4702 fos = new FileOutputStream(outFile);
4703 } catch (Exception e) {
4704 e.printStackTrace();
4705 }
4706 if (fos != null) {
4707 PrintWriter writer = new PrintWriter(fos);
4708
4709 writer.println(" ");
4710 writer.println("Debug logs: ");
4711 synchronized (sDumpLogs) {
4712 for (int i = 0; i < sDumpLogs.size(); i++) {
4713 writer.println(" " + sDumpLogs.get(i));
4714 }
4715 }
4716 writer.close();
4717 }
4718 try {
4719 if (fos != null) {
4720 fos.close();
4721 success = true;
4722 }
4723 } catch (IOException e) {
4724 e.printStackTrace();
4725 }
Michael Jurka43467462013-11-14 12:03:58 +01004726 return null;
Winson Chungede41292013-09-19 16:27:36 -07004727 }
Michael Jurka43467462013-11-14 12:03:58 +01004728 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004729 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004731}
Michael Jurka2763be32011-02-24 11:19:57 -08004732
Dan Sandlerd5024042014-01-09 15:01:33 -05004733interface DebugIntents {
4734 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4735 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004736}