blob: ce0f304c181e5c2f7ca2ec53ffdc7183682a3384 [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 Sandler6053b802013-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;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700398 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700399 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400400
401 @Thunk void setOrientation() {
402 if (mRotationEnabled) {
403 unlockScreenOrientation(true);
404 } else {
405 setRequestedOrientation(
406 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700407 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400408 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700409
Sunny Goyal7779d622015-06-11 16:18:39 -0700410 private Runnable mUpdateOrientationRunnable = new Runnable() {
411 public void run() {
412 setOrientation();
413 }
414 };
415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 @Override
417 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700418 if (DEBUG_STRICT_MODE) {
419 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
420 .detectDiskReads()
421 .detectDiskWrites()
422 .detectNetwork() // or .detectAll() for all detectable problems
423 .penaltyLog()
424 .build());
425 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
426 .detectLeakedSqlLiteObjects()
427 .detectLeakedClosableObjects()
428 .penaltyLog()
429 .penaltyDeath()
430 .build());
431 }
432
Adam Cohen9211d422014-10-07 18:14:53 -0700433 if (mLauncherCallbacks != null) {
434 mLauncherCallbacks.preOnCreate();
435 }
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400438
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400439 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400440 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700441 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700442
Adam Cohen2e6da152015-05-06 11:42:25 -0700443 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700444 mDeviceProfile = getResources().getConfiguration().orientation
445 == Configuration.ORIENTATION_LANDSCAPE ?
446 app.getInvariantDeviceProfile().landscapeProfile
447 : app.getInvariantDeviceProfile().portraitProfile;
448
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400449 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700450 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700451 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800452 mModel = app.setLauncher(this);
453 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700454
Joe Onorato41a12d22009-10-31 18:30:00 -0400455 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800457 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700458
Daniel Sandlerff02d492013-08-05 02:12:05 -0400459 mStats = new Stats(this);
460
Sunny Goyalffe83f12014-08-14 17:39:34 -0700461 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700462
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700463 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
464 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700465
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700466 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
467 // this also ensures that any synchronous binding below doesn't re-trigger another
468 // LauncherModel load.
469 mPaused = false;
470
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200472 android.os.Debug.startMethodTracing(
473 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700477
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700479 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800481 registerContentObservers();
482
Joe Onorato7c312c12009-08-13 21:36:53 -0700483 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 mSavedState = savedInstanceState;
486 restoreState(mSavedState);
487
488 if (PROFILE_STARTUP) {
489 android.os.Debug.stopMethodTracing();
490 }
491
492 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700493 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700494 // If the user leaves launcher, then we should just load items asynchronously when
495 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700496 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700497 } else {
498 // We only load the page synchronously if the user rotates (or triggers a
499 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700500 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 }
503
504 // For handling default keys
505 mDefaultKeySsb = new SpannableStringBuilder();
506 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800507
508 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
509 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800510
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700511 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
512 // In case we are on a device with locked rotation, we should look at preferences to check
513 // if the user has specifically allowed rotation.
514 if (!mRotationEnabled) {
Rahul Chaturvedie863fed2015-06-15 14:09:42 -0400515 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext(), false);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700516 }
517
518 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
519 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400520 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700521
Adam Cohen9211d422014-10-07 18:14:53 -0700522 if (mLauncherCallbacks != null) {
523 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700524 if (mLauncherCallbacks.hasLauncherOverlay()) {
525 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700526 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
527 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
528 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700529 mWorkspace.setLauncherOverlay(mLauncherOverlay);
530 }
Adam Cohen9211d422014-10-07 18:14:53 -0700531 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700532
533 if (shouldShowIntroScreen()) {
534 showIntroScreen();
535 } else {
536 showFirstRunActivity();
537 showFirstRunClings();
538 }
Adam Cohen9211d422014-10-07 18:14:53 -0700539 }
540
Sunny Goyal7779d622015-06-11 16:18:39 -0700541 @Override
542 public void onSettingsChanged(String settings, boolean value) {
543 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
544 mRotationEnabled = value;
545 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
546 mUpdateOrientationRunnable.run();
547 }
548 }
549 }
550
Adam Cohen9211d422014-10-07 18:14:53 -0700551 private LauncherCallbacks mLauncherCallbacks;
552
553 public void onPostCreate(Bundle savedInstanceState) {
554 super.onPostCreate(savedInstanceState);
555 if (mLauncherCallbacks != null) {
556 mLauncherCallbacks.onPostCreate(savedInstanceState);
557 }
558 }
559
560 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
561 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700562 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
563 private boolean mImportanceStored = false;
564 private int mWorkspaceImportanceForAccessibility =
565 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
566 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
567
568 @Override
569 public void onSearchOverlayOpened() {
570 if (mImportanceStored) {
571 return;
572 }
573 // The underlying workspace and hotseat are temporarily suppressed by the search
574 // overlay. So they sholudn't be accessible.
575 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
576 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
577 mWorkspace.setImportantForAccessibility(
578 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
579 mHotseat.setImportantForAccessibility(
580 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
581 mImportanceStored = true;
582 }
583
584 @Override
585 public void onSearchOverlayClosed() {
586 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
587 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
588 mImportanceStored = false;
589 }
590 });
Adam Cohen9211d422014-10-07 18:14:53 -0700591 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700592 }
593
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700594 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700595 public void onLauncherProviderChange() {
596 if (mLauncherCallbacks != null) {
597 mLauncherCallbacks.onLauncherProviderChange();
598 }
599 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700600
Winson Chungef7f8742015-06-04 17:18:17 -0700601 /**
602 * Updates the bounds of all the overlays to match the new fixed bounds.
603 */
604 public void updateOverlayBounds(Rect newBounds) {
605 mAppsView.setSearchBarBounds(newBounds);
606 mWidgetsView.setSearchBarBounds(newBounds);
607 }
608
Adam Cohen9211d422014-10-07 18:14:53 -0700609 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700610 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700611 if (mLauncherCallbacks != null) {
612 return mLauncherCallbacks.hasCustomContentToLeft();
613 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700614 return false;
615 }
616
Dave Hawkeya8881582013-09-17 15:55:33 -0600617 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500618 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 * {@link #addToCustomContentPage}. This will only be invoked if
620 * {@link #hasCustomContentToLeft()} is {@code true}.
621 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500622 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700623 if (mLauncherCallbacks != null) {
624 mLauncherCallbacks.populateCustomContentContainer();
625 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600626 }
627
628 /**
629 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
630 * ensure the custom content page is added or removed if necessary.
631 */
632 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600633 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600634 // Not bound yet, wait for bindScreens to be called.
635 return;
636 }
637
638 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
639 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500640 mWorkspace.createCustomContentContainer();
641 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600642 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
643 mWorkspace.removeCustomContentPage();
644 }
645 }
646
Anton Hanssona2f665f2013-09-26 12:18:32 +0100647 public Stats getStats() {
648 return mStats;
649 }
650
Bjorn Bringertc459e522013-06-07 19:36:01 +0100651 public LayoutInflater getInflater() {
652 return mInflater;
653 }
654
Hyunyoung Song3f471442015-04-08 19:01:34 -0700655 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700656 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
657 // that is subsequently removed from the workspace in startBinding().
658 return !mModel.isLoadingWorkspace();
659 }
660
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800661 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000662 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100663 if (Build.VERSION.SDK_INT >= 17) {
664 return View.generateViewId();
665 } else {
666 // View.generateViewId() is not available. The following fallback logic is a copy
667 // of its implementation.
668 for (;;) {
669 final int result = sNextGeneratedId.get();
670 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
671 int newValue = result + 1;
672 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
673 if (sNextGeneratedId.compareAndSet(result, newValue)) {
674 return result;
675 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000676 }
677 }
678 }
679
680 public int getViewIdForItem(ItemInfo info) {
681 // This cast is safe given the > 2B range for int.
682 int itemId = (int) info.id;
683 if (mItemIdToViewId.containsKey(itemId)) {
684 return mItemIdToViewId.get(itemId);
685 }
686 int viewId = generateViewId();
687 mItemIdToViewId.put(itemId, viewId);
688 return viewId;
689 }
690
691 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700692 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
693 * a configuration step, this allows the proper animations to run after other transitions.
694 */
Adam Cohendb364c32014-05-20 14:23:40 -0700695 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700696 long screenId = args.screenId;
697 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
698 // When the screen id represents an actual screen (as opposed to a rank) we make sure
699 // that the drop page actually exists.
700 screenId = ensurePendingDropLayoutExists(args.screenId);
701 }
Adam Cohendb364c32014-05-20 14:23:40 -0700702
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800704 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700705 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700706 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700707 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800708 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700709 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700710 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700711 case REQUEST_RECONFIGURE_APPWIDGET:
712 completeRestoreAppWidget(args.appWidgetId);
713 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 }
Michael Jurka27614d22012-04-02 06:40:22 -0700715 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
716 // if you turned the screen off and then back while in All Apps, Launcher would not
717 // return to the workspace. Clearing mAddInfo.container here fixes this issue
718 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700719 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800720 }
721
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800722 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700724 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700725 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700726 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800727 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700728
Adam Cohenad4e15c2013-10-17 16:21:35 -0700729 Runnable exitSpringLoaded = new Runnable() {
730 @Override
731 public void run() {
732 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
733 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
734 }
735 };
736
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
740 if (resultCode == RESULT_CANCELED) {
741 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700742 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700743 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800745 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700747 }
748 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200749 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
750 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700751 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200752 }
753 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200755
Adam Cohened66b2b2012-01-23 17:28:51 -0800756 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700757 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700758
Adam Cohendb364c32014-05-20 14:23:40 -0700759 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 // We have special handling for widgets
761 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800762 final int appWidgetId;
763 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
764 : -1;
765 if (widgetId < 0) {
766 appWidgetId = pendingAddWidgetId;
767 } else {
768 appWidgetId = widgetId;
769 }
770
Adam Cohenad4e15c2013-10-17 16:21:35 -0700771 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800772 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700773 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
774 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 result = RESULT_CANCELED;
776 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700777 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 @Override
779 public void run() {
780 exitSpringLoadedDragModeDelayed(false, 0, null);
781 }
782 };
Adam Cohendb364c32014-05-20 14:23:40 -0700783 if (workspaceLocked) {
784 // No need to remove the empty screen if we're mid-binding, as the
785 // the bind will not add the empty screen.
786 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
787 } else {
788 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
789 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
790 }
Winson Chung5aaab772012-04-27 15:24:02 -0700791 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700792 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700793 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
794 // When the screen id represents an actual screen (as opposed to a rank)
795 // we make sure that the drop page actually exists.
796 mPendingAddInfo.screenId =
797 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
798 }
Adam Cohendb364c32014-05-20 14:23:40 -0700799 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
800
801 dropLayout.setDropPending(true);
802 final Runnable onComplete = new Runnable() {
803 @Override
804 public void run() {
805 completeTwoStageWidgetDrop(resultCode, appWidgetId);
806 dropLayout.setDropPending(false);
807 }
808 };
809 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
810 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
811 } else {
812 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
813 mPendingAddInfo);
814 sPendingAddItem = args;
815 }
Winson Chung5aaab772012-04-27 15:24:02 -0700816 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800817 return;
818 }
819
Sunny Goyalff572272014-07-23 13:58:07 -0700820 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
821 if (resultCode == RESULT_OK) {
822 // Update the widget view.
823 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
824 pendingAddWidgetId, mPendingAddInfo);
825 if (workspaceLocked) {
826 sPendingAddItem = args;
827 } else {
828 completeAdd(args);
829 }
830 }
831 // Leave the widget in the pending state if the user canceled the configure.
832 return;
833 }
834
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 // The pattern used here is that a user PICKs a specific application,
836 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
839 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700840 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700841 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
842 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800843 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700844 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800845 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700846 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700847 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
848 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
Adam Cohen00074722013-10-11 17:46:09 -0700850 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700851 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700852 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800854 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800855
856 }
857
858 @Override
859 protected void onActivityResult(
860 final int requestCode, final int resultCode, final Intent data) {
861 handleActivityResult(requestCode, resultCode, data);
862 if (mLauncherCallbacks != null) {
863 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 }
866
Adam Cohendb364c32014-05-20 14:23:40 -0700867 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
868 appWidgetId, ItemInfo info) {
869 PendingAddArguments args = new PendingAddArguments();
870 args.requestCode = requestCode;
871 args.intent = data;
872 args.container = info.container;
873 args.screenId = info.screenId;
874 args.cellX = info.cellX;
875 args.cellY = info.cellY;
876 args.appWidgetId = appWidgetId;
877 return args;
878 }
879
880 /**
881 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
882 *
883 * @param screenId the screen id to check
884 * @return the new screen, or screenId if it exists
885 */
886 private long ensurePendingDropLayoutExists(long screenId) {
887 CellLayout dropLayout =
888 (CellLayout) mWorkspace.getScreenWithId(screenId);
889 if (dropLayout == null) {
890 // it's possible that the add screen was removed because it was
891 // empty and a re-bind occurred
892 mWorkspace.addExtraEmptyScreen();
893 return mWorkspace.commitExtraEmptyScreen();
894 } else {
895 return screenId;
896 }
897 }
898
Adam Cohen091440a2015-03-18 14:16:05 -0700899 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700900 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700901 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800902 Runnable onCompleteRunnable = null;
903 int animationType = 0;
904
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700905 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800906 if (resultCode == RESULT_OK) {
907 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
908 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700909 mPendingAddWidgetInfo);
910 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800911 onCompleteRunnable = new Runnable() {
912 @Override
913 public void run() {
914 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700915 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700916 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
917 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 }
919 };
920 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800921 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700924 if (mDragLayer.getAnimatedView() != null) {
925 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
926 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
927 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700928 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700929 // The animated view may be null in the case of a rotation during widget configuration
930 onCompleteRunnable.run();
931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
933
934 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700935 protected void onStop() {
936 super.onStop();
937 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700938
939 if (mLauncherCallbacks != null) {
940 mLauncherCallbacks.onStop();
941 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700942 }
943
944 @Override
945 protected void onStart() {
946 super.onStart();
947 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700948
949 if (mLauncherCallbacks != null) {
950 mLauncherCallbacks.onStart();
951 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700952 }
953
954 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200956 long startTime = 0;
957 if (DEBUG_RESUME_TIME) {
958 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400959 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200960 }
Adam Cohen9211d422014-10-07 18:14:53 -0700961
962 if (mLauncherCallbacks != null) {
963 mLauncherCallbacks.preOnResume();
964 }
965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700967
Winson Chung4a2afa32012-07-19 14:53:05 -0700968 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700969 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700970 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800971 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700972 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700973 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700974 // view after launching an app, as they may be depending on the UI to be static to
975 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700976 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -0700977 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800978 } else if (mOnResumeState == State.WIDGETS) {
979 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700980 }
981 mOnResumeState = State.NONE;
982
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700983 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700984 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
985 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700986
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800987 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700988 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700989 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700990 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700992 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700994 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200995 // We might have postponed some bind calls until onResume (see waitUntilResume) --
996 // execute them here
997 long startTimeCallbacks = 0;
998 if (DEBUG_RESUME_TIME) {
999 startTimeCallbacks = System.currentTimeMillis();
1000 }
1001
Michael Jurka1e2f4652013-07-08 18:03:46 -07001002 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1003 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001004 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001005 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001006 if (DEBUG_RESUME_TIME) {
1007 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1008 (System.currentTimeMillis() - startTimeCallbacks));
1009 }
Michael Jurka447bf842013-05-15 14:52:15 +02001010 }
Michael Jurka54554252013-08-01 12:52:23 +02001011 if (mOnResumeCallbacks.size() > 0) {
1012 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1013 mOnResumeCallbacks.get(i).run();
1014 }
1015 mOnResumeCallbacks.clear();
1016 }
Winson Chunge4e50662012-01-23 14:45:13 -08001017
1018 // Reset the pressed state of icons that were locked in the press state while activities
1019 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001020 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001021 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001022 mWaitingForResume.setStayPressed(false);
1023 }
Winson Chung82963d52013-10-09 11:20:57 -07001024
Adam Cohen06dff352012-06-01 17:17:08 -07001025 // It is possible that widgets can receive updates while launcher is not in the foreground.
1026 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1027 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1028 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001029 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001030 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001031
Michael Jurka447bf842013-05-15 14:52:15 +02001032 if (DEBUG_RESUME_TIME) {
1033 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1034 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001035
1036 if (mWorkspace.getCustomContentCallbacks() != null) {
1037 // If we are resuming and the custom content is the current page, we call onShow().
1038 // It is also poassible that onShow will instead be called slightly after first layout
1039 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1040 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001041 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001042 }
1043 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001044 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001045 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001046
Sunny Goyal756adbc2015-04-16 15:20:51 -07001047 if (!isWorkspaceLoading()) {
1048 // Process any items that were added while Launcher was away.
1049 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1050 }
Adam Cohen9211d422014-10-07 18:14:53 -07001051
1052 if (mLauncherCallbacks != null) {
1053 mLauncherCallbacks.onResume();
1054 }
Adam Cohen06dff352012-06-01 17:17:08 -07001055 }
1056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001059 // Ensure that items added to Launcher are queued until Launcher returns
1060 InstallShortcutReceiver.enableInstallQueue();
1061
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001062 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001063 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001064 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001065 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001066
1067 // We call onHide() aggressively. The custom content callbacks should be able to
1068 // debounce excess onHide calls.
1069 if (mWorkspace.getCustomContentCallbacks() != null) {
1070 mWorkspace.getCustomContentCallbacks().onHide();
1071 }
Romain Guycbb89e42009-06-08 15:52:54 -07001072
Adam Cohen9211d422014-10-07 18:14:53 -07001073 if (mLauncherCallbacks != null) {
1074 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001075 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001076 }
1077
1078 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001079 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1080 // by a onResume or by scrolling otherwise.
1081 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001082
1083 // Custom content is completely hidden
1084 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001085
1086 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1087 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001088
1089 // Indicates whether the user is allowed to scroll away from the custom content.
1090 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001091 }
1092
Adam Cohenc2d6e892014-10-16 09:49:24 -07001093 public interface LauncherOverlay {
1094
1095 /**
1096 * Touch interaction leading to overscroll has begun
1097 */
1098 public void onScrollInteractionBegin();
1099
1100 /**
1101 * Touch interaction related to overscroll has ended
1102 */
1103 public void onScrollInteractionEnd();
1104
1105 /**
1106 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1107 * screen (or in the case of RTL, the rightmost screen).
1108 */
1109 public void onScrollChange(int progress, boolean rtl);
1110
1111 /**
1112 * Screen has stopped scrolling
1113 */
1114 public void onScrollSettled();
1115
1116 /**
1117 * This method can be called by the Launcher in order to force the LauncherOverlay
1118 * to exit fully immersive mode.
1119 */
1120 public void forceExitFullImmersion();
1121 }
1122
Winson Chung0f785722015-04-08 10:27:49 -07001123 public interface LauncherAppsCallbacks {
1124 /**
1125 * Updates launcher to the available space that AllApps can take so as not to overlap with
1126 * any other views.
1127 */
Winson Chungef7f8742015-06-04 17:18:17 -07001128 @Deprecated
Winson Chung0f785722015-04-08 10:27:49 -07001129 public void onAllAppsBoundsChanged(Rect bounds);
1130
1131 /**
1132 * Called to dismiss all apps if it is showing.
1133 */
Winson Chungef7f8742015-06-04 17:18:17 -07001134 @Deprecated
Winson Chung0f785722015-04-08 10:27:49 -07001135 public void dismissAllApps();
Sunny Goyal5683f872015-05-29 14:54:40 -07001136
1137 /**
1138 * Sets the search manager to be used for app search.
1139 */
Winson Chungef7f8742015-06-04 17:18:17 -07001140 @Deprecated
1141 public void setSearchManager(Object manager);
Winson Chung0f785722015-04-08 10:27:49 -07001142 }
1143
Jun Mukai8af0cd82015-05-12 12:32:12 -07001144 public interface LauncherSearchCallbacks {
1145 /**
1146 * Called when the search overlay is shown.
1147 */
1148 public void onSearchOverlayOpened();
1149
1150 /**
1151 * Called when the search overlay is dismissed.
1152 */
1153 public void onSearchOverlayClosed();
1154 }
1155
Adam Cohenc2d6e892014-10-16 09:49:24 -07001156 public interface LauncherOverlayCallbacks {
1157 /**
1158 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1159 * however it doesn't modify any state within the launcher.
1160 */
1161 public boolean canEnterFullImmersion();
1162
1163 /**
1164 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1165 * eg. by occupying the full screen and handling all touch events.
1166 *
1167 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1168 * case, Launcher will modify any necessary state and assumes the overlay is
1169 * handling all interaction. If false, the LauncherOverlay should cancel any
1170 *
1171 */
1172 public boolean enterFullImmersion();
1173
1174 /**
1175 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1176 * full control over UI and state.
1177 */
1178 public void exitFullImmersion();
1179 }
1180
1181 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1182
1183 @Override
1184 public boolean canEnterFullImmersion() {
1185 return mState == State.WORKSPACE;
1186 }
1187
1188 @Override
1189 public boolean enterFullImmersion() {
1190 if (mState == State.WORKSPACE) {
1191 // When fully immersed, disregard any touches which fall through.
1192 mDragLayer.setBlockTouch(true);
1193 return true;
1194 }
1195 return false;
1196 }
1197
1198 @Override
1199 public void exitFullImmersion() {
1200 mDragLayer.setBlockTouch(false);
1201 }
1202 }
1203
Jorim Jaggid017f882014-01-14 17:08:48 -08001204 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001205 if (mLauncherCallbacks != null) {
1206 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001207 } else {
1208 // On devices with a locked orientation, we will at least have the allow rotation
1209 // setting.
1210 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001211 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001212 }
1213
Adam Cohen9211d422014-10-07 18:14:53 -07001214 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001215 CustomContentCallbacks callbacks, String description) {
1216 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001217 }
1218
Adam Cohenedb40762013-07-18 16:45:45 -07001219 // The custom content needs to offset its content to account for the QSB
1220 public int getTopOffsetForCustomContent() {
1221 return mWorkspace.getPaddingTop();
1222 }
1223
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001224 @Override
1225 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001226 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001227 if (mModel.isCurrentCallbacks(this)) {
1228 mModel.stopLoader();
1229 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001230 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1231
Romain Guy13c2e7b2010-03-10 19:45:00 -08001232 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001233 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001234
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001235 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001236 @Override
1237 public void onWindowFocusChanged(boolean hasFocus) {
1238 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001239 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001240
1241 if (mLauncherCallbacks != null) {
1242 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1243 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001244 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 private boolean acceptFilter() {
1247 final InputMethodManager inputManager = (InputMethodManager)
1248 getSystemService(Context.INPUT_METHOD_SERVICE);
1249 return !inputManager.isFullscreenMode();
1250 }
1251
1252 @Override
1253 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001254 final int uniChar = event.getUnicodeChar();
1255 final boolean handled = super.onKeyDown(keyCode, event);
1256 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1257 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1259 keyCode, event);
1260 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001261 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001262 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001263 // showSearchDialog()
1264 // If there are multiple keystrokes before the search dialog takes focus,
1265 // onSearchRequested() will be called for every keystroke,
1266 // but it is idempotent, so it's fine.
1267 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
1269 }
1270
Joe Onorato8a9625e2010-01-28 15:55:35 -08001271 // Eat the long press event so the keyboard doesn't come up.
1272 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1273 return true;
1274 }
1275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 return handled;
1277 }
1278
Karl Rosaen138a0412009-04-23 19:00:21 -07001279 private String getTypedText() {
1280 return mDefaultKeySsb.toString();
1281 }
1282
1283 private void clearTypedText() {
1284 mDefaultKeySsb.clear();
1285 mDefaultKeySsb.clearSpans();
1286 Selection.setSelection(mDefaultKeySsb, 0);
1287 }
1288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001290 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1291 * State
1292 */
1293 private static State intToState(int stateOrdinal) {
1294 State state = State.WORKSPACE;
1295 final State[] stateValues = State.values();
1296 for (int i = 0; i < stateValues.length; i++) {
1297 if (stateValues[i].ordinal() == stateOrdinal) {
1298 state = stateValues[i];
1299 break;
1300 }
1301 }
1302 return state;
1303 }
1304
1305 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 * Restores the previous state, if it exists.
1307 *
1308 * @param savedState The previous state.
1309 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001310 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 private void restoreState(Bundle savedState) {
1312 if (savedState == null) {
1313 return;
1314 }
1315
Michael Jurka883f55b2010-10-21 15:47:14 -07001316 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001317 if (state == State.APPS || state == State.WIDGETS) {
1318 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001319 }
1320
Adam Cohen21cd0022013-10-09 18:57:02 -07001321 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1322 PagedView.INVALID_RESTORE_PAGE);
1323 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001324 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326
Winson Chung3d503fb2011-07-13 17:25:49 -07001327 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001328 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001329
Winson Chung3d503fb2011-07-13 17:25:49 -07001330 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1331 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001332 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001333 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1334 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001335 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1336 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001337 AppWidgetProviderInfo info = savedState.getParcelable(
1338 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001339 mPendingAddWidgetInfo = info == null ?
1340 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1341
Adam Cohen4637b5a2013-11-04 18:21:24 -08001342 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001343 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 mRestoring = true;
1345 }
1346
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001347 mItemIdToViewId = (HashMap<Integer, Integer>)
1348 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 }
1350
1351 /**
1352 * Finds all the views we need and configure them properly.
1353 */
1354 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001355 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001356
Craig Mautner360310b2012-10-26 15:13:08 -07001357 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001358 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001359 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1360 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001361 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001362 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001363
John Spurlock77e1f472013-09-11 10:09:51 -04001364 mLauncherView.setSystemUiVisibility(
1365 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001366 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367
Winson Chung4c98d922011-05-31 16:50:48 -07001368 // Setup the drag layer
1369 mDragLayer.setup(this, dragController);
1370
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 // Setup the hotseat
1372 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1373 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001374 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001375 }
1376
Jorim Jaggid017f882014-01-14 17:08:48 -08001377 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001378 View widgetButton = findViewById(R.id.widget_button);
1379 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001380 @Override
1381 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001382 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001383 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001384 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001385 }
1386 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001387 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1388
1389 View wallpaperButton = findViewById(R.id.wallpaper_button);
1390 wallpaperButton.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 onClickWallpaperPicker(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 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1399
1400 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001401 if (hasSettings()) {
1402 settingsButton.setOnClickListener(new OnClickListener() {
1403 @Override
1404 public void onClick(View arg0) {
1405 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001406 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001407 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001408 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001409 });
1410 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1411 } else {
1412 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001413 }
1414
Adam Cohendbdff6b2013-09-18 19:09:15 -07001415 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001416
Winson Chung4c98d922011-05-31 16:50:48 -07001417 // Setup the workspace
1418 mWorkspace.setHapticFeedbackEnabled(false);
1419 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001420 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001421 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001422
Winson Chungf0ea4d32011-06-06 14:27:16 -07001423 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001424 mSearchDropTargetBar = (SearchDropTargetBar)
1425 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001426
Winson Chungef7f8742015-06-04 17:18:17 -07001427 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001428 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001429 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001430 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1431 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1432 } else {
1433 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1434 }
Craig Mautner360310b2012-10-26 15:13:08 -07001435
Winson Chung3d503fb2011-07-13 17:25:49 -07001436 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001437 dragController.setDragScoller(mWorkspace);
1438 dragController.setScrollView(mDragLayer);
1439 dragController.setMoveTarget(mWorkspace);
1440 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001441 if (mSearchDropTargetBar != null) {
1442 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001443 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001444 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001445
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001446 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001447 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001448 mWeightWatcher = new WeightWatcher(this);
1449 mWeightWatcher.setAlpha(0.5f);
1450 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001451 new FrameLayout.LayoutParams(
1452 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001453 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001454 Gravity.BOTTOM)
1455 );
Adam Cohen39a06042013-07-19 14:30:12 -07001456
1457 boolean show = shouldShowWeightWatcher();
1458 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461
1462 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001463 * Sets the all apps button. This method is called from {@link Hotseat}.
1464 */
1465 public void setAllAppsButton(View allAppsButton) {
1466 mAllAppsButton = allAppsButton;
1467 }
1468
1469 public View getAllAppsButton() {
1470 return mAllAppsButton;
1471 }
1472
1473 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 * Creates a view representing a shortcut.
1475 *
1476 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001478 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001479 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
1482 /**
1483 * Creates a view representing a shortcut inflated from the specified resource.
1484 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * @param parent The group the shortcut belongs to.
1486 * @param info The data structure describing the shortcut.
1487 *
1488 * @return A View inflated from layoutResId.
1489 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001490 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001491 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001492 parent, false);
1493 favorite.applyFromShortcutInfo(info, mIconCache);
1494 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001496 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 return favorite;
1498 }
1499
1500 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 * Add a shortcut to the workspace.
1502 *
1503 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001505 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001506 int cellY) {
1507 int[] cellXY = mTmpAddItemCellCoordinates;
1508 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001509 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001510
Sunny Goyal5c97f512015-05-19 16:03:28 -07001511 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001512 if (info == null) {
1513 return;
1514 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001515 final View view = createShortcut(info);
1516
Sunny Goyal5c97f512015-05-19 16:03:28 -07001517 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 // First we check if we already know the exact location where we want to add this item.
1519 if (cellX >= 0 && cellY >= 0) {
1520 cellXY[0] = cellX;
1521 cellXY[1] = cellY;
1522 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001523
1524 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001525 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 true, null,null)) {
1527 return;
1528 }
1529 DragObject dragObject = new DragObject();
1530 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1532 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001533 return;
1534 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001535 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001536 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001537 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001538 foundCellSpan = (result != null);
1539 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001540 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001541 }
1542
1543 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001544 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001545 return;
1546 }
1547
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001548 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001552 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554 }
1555
Adam Cohen2e6da152015-05-06 11:42:25 -07001556 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1557 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001558 // We want to account for the extra amount of padding that we are adding to the widget
1559 // to ensure that it gets the full amount of space that it has requested
1560 int requiredWidth = minWidth + padding.left + padding.right;
1561 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001562 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001563 }
1564
Adam Cohen2e6da152015-05-06 11:42:25 -07001565 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1566 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001567 }
1568
Adam Cohen2e6da152015-05-06 11:42:25 -07001569 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1570 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001571 }
1572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001574 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001576 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 */
Adam Cohen091440a2015-03-18 14:16:05 -07001578 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001579 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1580
1581 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001582 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001583 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1584 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001585 }
Romain Guycbb89e42009-06-08 15:52:54 -07001586
Adam Cohen59400422014-03-05 18:07:04 -08001587 if (appWidgetInfo.isCustomWidget) {
1588 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001589 }
1590
Adam Cohen59400422014-03-05 18:07:04 -08001591 LauncherAppWidgetInfo launcherInfo;
1592 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1593 launcherInfo.spanX = info.spanX;
1594 launcherInfo.spanY = info.spanY;
1595 launcherInfo.minSpanX = info.minSpanX;
1596 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001597 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001600 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601
1602 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001603 if (hostView == null) {
1604 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001605 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1606 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001607 } else {
1608 // The AppWidgetHostView has already been inflated and instantiated
1609 launcherInfo.hostView = hostView;
1610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001612 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001613 launcherInfo.notifyWidgetSizeChanged(this);
1614
Adam Cohen59400422014-03-05 18:07:04 -08001615 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1616 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001617
1618 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001620 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 }
Romain Guycbb89e42009-06-08 15:52:54 -07001622
Adam Cohended9f8d2010-11-03 13:25:16 -07001623 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1624 @Override
1625 public void onReceive(Context context, Intent intent) {
1626 final String action = intent.getAction();
1627 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1628 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001629 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001630 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001631
Winson Chungc100e8e2011-08-09 16:02:43 -07001632 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001633 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001634 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001635 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001636 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001637 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001638 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1639 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001640 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001641 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1642 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001643 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001644 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1645 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1646 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001647 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001648 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1649 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001650 }
1651 }
1652 };
1653
1654 @Override
1655 public void onAttachedToWindow() {
1656 super.onAttachedToWindow();
1657
1658 // Listen for broadcasts related to user-presence
1659 final IntentFilter filter = new IntentFilter();
1660 filter.addAction(Intent.ACTION_SCREEN_OFF);
1661 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001662 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001663 if (ENABLE_DEBUG_INTENTS) {
1664 filter.addAction(DebugIntents.DELETE_DATABASE);
1665 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1666 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001667 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001668 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001669 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001670 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001671 mVisible = true;
1672 }
1673
Adam Cohen0b395352014-06-09 22:54:36 +00001674 /**
1675 * Sets up transparent navigation and status bars in LMP.
1676 * This method is a no-op for other platform versions.
1677 */
Sunny Goyald0091012015-01-20 15:55:34 -08001678 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001679 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001680 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001681 Window window = getWindow();
1682 window.getAttributes().systemUiVisibility |=
1683 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1684 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1685 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1686 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1687 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1688 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1689 window.setStatusBarColor(Color.TRANSPARENT);
1690 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001691 }
1692 }
1693
Adam Cohended9f8d2010-11-03 13:25:16 -07001694 @Override
1695 public void onDetachedFromWindow() {
1696 super.onDetachedFromWindow();
1697 mVisible = false;
1698
Adam Cohend113e0c2010-11-11 10:48:05 -08001699 if (mAttached) {
1700 unregisterReceiver(mReceiver);
1701 mAttached = false;
1702 }
Winson Chungb745afb2015-03-02 11:51:23 -08001703 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 }
1705
1706 public void onWindowVisibilityChanged(int visibility) {
1707 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001708 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001709 // The following code used to be in onResume, but it turns out onResume is called when
1710 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1711 // is a more appropriate event to handle
1712 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001713 if (!mWorkspaceLoading) {
1714 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001715 // We want to let Launcher draw itself at least once before we force it to build
1716 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001717 // apps is nice and speedy.
1718 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001719 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001720 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001721 if (mStarted) return;
1722 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001723 // We delay the layer building a bit in order to give
1724 // other message processing a time to run. In particular
1725 // this avoids a delay in hiding the IME if it was
1726 // currently shown, because doing that may involve
1727 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001728 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001729 final ViewTreeObserver.OnDrawListener listener = this;
1730 mWorkspace.post(new Runnable() {
1731 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001732 if (mWorkspace != null &&
1733 mWorkspace.getViewTreeObserver() != null) {
1734 mWorkspace.getViewTreeObserver().
1735 removeOnDrawListener(listener);
1736 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001737 }
1738 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001739 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001740 }
1741 });
1742 }
1743 clearTypedText();
1744 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 }
1746
Adam Cohen091440a2015-03-18 14:16:05 -07001747 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 mHandler.removeMessages(ADVANCE_MSG);
1749 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1750 mHandler.sendMessageDelayed(msg, delay);
1751 mAutoAdvanceSentTime = System.currentTimeMillis();
1752 }
1753
Adam Cohen091440a2015-03-18 14:16:05 -07001754 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1756 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1757 mAutoAdvanceRunning = autoAdvanceRunning;
1758 if (autoAdvanceRunning) {
1759 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1760 sendAdvanceMessage(delay);
1761 } else {
1762 if (!mWidgetsToAdvance.isEmpty()) {
1763 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1764 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1765 }
1766 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001767 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
1769 }
1770 }
1771
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001772 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1773
Adam Cohended9f8d2010-11-03 13:25:16 -07001774 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001775 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 if (msg.what == ADVANCE_MSG) {
1777 int i = 0;
1778 for (View key: mWidgetsToAdvance.keySet()) {
1779 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1780 final int delay = mAdvanceStagger * i;
1781 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001782 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001783 public void run() {
1784 ((Advanceable) v).advance();
1785 }
1786 }, delay);
1787 }
1788 i++;
1789 }
1790 sendAdvanceMessage(mAdvanceInterval);
1791 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001792 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001793 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001794 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001795
1796 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001797 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001798 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1799 if (v instanceof Advanceable) {
1800 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001801 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001802 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001803 }
1804 }
1805
1806 void removeWidgetToAutoAdvance(View hostView) {
1807 if (mWidgetsToAdvance.containsKey(hostView)) {
1808 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001809 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001810 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001811 }
1812
Joe Onorato9c1289c2009-08-17 11:03:03 -04001813 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001814 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001815 launcherInfo.hostView = null;
1816 }
1817
Hyunyoung Song3f471442015-04-08 19:01:34 -07001818 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001819 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1820 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001821 }
1822
Winson Chunga6945242014-01-08 14:04:34 -08001823 public DragLayer getDragLayer() {
1824 return mDragLayer;
1825 }
1826
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001827 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001828 return mAppsView;
1829 }
1830
Hyunyoung Song3f471442015-04-08 19:01:34 -07001831 public WidgetsContainerView getWidgetsView() {
1832 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001833 }
1834
Winson Chunga6945242014-01-08 14:04:34 -08001835 public Workspace getWorkspace() {
1836 return mWorkspace;
1837 }
1838
1839 public Hotseat getHotseat() {
1840 return mHotseat;
1841 }
1842
Jorim Jaggid017f882014-01-14 17:08:48 -08001843 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001844 return mOverviewPanel;
1845 }
1846
1847 public SearchDropTargetBar getSearchBar() {
1848 return mSearchDropTargetBar;
1849 }
1850
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001851 public LauncherAppWidgetHost getAppWidgetHost() {
1852 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 }
Romain Guycbb89e42009-06-08 15:52:54 -07001854
Winson Chunga9abd0e2010-10-27 17:18:37 -07001855 public LauncherModel getModel() {
1856 return mModel;
1857 }
1858
Winson Chunga6945242014-01-08 14:04:34 -08001859 protected SharedPreferences getSharedPrefs() {
1860 return mSharedPrefs;
1861 }
1862
Adam Cohen2e6da152015-05-06 11:42:25 -07001863 public DeviceProfile getDeviceProfile() {
1864 return mDeviceProfile;
1865 }
1866
Bjorn Bringertc459e522013-06-07 19:36:01 +01001867 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001868 getWindow().closeAllPanels();
1869
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001870 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001871 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001872 }
1873
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 @Override
1875 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001876 long startTime = 0;
1877 if (DEBUG_RESUME_TIME) {
1878 startTime = System.currentTimeMillis();
1879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 super.onNewIntent(intent);
1881
1882 // Close the menu
1883 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001884 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001885 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001886
Adam Cohened307df2013-10-02 09:37:31 -07001887 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1888 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1889 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001890
Adam Cohen6fecd412013-10-02 17:41:50 -07001891 if (mWorkspace == null) {
1892 // Can be cases where mWorkspace is null, this prevents a NPE
1893 return;
1894 }
1895 Folder openFolder = mWorkspace.getOpenFolder();
1896 // In all these cases, only animate if we're already on home
1897 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001898
1899 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1900 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001901 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001902 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001903 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001904 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001905
Adam Cohen6fecd412013-10-02 17:41:50 -07001906 closeFolder();
1907 exitSpringLoadedDragMode();
1908
1909 // If we are already on home, then just animate back to the workspace,
1910 // otherwise, just wait until onResume to set the state back to Workspace
1911 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001912 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001913 } else {
1914 mOnResumeState = State.WORKSPACE;
1915 }
1916
1917 final View v = getWindow().peekDecorView();
1918 if (v != null && v.getWindowToken() != null) {
1919 InputMethodManager imm = (InputMethodManager)getSystemService(
1920 INPUT_METHOD_SERVICE);
1921 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1922 }
1923
Winson Chungb745afb2015-03-02 11:51:23 -08001924 // Reset the apps view
1925 if (!alreadyOnHome && mAppsView != null) {
1926 mAppsView.scrollToTop();
1927 }
1928
Hyunyoung Song3f471442015-04-08 19:01:34 -07001929 // Reset the widgets view
1930 if (!alreadyOnHome && mWidgetsView != null) {
1931 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001933
Adam Cohen9211d422014-10-07 18:14:53 -07001934 if (mLauncherCallbacks != null) {
1935 mLauncherCallbacks.onHomeIntent();
1936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 }
Adam Cohened307df2013-10-02 09:37:31 -07001938
Michael Jurka447bf842013-05-15 14:52:15 +02001939 if (DEBUG_RESUME_TIME) {
1940 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942
Adam Cohen9211d422014-10-07 18:14:53 -07001943 if (mLauncherCallbacks != null) {
1944 mLauncherCallbacks.onNewIntent(intent);
1945 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001946 }
1947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001949 public void onRestoreInstanceState(Bundle state) {
1950 super.onRestoreInstanceState(state);
1951 for (int page: mSynchronouslyBoundPages) {
1952 mWorkspace.restoreInstanceStateForChild(page);
1953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955
1956 @Override
1957 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001958 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001959 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1960 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001961 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001962 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963
Michael Jurka883f55b2010-10-21 15:47:14 -07001964 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001965 // We close any open folder since it will not be re-opened, and we need to make sure
1966 // this state is reflected.
1967 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968
Adam Cohendcd297f2013-06-18 13:13:40 -07001969 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001970 mWaitingForResult) {
1971 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001972 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001973 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1974 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001975 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1976 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1977 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001978 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 }
1980
Hyunyoung Song3f471442015-04-08 19:01:34 -07001981 // Save the current widgets tray?
1982 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001983 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001984
1985 if (mLauncherCallbacks != null) {
1986 mLauncherCallbacks.onSaveInstanceState(outState);
1987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989
1990 @Override
1991 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001993
Winson Chunge7a03942011-08-05 15:05:12 -07001994 // Remove all pending runnables
1995 mHandler.removeMessages(ADVANCE_MSG);
1996 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001997 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001998
Winson Chungcd2b0142011-06-08 16:02:26 -07001999 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002000 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002001
2002 // It's possible to receive onDestroy after a new Launcher activity has
2003 // been created. In this case, don't interfere with the new Launcher.
2004 if (mModel.isCurrentCallbacks(this)) {
2005 mModel.stopLoader();
2006 app.setLauncher(null);
2007 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002010 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002012 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002014 mAppWidgetHost = null;
2015
2016 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017
2018 TextKeyListener.getInstance().release();
2019
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002020 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002021 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002022
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002023 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002024 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002025 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002026 mWorkspace = null;
2027 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002028
Michael Jurka2ecf9952012-06-18 12:52:28 -07002029 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002030
2031 if (mLauncherCallbacks != null) {
2032 mLauncherCallbacks.onDestroy();
2033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
2035
Adam Cohena9cf38f2011-05-02 15:36:58 -07002036 public DragController getDragController() {
2037 return mDragController;
2038 }
2039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 @Override
2041 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002042 onStartForResult(requestCode);
2043 super.startActivityForResult(intent, requestCode);
2044 }
2045
2046 @Override
2047 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2048 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2049 onStartForResult(requestCode);
2050 try {
2051 super.startIntentSenderForResult(intent, requestCode,
2052 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2053 } catch (IntentSender.SendIntentException e) {
2054 throw new ActivityNotFoundException();
2055 }
2056 }
2057
2058 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002059 if (requestCode >= 0) {
2060 setWaitingForResult(true);
2061 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063
Winson Chung70d72102011-08-12 11:24:35 -07002064 /**
2065 * Indicates that we want global search for this activity by setting the globalSearch
2066 * argument for {@link #startSearch} to true.
2067 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002069 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002071
Karl Rosaen138a0412009-04-23 19:00:21 -07002072 if (initialQuery == null) {
2073 // Use any text typed in the launcher as the initial query
2074 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002075 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 if (appSearchData == null) {
2077 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002078 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 }
Winson Chungadf0c182012-08-23 14:59:07 -07002080 Rect sourceBounds = new Rect();
2081 if (mSearchDropTargetBar != null) {
2082 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2083 }
Romain Guycbb89e42009-06-08 15:52:54 -07002084
Ian Parkinson047036e2014-09-01 15:40:53 +01002085 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002086 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002087 if (clearTextImmediately) {
2088 clearTypedText();
2089 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002090
2091 // We need to show the workspace after starting the search
2092 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002093 }
2094
Ian Parkinson047036e2014-09-01 15:40:53 +01002095 /**
2096 * Start a text search.
2097 *
2098 * @return {@code true} if the search will start immediately, so any further keypresses
2099 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2100 * to buffer keypresses.
2101 */
2102 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002103 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002104 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2105 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2106 sourceBounds);
2107 }
2108
Michael Jurkaa33411c2012-06-14 16:18:21 -07002109 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002110 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002111 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002112 }
2113
2114 /**
2115 * Starts the global search activity. This code is a copied from SearchManager
2116 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002117 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002118 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002119 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002120 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2121 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2122 if (globalSearchActivity == null) {
2123 Log.w(TAG, "No global search activity found.");
2124 return;
2125 }
2126 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2127 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2128 intent.setComponent(globalSearchActivity);
2129 // Make sure that we have a Bundle to put source in
2130 if (appSearchData == null) {
2131 appSearchData = new Bundle();
2132 } else {
2133 appSearchData = new Bundle(appSearchData);
2134 }
Adam Cohen9211d422014-10-07 18:14:53 -07002135 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002136 if (!appSearchData.containsKey("source")) {
2137 appSearchData.putString("source", getPackageName());
2138 }
2139 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2140 if (!TextUtils.isEmpty(initialQuery)) {
2141 intent.putExtra(SearchManager.QUERY, initialQuery);
2142 }
2143 if (selectInitialQuery) {
2144 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2145 }
2146 intent.setSourceBounds(sourceBounds);
2147 try {
2148 startActivity(intent);
2149 } catch (ActivityNotFoundException ex) {
2150 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 }
2153
Yura4f93ec62014-02-04 14:15:21 +00002154 public boolean isOnCustomContent() {
2155 return mWorkspace.isOnOrMovingToCustomContent();
2156 }
2157
Winson Chung70d72102011-08-12 11:24:35 -07002158 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002159 public boolean onPrepareOptionsMenu(Menu menu) {
2160 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002161 if (!isOnCustomContent()) {
2162 // Close any open folders
2163 closeFolder();
2164 // Stop resizing any widgets
2165 mWorkspace.exitWidgetResizeMode();
2166 if (!mWorkspace.isInOverviewMode()) {
2167 // Show the overview mode
2168 showOverviewMode(true);
2169 } else {
2170 showWorkspace(true);
2171 }
Winson Chunge0298742014-01-17 12:03:00 -08002172 }
Adam Cohen9211d422014-10-07 18:14:53 -07002173 if (mLauncherCallbacks != null) {
2174 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2175 }
2176
Michael Jurkab94f3f82013-09-11 18:08:54 +02002177 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002178 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002180 @Override
2181 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002182 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002183 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002184 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002185 }
2186
Joe Onorato9c1289c2009-08-17 11:03:03 -04002187 public boolean isWorkspaceLocked() {
2188 return mWorkspaceLoading || mWaitingForResult;
2189 }
2190
Adam Cohen517a7f52014-03-01 12:12:59 -08002191 public boolean isWorkspaceLoading() {
2192 return mWorkspaceLoading;
2193 }
2194
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002195 private void setWorkspaceLoading(boolean value) {
2196 boolean isLocked = isWorkspaceLocked();
2197 mWorkspaceLoading = value;
2198 if (isLocked != isWorkspaceLocked()) {
2199 onWorkspaceLockedChanged();
2200 }
2201 }
2202
2203 private void setWaitingForResult(boolean value) {
2204 boolean isLocked = isWorkspaceLocked();
2205 mWaitingForResult = value;
2206 if (isLocked != isWorkspaceLocked()) {
2207 onWorkspaceLockedChanged();
2208 }
2209 }
2210
Adam Cohen9211d422014-10-07 18:14:53 -07002211 protected void onWorkspaceLockedChanged() {
2212 if (mLauncherCallbacks != null) {
2213 mLauncherCallbacks.onWorkspaceLockedChanged();
2214 }
2215 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002216
Michael Jurka0280c3b2010-09-17 15:00:07 -07002217 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002218 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002219 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002220 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2221 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002222 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002223 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002224 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002225
Sunny Goyale6b63a32015-01-16 16:14:29 -08002226 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002227 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002228 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2229 }
2230
Sunny Goyale6b63a32015-01-16 16:14:29 -08002231 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002232 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2233 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002234 if (appWidgetInfo.configure != null) {
2235 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002236 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002237
Bjorn Bringert7984c942009-12-09 15:38:25 +00002238 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002239 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2240 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002243 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002244 Runnable onComplete = new Runnable() {
2245 @Override
2246 public void run() {
2247 // Exit spring loaded mode if necessary after adding the widget
2248 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2249 null);
2250 }
2251 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002253 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002254 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 }
2256 }
Romain Guycbb89e42009-06-08 15:52:54 -07002257
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002258 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002259 // Close any folders that may be open.
2260 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002261 mWorkspace.moveToCustomContentScreen(animate);
2262 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002263
2264 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2265 int[] cell, int spanX, int spanY) {
2266 switch (info.itemType) {
2267 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2268 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2269 int span[] = new int[2];
2270 span[0] = spanX;
2271 span[1] = spanY;
2272 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2273 container, screenId, cell, span);
2274 break;
2275 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2276 processShortcutFromDrop(info.componentName, container, screenId, cell);
2277 break;
2278 default:
2279 throw new IllegalStateException("Unknown item type: " + info.itemType);
2280 }
2281 }
2282
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 /**
2284 * Process a shortcut drop.
2285 *
2286 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002287 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002289 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002290 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2291 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002292 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002293 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002294 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002295 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296
2297 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 mPendingAddInfo.cellX = cell[0];
2299 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002301
2302 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2303 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002304 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002305 }
2306
Adam Cohenfbba09b2011-07-18 21:43:05 -07002307 /**
2308 * Process a widget drop.
2309 *
2310 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002311 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002312 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002313 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002314 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2315 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002316 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002317 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002318 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002319 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002320 mPendingAddInfo.minSpanX = info.minSpanX;
2321 mPendingAddInfo.minSpanY = info.minSpanY;
2322
Adam Cohenfbba09b2011-07-18 21:43:05 -07002323 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002324 mPendingAddInfo.cellX = cell[0];
2325 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002326 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002327 if (span != null) {
2328 mPendingAddInfo.spanX = span[0];
2329 mPendingAddInfo.spanY = span[1];
2330 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002331
Adam Cohened66b2b2012-01-23 17:28:51 -08002332 AppWidgetHostView hostView = info.boundWidget;
2333 int appWidgetId;
2334 if (hostView != null) {
2335 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002336 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002337
2338 // Clear the boundWidget so that it doesn't get destroyed.
2339 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002340 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002341 // In this case, we either need to start an activity to get permission to bind
2342 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002343 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002344 Bundle options = info.bindOptions;
2345
Sunny Goyalffe83f12014-08-14 17:39:34 -07002346 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2347 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002348 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002349 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002350 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002351 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002352 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2353 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2354 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002355 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2356 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002357 // TODO: we need to make sure that this accounts for the options bundle.
2358 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002359 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2360 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002361 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362 }
2363
Adam Cohendcd297f2013-06-18 13:13:40 -07002364 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002365 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002366 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 folderInfo.title = getText(R.string.folder_name);
2368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002370 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2371 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002372 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373
2374 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002375 FolderIcon newFolder =
2376 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002377 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002378 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002379 // Force measure the new folder icon
2380 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2381 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002382 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 }
Romain Guycbb89e42009-06-08 15:52:54 -07002384
Joe Onorato9c1289c2009-08-17 11:03:03 -04002385 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002386 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002387 }
2388
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002389 /**
2390 * Registers various content observers. The current implementation registers
2391 * only a favorites observer to keep track of the favorites applications.
2392 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002393 private void registerContentObservers() {
2394 ContentResolver resolver = getContentResolver();
2395 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2396 true, mWidgetObserver);
2397 }
2398
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 @Override
2400 public boolean dispatchKeyEvent(KeyEvent event) {
2401 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2402 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002404 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002405 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002406 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002407 dumpState();
2408 return true;
2409 }
2410 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002411 }
2412 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2413 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002414 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 return true;
2416 }
2417 }
2418
2419 return super.dispatchKeyEvent(event);
2420 }
2421
Joe Onorato88ec0992009-11-19 13:16:06 -08002422 @Override
2423 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002424 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2425 return;
2426 }
2427
Sunny Goyal45478022015-06-08 16:52:41 -07002428 if (mDragController.isDragging()) {
2429 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002430 return;
2431 }
2432
Winson Chungb745afb2015-03-02 11:51:23 -08002433 if (isAppsViewVisible()) {
2434 showWorkspace(true);
2435 } else if (isWidgetsViewVisible()) {
2436 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002437 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002438 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002439 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002440 Folder openFolder = mWorkspace.getOpenFolder();
2441 if (openFolder.isEditingName()) {
2442 openFolder.dismissEditingName();
2443 } else {
2444 closeFolder();
2445 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002446 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002447 mWorkspace.exitWidgetResizeMode();
2448
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002449 // Back button is a no-op here, but give at least some feedback for the button press
2450 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002451 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002452 }
2453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002455 * Re-listen when widgets are reset.
2456 */
Adam Cohen091440a2015-03-18 14:16:05 -07002457 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002458 if (mAppWidgetHost != null) {
2459 mAppWidgetHost.startListening();
2460 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002461 }
2462
2463 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 * Launches the intent referred by the clicked shortcut.
2465 *
2466 * @param v The view representing the clicked shortcut.
2467 */
2468 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002469 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2470 // view has detached (it's possible for this to happen if the view is removed mid touch).
2471 if (v.getWindowToken() == null) {
2472 return;
2473 }
2474
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002475 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002476 return;
2477 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002478
Adam Cohen1697b792013-09-17 19:08:21 -07002479 if (v instanceof Workspace) {
2480 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002481 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002482 }
2483 return;
2484 }
2485
2486 if (v instanceof CellLayout) {
2487 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002488 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002489 }
2490 }
2491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002493 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002494 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002496 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002497 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002498 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002499 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002500 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002501 } else if (tag instanceof AppInfo) {
2502 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002503 } else if (tag instanceof LauncherAppWidgetInfo) {
2504 if (v instanceof PendingAppWidgetHostView) {
2505 onClickPendingWidget((PendingAppWidgetHostView) v);
2506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 }
2508 }
2509
Sunny Goyal70660032015-05-14 00:07:08 -07002510 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002511 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002512 return false;
2513 }
2514
Michael Jurkaaf442092010-06-10 17:01:57 -07002515 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002516 * Event handler for the app widget view which has not fully restored.
2517 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002518 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002519 if (mIsSafeModeEnabled) {
2520 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2521 return;
2522 }
2523
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002524 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002525 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002526 int widgetId = info.appWidgetId;
2527 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2528 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002529 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2530 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002531 mPendingAddInfo.copyFrom(info);
2532 mPendingAddWidgetId = widgetId;
2533
Sunny Goyalffe83f12014-08-14 17:39:34 -07002534 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2535 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002536 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002537 } else if (info.installProgress < 0) {
2538 // The install has not been queued
2539 final String packageName = info.providerName.getPackageName();
2540 showBrokenAppInstallDialog(packageName,
2541 new DialogInterface.OnClickListener() {
2542 public void onClick(DialogInterface dialog, int id) {
2543 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2544 }
2545 });
2546 } else {
2547 // Download has started.
2548 final String packageName = info.providerName.getPackageName();
2549 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002550 }
2551 }
2552
2553 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002554 * Event handler for the "grid" button that appears on the home screen, which
2555 * enters all apps mode.
2556 *
2557 * @param v The view that was clicked.
2558 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002559 protected void onClickAllAppsButton(View v) {
2560 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002561 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002562 showWorkspace(true);
2563 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002564 // Try and refresh the set of predicted apps before we enter launcher
2565 showAppsView(true /* animated */, false /* resetListToTop */,
2566 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002567 }
2568 }
2569
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002570 private void showBrokenAppInstallDialog(final String packageName,
2571 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002572 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002573 .setTitle(R.string.abandoned_promises_title)
2574 .setMessage(R.string.abandoned_promise_explanation)
2575 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2576 .setNeutralButton(R.string.abandoned_clean_this,
2577 new DialogInterface.OnClickListener() {
2578 public void onClick(DialogInterface dialog, int id) {
2579 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2580 mWorkspace.removeAbandonedPromise(packageName, user);
2581 }
2582 })
2583 .create().show();
2584 return;
2585 }
2586
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002587 /**
2588 * Event handler for an app shortcut click.
2589 *
2590 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2591 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002592 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002593 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2594 Object tag = v.getTag();
2595 if (!(tag instanceof ShortcutInfo)) {
2596 throw new IllegalArgumentException("Input must be a Shortcut");
2597 }
2598
2599 // Open shortcut
2600 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002601
2602 if (shortcut.isDisabled != 0) {
2603 int error = R.string.activity_not_available;
2604 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2605 error = R.string.safemode_shortcut_error;
2606 }
2607 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2608 return;
2609 }
2610
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002611 final Intent intent = shortcut.intent;
2612
2613 // Check for special shortcuts
2614 if (intent.getComponent() != null) {
2615 final String shortcutClass = intent.getComponent().getClassName();
2616
2617 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2618 MemoryDumpActivity.startDump(this);
2619 return;
2620 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2621 toggleShowWeightWatcher();
2622 return;
2623 }
2624 }
2625
Chris Wren40c5ed32014-06-24 18:24:23 -04002626 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002627 if ((v instanceof BubbleTextView)
2628 && shortcut.isPromise()
2629 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002630 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002631 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002632 new DialogInterface.OnClickListener() {
2633 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002634 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002636 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 return;
2638 }
2639
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002640 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002641 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002642
2643 if (mLauncherCallbacks != null) {
2644 mLauncherCallbacks.onClickAppShortcut(v);
2645 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002646 }
2647
Adam Cohen091440a2015-03-18 14:16:05 -07002648 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002650 final ShortcutInfo shortcut;
2651 final Intent intent;
2652 if (tag instanceof ShortcutInfo) {
2653 shortcut = (ShortcutInfo) tag;
2654 intent = shortcut.intent;
2655 int[] pos = new int[2];
2656 v.getLocationOnScreen(pos);
2657 intent.setSourceBounds(new Rect(pos[0], pos[1],
2658 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002659
Sunny Goyal508da152014-08-14 10:53:27 -07002660 } else if (tag instanceof AppInfo) {
2661 shortcut = null;
2662 intent = ((AppInfo) tag).intent;
2663 } else {
2664 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2665 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002666
2667 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002668 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002669
2670 if (success && v instanceof BubbleTextView) {
2671 mWaitingForResume = (BubbleTextView) v;
2672 mWaitingForResume.setStayPressed(true);
2673 }
2674 }
2675
2676 /**
2677 * Event handler for a folder icon click.
2678 *
2679 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2680 */
2681 protected void onClickFolderIcon(View v) {
2682 if (LOGD) Log.d(TAG, "onClickFolder");
2683 if (!(v instanceof FolderIcon)){
2684 throw new IllegalArgumentException("Input must be a FolderIcon");
2685 }
2686
2687 FolderIcon folderIcon = (FolderIcon) v;
2688 final FolderInfo info = folderIcon.getFolderInfo();
2689 Folder openFolder = mWorkspace.getFolderForTag(info);
2690
2691 // If the folder info reports that the associated folder is open, then verify that
2692 // it is actually opened. There have been a few instances where this gets out of sync.
2693 if (info.opened && openFolder == null) {
2694 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2695 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2696 info.opened = false;
2697 }
2698
2699 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2700 // Close any open folder
2701 closeFolder();
2702 // Open the requested folder
2703 openFolder(folderIcon);
2704 } else {
2705 // Find the open folder...
2706 int folderScreen;
2707 if (openFolder != null) {
2708 folderScreen = mWorkspace.getPageForView(openFolder);
2709 // .. and close it
2710 closeFolder(openFolder);
2711 if (folderScreen != mWorkspace.getCurrentPage()) {
2712 // Close any folder open on the current screen
2713 closeFolder();
2714 // Pull the folder onto this screen
2715 openFolder(folderIcon);
2716 }
2717 }
2718 }
Adam Cohen9211d422014-10-07 18:14:53 -07002719
2720 if (mLauncherCallbacks != null) {
2721 mLauncherCallbacks.onClickFolderIcon(v);
2722 }
Michael Jurka5130e402011-10-13 04:55:35 -07002723 }
2724
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002725 /**
2726 * Event handler for the (Add) Widgets button that appears after a long press
2727 * on the home screen.
2728 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002729 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002730 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002731 if (mIsSafeModeEnabled) {
2732 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2733 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002734 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002735 if (mLauncherCallbacks != null) {
2736 mLauncherCallbacks.onClickAddWidgetButton(view);
2737 }
Adam Cohen9211d422014-10-07 18:14:53 -07002738 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002739 }
2740
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002741 /**
2742 * Event handler for the wallpaper picker button that appears after a long press
2743 * on the home screen.
2744 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002745 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002746 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002747 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2748 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002749
2750 if (mLauncherCallbacks != null) {
2751 mLauncherCallbacks.onClickWallpaperPicker(v);
2752 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002753 }
2754
2755 /**
2756 * Event handler for a click on the settings button that appears after a long press
2757 * on the home screen.
2758 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002759 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002760 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002761 if (mLauncherCallbacks != null) {
2762 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002763 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002764 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002765 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002766 }
2767
Adam Cohen61f560d2013-09-30 15:58:20 -07002768 public View.OnTouchListener getHapticFeedbackTouchListener() {
2769 if (mHapticFeedbackTouchListener == null) {
2770 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002771 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002772 @Override
2773 public boolean onTouch(View v, MotionEvent event) {
2774 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2775 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2776 }
2777 return false;
2778 }
2779 };
2780 }
2781 return mHapticFeedbackTouchListener;
2782 }
2783
Adam Cohen9211d422014-10-07 18:14:53 -07002784 public void onDragStarted(View view) {
2785 if (isOnCustomContent()) {
2786 // Custom content screen doesn't participate in drag and drop. If on custom
2787 // content screen, move to default.
2788 moveWorkspaceToDefaultScreen();
2789 }
2790
2791 if (mLauncherCallbacks != null) {
2792 mLauncherCallbacks.onDragStarted(view);
2793 }
2794 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002795
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002796 /**
2797 * Called when the user stops interacting with the launcher.
2798 * This implies that the user is now on the homescreen and is not doing housekeeping.
2799 */
Adam Cohen9211d422014-10-07 18:14:53 -07002800 protected void onInteractionEnd() {
2801 if (mLauncherCallbacks != null) {
2802 mLauncherCallbacks.onInteractionEnd();
2803 }
2804 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002805
2806 /**
2807 * Called when the user starts interacting with the launcher.
2808 * The possible interactions are:
2809 * - open all apps
2810 * - reorder an app shortcut, or a widget
2811 * - open the overview mode.
2812 * This is a good time to stop doing things that only make sense
2813 * when the user is on the homescreen and not doing housekeeping.
2814 */
Adam Cohen9211d422014-10-07 18:14:53 -07002815 protected void onInteractionBegin() {
2816 if (mLauncherCallbacks != null) {
2817 mLauncherCallbacks.onInteractionBegin();
2818 }
2819 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002820
Winson Chungcd99cd32015-04-29 11:03:24 -07002821 /** Updates the interaction state. */
2822 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002823 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002824 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002825 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2826 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002827 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002828 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002829 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002830 onInteractionEnd();
2831 }
2832 }
2833
Kenny Guyf07af7b2014-07-31 11:39:16 +01002834 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002835 try {
2836 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002837 launcherApps.showAppDetailsForProfile(componentName, user);
2838 } catch (SecurityException e) {
2839 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2840 Log.e(TAG, "Launcher does not have permission to launch settings");
2841 } catch (ActivityNotFoundException e) {
2842 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2843 Log.e(TAG, "Unable to launch settings");
2844 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002845 }
2846
Michael Jurka1e2f4652013-07-08 18:03:46 -07002847 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002848 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2849 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002850 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002851 // System applications cannot be installed. For now, show a toast explaining that.
2852 // We may give them the option of disabling apps this way.
2853 int messageId = R.string.uninstall_system_app_text;
2854 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002855 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002856 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002857 String packageName = componentName.getPackageName();
2858 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002859 Intent intent = new Intent(
2860 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002861 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2862 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002863 if (user != null) {
2864 user.addToIntent(intent, Intent.EXTRA_USER);
2865 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002866 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002867 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002868 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002869 }
2870
Winson Chung8f1eff72015-05-28 17:33:40 -07002871 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002872 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002874 // Only launch using the new animation if the shortcut has not opted out (this is a
2875 // private contract between launcher and may be ignored in the future).
2876 boolean useLaunchAnimation = (v != null) &&
2877 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002878 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2879 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002880
Kenny Guy1317e2d2014-05-08 18:52:50 +01002881 UserHandleCompat user = null;
2882 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2883 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2884 user = userManager.getUserForSerialNumber(serialNumber);
2885 }
2886
2887 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002888 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002889 ActivityOptions opts = null;
2890 if (sClipRevealMethod != null) {
2891 // TODO: call method directly when Launcher3 can depend on M APIs
2892 int left = 0, top = 0;
2893 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2894 if (v instanceof TextView) {
2895 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002896 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2897 if (icon != null) {
2898 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002899 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002900 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002901 width = bounds.width();
2902 height = bounds.height();
2903 }
2904 }
2905 try {
2906 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2907 left, top, width, height);
2908 } catch (IllegalAccessException e) {
2909 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2910 sClipRevealMethod = null;
2911 } catch (InvocationTargetException e) {
2912 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2913 sClipRevealMethod = null;
2914 }
2915 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002916 if (opts == null && !Utilities.isLmpOrAbove()) {
2917 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002918 v.getMeasuredWidth(), v.getMeasuredHeight());
2919 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002920 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002921 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002922
2923 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2924 // Could be launching some bookkeeping activity
2925 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002926 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002927 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002928 launcherApps.startActivityForProfile(intent.getComponent(), user,
2929 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002930 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002931 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 } catch (SecurityException e) {
2933 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002934 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002936 "or use the exported attribute for this activity. "
2937 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002939 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002941
Winson Chung8f1eff72015-05-28 17:33:40 -07002942 private boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002943 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002944 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2945 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2946 return false;
2947 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002948 try {
2949 success = startActivity(v, intent, tag);
2950 } catch (ActivityNotFoundException e) {
2951 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2952 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2953 }
2954 return success;
2955 }
2956
Adam Cohen268c4752012-06-06 17:47:33 -07002957 /**
2958 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2959 * in the DragLayer in the exact absolute location of the original FolderIcon.
2960 */
2961 private void copyFolderIconToImage(FolderIcon fi) {
2962 final int width = fi.getMeasuredWidth();
2963 final int height = fi.getMeasuredHeight();
2964
2965 // Lazy load ImageView, Bitmap and Canvas
2966 if (mFolderIconImageView == null) {
2967 mFolderIconImageView = new ImageView(this);
2968 }
2969 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2970 mFolderIconBitmap.getHeight() != height) {
2971 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2972 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2973 }
2974
2975 DragLayer.LayoutParams lp;
2976 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2977 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2978 } else {
2979 lp = new DragLayer.LayoutParams(width, height);
2980 }
2981
Adam Cohen307fe232012-08-16 17:55:58 -07002982 // The layout from which the folder is being opened may be scaled, adjust the starting
2983 // view size by this scale factor.
2984 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002985 lp.customPosition = true;
2986 lp.x = mRectForFolderAnimation.left;
2987 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002988 lp.width = (int) (scale * width);
2989 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002990
2991 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2992 fi.draw(mFolderIconCanvas);
2993 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002994 if (fi.getFolder() != null) {
2995 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2996 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002997 }
Adam Cohen268c4752012-06-06 17:47:33 -07002998 // Just in case this image view is still in the drag layer from a previous animation,
2999 // we remove it and re-add it.
3000 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3001 mDragLayer.removeView(mFolderIconImageView);
3002 }
3003 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003004 if (fi.getFolder() != null) {
3005 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003006 }
Adam Cohen268c4752012-06-06 17:47:33 -07003007 }
3008
Adam Cohen2801caf2011-05-13 20:57:39 -07003009 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003010 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003011 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3012 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3013 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3014
Adam Cohenc51934b2011-07-26 21:07:43 -07003015 FolderInfo info = (FolderInfo) fi.getTag();
3016 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3017 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003018 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3019 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003020 }
3021
Adam Cohen268c4752012-06-06 17:47:33 -07003022 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3023 copyFolderIconToImage(fi);
3024 fi.setVisibility(View.INVISIBLE);
3025
Michael Jurka2ecf9952012-06-18 12:52:28 -07003026 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003027 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003028 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003029 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3030 }
3031 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003032 oa.start();
3033 }
3034
Adam Cohen268c4752012-06-06 17:47:33 -07003035 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003036 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003037 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3038 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3039 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3040
Adam Cohen268c4752012-06-06 17:47:33 -07003041 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003042
Adam Cohen268c4752012-06-06 17:47:33 -07003043 // We remove and re-draw the FolderIcon in-case it has changed
3044 mDragLayer.removeView(mFolderIconImageView);
3045 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003046 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003047 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003048 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003049 oa.addListener(new AnimatorListenerAdapter() {
3050 @Override
3051 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003052 if (cl != null) {
3053 cl.clearFolderLeaveBehind();
3054 // Remove the ImageView copy of the FolderIcon and make the original visible.
3055 mDragLayer.removeView(mFolderIconImageView);
3056 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003057 }
3058 }
3059 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003060 oa.start();
3061 }
3062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003063 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003064 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065 * is animated relative to the specified View. If the View is null, no animation
3066 * is played.
3067 *
3068 * @param folderInfo The FolderInfo describing the folder to open.
3069 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003070 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003071 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003072 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3073 if (openFolder != null && openFolder != folder) {
3074 // Close any open folder before opening a folder.
3075 closeFolder();
3076 }
3077
Adam Cohena9cf38f2011-05-02 15:36:58 -07003078 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003079
Adam Cohena9cf38f2011-05-02 15:36:58 -07003080 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003081
Sunny Goyalf4066152015-04-15 09:42:19 -07003082 // While the folder is open, the position of the icon cannot change.
3083 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3084
Adam Cohen4554ee12011-08-03 16:13:21 -07003085 // Just verify that the folder hasn't already been added to the DragLayer.
3086 // There was a one-off crash where the folder had a parent already.
3087 if (folder.getParent() == null) {
3088 mDragLayer.addView(folder);
3089 mDragController.addDropTarget((DropTarget) folder);
3090 } else {
3091 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3092 folder.getParent() + ").");
3093 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003094 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003095 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003096
3097 // Notify the accessibility manager that this folder "window" has appeared and occluded
3098 // the workspace items
3099 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3100 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003101 }
3102
3103 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003104 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003105 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003106 if (folder.isEditingName()) {
3107 folder.dismissEditingName();
3108 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003109 closeFolder(folder);
3110 }
3111 }
3112
Sunny Goyal83a8f042015-05-19 12:52:12 -07003113 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003114 folder.getInfo().opened = false;
3115
3116 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3117 if (parent != null) {
3118 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3119 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003120 if (fi != null) {
3121 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3122 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003123 }
3124 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003125
3126 // Notify the accessibility manager that this folder "window" has disappeard and no
3127 // longer occludeds the workspace items
3128 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129 }
3130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003131 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003132 if (!isDraggingEnabled()) return false;
3133 if (isWorkspaceLocked()) return false;
3134 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135
Adam Cohen1697b792013-09-17 19:08:21 -07003136 if (v instanceof Workspace) {
3137 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003138 if (!mWorkspace.isTouchActive()) {
3139 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003140 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3141 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3142 return true;
3143 } else {
3144 return false;
3145 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003146 } else {
3147 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003148 }
Adam Cohen1697b792013-09-17 19:08:21 -07003149 }
3150
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003151 CellLayout.CellInfo longClickCellInfo = null;
3152 View itemUnderLongClick = null;
3153 if (v.getTag() instanceof ItemInfo) {
3154 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003155 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003156 itemUnderLongClick = longClickCellInfo.cell;
3157 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003158 }
3159
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 // The hotseat touch handling does not go through Workspace, and we always allow long press
3161 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003162 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003163 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003164 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003165 // User long pressed on empty space
3166 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3167 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003168 if (mWorkspace.isInOverviewMode()) {
3169 mWorkspace.startReordering(v);
3170 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003171 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003174 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3175 mHotseat.getOrderInHotseat(
3176 longClickCellInfo.cellX,
3177 longClickCellInfo.cellY));
3178 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003179 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003180 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 }
3182 }
3183 }
3184 return true;
3185 }
3186
Winson Chung3d503fb2011-07-13 17:25:49 -07003187 boolean isHotseatLayout(View layout) {
3188 return mHotseat != null && layout != null &&
3189 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3190 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003191
Winson Chung3d503fb2011-07-13 17:25:49 -07003192 /**
3193 * Returns the CellLayout of the specified container at the specified screen.
3194 */
Sunny Goyal92820592015-03-02 11:31:35 -08003195 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003196 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3197 if (mHotseat != null) {
3198 return mHotseat.getLayout();
3199 } else {
3200 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003201 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003202 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003203 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003204 }
3205 }
3206
Winson Chungb745afb2015-03-02 11:51:23 -08003207 /**
3208 * For overridden classes.
3209 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003210 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003211 return isAppsViewVisible();
3212 }
3213
3214 public boolean isAppsViewVisible() {
3215 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3216 }
3217
3218 public boolean isWidgetsViewVisible() {
3219 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003220 }
3221
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003222 private void setWorkspaceBackground(int background) {
3223 switch (background) {
3224 case WORKSPACE_BACKGROUND_TRANSPARENT:
3225 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3226 break;
3227 case WORKSPACE_BACKGROUND_BLACK:
3228 getWindow().setBackgroundDrawable(null);
3229 break;
3230 default:
3231 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3232 }
Craig Mautner360310b2012-10-26 15:13:08 -07003233 }
3234
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003235 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003236 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3237 int curflags = getWindow().getAttributes().flags
3238 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3239 if (wpflags != curflags) {
3240 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3241 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003242 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003243 }
3244
Michael Jurkae326f182011-11-21 14:05:46 -08003245 @Override
3246 public void onTrimMemory(int level) {
3247 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003248 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3249 // The widget preview db can result in holding onto over
3250 // 3MB of memory for caching which isn't necessary.
3251 SQLiteDatabase.releaseMemory();
3252
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003253 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003254 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003255 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003256 if (mLauncherCallbacks != null) {
3257 mLauncherCallbacks.onTrimMemory(level);
3258 }
Michael Jurkae326f182011-11-21 14:05:46 -08003259 }
3260
Winson Chungb745afb2015-03-02 11:51:23 -08003261 @Override
3262 public void onStateTransitionHideSearchBar() {
3263 // Hide the search bar
3264 if (mSearchDropTargetBar != null) {
3265 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3266 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003267 }
3268
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003269 public void showWorkspace(boolean animated) {
Winson Chungef7f8742015-06-04 17:18:17 -07003270 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003271 }
3272
Sunny Goyal83a8f042015-05-19 12:52:12 -07003273 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003274 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
Winson Chungef7f8742015-06-04 17:18:17 -07003275 onCompleteRunnable);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003276 }
3277
3278 protected void showWorkspace(int snapToPage, boolean animated) {
Winson Chungef7f8742015-06-04 17:18:17 -07003279 showWorkspace(snapToPage, animated, null);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003280 }
3281
Winson Chungef7f8742015-06-04 17:18:17 -07003282 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003283 boolean changed = mState != State.WORKSPACE ||
3284 mWorkspace.getState() != Workspace.State.NORMAL;
3285 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003286 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003287 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003288 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003289 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003290
Winson Chungc7d2b602012-05-16 17:02:20 -07003291 // Show the search bar (only animate if we were showing the drop target bar in spring
3292 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003293 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003294 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003295 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003296
Michael Jurkab3e22d92011-10-31 15:58:33 -07003297 // Set focus to the AppsCustomize button
3298 if (mAllAppsButton != null) {
3299 mAllAppsButton.requestFocus();
3300 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003301 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003302
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003303 // Change the state *after* we've called all the transition code
3304 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003305
Winson Chung5fb63472011-02-02 17:03:37 -08003306 // Resume the auto-advance of widgets
3307 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003308 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003309
Winson Chung0f785722015-04-08 10:27:49 -07003310 if (changed) {
3311 // Send an accessibility event to announce the context change
3312 getWindow().getDecorView()
3313 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003314 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003315 }
3316
Adam Cohened307df2013-10-02 09:37:31 -07003317 void showOverviewMode(boolean animated) {
3318 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003319 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003320 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3321 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003322 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003323 }
3324
Winson Chungb745afb2015-03-02 11:51:23 -08003325 /**
3326 * Shows the apps view.
3327 */
Winson Chung4ac30062015-05-08 17:34:17 -07003328 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003329 if (resetListToTop) {
3330 mAppsView.scrollToTop();
3331 }
Winson Chung4ac30062015-05-08 17:34:17 -07003332 if (updatePredictedApps) {
3333 tryAndUpdatePredictedApps();
3334 }
Winson Chungb745afb2015-03-02 11:51:23 -08003335 showAppsOrWidgets(animated, State.APPS);
3336 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003337
Winson Chungb745afb2015-03-02 11:51:23 -08003338 /**
3339 * Shows the widgets view.
3340 */
3341 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003342 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003343 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003344 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003345 }
Winson Chungb745afb2015-03-02 11:51:23 -08003346 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003347
3348 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003349 @Override
3350 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003351 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003352 }
3353 });
Winson Chungb745afb2015-03-02 11:51:23 -08003354 }
3355
3356 /**
3357 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003358 *
3359 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003360 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003361 // TODO: calling method should use the return value so that when {@code false} is returned
3362 // the workspace transition doesn't fall into invalid state.
3363 private boolean showAppsOrWidgets(boolean animated, State toState) {
3364 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3365 mState != State.WIDGETS_SPRING_LOADED) {
3366 return false;
3367 }
3368 if (toState != State.APPS && toState != State.WIDGETS) {
3369 return false;
3370 }
Winson Chungb745afb2015-03-02 11:51:23 -08003371
3372 if (toState == State.APPS) {
Winson Chungef7f8742015-06-04 17:18:17 -07003373 mStateTransitionAnimation.startAnimationToAllApps(mState, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003374 } else {
3375 mStateTransitionAnimation.startAnimationToWidgets(animated);
3376 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003377
Michael Jurkab3e22d92011-10-31 15:58:33 -07003378 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003379 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003380
3381 // Pause the auto-advance of widgets until we are out of AllApps
3382 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003383 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003384 closeFolder();
3385
3386 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003387 getWindow().getDecorView()
3388 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003389 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003390 }
3391
Winson Chungcd99cd32015-04-29 11:03:24 -07003392 /**
3393 * Updates the workspace and interaction state on state change, and return the animation to this
3394 * new state.
3395 */
3396 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
Winson Chungef7f8742015-06-04 17:18:17 -07003397 boolean animated, boolean hasOverlaySearchBar, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003398 Workspace.State fromState = mWorkspace.getState();
Winson Chungef7f8742015-06-04 17:18:17 -07003399 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated,
3400 hasOverlaySearchBar, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003401 updateInteraction(fromState, toState);
3402 return anim;
3403 }
3404
Hyunyoung Song3f471442015-04-08 19:01:34 -07003405 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003406 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003407 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3408 mState == State.WIDGETS_SPRING_LOADED) {
3409 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003410 }
Winson Chungb745afb2015-03-02 11:51:23 -08003411
3412 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003413 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3414 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003415 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003416 }
Adam Cohen7777d962011-08-18 18:58:38 -07003417
Hyunyoung Song3f471442015-04-08 19:01:34 -07003418 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003419 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003420 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003421
Winson Chunge7a03942011-08-05 15:05:12 -07003422 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003423 @Override
3424 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003425 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003426 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3427 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003428 // Before we show workspace, hide all apps again because
3429 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3430 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003431 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003432 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003433 } else {
3434 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003435 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003436 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003437 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003438 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003439
Michael Jurkad3ef3062010-11-23 16:23:58 -08003440 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003441 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003442 showAppsView(true /* animated */, false /* resetListToTop */,
3443 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003444 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003445 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003446 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003447 }
3448
Winson Chung4ac30062015-05-08 17:34:17 -07003449 /**
3450 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3451 * resumed.
3452 */
3453 private void tryAndUpdatePredictedApps() {
3454 if (mLauncherCallbacks != null) {
3455 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3456 if (!apps.isEmpty()) {
3457 mAppsView.setPredictedApps(apps);
3458 }
3459 }
3460 }
3461
Michael Jurkab3e22d92011-10-31 15:58:33 -07003462 void lockAllApps() {
3463 // TODO
3464 }
3465
3466 void unlockAllApps() {
3467 // TODO
3468 }
3469
Sunny Goyal594d76d2014-11-06 10:12:54 -08003470 protected void disableVoiceButtonProxy(boolean disable) {
3471 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003472 }
3473
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003474 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003475 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3476 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003477 }
3478
Adam Cohen24ce0b32014-01-14 16:18:14 -08003479 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003480 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3481 if (searchProvider == null) {
3482 return null;
3483 }
3484
3485 Bundle opts = new Bundle();
3486 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003487 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003488
3489 SharedPreferences sp = getSharedPreferences(
3490 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3491 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003492 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003493 if (!searchProvider.provider.flattenToString().equals(
3494 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003495 || (widgetInfo == null)
3496 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003497 // A valid widget is not already bound.
3498 if (widgetId > -1) {
3499 mAppWidgetHost.deleteAppWidgetId(widgetId);
3500 widgetId = -1;
3501 }
3502
3503 // Try to bind a new widget
3504 widgetId = mAppWidgetHost.allocateAppWidgetId();
3505
3506 if (!AppWidgetManagerCompat.getInstance(this)
3507 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3508 mAppWidgetHost.deleteAppWidgetId(widgetId);
3509 widgetId = -1;
3510 }
3511
3512 sp.edit()
3513 .putInt(QSB_WIDGET_ID, widgetId)
3514 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3515 .commit();
3516 }
3517
3518 if (widgetId != -1) {
3519 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3520 mQsb.updateAppWidgetOptions(opts);
3521 mQsb.setPadding(0, 0, 0, 0);
3522 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003523 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003524 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003525 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003526 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003527 }
3528
Sunny Goyal22235bc2015-04-03 09:23:43 -07003529 private void reinflateQSBIfNecessary() {
3530 if (mQsb instanceof LauncherAppWidgetHostView &&
3531 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3532 mSearchDropTargetBar.removeView(mQsb);
3533 mQsb = null;
3534 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3535 }
3536 }
3537
Michael Jurkad7c28052012-04-27 15:43:36 -07003538 @Override
3539 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003540 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003541 final List<CharSequence> text = event.getText();
3542 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003543 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003544 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003545 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003546 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003547 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003548 } else {
3549 text.add(getString(R.string.all_apps_home_button_label));
3550 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003551 return result;
3552 }
3553
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003554 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003555 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003556 */
Adam Cohen091440a2015-03-18 14:16:05 -07003557 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003558 @Override
3559 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003560 closeSystemDialogs();
3561 }
3562 }
3563
3564 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003565 * Receives notifications whenever the appwidgets are reset.
3566 */
3567 private class AppWidgetResetObserver extends ContentObserver {
3568 public AppWidgetResetObserver() {
3569 super(new Handler());
3570 }
3571
3572 @Override
3573 public void onChange(boolean selfChange) {
3574 onAppWidgetReset();
3575 }
3576 }
3577
3578 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003579 * If the activity is currently paused, signal that we need to run the passed Runnable
3580 * in onResume.
3581 *
3582 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003583 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3584 * wrong when we're not running, and if the activity comes back to what the configuration was
3585 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003586 *
3587 * Implementation of the method from LauncherModel.Callbacks.
3588 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003589 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003590 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003591 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003592 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003593 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003594 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003595 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003596 }
3597 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003598 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003599 return true;
3600 } else {
3601 return false;
3602 }
3603 }
3604
Michael Jurkac402cd92013-05-20 15:49:32 +02003605 private boolean waitUntilResume(Runnable run) {
3606 return waitUntilResume(run, false);
3607 }
3608
Michael Jurka1e2f4652013-07-08 18:03:46 -07003609 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003610 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003611 }
3612
Michael Jurka7607c2f2013-04-03 14:33:19 -07003613 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003614 * If the activity is currently paused, signal that we need to re-run the loader
3615 * in onResume.
3616 *
3617 * This needs to be called from incoming places where resources might have been loaded
3618 * while we are paused. That is becaues the Configuration might be wrong
3619 * when we're not running, and if it comes back to what it was when we
3620 * were paused, we are not restarted.
3621 *
3622 * Implementation of the method from LauncherModel.Callbacks.
3623 *
3624 * @return true if we are currently paused. The caller might be able to
3625 * skip some work in that case since we will come back again.
3626 */
3627 public boolean setLoadOnResume() {
3628 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003629 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003630 mOnResumeNeedsLoad = true;
3631 return true;
3632 } else {
3633 return false;
3634 }
3635 }
3636
3637 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003639 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003640 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003641 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003642 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003643 } else {
3644 return SCREEN_COUNT / 2;
3645 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003646 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003647
Joe Onorato9c1289c2009-08-17 11:03:03 -04003648 /**
3649 * Refreshes the shortcuts shown on the workspace.
3650 *
3651 * Implementation of the method from LauncherModel.Callbacks.
3652 */
3653 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003654 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003655
Michael Jurka7607c2f2013-04-03 14:33:19 -07003656 // If we're starting binding all over again, clear any bind calls we'd postponed in
3657 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3658 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003659 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003660
Winson Chungd64d1762013-08-20 14:37:16 -07003661 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003662 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003663 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003664
Michael Jurka05bf644e2011-11-30 20:28:53 -08003665 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003666 if (mHotseat != null) {
3667 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003668 }
3669 }
3670
Adam Cohendcd297f2013-06-18 13:13:40 -07003671 @Override
3672 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003673 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003674
Adam Cohen5084cba2013-09-03 12:01:16 -07003675 // If there are no screens, we need to have an empty screen
3676 if (orderedScreenIds.size() == 0) {
3677 mWorkspace.addExtraEmptyScreen();
3678 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003679
3680 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003681 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003682 if (hasCustomContentToLeft()) {
3683 mWorkspace.createCustomContentContainer();
3684 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003685 }
Winson Chung64359a52013-07-08 17:17:08 -07003686 }
3687
3688 @Override
3689 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003690 int count = orderedScreenIds.size();
3691 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003692 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003693 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003694 }
3695
Adam Cohen39a06042013-07-19 14:30:12 -07003696 private boolean shouldShowWeightWatcher() {
3697 String spKey = LauncherAppState.getSharedPreferencesKey();
3698 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003699 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003700
3701 return show;
3702 }
3703
3704 private void toggleShowWeightWatcher() {
3705 String spKey = LauncherAppState.getSharedPreferencesKey();
3706 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3707 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3708
3709 show = !show;
3710
3711 SharedPreferences.Editor editor = sp.edit();
3712 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3713 editor.commit();
3714
3715 if (mWeightWatcher != null) {
3716 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3717 }
3718 }
3719
Winson Chungd64d1762013-08-20 14:37:16 -07003720 public void bindAppsAdded(final ArrayList<Long> newScreens,
3721 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003722 final ArrayList<ItemInfo> addAnimated,
3723 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003724 Runnable r = new Runnable() {
3725 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003726 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003727 }
3728 };
3729 if (waitUntilResume(r)) {
3730 return;
3731 }
3732
Winson Chungd64d1762013-08-20 14:37:16 -07003733 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003734 if (newScreens != null) {
3735 bindAddScreens(newScreens);
3736 }
Winson Chungd64d1762013-08-20 14:37:16 -07003737
3738 // We add the items without animation on non-visible pages, and with
3739 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003740 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003741 bindItems(addNotAnimated, 0,
3742 addNotAnimated.size(), false);
3743 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003744 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003745 bindItems(addAnimated, 0,
3746 addAnimated.size(), true);
3747 }
Winson Chungc58497e2013-09-03 17:48:37 -07003748
Winson Chung87412982013-10-03 18:34:14 -07003749 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003750 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003751
Winson Chungb745afb2015-03-02 11:51:23 -08003752 if (addedApps != null && mAppsView != null) {
3753 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003754 }
Winson Chungd64d1762013-08-20 14:37:16 -07003755 }
3756
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003757 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003758 * Bind the items start-end from the list.
3759 *
3760 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003761 */
Winson Chung64359a52013-07-08 17:17:08 -07003762 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3763 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003764 Runnable r = new Runnable() {
3765 public void run() {
3766 bindItems(shortcuts, start, end, forceAnimateIcons);
3767 }
3768 };
3769 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003770 return;
3771 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003772
Winson Chungf0c6ae02012-03-21 16:10:31 -07003773 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003774 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3775 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003776 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003777 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003778 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003779 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003780 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003781
3782 // Short circuit if we are loading dock items for a configuration which has no dock
3783 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3784 mHotseat == null) {
3785 continue;
3786 }
3787
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 switch (item.itemType) {
3789 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3790 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003791 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003792 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003793
Winson Chung64359a52013-07-08 17:17:08 -07003794 /*
3795 * TODO: FIX collision case
3796 */
Winson Chungce376632013-07-11 16:12:41 -07003797 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3798 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3799 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003800 View v = cl.getChildAt(item.cellX, item.cellY);
3801 Object tag = v.getTag();
3802 String desc = "Collision while binding workspace item: " + item
3803 + ". Collides with " + tag;
3804 if (LauncherAppState.isDogfoodBuild()) {
3805 throw (new RuntimeException(desc));
3806 } else {
3807 Log.d(TAG, desc);
3808 }
Winson Chungce376632013-07-11 16:12:41 -07003809 }
Winson Chung64359a52013-07-08 17:17:08 -07003810 }
3811
Adam Cohendcd297f2013-06-18 13:13:40 -07003812 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3813 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003814 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003815 // Animate all the applications up now
3816 shortcut.setAlpha(0f);
3817 shortcut.setScaleX(0f);
3818 shortcut.setScaleY(0f);
3819 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003820 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003821 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003822 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003823 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003824 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003825 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003826 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003827 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3828 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003829 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003830 default:
3831 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003833 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003834
Winson Chung997a9232013-07-24 15:33:46 -07003835 if (animateIcons) {
3836 // Animate to the correct page
3837 if (newShortcutsScreenId > -1) {
3838 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003839 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003840 final Runnable startBounceAnimRunnable = new Runnable() {
3841 public void run() {
3842 anim.playTogether(bounceAnims);
3843 anim.start();
3844 }
3845 };
Winson Chung997a9232013-07-24 15:33:46 -07003846 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003847 // We post the animation slightly delayed to prevent slowdowns
3848 // when we are loading right after we return to launcher.
3849 mWorkspace.postDelayed(new Runnable() {
3850 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003851 if (mWorkspace != null) {
3852 mWorkspace.snapToPage(newScreenIndex);
3853 mWorkspace.postDelayed(startBounceAnimRunnable,
3854 NEW_APPS_ANIMATION_DELAY);
3855 }
Winson Chung94d67682013-09-25 16:29:40 -07003856 }
3857 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003858 } else {
3859 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003860 }
3861 }
Winson Chung64359a52013-07-08 17:17:08 -07003862 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003863 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003864 }
3865
Joe Onorato9c1289c2009-08-17 11:03:03 -04003866 /**
3867 * Implementation of the method from LauncherModel.Callbacks.
3868 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003869 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003870 Runnable r = new Runnable() {
3871 public void run() {
3872 bindFolders(folders);
3873 }
3874 };
3875 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003876 return;
3877 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003878 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003879 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003880
3881 /**
3882 * Add the views for a widget to the workspace.
3883 *
3884 * Implementation of the method from LauncherModel.Callbacks.
3885 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003886 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003887 Runnable r = new Runnable() {
3888 public void run() {
3889 bindAppWidget(item);
3890 }
3891 };
3892 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003893 return;
3894 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003895
Daniel Sandler843e8602010-06-07 14:59:01 -04003896 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3897 if (DEBUG_WIDGETS) {
3898 Log.d(TAG, "bindAppWidget: " + item);
3899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900 final Workspace workspace = mWorkspace;
3901
Adam Cohen59400422014-03-05 18:07:04 -08003902 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003903 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003904
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003905 if (!mIsSafeModeEnabled
3906 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3907 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003908 if (appWidgetInfo == null) {
3909 if (DEBUG_WIDGETS) {
3910 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3911 + " belongs to component " + item.providerName
3912 + ", as the povider is null");
3913 }
3914 LauncherModel.deleteItemFromDatabase(this, item);
3915 return;
3916 }
3917 // Note: This assumes that the id remap broadcast is received before this step.
3918 // If that is not the case, the id remap will be ignored and user may see the
3919 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07003920 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003921 pendingInfo.spanX = item.spanX;
3922 pendingInfo.spanY = item.spanY;
3923 pendingInfo.minSpanX = item.minSpanX;
3924 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003925 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003926 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003927
Sunny Goyalff572272014-07-23 13:58:07 -07003928 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003929 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3930 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003931
3932 // TODO consider showing a permission dialog when the widget is clicked.
3933 if (!success) {
3934 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3935 if (DEBUG_WIDGETS) {
3936 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3937 + " belongs to component " + item.providerName
3938 + ", as the launcher is unable to bing a new widget id");
3939 }
3940 LauncherModel.deleteItemFromDatabase(this, item);
3941 return;
3942 }
3943
3944 item.appWidgetId = newWidgetId;
3945
3946 // If the widget has a configure activity, it is still needs to set it up, otherwise
3947 // the widget is ready to go.
3948 item.restoreStatus = (appWidgetInfo.configure == null)
3949 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3950 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3951
3952 LauncherModel.updateItemInDatabase(this, item);
3953 }
3954
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003955 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003956 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003957 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003958 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3959 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003960 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003961
Sunny Goyal651077b2014-06-30 14:15:31 -07003962 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3963 } else {
3964 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003965 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3966 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003967 view.updateIcon(mIconCache);
3968 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003969 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003970 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003971 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003972
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003974 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003975
Adam Cohendcd297f2013-06-18 13:13:40 -07003976 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003978 if (!item.isCustomWidget()) {
3979 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3980 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003981
Joe Onorato9c1289c2009-08-17 11:03:03 -04003982 workspace.requestLayout();
3983
Daniel Sandler843e8602010-06-07 14:59:01 -04003984 if (DEBUG_WIDGETS) {
3985 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3986 + (SystemClock.uptimeMillis()-start) + "ms");
3987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003988 }
3989
Sunny Goyalff572272014-07-23 13:58:07 -07003990 /**
3991 * Restores a pending widget.
3992 *
3993 * @param appWidgetId The app widget id
3994 * @param cellInfo The position on screen where to create the widget.
3995 */
3996 private void completeRestoreAppWidget(final int appWidgetId) {
3997 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3998 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3999 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4000 return;
4001 }
4002
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004003 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004004 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4005
4006 mWorkspace.reinflateWidgetsIfNecessary();
4007 LauncherModel.updateItemInDatabase(this, info);
4008 }
4009
Adam Cohen1462de32012-07-24 22:34:36 -07004010 public void onPageBoundSynchronously(int page) {
4011 mSynchronouslyBoundPages.add(page);
4012 }
4013
Joe Onorato9c1289c2009-08-17 11:03:03 -04004014 /**
4015 * Callback saying that there aren't any more items to bind.
4016 *
4017 * Implementation of the method from LauncherModel.Callbacks.
4018 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004019 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004020 Runnable r = new Runnable() {
4021 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004022 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004023 }
4024 };
4025 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004026 return;
4027 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004028 if (mSavedState != null) {
4029 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004030 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004031 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004032 mSavedState = null;
4033 }
4034
Adam Cohen1462de32012-07-24 22:34:36 -07004035 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004037 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004038 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004039
4040 // If we received the result of any pending adds while the loader was running (e.g. the
4041 // widget configuration forced an orientation change), process them now.
4042 if (sPendingAddItem != null) {
4043 final long screenId = completeAdd(sPendingAddItem);
4044
4045 // TODO: this moves the user to the page where the pending item was added. Ideally,
4046 // the screen would be guaranteed to exist after bind, and the page would be set through
4047 // the workspace restore process.
4048 mWorkspace.post(new Runnable() {
4049 @Override
4050 public void run() {
4051 mWorkspace.snapToScreenId(screenId);
4052 }
4053 });
4054 sPendingAddItem = null;
4055 }
4056
Sunny Goyal756adbc2015-04-16 15:20:51 -07004057 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004058
4059 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004060 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004061 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004062 }
4063
Adam Cohen3ed316a2014-07-23 18:21:20 -07004064 private void sendLoadingCompleteBroadcastIfNecessary() {
4065 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4066 String permission =
4067 getResources().getString(R.string.receive_first_load_broadcast_permission);
4068 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4069 sendBroadcast(intent, permission);
4070 SharedPreferences.Editor editor = mSharedPrefs.edit();
4071 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4072 editor.apply();
4073 }
4074 }
4075
Winson Chunga0b7e862013-09-05 16:03:15 -07004076 public boolean isAllAppsButtonRank(int rank) {
4077 if (mHotseat != null) {
4078 return mHotseat.isAllAppsButtonRank(rank);
4079 }
4080 return false;
4081 }
4082
Winson Chunga2413752012-04-03 14:22:34 -07004083 private boolean canRunNewAppsAnimation() {
4084 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4085 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4086 }
4087
Winson Chungc9168342013-06-26 14:54:55 -07004088 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4089 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4090 PropertyValuesHolder.ofFloat("alpha", 1f),
4091 PropertyValuesHolder.ofFloat("scaleX", 1f),
4092 PropertyValuesHolder.ofFloat("scaleY", 1f));
4093 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4094 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004095 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004096 return bounceAnim;
4097 }
4098
Adam Cohen27772732013-11-11 14:00:56 +00004099 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004100 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004101 }
4102
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004103 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004104 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004105 }
4106
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004107 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004108 if (mSearchDropTargetBar == null) {
4109 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004110 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004111 if (mQsb != null) {
4112 mSearchDropTargetBar.removeView(mQsb);
4113 mQsb = null;
4114 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004115 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004116 }
4117
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004118 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004119 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4120 * multiple calls to bind the same list.)
4121 */
4122 @Thunk ArrayList<AppInfo> mTmpAppsList;
4123 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4124 public void run() {
4125 bindAllApplications(mTmpAppsList);
4126 mTmpAppsList = null;
4127 }
4128 };
4129
4130 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004131 * Add the icons for all apps.
4132 *
4133 * Implementation of the method from LauncherModel.Callbacks.
4134 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004135 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004136 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4137 mTmpAppsList = apps;
4138 return;
4139 }
4140
Winson Chungb745afb2015-03-02 11:51:23 -08004141 if (mAppsView != null) {
4142 mAppsView.setApps(apps);
4143 }
Adam Cohen9211d422014-10-07 18:14:53 -07004144 if (mLauncherCallbacks != null) {
4145 mLauncherCallbacks.bindAllApplications(apps);
4146 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004147 }
4148
4149 /**
4150 * A package was updated.
4151 *
4152 * Implementation of the method from LauncherModel.Callbacks.
4153 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004154 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004155 Runnable r = new Runnable() {
4156 public void run() {
4157 bindAppsUpdated(apps);
4158 }
4159 };
4160 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004161 return;
4162 }
4163
Winson Chungb745afb2015-03-02 11:51:23 -08004164 if (mAppsView != null) {
4165 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004166 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004167 }
4168
Sunny Goyal4390ace2014-10-13 11:33:11 -07004169 @Override
4170 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4171 Runnable r = new Runnable() {
4172 public void run() {
4173 bindWidgetsRestored(widgets);
4174 }
4175 };
4176 if (waitUntilResume(r)) {
4177 return;
4178 }
4179 mWorkspace.widgetsRestored(widgets);
4180 }
4181
Joe Onorato9c1289c2009-08-17 11:03:03 -04004182 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004183 * Some shortcuts were updated in the background.
4184 *
4185 * Implementation of the method from LauncherModel.Callbacks.
4186 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004187 @Override
4188 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4189 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004190 Runnable r = new Runnable() {
4191 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004192 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004193 }
4194 };
4195 if (waitUntilResume(r)) {
4196 return;
4197 }
4198
Sunny Goyal4390ace2014-10-13 11:33:11 -07004199 if (!updated.isEmpty()) {
4200 mWorkspace.updateShortcuts(updated);
4201 }
4202
4203 if (!removed.isEmpty()) {
4204 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4205 for (ShortcutInfo si : removed) {
4206 removedComponents.add(si.getTargetComponent());
4207 }
4208 mWorkspace.removeItemsByComponentName(removedComponents, user);
4209 // Notify the drag controller
4210 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004211 }
4212 }
4213
4214 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004215 * Update the state of a package, typically related to install state.
4216 *
4217 * Implementation of the method from LauncherModel.Callbacks.
4218 */
Sunny Goyale755d462014-07-22 13:48:29 -07004219 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004220 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4221 Runnable r = new Runnable() {
4222 public void run() {
4223 bindRestoreItemsChange(updates);
4224 }
4225 };
4226 if (waitUntilResume(r)) {
4227 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004228 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004229
Sunny Goyal756adbc2015-04-16 15:20:51 -07004230 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004231 }
4232
4233 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004234 * A package was uninstalled. We take both the super set of packageNames
4235 * in addition to specific applications to remove, the reason being that
4236 * this can be called when a package is updated as well. In that scenario,
4237 * we only remove specific components from the workspace, where as
4238 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004239 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004240 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004241 * Implementation of the method from LauncherModel.Callbacks.
4242 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004243 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004244 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004245 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004246 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004247 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004248 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004249 }
Winson Chungd64d1762013-08-20 14:37:16 -07004250 };
4251 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004252 return;
4253 }
4254
Sunny Goyal1a745e82014-10-02 15:58:31 -07004255 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004256 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4257 for (AppInfo info : appInfos) {
4258 removedComponents.add(info.componentName);
4259 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004260 if (!packageNames.isEmpty()) {
4261 mWorkspace.removeItemsByPackageName(packageNames, user);
4262 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004263 if (!removedComponents.isEmpty()) {
4264 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004265 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004266 // Notify the drag controller
4267 mDragController.onAppsRemoved(packageNames, removedComponents);
4268
Sunny Goyal1a745e82014-10-02 15:58:31 -07004269 } else {
4270 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004271 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004272
Winson Chungdf95eb12013-10-16 14:57:07 -07004273 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004274 if (mAppsView != null) {
4275 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004276 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004277 }
Joe Onoratobe386092009-11-17 17:32:16 -08004278
Michael Jurkac402cd92013-05-20 15:49:32 +02004279 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004280 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004281 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004282 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004283 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004284
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004285 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004286 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004287 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004288 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004289 return;
4290 }
4291
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004292 if (mWidgetsView != null && model != null) {
4293 mWidgetsView.addWidgets(model);
4294 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004295 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004296 }
4297
Winson Chung400438b2011-01-16 17:53:48 -08004298 private int mapConfigurationOriActivityInfoOri(int configOri) {
4299 final Display d = getWindowManager().getDefaultDisplay();
4300 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4301 switch (d.getRotation()) {
4302 case Surface.ROTATION_0:
4303 case Surface.ROTATION_180:
4304 // We are currently in the same basic orientation as the natural orientation
4305 naturalOri = configOri;
4306 break;
4307 case Surface.ROTATION_90:
4308 case Surface.ROTATION_270:
4309 // We are currently in the other basic orientation to the natural orientation
4310 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4311 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4312 break;
4313 }
4314
4315 int[] oriMap = {
4316 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4317 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4318 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4319 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4320 };
4321 // Since the map starts at portrait, we need to offset if this device's natural orientation
4322 // is landscape.
4323 int indexOffset = 0;
4324 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4325 indexOffset = 1;
4326 }
4327 return oriMap[(d.getRotation() + indexOffset) % 4];
4328 }
Adam Cohen446e9402011-09-15 18:21:21 -07004329
Winson Chung4b919f82012-05-01 10:44:08 -07004330 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004331 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004332 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4333 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4334 .getConfiguration().orientation));
4335 } else {
4336 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4337 }
Winson Chung4b919f82012-05-01 10:44:08 -07004338 }
4339 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004340
Winson Chung4b919f82012-05-01 10:44:08 -07004341 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004342 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004343 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004344 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004345 } else {
4346 mHandler.postDelayed(new Runnable() {
4347 public void run() {
4348 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4349 }
4350 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004351 }
Winson Chung4b919f82012-05-01 10:44:08 -07004352 }
Winson Chung400438b2011-01-16 17:53:48 -08004353 }
4354
Winson Chunge43a1e72014-01-15 10:33:02 -08004355 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004356 if (mLauncherCallbacks != null) {
4357 return mLauncherCallbacks.isLauncherPreinstalled();
4358 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004359 PackageManager pm = getPackageManager();
4360 try {
4361 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4362 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4363 return true;
4364 } else {
4365 return false;
4366 }
4367 } catch (NameNotFoundException e) {
4368 e.printStackTrace();
4369 return false;
4370 }
4371 }
4372
Adam Cohenea90f832014-05-21 15:03:34 -07004373 /**
4374 * This method indicates whether or not we should suggest default wallpaper dimensions
4375 * when our wallpaper cropper was not yet used to set a wallpaper.
4376 */
4377 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004378 if (mLauncherCallbacks != null) {
4379 return mLauncherCallbacks.overrideWallpaperDimensions();
4380 }
Adam Cohenea90f832014-05-21 15:03:34 -07004381 return true;
4382 }
4383
Adam Cohen432609a2014-03-13 17:03:22 -07004384 /**
4385 * To be overridden by subclasses to indicate that there is an activity to launch
4386 * before showing the standard launcher experience.
4387 */
4388 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004389 if (mLauncherCallbacks != null) {
4390 return mLauncherCallbacks.hasFirstRunActivity();
4391 }
Adam Cohen432609a2014-03-13 17:03:22 -07004392 return false;
4393 }
4394
4395 /**
4396 * To be overridden by subclasses to launch any first run activity
4397 */
4398 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004399 if (mLauncherCallbacks != null) {
4400 return mLauncherCallbacks.getFirstRunActivity();
4401 }
Adam Cohen432609a2014-03-13 17:03:22 -07004402 return null;
4403 }
4404
Winson Chunga6945242014-01-08 14:04:34 -08004405 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004406 return !ActivityManager.isRunningInTestHarness() &&
4407 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004408 }
4409
Adam Cohen4a9423d2014-03-28 14:22:31 -07004410 protected boolean hasRunFirstRunActivity() {
4411 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4412 }
4413
Adam Cohen432609a2014-03-13 17:03:22 -07004414 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004415 if (shouldRunFirstRunActivity() &&
4416 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004417 Intent firstRunIntent = getFirstRunActivity();
4418 if (firstRunIntent != null) {
4419 startActivity(firstRunIntent);
4420 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004421 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004422 }
4423 }
Adam Cohen432609a2014-03-13 17:03:22 -07004424 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004425 }
4426
4427 private void markFirstRunActivityShown() {
4428 SharedPreferences.Editor editor = mSharedPrefs.edit();
4429 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4430 editor.apply();
4431 }
4432
Adam Cohen432609a2014-03-13 17:03:22 -07004433 /**
4434 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4435 * screen that must be displayed and dismissed.
4436 */
4437 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004438 if (mLauncherCallbacks != null) {
4439 return mLauncherCallbacks.hasDismissableIntroScreen();
4440 }
Adam Cohen432609a2014-03-13 17:03:22 -07004441 return false;
4442 }
4443
4444 /**
4445 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4446 */
4447 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004448 if (mLauncherCallbacks != null) {
4449 return mLauncherCallbacks.getIntroScreen();
4450 }
Adam Cohen432609a2014-03-13 17:03:22 -07004451 return null;
4452 }
4453
4454 /**
4455 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4456 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4457 */
4458 private boolean shouldShowIntroScreen() {
4459 return hasDismissableIntroScreen() &&
4460 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4461 }
4462
4463 protected void showIntroScreen() {
4464 View introScreen = getIntroScreen();
4465 changeWallpaperVisiblity(false);
4466 if (introScreen != null) {
4467 mDragLayer.showOverlayView(introScreen);
4468 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004469 if (mLauncherOverlayContainer != null) {
4470 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4471 }
Adam Cohen432609a2014-03-13 17:03:22 -07004472 }
4473
4474 public void dismissIntroScreen() {
4475 markIntroScreenDismissed();
4476 if (showFirstRunActivity()) {
4477 // We delay hiding the intro view until the first run activity is showing. This
4478 // avoids a blip.
4479 mWorkspace.postDelayed(new Runnable() {
4480 @Override
4481 public void run() {
4482 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004483 if (mLauncherOverlayContainer != null) {
4484 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4485 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004486 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004487 }
4488 }, ACTIVITY_START_DELAY);
4489 } else {
4490 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004491 if (mLauncherOverlayContainer != null) {
4492 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4493 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004494 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004495 }
4496 changeWallpaperVisiblity(true);
4497 }
4498
4499 private void markIntroScreenDismissed() {
4500 SharedPreferences.Editor editor = mSharedPrefs.edit();
4501 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4502 editor.apply();
4503 }
4504
Adam Cohen091440a2015-03-18 14:16:05 -07004505 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004506 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4507 // on the device, then we always show the first run cling experience (or if there is no
4508 // launcher2). Otherwise, we prompt the user upon started for migration
4509 LauncherClings launcherClings = new LauncherClings(this);
4510 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4511 if (mModel.canMigrateFromOldLauncherDb(this)) {
4512 launcherClings.showMigrationCling();
4513 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004514 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004515 }
4516 }
4517 }
4518
Winson Chunga6945242014-01-08 14:04:34 -08004519 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004520 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4521 if (mHotseat != null) mHotseat.setAlpha(1f);
4522 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4523 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004524 }
4525
4526 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004527 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4528 if (mHotseat != null) mHotseat.setAlpha(0f);
4529 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4530 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004531 }
4532
Mathew Inwood72fbec12013-11-19 15:45:07 +00004533 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004534 // Called from search suggestion, not supported in other profiles.
4535 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4536 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4537 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4538 myUser);
4539 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004540 return null;
4541 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004542 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004543 }
4544
4545 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4546 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004547 // Called from search suggestion, not supported in other profiles.
4548 return createShortcutDragInfo(shortcutIntent, caption, icon,
4549 UserHandleCompat.myUserHandle());
4550 }
4551
4552 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4553 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004554 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004555 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004556 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004557 }
4558
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004559 protected void moveWorkspaceToDefaultScreen() {
4560 mWorkspace.moveToDefaultScreen(false);
4561 }
4562
Mathew Inwood72fbec12013-11-19 15:45:07 +00004563 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4564 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004565 mWorkspace.onExternalDragStartedWithItem(dragView);
4566 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004567 }
4568
Anjali Koppalf5d29132014-02-28 13:33:27 -08004569 @Override
4570 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004571 if (mLauncherCallbacks != null) {
4572 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4573 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004574 }
4575
Winson Chung80baf5a2010-08-09 16:03:15 -07004576 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004577 * Returns a FastBitmapDrawable with the icon, accurately sized.
4578 */
4579 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4580 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4581 d.setFilterBitmap(true);
4582 resizeIconDrawable(d);
4583 return d;
4584 }
4585
4586 /**
4587 * Resizes an icon drawable to the correct icon size.
4588 */
4589 public void resizeIconDrawable(Drawable icon) {
4590 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4591 }
4592
4593 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004594 * Prints out out state for debugging.
4595 */
4596 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004597 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004598 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004599 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4600 Log.d(TAG, "mRestoring=" + mRestoring);
4601 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4602 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004603 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004604 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004605 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004606
Daniel Sandler325dc232013-06-05 22:57:57 -04004607 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004608 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004609
4610 @Override
4611 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4612 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004613 synchronized (sDumpLogs) {
4614 writer.println(" ");
4615 writer.println("Debug logs: ");
4616 for (int i = 0; i < sDumpLogs.size(); i++) {
4617 writer.println(" " + sDumpLogs.get(i));
4618 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004619 }
Adam Cohen9211d422014-10-07 18:14:53 -07004620 if (mLauncherCallbacks != null) {
4621 mLauncherCallbacks.dump(prefix, fd, writer, args);
4622 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004623 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004624
4625 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004626 if (DEBUG_DUMP_LOG) {
4627 synchronized (sDumpLogs) {
4628 Log.d(TAG, "");
4629 Log.d(TAG, "*********************");
4630 Log.d(TAG, "Launcher debug logs: ");
4631 for (int i = 0; i < sDumpLogs.size(); i++) {
4632 Log.d(TAG, " " + sDumpLogs.get(i));
4633 }
4634 Log.d(TAG, "*********************");
4635 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004636 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004637 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004638 }
4639
4640 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004641 addDumpLog(tag, log, null, debugLog);
4642 }
4643
4644 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004645 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004646 if (e != null) {
4647 Log.d(tag, log, e);
4648 } else {
4649 Log.d(tag, log);
4650 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004651 }
Winson Chungede41292013-09-19 16:27:36 -07004652 if (DEBUG_DUMP_LOG) {
4653 sDateStamp.setTime(System.currentTimeMillis());
4654 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004655 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4656 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004657 }
Winson Chungede41292013-09-19 16:27:36 -07004658 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004659 }
4660
Adam Cohen59400422014-03-05 18:07:04 -08004661 public static CustomAppWidget getCustomAppWidget(String name) {
4662 return sCustomAppWidgets.get(name);
4663 }
4664
4665 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4666 return sCustomAppWidgets;
4667 }
4668
Winson Chungede41292013-09-19 16:27:36 -07004669 public void dumpLogsToLocalData() {
4670 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004671 new AsyncTask<Void, Void, Void>() {
4672 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004673 boolean success = false;
4674 sDateStamp.setTime(sRunStart);
4675 String FILENAME = sDateStamp.getMonth() + "-"
4676 + sDateStamp.getDay() + "_"
4677 + sDateStamp.getHours() + "-"
4678 + sDateStamp.getMinutes() + "_"
4679 + sDateStamp.getSeconds() + ".txt";
4680
4681 FileOutputStream fos = null;
4682 File outFile = null;
4683 try {
4684 outFile = new File(getFilesDir(), FILENAME);
4685 outFile.createNewFile();
4686 fos = new FileOutputStream(outFile);
4687 } catch (Exception e) {
4688 e.printStackTrace();
4689 }
4690 if (fos != null) {
4691 PrintWriter writer = new PrintWriter(fos);
4692
4693 writer.println(" ");
4694 writer.println("Debug logs: ");
4695 synchronized (sDumpLogs) {
4696 for (int i = 0; i < sDumpLogs.size(); i++) {
4697 writer.println(" " + sDumpLogs.get(i));
4698 }
4699 }
4700 writer.close();
4701 }
4702 try {
4703 if (fos != null) {
4704 fos.close();
4705 success = true;
4706 }
4707 } catch (IOException e) {
4708 e.printStackTrace();
4709 }
Michael Jurka43467462013-11-14 12:03:58 +01004710 return null;
Winson Chungede41292013-09-19 16:27:36 -07004711 }
Michael Jurka43467462013-11-14 12:03:58 +01004712 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004713 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004715}
Michael Jurka2763be32011-02-24 11:19:57 -08004716
Dan Sandlerd5024042014-01-09 15:01:33 -05004717interface DebugIntents {
4718 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4719 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004720}