blob: 13f7343c9423788951487f16571fd8322ea2dba3 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080095import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070096import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070097
Sunny Goyal651077b2014-06-30 14:15:31 -070098import com.android.launcher3.DropTarget.DragObject;
99import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyal83a8f042015-05-19 12:52:12 -0700100import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700101import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherActivityInfoCompat;
104import com.android.launcher3.compat.LauncherAppsCompat;
105import com.android.launcher3.compat.UserHandleCompat;
106import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700107import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700108import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700109import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700110import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700111import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700112import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700113
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.DataInputStream;
115import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700121import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800122import java.lang.reflect.InvocationTargetException;
123import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700124import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700126import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700127import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700129import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700130import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000131import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133/**
134 * Default launcher application.
135 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100136public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700137 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800138 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
139 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700140 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700141 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Winson Chung83f59ab2015-05-05 17:21:58 -0700143 // Temporary flag
144 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
145
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700147 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700148 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700150 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700151
Dan Sandlerd5024042014-01-09 15:01:33 -0500152 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700157 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700162 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
163 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
164 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
165
Mathew Inwood876a8462013-06-14 14:12:41 +0100166 /**
167 * IntentStarter uses request codes starting with this. This must be greater than all activity
168 * request codes used internally.
169 */
170 protected static final int REQUEST_LAST = 100;
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
173
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800174 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
177 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
185 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700186 // Type: int
187 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700189 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
190 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
192 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700193 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700195 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700196 // Type: int
197 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
198 // Type: int
199 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
200 // Type: parcelable
201 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000202 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800203 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000204 // Type: int[]
205 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Adam Cohen432609a2014-03-13 17:03:22 -0700207 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800208 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
209
Adam Cohen3ed316a2014-07-23 18:21:20 -0700210 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
211 static final String ACTION_FIRST_LOAD_COMPLETE =
212 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
213
Adam Cohen39a06042013-07-19 14:30:12 -0700214 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700215 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700216
Sunny Goyal594d76d2014-11-06 10:12:54 -0800217 private static final String QSB_WIDGET_ID = "qsb_widget_id";
218 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
219
Winson Chunga6945242014-01-08 14:04:34 -0800220 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
221
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700222 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800223 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700226
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700227 private boolean mIsSafeModeEnabled;
228
Adam Cohenc2d6e892014-10-16 09:49:24 -0700229 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
230 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700231 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700232
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700234 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
235 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700236 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000238 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
239 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
240
Winson Chunga2413752012-04-03 14:22:34 -0700241 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700242 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
243 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700245
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800246 private final BroadcastReceiver mCloseSystemDialogsReceiver
247 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800248 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private LayoutInflater mInflater;
251
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700253 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800254 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800256 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700257 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700258
Sunny Goyalffe83f12014-08-14 17:39:34 -0700259 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700261
Adam Cohen091440a2015-03-18 14:16:05 -0700262 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800263 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800264 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700265
Michael Jurka0280c3b2010-09-17 15:00:07 -0700266 private int[] mTmpAddItemCellCoordinates = new int[2];
267
Winson Chung3d503fb2011-07-13 17:25:49 -0700268 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800269 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700270
Michael Jurka838a4ca2011-02-07 13:33:06 -0800271 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700272
Winson Chungc51db6a2011-10-05 11:44:49 -0700273 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700274
275 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700276 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700277
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700278 // Main container view and the model for the widget tray screen.
279 @Thunk WidgetsContainerView mWidgetsView;
280 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700281
Winson Chungf0ea4d32011-06-06 14:27:16 -0700282 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800283 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700286 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
287 // scroll issues (because the workspace may not have been measured yet) and extra work.
288 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
289 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
291 private SpannableStringBuilder mDefaultKeySsb = null;
292
Adam Cohen091440a2015-03-18 14:16:05 -0700293 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800295 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 private boolean mRestoring;
297 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700298 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299
Michael Jurka1e2f4652013-07-08 18:03:46 -0700300 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700301 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 private Bundle mSavedInstanceState;
304
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800306 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700307 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800308 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700309 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800310 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400311
Adam Cohen091440a2015-03-18 14:16:05 -0700312 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700313
Sunny Goyale2df0622015-04-24 11:27:00 -0700314 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700315
Adam Cohen61f560d2013-09-30 15:58:20 -0700316 private View.OnTouchListener mHapticFeedbackTouchListener;
317
Adam Cohended9f8d2010-11-03 13:25:16 -0700318 // Related to the auto-advancing of widgets
319 private final int ADVANCE_MSG = 1;
320 private final int mAdvanceInterval = 20000;
321 private final int mAdvanceStagger = 250;
322 private long mAutoAdvanceSentTime;
323 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700324 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700325 new HashMap<View, AppWidgetProviderInfo>();
326
Winson Chung400438b2011-01-16 17:53:48 -0800327 // Determines how long to wait after a rotation before restoring the screen orientation to
328 // match the sensor state.
329 private final int mRestoreScreenOrientationDelay = 500;
330
Adam Cohen091440a2015-03-18 14:16:05 -0700331 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700332
Adam Cohen1462de32012-07-24 22:34:36 -0700333 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700334 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700335
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700336 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700337 static Date sDateStamp = new Date();
338 static DateFormat sDateFormat =
339 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
340 static long sRunStart = System.currentTimeMillis();
341 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700342
Winson Chung46353de2012-02-16 14:05:10 -0800343 // We only want to get the SharedPreferences once since it does an FS stat each time we get
344 // it from the context.
345 private SharedPreferences mSharedPrefs;
346
Winson Chungf0c6ae02012-03-21 16:10:31 -0700347 // Holds the page that we need to animate to, and the icon views that we need to animate up
348 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700349 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700350 private Bitmap mFolderIconBitmap;
351 private Canvas mFolderIconCanvas;
352 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700353
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700354 private DeviceProfile mDeviceProfile;
355
Winson Chung13eb5272015-05-11 16:30:13 -0700356 // This is set to the view that launched the activity that navigated the user away from
357 // launcher. Since there is no callback for when the activity has finished launching, enable
358 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800359 private BubbleTextView mWaitingForResume;
360
Adam Cohen59400422014-03-05 18:07:04 -0800361 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
362 new HashMap<String, CustomAppWidget>();
363
364 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
365 static {
366 if (ENABLE_CUSTOM_WIDGET_TEST) {
367 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
368 }
369 }
370
Chet Haasea8f996d2015-02-17 12:56:04 -0800371 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
372 private static Method sClipRevealMethod = null;
373 static {
374 Class<?> activityOptionsClass = ActivityOptions.class;
375 try {
376 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
377 View.class, int.class, int.class, int.class, int.class);
378 } catch (Exception e) {
379 // Earlier version
380 }
381 }
382
Adam Cohen091440a2015-03-18 14:16:05 -0700383 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700384 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700385 if (mWorkspace != null) {
386 mWorkspace.buildPageHardwareLayers();
387 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700388 }
389 };
390
Adam Cohendb364c32014-05-20 14:23:40 -0700391 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392
Adam Cohen091440a2015-03-18 14:16:05 -0700393 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800394 int requestCode;
395 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700396 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700397 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800398 int cellX;
399 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700400 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800401 }
402
Daniel Sandlerff02d492013-08-05 02:12:05 -0400403 private Stats mStats;
404
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700405 FocusIndicatorView mFocusHandler;
406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 @Override
408 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700409 if (DEBUG_STRICT_MODE) {
410 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
411 .detectDiskReads()
412 .detectDiskWrites()
413 .detectNetwork() // or .detectAll() for all detectable problems
414 .penaltyLog()
415 .build());
416 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
417 .detectLeakedSqlLiteObjects()
418 .detectLeakedClosableObjects()
419 .penaltyLog()
420 .penaltyDeath()
421 .build());
422 }
423
Adam Cohen9211d422014-10-07 18:14:53 -0700424 if (mLauncherCallbacks != null) {
425 mLauncherCallbacks.preOnCreate();
426 }
427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400429
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400430 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400431 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700432 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700433
Adam Cohen2e6da152015-05-06 11:42:25 -0700434 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700435 mDeviceProfile = getResources().getConfiguration().orientation
436 == Configuration.ORIENTATION_LANDSCAPE ?
437 app.getInvariantDeviceProfile().landscapeProfile
438 : app.getInvariantDeviceProfile().portraitProfile;
439
440 // TODO: Move this to icon cache.
441 Utilities.setIconSize(mDeviceProfile.iconSizePx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700442
443 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400444 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700445 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700446 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800447 mModel = app.setLauncher(this);
448 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700449
Joe Onorato41a12d22009-10-31 18:30:00 -0400450 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800452 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700453
Daniel Sandlerff02d492013-08-05 02:12:05 -0400454 mStats = new Stats(this);
455
Sunny Goyalffe83f12014-08-14 17:39:34 -0700456 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700457
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700458 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
459 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700460
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700461 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
462 // this also ensures that any synchronous binding below doesn't re-trigger another
463 // LauncherModel load.
464 mPaused = false;
465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200467 android.os.Debug.startMethodTracing(
468 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470
471 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700473
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700475 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800477 registerContentObservers();
478
Joe Onorato7c312c12009-08-13 21:36:53 -0700479 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 mSavedState = savedInstanceState;
482 restoreState(mSavedState);
483
484 if (PROFILE_STARTUP) {
485 android.os.Debug.stopMethodTracing();
486 }
487
488 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700489 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700490 // If the user leaves launcher, then we should just load items asynchronously when
491 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700492 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700493 } else {
494 // We only load the page synchronously if the user rotates (or triggers a
495 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700496 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 }
499
500 // For handling default keys
501 mDefaultKeySsb = new SpannableStringBuilder();
502 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800503
504 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
505 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800506
Adam Cohenf9426d52012-06-04 17:26:21 -0700507 // On large interfaces, we want the screen to auto-rotate based on the current orientation
508 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700509
Adam Cohen9211d422014-10-07 18:14:53 -0700510 if (mLauncherCallbacks != null) {
511 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700512 if (mLauncherCallbacks.hasLauncherOverlay()) {
513 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700514 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
515 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
516 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700517 mWorkspace.setLauncherOverlay(mLauncherOverlay);
518 }
Adam Cohen9211d422014-10-07 18:14:53 -0700519 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700520
521 if (shouldShowIntroScreen()) {
522 showIntroScreen();
523 } else {
524 showFirstRunActivity();
525 showFirstRunClings();
526 }
Adam Cohen9211d422014-10-07 18:14:53 -0700527 }
528
529 private LauncherCallbacks mLauncherCallbacks;
530
531 public void onPostCreate(Bundle savedInstanceState) {
532 super.onPostCreate(savedInstanceState);
533 if (mLauncherCallbacks != null) {
534 mLauncherCallbacks.onPostCreate(savedInstanceState);
535 }
536 }
537
538 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
539 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700540 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
541 @Override
542 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700543 if (LOGD) {
544 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
545 }
Winson Chung94804152015-05-08 13:06:44 -0700546 mAppsView.setFixedBounds(bounds);
547 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700548 }
549
550 @Override
551 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700552 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
553 // Dismiss All Apps if we aren't already paused/invisible
554 if (!mPaused) {
555 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
556 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
557 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700558 }
Winson Chung0f785722015-04-08 10:27:49 -0700559 }
560 });
Adam Cohen9211d422014-10-07 18:14:53 -0700561 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700562 }
563
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700564 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700565 public void onLauncherProviderChange() {
566 if (mLauncherCallbacks != null) {
567 mLauncherCallbacks.onLauncherProviderChange();
568 }
569 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700570
Adam Cohen9211d422014-10-07 18:14:53 -0700571 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700572 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700573 if (mLauncherCallbacks != null) {
574 return mLauncherCallbacks.hasCustomContentToLeft();
575 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700576 return false;
577 }
578
Dave Hawkeya8881582013-09-17 15:55:33 -0600579 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500580 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600581 * {@link #addToCustomContentPage}. This will only be invoked if
582 * {@link #hasCustomContentToLeft()} is {@code true}.
583 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500584 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700585 if (mLauncherCallbacks != null) {
586 mLauncherCallbacks.populateCustomContentContainer();
587 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600588 }
589
590 /**
591 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
592 * ensure the custom content page is added or removed if necessary.
593 */
594 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600595 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 // Not bound yet, wait for bindScreens to be called.
597 return;
598 }
599
600 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
601 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500602 mWorkspace.createCustomContentContainer();
603 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
605 mWorkspace.removeCustomContentPage();
606 }
607 }
608
Adam Cohen091440a2015-03-18 14:16:05 -0700609 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700610 if (sLocaleConfiguration == null) {
611 new AsyncTask<Void, Void, LocaleConfiguration>() {
612 @Override
613 protected LocaleConfiguration doInBackground(Void... unused) {
614 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
615 readConfiguration(Launcher.this, localeConfiguration);
616 return localeConfiguration;
617 }
618
619 @Override
620 protected void onPostExecute(LocaleConfiguration result) {
621 sLocaleConfiguration = result;
622 checkForLocaleChange(); // recursive, but now with a locale configuration
623 }
624 }.execute();
625 return;
626 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 final Configuration configuration = getResources().getConfiguration();
629
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700630 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 final String locale = configuration.locale.toString();
632
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700633 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 final int mcc = configuration.mcc;
635
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700636 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 final int mnc = configuration.mnc;
638
Romain Guy84f296c2009-11-04 15:00:44 -0800639 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640
Romain Guy84f296c2009-11-04 15:00:44 -0800641 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700642 sLocaleConfiguration.locale = locale;
643 sLocaleConfiguration.mcc = mcc;
644 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700645
Joe Onorato0589f0f2010-02-08 13:44:00 -0800646 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700647
648 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100649 new AsyncTask<Void, Void, Void>() {
650 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700651 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100652 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700653 }
Michael Jurka43467462013-11-14 12:03:58 +0100654 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700655 }
656 }
657
Adam Cohen091440a2015-03-18 14:16:05 -0700658 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700659 public String locale;
660 public int mcc = -1;
661 public int mnc = -1;
662 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700663
Adam Cohen091440a2015-03-18 14:16:05 -0700664 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700665 DataInputStream in = null;
666 try {
Helena Josol28db2802014-10-09 17:04:09 +0100667 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700668 configuration.locale = in.readUTF();
669 configuration.mcc = in.readInt();
670 configuration.mnc = in.readInt();
671 } catch (FileNotFoundException e) {
672 // Ignore
673 } catch (IOException e) {
674 // Ignore
675 } finally {
676 if (in != null) {
677 try {
678 in.close();
679 } catch (IOException e) {
680 // Ignore
681 }
682 }
683 }
684 }
685
Adam Cohen091440a2015-03-18 14:16:05 -0700686 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700687 DataOutputStream out = null;
688 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100689 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100690 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700691 out.writeUTF(configuration.locale);
692 out.writeInt(configuration.mcc);
693 out.writeInt(configuration.mnc);
694 out.flush();
695 } catch (FileNotFoundException e) {
696 // Ignore
697 } catch (IOException e) {
698 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100699 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700700 } finally {
701 if (out != null) {
702 try {
703 out.close();
704 } catch (IOException e) {
705 // Ignore
706 }
707 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 }
709 }
710
Anton Hanssona2f665f2013-09-26 12:18:32 +0100711 public Stats getStats() {
712 return mStats;
713 }
714
Bjorn Bringertc459e522013-06-07 19:36:01 +0100715 public LayoutInflater getInflater() {
716 return mInflater;
717 }
718
Hyunyoung Song3f471442015-04-08 19:01:34 -0700719 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700720 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
721 // that is subsequently removed from the workspace in startBinding().
722 return !mModel.isLoadingWorkspace();
723 }
724
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800725 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000726 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100727 if (Build.VERSION.SDK_INT >= 17) {
728 return View.generateViewId();
729 } else {
730 // View.generateViewId() is not available. The following fallback logic is a copy
731 // of its implementation.
732 for (;;) {
733 final int result = sNextGeneratedId.get();
734 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
735 int newValue = result + 1;
736 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
737 if (sNextGeneratedId.compareAndSet(result, newValue)) {
738 return result;
739 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000740 }
741 }
742 }
743
744 public int getViewIdForItem(ItemInfo info) {
745 // This cast is safe given the > 2B range for int.
746 int itemId = (int) info.id;
747 if (mItemIdToViewId.containsKey(itemId)) {
748 return mItemIdToViewId.get(itemId);
749 }
750 int viewId = generateViewId();
751 mItemIdToViewId.put(itemId, viewId);
752 return viewId;
753 }
754
755 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700756 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
757 * a configuration step, this allows the proper animations to run after other transitions.
758 */
Adam Cohendb364c32014-05-20 14:23:40 -0700759 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700760 long screenId = args.screenId;
761 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
762 // When the screen id represents an actual screen (as opposed to a rank) we make sure
763 // that the drop page actually exists.
764 screenId = ensurePendingDropLayoutExists(args.screenId);
765 }
Adam Cohendb364c32014-05-20 14:23:40 -0700766
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800767 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800768 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700769 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700770 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700771 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800772 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700773 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700774 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700775 case REQUEST_RECONFIGURE_APPWIDGET:
776 completeRestoreAppWidget(args.appWidgetId);
777 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800778 }
Michael Jurka27614d22012-04-02 06:40:22 -0700779 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
780 // if you turned the screen off and then back while in All Apps, Launcher would not
781 // return to the workspace. Clearing mAddInfo.container here fixes this issue
782 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700783 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800784 }
785
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800786 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700787 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700788 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700789 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700790 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800791 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700792
Adam Cohenad4e15c2013-10-17 16:21:35 -0700793 Runnable exitSpringLoaded = new Runnable() {
794 @Override
795 public void run() {
796 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
797 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
798 }
799 };
800
Michael Jurka8b805b12012-04-18 14:23:14 -0700801 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700802 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700803 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
804 if (resultCode == RESULT_CANCELED) {
805 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700806 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700808 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800809 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700810 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700811 }
812 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200813 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
814 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700815 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200816 }
817 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700818 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200819
Adam Cohened66b2b2012-01-23 17:28:51 -0800820 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700821 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700822
Adam Cohendb364c32014-05-20 14:23:40 -0700823 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800824 // We have special handling for widgets
825 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800826 final int appWidgetId;
827 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
828 : -1;
829 if (widgetId < 0) {
830 appWidgetId = pendingAddWidgetId;
831 } else {
832 appWidgetId = widgetId;
833 }
834
Adam Cohenad4e15c2013-10-17 16:21:35 -0700835 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800836 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700837 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
838 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700839 result = RESULT_CANCELED;
840 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700841 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700842 @Override
843 public void run() {
844 exitSpringLoadedDragModeDelayed(false, 0, null);
845 }
846 };
Adam Cohendb364c32014-05-20 14:23:40 -0700847 if (workspaceLocked) {
848 // No need to remove the empty screen if we're mid-binding, as the
849 // the bind will not add the empty screen.
850 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
851 } else {
852 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
853 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
854 }
Winson Chung5aaab772012-04-27 15:24:02 -0700855 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700856 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700857 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
858 // When the screen id represents an actual screen (as opposed to a rank)
859 // we make sure that the drop page actually exists.
860 mPendingAddInfo.screenId =
861 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
862 }
Adam Cohendb364c32014-05-20 14:23:40 -0700863 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
864
865 dropLayout.setDropPending(true);
866 final Runnable onComplete = new Runnable() {
867 @Override
868 public void run() {
869 completeTwoStageWidgetDrop(resultCode, appWidgetId);
870 dropLayout.setDropPending(false);
871 }
872 };
873 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
874 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
875 } else {
876 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
877 mPendingAddInfo);
878 sPendingAddItem = args;
879 }
Winson Chung5aaab772012-04-27 15:24:02 -0700880 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800881 return;
882 }
883
Sunny Goyalff572272014-07-23 13:58:07 -0700884 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
885 if (resultCode == RESULT_OK) {
886 // Update the widget view.
887 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
888 pendingAddWidgetId, mPendingAddInfo);
889 if (workspaceLocked) {
890 sPendingAddItem = args;
891 } else {
892 completeAdd(args);
893 }
894 }
895 // Leave the widget in the pending state if the user canceled the configure.
896 return;
897 }
898
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 // The pattern used here is that a user PICKs a specific application,
900 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700901
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
903 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700904 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700905 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
906 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800907 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700908 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800909 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700910 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700911 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
912 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 }
Adam Cohen00074722013-10-11 17:46:09 -0700914 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700915 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700916 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800919
920 }
921
922 @Override
923 protected void onActivityResult(
924 final int requestCode, final int resultCode, final Intent data) {
925 handleActivityResult(requestCode, resultCode, data);
926 if (mLauncherCallbacks != null) {
927 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
928 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 }
930
Adam Cohendb364c32014-05-20 14:23:40 -0700931 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
932 appWidgetId, ItemInfo info) {
933 PendingAddArguments args = new PendingAddArguments();
934 args.requestCode = requestCode;
935 args.intent = data;
936 args.container = info.container;
937 args.screenId = info.screenId;
938 args.cellX = info.cellX;
939 args.cellY = info.cellY;
940 args.appWidgetId = appWidgetId;
941 return args;
942 }
943
944 /**
945 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
946 *
947 * @param screenId the screen id to check
948 * @return the new screen, or screenId if it exists
949 */
950 private long ensurePendingDropLayoutExists(long screenId) {
951 CellLayout dropLayout =
952 (CellLayout) mWorkspace.getScreenWithId(screenId);
953 if (dropLayout == null) {
954 // it's possible that the add screen was removed because it was
955 // empty and a re-bind occurred
956 mWorkspace.addExtraEmptyScreen();
957 return mWorkspace.commitExtraEmptyScreen();
958 } else {
959 return screenId;
960 }
961 }
962
Adam Cohen091440a2015-03-18 14:16:05 -0700963 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700964 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700965 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800966 Runnable onCompleteRunnable = null;
967 int animationType = 0;
968
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700969 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800970 if (resultCode == RESULT_OK) {
971 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
972 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700973 mPendingAddWidgetInfo);
974 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800975 onCompleteRunnable = new Runnable() {
976 @Override
977 public void run() {
978 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700979 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700980 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
981 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800982 }
983 };
984 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800985 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800986 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800987 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700988 if (mDragLayer.getAnimatedView() != null) {
989 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
990 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
991 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700992 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700993 // The animated view may be null in the case of a rotation during widget configuration
994 onCompleteRunnable.run();
995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997
998 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700999 protected void onStop() {
1000 super.onStop();
1001 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -07001002
1003 if (mLauncherCallbacks != null) {
1004 mLauncherCallbacks.onStop();
1005 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001006 }
1007
1008 @Override
1009 protected void onStart() {
1010 super.onStart();
1011 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -07001012
1013 if (mLauncherCallbacks != null) {
1014 mLauncherCallbacks.onStart();
1015 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001016 }
1017
1018 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001020 long startTime = 0;
1021 if (DEBUG_RESUME_TIME) {
1022 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001023 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001024 }
Adam Cohen9211d422014-10-07 18:14:53 -07001025
1026 if (mLauncherCallbacks != null) {
1027 mLauncherCallbacks.preOnResume();
1028 }
1029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001031
Winson Chung4a2afa32012-07-19 14:53:05 -07001032 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001033 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001034 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001035 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001036 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001037 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001038 // view after launching an app, as they may be depending on the UI to be static to
1039 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001040 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001041 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001042 } else if (mOnResumeState == State.WIDGETS) {
1043 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001044 }
1045 mOnResumeState = State.NONE;
1046
Winson Chungcd99cd32015-04-29 11:03:24 -07001047 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001048 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1049 // Otherwise, notify the callbacks if we are in all apps mode
1050 if (mState == State.APPS) {
1051 if (mLauncherCallbacks != null) {
1052 mLauncherCallbacks.onAllAppsShown();
1053 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001054 }
1055 }
1056
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001057 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001058 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1059 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001060
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001061 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001062 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001063 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001064 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001066 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001068 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001069 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1070 // execute them here
1071 long startTimeCallbacks = 0;
1072 if (DEBUG_RESUME_TIME) {
1073 startTimeCallbacks = System.currentTimeMillis();
1074 }
1075
Michael Jurka1e2f4652013-07-08 18:03:46 -07001076 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1077 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001078 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001079 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001080 if (DEBUG_RESUME_TIME) {
1081 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1082 (System.currentTimeMillis() - startTimeCallbacks));
1083 }
Michael Jurka447bf842013-05-15 14:52:15 +02001084 }
Michael Jurka54554252013-08-01 12:52:23 +02001085 if (mOnResumeCallbacks.size() > 0) {
1086 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1087 mOnResumeCallbacks.get(i).run();
1088 }
1089 mOnResumeCallbacks.clear();
1090 }
Winson Chunge4e50662012-01-23 14:45:13 -08001091
1092 // Reset the pressed state of icons that were locked in the press state while activities
1093 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001094 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001095 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001096 mWaitingForResume.setStayPressed(false);
1097 }
Winson Chung82963d52013-10-09 11:20:57 -07001098
Adam Cohen06dff352012-06-01 17:17:08 -07001099 // It is possible that widgets can receive updates while launcher is not in the foreground.
1100 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1101 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1102 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001103 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001104 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001105
Michael Jurka447bf842013-05-15 14:52:15 +02001106 if (DEBUG_RESUME_TIME) {
1107 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1108 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001109
1110 if (mWorkspace.getCustomContentCallbacks() != null) {
1111 // If we are resuming and the custom content is the current page, we call onShow().
1112 // It is also poassible that onShow will instead be called slightly after first layout
1113 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1114 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001115 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001116 }
1117 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001118 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001119 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001120
Sunny Goyal756adbc2015-04-16 15:20:51 -07001121 if (!isWorkspaceLoading()) {
1122 // Process any items that were added while Launcher was away.
1123 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1124 }
Adam Cohen9211d422014-10-07 18:14:53 -07001125
1126 if (mLauncherCallbacks != null) {
1127 mLauncherCallbacks.onResume();
1128 }
Adam Cohen06dff352012-06-01 17:17:08 -07001129 }
1130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001132 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001133 // Ensure that items added to Launcher are queued until Launcher returns
1134 InstallShortcutReceiver.enableInstallQueue();
1135
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001136 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001137 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001138 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001139 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001140
1141 // We call onHide() aggressively. The custom content callbacks should be able to
1142 // debounce excess onHide calls.
1143 if (mWorkspace.getCustomContentCallbacks() != null) {
1144 mWorkspace.getCustomContentCallbacks().onHide();
1145 }
Romain Guycbb89e42009-06-08 15:52:54 -07001146
Adam Cohen9211d422014-10-07 18:14:53 -07001147 if (mLauncherCallbacks != null) {
1148 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001149 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001150 }
1151
1152 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001153 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1154 // by a onResume or by scrolling otherwise.
1155 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001156
1157 // Custom content is completely hidden
1158 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001159
1160 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1161 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001162
1163 // Indicates whether the user is allowed to scroll away from the custom content.
1164 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001165 }
1166
Adam Cohenc2d6e892014-10-16 09:49:24 -07001167 public interface LauncherOverlay {
1168
1169 /**
1170 * Touch interaction leading to overscroll has begun
1171 */
1172 public void onScrollInteractionBegin();
1173
1174 /**
1175 * Touch interaction related to overscroll has ended
1176 */
1177 public void onScrollInteractionEnd();
1178
1179 /**
1180 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1181 * screen (or in the case of RTL, the rightmost screen).
1182 */
1183 public void onScrollChange(int progress, boolean rtl);
1184
1185 /**
1186 * Screen has stopped scrolling
1187 */
1188 public void onScrollSettled();
1189
1190 /**
1191 * This method can be called by the Launcher in order to force the LauncherOverlay
1192 * to exit fully immersive mode.
1193 */
1194 public void forceExitFullImmersion();
1195 }
1196
Winson Chung0f785722015-04-08 10:27:49 -07001197 public interface LauncherAppsCallbacks {
1198 /**
1199 * Updates launcher to the available space that AllApps can take so as not to overlap with
1200 * any other views.
1201 */
1202 public void onAllAppsBoundsChanged(Rect bounds);
1203
1204 /**
1205 * Called to dismiss all apps if it is showing.
1206 */
1207 public void dismissAllApps();
1208 }
1209
Adam Cohenc2d6e892014-10-16 09:49:24 -07001210 public interface LauncherOverlayCallbacks {
1211 /**
1212 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1213 * however it doesn't modify any state within the launcher.
1214 */
1215 public boolean canEnterFullImmersion();
1216
1217 /**
1218 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1219 * eg. by occupying the full screen and handling all touch events.
1220 *
1221 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1222 * case, Launcher will modify any necessary state and assumes the overlay is
1223 * handling all interaction. If false, the LauncherOverlay should cancel any
1224 *
1225 */
1226 public boolean enterFullImmersion();
1227
1228 /**
1229 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1230 * full control over UI and state.
1231 */
1232 public void exitFullImmersion();
1233 }
1234
1235 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1236
1237 @Override
1238 public boolean canEnterFullImmersion() {
1239 return mState == State.WORKSPACE;
1240 }
1241
1242 @Override
1243 public boolean enterFullImmersion() {
1244 if (mState == State.WORKSPACE) {
1245 // When fully immersed, disregard any touches which fall through.
1246 mDragLayer.setBlockTouch(true);
1247 return true;
1248 }
1249 return false;
1250 }
1251
1252 @Override
1253 public void exitFullImmersion() {
1254 mDragLayer.setBlockTouch(false);
1255 }
1256 }
1257
Jorim Jaggid017f882014-01-14 17:08:48 -08001258 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001259 if (mLauncherCallbacks != null) {
1260 return mLauncherCallbacks.hasSettings();
1261 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001262 return false;
1263 }
1264
Adam Cohen9211d422014-10-07 18:14:53 -07001265 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001266 CustomContentCallbacks callbacks, String description) {
1267 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001268 }
1269
Adam Cohenedb40762013-07-18 16:45:45 -07001270 // The custom content needs to offset its content to account for the QSB
1271 public int getTopOffsetForCustomContent() {
1272 return mWorkspace.getPaddingTop();
1273 }
1274
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001275 @Override
1276 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001277 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001278 if (mModel.isCurrentCallbacks(this)) {
1279 mModel.stopLoader();
1280 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001281 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1282
Romain Guy13c2e7b2010-03-10 19:45:00 -08001283 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001284 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001285
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001286 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001287 @Override
1288 public void onWindowFocusChanged(boolean hasFocus) {
1289 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001290 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001291
1292 if (mLauncherCallbacks != null) {
1293 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1294 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001295 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 private boolean acceptFilter() {
1298 final InputMethodManager inputManager = (InputMethodManager)
1299 getSystemService(Context.INPUT_METHOD_SERVICE);
1300 return !inputManager.isFullscreenMode();
1301 }
1302
1303 @Override
1304 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001305 final int uniChar = event.getUnicodeChar();
1306 final boolean handled = super.onKeyDown(keyCode, event);
1307 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1308 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1310 keyCode, event);
1311 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001312 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001313 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001314 // showSearchDialog()
1315 // If there are multiple keystrokes before the search dialog takes focus,
1316 // onSearchRequested() will be called for every keystroke,
1317 // but it is idempotent, so it's fine.
1318 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320 }
1321
Joe Onorato8a9625e2010-01-28 15:55:35 -08001322 // Eat the long press event so the keyboard doesn't come up.
1323 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1324 return true;
1325 }
1326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 return handled;
1328 }
1329
Karl Rosaen138a0412009-04-23 19:00:21 -07001330 private String getTypedText() {
1331 return mDefaultKeySsb.toString();
1332 }
1333
1334 private void clearTypedText() {
1335 mDefaultKeySsb.clear();
1336 mDefaultKeySsb.clearSpans();
1337 Selection.setSelection(mDefaultKeySsb, 0);
1338 }
1339
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001341 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1342 * State
1343 */
1344 private static State intToState(int stateOrdinal) {
1345 State state = State.WORKSPACE;
1346 final State[] stateValues = State.values();
1347 for (int i = 0; i < stateValues.length; i++) {
1348 if (stateValues[i].ordinal() == stateOrdinal) {
1349 state = stateValues[i];
1350 break;
1351 }
1352 }
1353 return state;
1354 }
1355
1356 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 * Restores the previous state, if it exists.
1358 *
1359 * @param savedState The previous state.
1360 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001361 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 private void restoreState(Bundle savedState) {
1363 if (savedState == null) {
1364 return;
1365 }
1366
Michael Jurka883f55b2010-10-21 15:47:14 -07001367 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001368 if (state == State.APPS || state == State.WIDGETS) {
1369 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001370 }
1371
Adam Cohen21cd0022013-10-09 18:57:02 -07001372 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1373 PagedView.INVALID_RESTORE_PAGE);
1374 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001375 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
1377
Winson Chung3d503fb2011-07-13 17:25:49 -07001378 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001379 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001380
Winson Chung3d503fb2011-07-13 17:25:49 -07001381 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1382 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001383 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001384 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1385 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001386 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1387 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001388 AppWidgetProviderInfo info = savedState.getParcelable(
1389 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001390 mPendingAddWidgetInfo = info == null ?
1391 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1392
Adam Cohen4637b5a2013-11-04 18:21:24 -08001393 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001394 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 mRestoring = true;
1396 }
1397
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001398 mItemIdToViewId = (HashMap<Integer, Integer>)
1399 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
1401
1402 /**
1403 * Finds all the views we need and configure them properly.
1404 */
1405 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001406 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001407
Craig Mautner360310b2012-10-26 15:13:08 -07001408 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001409 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001410 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1411 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001412 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001413 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001414
John Spurlock77e1f472013-09-11 10:09:51 -04001415 mLauncherView.setSystemUiVisibility(
1416 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001417 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418
Winson Chung4c98d922011-05-31 16:50:48 -07001419 // Setup the drag layer
1420 mDragLayer.setup(this, dragController);
1421
Winson Chung3d503fb2011-07-13 17:25:49 -07001422 // Setup the hotseat
1423 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1424 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001425 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001426 }
1427
Jorim Jaggid017f882014-01-14 17:08:48 -08001428 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001429 View widgetButton = findViewById(R.id.widget_button);
1430 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001431 @Override
1432 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001433 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001434 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001435 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001436 }
1437 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001438 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1439
1440 View wallpaperButton = findViewById(R.id.wallpaper_button);
1441 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001442 @Override
1443 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001444 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001445 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001446 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001447 }
1448 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001449 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1450
1451 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001452 if (hasSettings()) {
1453 settingsButton.setOnClickListener(new OnClickListener() {
1454 @Override
1455 public void onClick(View arg0) {
1456 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001457 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001458 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001459 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001460 });
1461 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1462 } else {
1463 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001464 }
1465
Adam Cohendbdff6b2013-09-18 19:09:15 -07001466 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001467
Winson Chung4c98d922011-05-31 16:50:48 -07001468 // Setup the workspace
1469 mWorkspace.setHapticFeedbackEnabled(false);
1470 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001471 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001472 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001473
Winson Chungf0ea4d32011-06-06 14:27:16 -07001474 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001475 mSearchDropTargetBar = (SearchDropTargetBar)
1476 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001477
Winson Chungb745afb2015-03-02 11:51:23 -08001478 // Setup Apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001479 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001480 if (isAllAppsSearchOverridden()) {
1481 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001482 }
Winson Chungb745afb2015-03-02 11:51:23 -08001483
Winson Chungf0ea4d32011-06-06 14:27:16 -07001484 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001485 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001486
Winson Chung3d503fb2011-07-13 17:25:49 -07001487 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001488 dragController.setDragScoller(mWorkspace);
1489 dragController.setScrollView(mDragLayer);
1490 dragController.setMoveTarget(mWorkspace);
1491 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001492 if (mSearchDropTargetBar != null) {
1493 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001494 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001495 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001496
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001497 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001498 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001499 mWeightWatcher = new WeightWatcher(this);
1500 mWeightWatcher.setAlpha(0.5f);
1501 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001502 new FrameLayout.LayoutParams(
1503 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001504 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001505 Gravity.BOTTOM)
1506 );
Adam Cohen39a06042013-07-19 14:30:12 -07001507
1508 boolean show = shouldShowWeightWatcher();
1509 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 }
1512
1513 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001514 * Sets the all apps button. This method is called from {@link Hotseat}.
1515 */
1516 public void setAllAppsButton(View allAppsButton) {
1517 mAllAppsButton = allAppsButton;
1518 }
1519
1520 public View getAllAppsButton() {
1521 return mAllAppsButton;
1522 }
1523
1524 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 * Creates a view representing a shortcut.
1526 *
1527 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001529 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001530 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
1532
1533 /**
1534 * Creates a view representing a shortcut inflated from the specified resource.
1535 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 * @param parent The group the shortcut belongs to.
1537 * @param info The data structure describing the shortcut.
1538 *
1539 * @return A View inflated from layoutResId.
1540 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001541 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001542 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001543 parent, false);
1544 favorite.applyFromShortcutInfo(info, mIconCache);
1545 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001547 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 return favorite;
1549 }
1550
1551 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 * Add a shortcut to the workspace.
1553 *
1554 * @param data The intent describing the shortcut.
1555 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 int cellY) {
1559 int[] cellXY = mTmpAddItemCellCoordinates;
1560 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001562
Sunny Goyal5c97f512015-05-19 16:03:28 -07001563 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001564 if (info == null) {
1565 return;
1566 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001567 final View view = createShortcut(info);
1568
Sunny Goyal5c97f512015-05-19 16:03:28 -07001569 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001570 // First we check if we already know the exact location where we want to add this item.
1571 if (cellX >= 0 && cellY >= 0) {
1572 cellXY[0] = cellX;
1573 cellXY[1] = cellY;
1574 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001575
1576 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001577 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001578 true, null,null)) {
1579 return;
1580 }
1581 DragObject dragObject = new DragObject();
1582 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001583 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1584 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001585 return;
1586 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001587 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001588 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001589 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001590 foundCellSpan = (result != null);
1591 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001592 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001593 }
1594
1595 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001596 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001597 return;
1598 }
1599
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001600 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601
1602 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001603 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001604 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 }
1606 }
1607
Adam Cohen2e6da152015-05-06 11:42:25 -07001608 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1609 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001610 // We want to account for the extra amount of padding that we are adding to the widget
1611 // to ensure that it gets the full amount of space that it has requested
1612 int requiredWidth = minWidth + padding.left + padding.right;
1613 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001614 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001615 }
1616
Adam Cohen2e6da152015-05-06 11:42:25 -07001617 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1618 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001619 }
1620
Adam Cohen2e6da152015-05-06 11:42:25 -07001621 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1622 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001623 }
1624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001626 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001628 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 */
Adam Cohen091440a2015-03-18 14:16:05 -07001630 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001631 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1632
1633 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001634 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001635 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1636 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001637 }
Romain Guycbb89e42009-06-08 15:52:54 -07001638
Adam Cohen59400422014-03-05 18:07:04 -08001639 if (appWidgetInfo.isCustomWidget) {
1640 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001641 }
1642
Adam Cohen59400422014-03-05 18:07:04 -08001643 LauncherAppWidgetInfo launcherInfo;
1644 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1645 launcherInfo.spanX = info.spanX;
1646 launcherInfo.spanY = info.spanY;
1647 launcherInfo.minSpanX = info.minSpanX;
1648 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001649 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001652 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653
1654 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001655 if (hostView == null) {
1656 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001657 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1658 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001659 } else {
1660 // The AppWidgetHostView has already been inflated and instantiated
1661 launcherInfo.hostView = hostView;
1662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001664 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001665 launcherInfo.notifyWidgetSizeChanged(this);
1666
Adam Cohen59400422014-03-05 18:07:04 -08001667 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1668 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001669
1670 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001672 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
Romain Guycbb89e42009-06-08 15:52:54 -07001674
Adam Cohended9f8d2010-11-03 13:25:16 -07001675 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1676 @Override
1677 public void onReceive(Context context, Intent intent) {
1678 final String action = intent.getAction();
1679 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1680 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001681 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001682 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001683
Winson Chungc100e8e2011-08-09 16:02:43 -07001684 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001685 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001686 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001687 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001688 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001689 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001690 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1691 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001692 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001693 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1694 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001695 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001696 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1697 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1698 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001699 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001700 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1701 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 }
1703 }
1704 };
1705
1706 @Override
1707 public void onAttachedToWindow() {
1708 super.onAttachedToWindow();
1709
1710 // Listen for broadcasts related to user-presence
1711 final IntentFilter filter = new IntentFilter();
1712 filter.addAction(Intent.ACTION_SCREEN_OFF);
1713 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001714 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001715 if (ENABLE_DEBUG_INTENTS) {
1716 filter.addAction(DebugIntents.DELETE_DATABASE);
1717 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1718 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001720 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001721 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001722 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 mVisible = true;
1724 }
1725
Adam Cohen0b395352014-06-09 22:54:36 +00001726 /**
1727 * Sets up transparent navigation and status bars in LMP.
1728 * This method is a no-op for other platform versions.
1729 */
Sunny Goyald0091012015-01-20 15:55:34 -08001730 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001731 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001732 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001733 Window window = getWindow();
1734 window.getAttributes().systemUiVisibility |=
1735 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1736 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1737 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1738 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1739 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1740 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1741 window.setStatusBarColor(Color.TRANSPARENT);
1742 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001743 }
1744 }
1745
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 @Override
1747 public void onDetachedFromWindow() {
1748 super.onDetachedFromWindow();
1749 mVisible = false;
1750
Adam Cohend113e0c2010-11-11 10:48:05 -08001751 if (mAttached) {
1752 unregisterReceiver(mReceiver);
1753 mAttached = false;
1754 }
Winson Chungb745afb2015-03-02 11:51:23 -08001755 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 }
1757
1758 public void onWindowVisibilityChanged(int visibility) {
1759 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001760 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001761 // The following code used to be in onResume, but it turns out onResume is called when
1762 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1763 // is a more appropriate event to handle
1764 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001765 if (!mWorkspaceLoading) {
1766 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001767 // We want to let Launcher draw itself at least once before we force it to build
1768 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001769 // apps is nice and speedy.
1770 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001771 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001772 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001773 if (mStarted) return;
1774 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001775 // We delay the layer building a bit in order to give
1776 // other message processing a time to run. In particular
1777 // this avoids a delay in hiding the IME if it was
1778 // currently shown, because doing that may involve
1779 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001780 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001781 final ViewTreeObserver.OnDrawListener listener = this;
1782 mWorkspace.post(new Runnable() {
1783 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001784 if (mWorkspace != null &&
1785 mWorkspace.getViewTreeObserver() != null) {
1786 mWorkspace.getViewTreeObserver().
1787 removeOnDrawListener(listener);
1788 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001789 }
1790 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001791 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001792 }
1793 });
1794 }
1795 clearTypedText();
1796 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001797 }
1798
Adam Cohen091440a2015-03-18 14:16:05 -07001799 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001800 mHandler.removeMessages(ADVANCE_MSG);
1801 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1802 mHandler.sendMessageDelayed(msg, delay);
1803 mAutoAdvanceSentTime = System.currentTimeMillis();
1804 }
1805
Adam Cohen091440a2015-03-18 14:16:05 -07001806 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001807 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1808 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1809 mAutoAdvanceRunning = autoAdvanceRunning;
1810 if (autoAdvanceRunning) {
1811 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1812 sendAdvanceMessage(delay);
1813 } else {
1814 if (!mWidgetsToAdvance.isEmpty()) {
1815 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1816 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1817 }
1818 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001819 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001820 }
1821 }
1822 }
1823
1824 private final Handler mHandler = new Handler() {
1825 @Override
1826 public void handleMessage(Message msg) {
1827 if (msg.what == ADVANCE_MSG) {
1828 int i = 0;
1829 for (View key: mWidgetsToAdvance.keySet()) {
1830 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1831 final int delay = mAdvanceStagger * i;
1832 if (v instanceof Advanceable) {
1833 postDelayed(new Runnable() {
1834 public void run() {
1835 ((Advanceable) v).advance();
1836 }
1837 }, delay);
1838 }
1839 i++;
1840 }
1841 sendAdvanceMessage(mAdvanceInterval);
1842 }
1843 }
1844 };
1845
1846 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001847 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001848 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1849 if (v instanceof Advanceable) {
1850 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001851 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001852 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001853 }
1854 }
1855
1856 void removeWidgetToAutoAdvance(View hostView) {
1857 if (mWidgetsToAdvance.containsKey(hostView)) {
1858 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001859 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001860 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001861 }
1862
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001864 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001865 launcherInfo.hostView = null;
1866 }
1867
Hyunyoung Song3f471442015-04-08 19:01:34 -07001868 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001869 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1870 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001871 }
1872
Winson Chunga6945242014-01-08 14:04:34 -08001873 public DragLayer getDragLayer() {
1874 return mDragLayer;
1875 }
1876
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001877 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001878 return mAppsView;
1879 }
1880
Hyunyoung Song3f471442015-04-08 19:01:34 -07001881 public WidgetsContainerView getWidgetsView() {
1882 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001883 }
1884
Winson Chunga6945242014-01-08 14:04:34 -08001885 public Workspace getWorkspace() {
1886 return mWorkspace;
1887 }
1888
1889 public Hotseat getHotseat() {
1890 return mHotseat;
1891 }
1892
Jorim Jaggid017f882014-01-14 17:08:48 -08001893 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001894 return mOverviewPanel;
1895 }
1896
1897 public SearchDropTargetBar getSearchBar() {
1898 return mSearchDropTargetBar;
1899 }
1900
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001901 public LauncherAppWidgetHost getAppWidgetHost() {
1902 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 }
Romain Guycbb89e42009-06-08 15:52:54 -07001904
Winson Chunga9abd0e2010-10-27 17:18:37 -07001905 public LauncherModel getModel() {
1906 return mModel;
1907 }
1908
Winson Chunga6945242014-01-08 14:04:34 -08001909 protected SharedPreferences getSharedPrefs() {
1910 return mSharedPrefs;
1911 }
1912
Adam Cohen2e6da152015-05-06 11:42:25 -07001913 public DeviceProfile getDeviceProfile() {
1914 return mDeviceProfile;
1915 }
1916
Bjorn Bringertc459e522013-06-07 19:36:01 +01001917 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001918 getWindow().closeAllPanels();
1919
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001920 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001921 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001922 }
1923
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 @Override
1925 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001926 long startTime = 0;
1927 if (DEBUG_RESUME_TIME) {
1928 startTime = System.currentTimeMillis();
1929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 super.onNewIntent(intent);
1931
1932 // Close the menu
1933 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001934 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001935 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001936
Adam Cohened307df2013-10-02 09:37:31 -07001937 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1938 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1939 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001940
Adam Cohen6fecd412013-10-02 17:41:50 -07001941 if (mWorkspace == null) {
1942 // Can be cases where mWorkspace is null, this prevents a NPE
1943 return;
1944 }
1945 Folder openFolder = mWorkspace.getOpenFolder();
1946 // In all these cases, only animate if we're already on home
1947 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001948
1949 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1950 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001951 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001952 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001953 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001954 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001955
Adam Cohen6fecd412013-10-02 17:41:50 -07001956 closeFolder();
1957 exitSpringLoadedDragMode();
1958
1959 // If we are already on home, then just animate back to the workspace,
1960 // otherwise, just wait until onResume to set the state back to Workspace
1961 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001962 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001963 } else {
1964 mOnResumeState = State.WORKSPACE;
1965 }
1966
1967 final View v = getWindow().peekDecorView();
1968 if (v != null && v.getWindowToken() != null) {
1969 InputMethodManager imm = (InputMethodManager)getSystemService(
1970 INPUT_METHOD_SERVICE);
1971 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1972 }
1973
Winson Chungb745afb2015-03-02 11:51:23 -08001974 // Reset the apps view
1975 if (!alreadyOnHome && mAppsView != null) {
1976 mAppsView.scrollToTop();
1977 }
1978
Hyunyoung Song3f471442015-04-08 19:01:34 -07001979 // Reset the widgets view
1980 if (!alreadyOnHome && mWidgetsView != null) {
1981 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001982 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001983
Adam Cohen9211d422014-10-07 18:14:53 -07001984 if (mLauncherCallbacks != null) {
1985 mLauncherCallbacks.onHomeIntent();
1986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 }
Adam Cohened307df2013-10-02 09:37:31 -07001988
Michael Jurka447bf842013-05-15 14:52:15 +02001989 if (DEBUG_RESUME_TIME) {
1990 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1991 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
Adam Cohen9211d422014-10-07 18:14:53 -07001993 if (mLauncherCallbacks != null) {
1994 mLauncherCallbacks.onNewIntent(intent);
1995 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001996 }
1997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001999 public void onRestoreInstanceState(Bundle state) {
2000 super.onRestoreInstanceState(state);
2001 for (int page: mSynchronouslyBoundPages) {
2002 mWorkspace.restoreInstanceStateForChild(page);
2003 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 }
2005
2006 @Override
2007 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002008 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002009 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2010 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002011 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002012 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013
Michael Jurka883f55b2010-10-21 15:47:14 -07002014 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002015 // We close any open folder since it will not be re-opened, and we need to make sure
2016 // this state is reflected.
2017 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018
Adam Cohendcd297f2013-06-18 13:13:40 -07002019 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002020 mWaitingForResult) {
2021 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002022 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002023 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2024 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002025 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2026 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2027 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002028 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
2030
Hyunyoung Song3f471442015-04-08 19:01:34 -07002031 // Save the current widgets tray?
2032 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002033 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002034
2035 if (mLauncherCallbacks != null) {
2036 mLauncherCallbacks.onSaveInstanceState(outState);
2037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
2039
2040 @Override
2041 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002043
Winson Chunge7a03942011-08-05 15:05:12 -07002044 // Remove all pending runnables
2045 mHandler.removeMessages(ADVANCE_MSG);
2046 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002047 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002048
Winson Chungcd2b0142011-06-08 16:02:26 -07002049 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002050 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002051
2052 // It's possible to receive onDestroy after a new Launcher activity has
2053 // been created. In this case, don't interfere with the new Launcher.
2054 if (mModel.isCurrentCallbacks(this)) {
2055 mModel.stopLoader();
2056 app.setLauncher(null);
2057 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002060 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002062 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002064 mAppWidgetHost = null;
2065
2066 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067
2068 TextKeyListener.getInstance().release();
2069
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002070 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002071 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002072
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002073 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002074 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002075 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002076 mWorkspace = null;
2077 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002078
Michael Jurka2ecf9952012-06-18 12:52:28 -07002079 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002080
2081 if (mLauncherCallbacks != null) {
2082 mLauncherCallbacks.onDestroy();
2083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 }
2085
Adam Cohena9cf38f2011-05-02 15:36:58 -07002086 public DragController getDragController() {
2087 return mDragController;
2088 }
2089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 @Override
2091 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002092 onStartForResult(requestCode);
2093 super.startActivityForResult(intent, requestCode);
2094 }
2095
2096 @Override
2097 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2098 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2099 onStartForResult(requestCode);
2100 try {
2101 super.startIntentSenderForResult(intent, requestCode,
2102 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2103 } catch (IntentSender.SendIntentException e) {
2104 throw new ActivityNotFoundException();
2105 }
2106 }
2107
2108 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002109 if (requestCode >= 0) {
2110 setWaitingForResult(true);
2111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 }
2113
Winson Chung70d72102011-08-12 11:24:35 -07002114 /**
2115 * Indicates that we want global search for this activity by setting the globalSearch
2116 * argument for {@link #startSearch} to true.
2117 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002119 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002121
Karl Rosaen138a0412009-04-23 19:00:21 -07002122 if (initialQuery == null) {
2123 // Use any text typed in the launcher as the initial query
2124 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 if (appSearchData == null) {
2127 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002128 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 }
Winson Chungadf0c182012-08-23 14:59:07 -07002130 Rect sourceBounds = new Rect();
2131 if (mSearchDropTargetBar != null) {
2132 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2133 }
Romain Guycbb89e42009-06-08 15:52:54 -07002134
Ian Parkinson047036e2014-09-01 15:40:53 +01002135 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002136 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002137 if (clearTextImmediately) {
2138 clearTypedText();
2139 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002140
2141 // We need to show the workspace after starting the search
2142 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002143 }
2144
Ian Parkinson047036e2014-09-01 15:40:53 +01002145 /**
2146 * Start a text search.
2147 *
2148 * @return {@code true} if the search will start immediately, so any further keypresses
2149 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2150 * to buffer keypresses.
2151 */
2152 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002153 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002154 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2155 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2156 sourceBounds);
2157 }
2158
Michael Jurkaa33411c2012-06-14 16:18:21 -07002159 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002160 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002161 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002162 }
2163
2164 /**
2165 * Starts the global search activity. This code is a copied from SearchManager
2166 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002167 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002168 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002169 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002170 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2171 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2172 if (globalSearchActivity == null) {
2173 Log.w(TAG, "No global search activity found.");
2174 return;
2175 }
2176 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2177 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2178 intent.setComponent(globalSearchActivity);
2179 // Make sure that we have a Bundle to put source in
2180 if (appSearchData == null) {
2181 appSearchData = new Bundle();
2182 } else {
2183 appSearchData = new Bundle(appSearchData);
2184 }
Adam Cohen9211d422014-10-07 18:14:53 -07002185 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002186 if (!appSearchData.containsKey("source")) {
2187 appSearchData.putString("source", getPackageName());
2188 }
2189 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2190 if (!TextUtils.isEmpty(initialQuery)) {
2191 intent.putExtra(SearchManager.QUERY, initialQuery);
2192 }
2193 if (selectInitialQuery) {
2194 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2195 }
2196 intent.setSourceBounds(sourceBounds);
2197 try {
2198 startActivity(intent);
2199 } catch (ActivityNotFoundException ex) {
2200 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2201 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 }
2203
Yura4f93ec62014-02-04 14:15:21 +00002204 public boolean isOnCustomContent() {
2205 return mWorkspace.isOnOrMovingToCustomContent();
2206 }
2207
Winson Chung70d72102011-08-12 11:24:35 -07002208 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002209 public boolean onPrepareOptionsMenu(Menu menu) {
2210 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002211 if (!isOnCustomContent()) {
2212 // Close any open folders
2213 closeFolder();
2214 // Stop resizing any widgets
2215 mWorkspace.exitWidgetResizeMode();
2216 if (!mWorkspace.isInOverviewMode()) {
2217 // Show the overview mode
2218 showOverviewMode(true);
2219 } else {
2220 showWorkspace(true);
2221 }
Winson Chunge0298742014-01-17 12:03:00 -08002222 }
Adam Cohen9211d422014-10-07 18:14:53 -07002223 if (mLauncherCallbacks != null) {
2224 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2225 }
2226
Michael Jurkab94f3f82013-09-11 18:08:54 +02002227 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002230 @Override
2231 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002232 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002233 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002234 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002235 }
2236
Joe Onorato9c1289c2009-08-17 11:03:03 -04002237 public boolean isWorkspaceLocked() {
2238 return mWorkspaceLoading || mWaitingForResult;
2239 }
2240
Adam Cohen517a7f52014-03-01 12:12:59 -08002241 public boolean isWorkspaceLoading() {
2242 return mWorkspaceLoading;
2243 }
2244
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002245 private void setWorkspaceLoading(boolean value) {
2246 boolean isLocked = isWorkspaceLocked();
2247 mWorkspaceLoading = value;
2248 if (isLocked != isWorkspaceLocked()) {
2249 onWorkspaceLockedChanged();
2250 }
2251 }
2252
2253 private void setWaitingForResult(boolean value) {
2254 boolean isLocked = isWorkspaceLocked();
2255 mWaitingForResult = value;
2256 if (isLocked != isWorkspaceLocked()) {
2257 onWorkspaceLockedChanged();
2258 }
2259 }
2260
Adam Cohen9211d422014-10-07 18:14:53 -07002261 protected void onWorkspaceLockedChanged() {
2262 if (mLauncherCallbacks != null) {
2263 mLauncherCallbacks.onWorkspaceLockedChanged();
2264 }
2265 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002266
Michael Jurka0280c3b2010-09-17 15:00:07 -07002267 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002268 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002269 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002270 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2271 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002272 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002274 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002275
Sunny Goyale6b63a32015-01-16 16:14:29 -08002276 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002277 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002278 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2279 }
2280
Sunny Goyale6b63a32015-01-16 16:14:29 -08002281 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002282 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2283 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002284 if (appWidgetInfo.configure != null) {
2285 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002286 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002287
Bjorn Bringert7984c942009-12-09 15:38:25 +00002288 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002289 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2290 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002293 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002294 Runnable onComplete = new Runnable() {
2295 @Override
2296 public void run() {
2297 // Exit spring loaded mode if necessary after adding the widget
2298 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2299 null);
2300 }
2301 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002302 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002303 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002304 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 }
2306 }
Romain Guycbb89e42009-06-08 15:52:54 -07002307
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002308 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002309 // Close any folders that may be open.
2310 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002311 mWorkspace.moveToCustomContentScreen(animate);
2312 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002313
2314 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2315 int[] cell, int spanX, int spanY) {
2316 switch (info.itemType) {
2317 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2318 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2319 int span[] = new int[2];
2320 span[0] = spanX;
2321 span[1] = spanY;
2322 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2323 container, screenId, cell, span);
2324 break;
2325 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2326 processShortcutFromDrop(info.componentName, container, screenId, cell);
2327 break;
2328 default:
2329 throw new IllegalStateException("Unknown item type: " + info.itemType);
2330 }
2331 }
2332
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333 /**
2334 * Process a shortcut drop.
2335 *
2336 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002337 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002338 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002339 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002340 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2341 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002342 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002343 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002345 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346
2347 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002348 mPendingAddInfo.cellX = cell[0];
2349 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002350 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002351
2352 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2353 createShortcutIntent.setComponent(componentName);
2354 processShortcut(createShortcutIntent);
2355 }
2356
Adam Cohenfbba09b2011-07-18 21:43:05 -07002357 /**
2358 * Process a widget drop.
2359 *
2360 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002361 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002363 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002364 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2365 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002366 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002367 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002368 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002369 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002370 mPendingAddInfo.minSpanX = info.minSpanX;
2371 mPendingAddInfo.minSpanY = info.minSpanY;
2372
Adam Cohenfbba09b2011-07-18 21:43:05 -07002373 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002374 mPendingAddInfo.cellX = cell[0];
2375 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002376 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002377 if (span != null) {
2378 mPendingAddInfo.spanX = span[0];
2379 mPendingAddInfo.spanY = span[1];
2380 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002381
Adam Cohened66b2b2012-01-23 17:28:51 -08002382 AppWidgetHostView hostView = info.boundWidget;
2383 int appWidgetId;
2384 if (hostView != null) {
2385 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002386 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002387 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002388 // In this case, we either need to start an activity to get permission to bind
2389 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002390 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002391 Bundle options = info.bindOptions;
2392
Sunny Goyalffe83f12014-08-14 17:39:34 -07002393 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2394 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002395 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002396 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002397 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002398 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002399 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2400 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2401 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002402 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2403 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002404 // TODO: we need to make sure that this accounts for the options bundle.
2405 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002406 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2407 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002408 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002409 }
2410
Joe Onoratodeb98af2010-02-19 14:59:39 -08002411 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002412 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 }
2414
Winson Chung24ab2f12010-09-16 14:10:47 -07002415 void processWallpaper(Intent intent) {
2416 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2417 }
2418
Adam Cohendcd297f2013-06-18 13:13:40 -07002419 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002420 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002421 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 folderInfo.title = getText(R.string.folder_name);
2423
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002425 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2426 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002427 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428
2429 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002430 FolderIcon newFolder =
2431 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002432 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002433 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002434 // Force measure the new folder icon
2435 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2436 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002437 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 }
Romain Guycbb89e42009-06-08 15:52:54 -07002439
Joe Onorato9c1289c2009-08-17 11:03:03 -04002440 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002441 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002442 }
2443
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002444 /**
2445 * Registers various content observers. The current implementation registers
2446 * only a favorites observer to keep track of the favorites applications.
2447 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002448 private void registerContentObservers() {
2449 ContentResolver resolver = getContentResolver();
2450 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2451 true, mWidgetObserver);
2452 }
2453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 @Override
2455 public boolean dispatchKeyEvent(KeyEvent event) {
2456 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2457 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002459 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002460 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002461 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002462 dumpState();
2463 return true;
2464 }
2465 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002466 }
2467 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2468 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002469 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 return true;
2471 }
2472 }
2473
2474 return super.dispatchKeyEvent(event);
2475 }
2476
Joe Onorato88ec0992009-11-19 13:16:06 -08002477 @Override
2478 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002479 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2480 return;
2481 }
2482
Winson Chung3d9490a2015-03-24 17:38:42 -07002483 LauncherAccessibilityDelegate delegate =
2484 LauncherAppState.getInstance().getAccessibilityDelegate();
2485 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002486 return;
2487 }
2488
Winson Chungb745afb2015-03-02 11:51:23 -08002489 if (isAppsViewVisible()) {
2490 showWorkspace(true);
2491 } else if (isWidgetsViewVisible()) {
2492 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002493 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002494 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002495 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002496 Folder openFolder = mWorkspace.getOpenFolder();
2497 if (openFolder.isEditingName()) {
2498 openFolder.dismissEditingName();
2499 } else {
2500 closeFolder();
2501 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002502 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002503 mWorkspace.exitWidgetResizeMode();
2504
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002505 // Back button is a no-op here, but give at least some feedback for the button press
2506 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002507 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002508 }
2509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002511 * Re-listen when widgets are reset.
2512 */
Adam Cohen091440a2015-03-18 14:16:05 -07002513 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002514 if (mAppWidgetHost != null) {
2515 mAppWidgetHost.startListening();
2516 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002517 }
2518
2519 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 * Launches the intent referred by the clicked shortcut.
2521 *
2522 * @param v The view representing the clicked shortcut.
2523 */
2524 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002525 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2526 // view has detached (it's possible for this to happen if the view is removed mid touch).
2527 if (v.getWindowToken() == null) {
2528 return;
2529 }
2530
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002531 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002532 return;
2533 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002534
Adam Cohen1697b792013-09-17 19:08:21 -07002535 if (v instanceof Workspace) {
2536 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002537 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002538 }
2539 return;
2540 }
2541
2542 if (v instanceof CellLayout) {
2543 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002544 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002545 }
2546 }
2547
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002549 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002550 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002552 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002553 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002554 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002555 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002556 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002557 } else if (tag instanceof AppInfo) {
2558 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002559 } else if (tag instanceof LauncherAppWidgetInfo) {
2560 if (v instanceof PendingAppWidgetHostView) {
2561 onClickPendingWidget((PendingAppWidgetHostView) v);
2562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 }
2564 }
2565
Sunny Goyal508da152014-08-14 10:53:27 -07002566 public void onClickPagedViewIcon(View v) {
2567 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002568 if (mLauncherCallbacks != null) {
2569 mLauncherCallbacks.onClickPagedViewIcon(v);
2570 }
Sunny Goyal508da152014-08-14 10:53:27 -07002571 }
2572
Sunny Goyal70660032015-05-14 00:07:08 -07002573 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002574 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002575 return false;
2576 }
2577
Michael Jurkaaf442092010-06-10 17:01:57 -07002578 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002579 * Event handler for the app widget view which has not fully restored.
2580 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002581 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002582 if (mIsSafeModeEnabled) {
2583 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2584 return;
2585 }
2586
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002587 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002588 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002589 int widgetId = info.appWidgetId;
2590 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2591 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002592 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2593 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002594 mPendingAddInfo.copyFrom(info);
2595 mPendingAddWidgetId = widgetId;
2596
Sunny Goyalffe83f12014-08-14 17:39:34 -07002597 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2598 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002599 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002600 } else if (info.installProgress < 0) {
2601 // The install has not been queued
2602 final String packageName = info.providerName.getPackageName();
2603 showBrokenAppInstallDialog(packageName,
2604 new DialogInterface.OnClickListener() {
2605 public void onClick(DialogInterface dialog, int id) {
2606 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2607 }
2608 });
2609 } else {
2610 // Download has started.
2611 final String packageName = info.providerName.getPackageName();
2612 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002613 }
2614 }
2615
2616 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002617 * Event handler for the "grid" button that appears on the home screen, which
2618 * enters all apps mode.
2619 *
2620 * @param v The view that was clicked.
2621 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002622 protected void onClickAllAppsButton(View v) {
2623 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002624 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 showWorkspace(true);
2626 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002627 // Try and refresh the set of predicted apps before we enter launcher
2628 showAppsView(true /* animated */, false /* resetListToTop */,
2629 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002630 }
2631 }
2632
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002633 private void showBrokenAppInstallDialog(final String packageName,
2634 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002635 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002636 .setTitle(R.string.abandoned_promises_title)
2637 .setMessage(R.string.abandoned_promise_explanation)
2638 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2639 .setNeutralButton(R.string.abandoned_clean_this,
2640 new DialogInterface.OnClickListener() {
2641 public void onClick(DialogInterface dialog, int id) {
2642 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2643 mWorkspace.removeAbandonedPromise(packageName, user);
2644 }
2645 })
2646 .create().show();
2647 return;
2648 }
2649
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002650 /**
2651 * Event handler for an app shortcut click.
2652 *
2653 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2654 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002655 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002656 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2657 Object tag = v.getTag();
2658 if (!(tag instanceof ShortcutInfo)) {
2659 throw new IllegalArgumentException("Input must be a Shortcut");
2660 }
2661
2662 // Open shortcut
2663 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002664
2665 if (shortcut.isDisabled != 0) {
2666 int error = R.string.activity_not_available;
2667 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2668 error = R.string.safemode_shortcut_error;
2669 }
2670 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2671 return;
2672 }
2673
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002674 final Intent intent = shortcut.intent;
2675
2676 // Check for special shortcuts
2677 if (intent.getComponent() != null) {
2678 final String shortcutClass = intent.getComponent().getClassName();
2679
2680 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2681 MemoryDumpActivity.startDump(this);
2682 return;
2683 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2684 toggleShowWeightWatcher();
2685 return;
2686 }
2687 }
2688
Chris Wren40c5ed32014-06-24 18:24:23 -04002689 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002690 if ((v instanceof BubbleTextView)
2691 && shortcut.isPromise()
2692 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002693 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002694 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002695 new DialogInterface.OnClickListener() {
2696 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002697 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002698 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002699 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002700 return;
2701 }
2702
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002703 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002704 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002705
2706 if (mLauncherCallbacks != null) {
2707 mLauncherCallbacks.onClickAppShortcut(v);
2708 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002709 }
2710
Adam Cohen091440a2015-03-18 14:16:05 -07002711 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002712 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002713 final ShortcutInfo shortcut;
2714 final Intent intent;
2715 if (tag instanceof ShortcutInfo) {
2716 shortcut = (ShortcutInfo) tag;
2717 intent = shortcut.intent;
2718 int[] pos = new int[2];
2719 v.getLocationOnScreen(pos);
2720 intent.setSourceBounds(new Rect(pos[0], pos[1],
2721 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002722
Sunny Goyal508da152014-08-14 10:53:27 -07002723 } else if (tag instanceof AppInfo) {
2724 shortcut = null;
2725 intent = ((AppInfo) tag).intent;
2726 } else {
2727 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2728 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002729
2730 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002731 mStats.recordLaunch(intent, shortcut);
2732
2733 if (success && v instanceof BubbleTextView) {
2734 mWaitingForResume = (BubbleTextView) v;
2735 mWaitingForResume.setStayPressed(true);
2736 }
2737 }
2738
2739 /**
2740 * Event handler for a folder icon click.
2741 *
2742 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2743 */
2744 protected void onClickFolderIcon(View v) {
2745 if (LOGD) Log.d(TAG, "onClickFolder");
2746 if (!(v instanceof FolderIcon)){
2747 throw new IllegalArgumentException("Input must be a FolderIcon");
2748 }
2749
2750 FolderIcon folderIcon = (FolderIcon) v;
2751 final FolderInfo info = folderIcon.getFolderInfo();
2752 Folder openFolder = mWorkspace.getFolderForTag(info);
2753
2754 // If the folder info reports that the associated folder is open, then verify that
2755 // it is actually opened. There have been a few instances where this gets out of sync.
2756 if (info.opened && openFolder == null) {
2757 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2758 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2759 info.opened = false;
2760 }
2761
2762 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2763 // Close any open folder
2764 closeFolder();
2765 // Open the requested folder
2766 openFolder(folderIcon);
2767 } else {
2768 // Find the open folder...
2769 int folderScreen;
2770 if (openFolder != null) {
2771 folderScreen = mWorkspace.getPageForView(openFolder);
2772 // .. and close it
2773 closeFolder(openFolder);
2774 if (folderScreen != mWorkspace.getCurrentPage()) {
2775 // Close any folder open on the current screen
2776 closeFolder();
2777 // Pull the folder onto this screen
2778 openFolder(folderIcon);
2779 }
2780 }
2781 }
Adam Cohen9211d422014-10-07 18:14:53 -07002782
2783 if (mLauncherCallbacks != null) {
2784 mLauncherCallbacks.onClickFolderIcon(v);
2785 }
Michael Jurka5130e402011-10-13 04:55:35 -07002786 }
2787
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002788 /**
2789 * Event handler for the (Add) Widgets button that appears after a long press
2790 * on the home screen.
2791 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002792 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002793 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002794 if (mIsSafeModeEnabled) {
2795 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2796 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002797 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002798 if (mLauncherCallbacks != null) {
2799 mLauncherCallbacks.onClickAddWidgetButton(view);
2800 }
Adam Cohen9211d422014-10-07 18:14:53 -07002801 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002802 }
2803
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002804 /**
2805 * Event handler for the wallpaper picker button that appears after a long press
2806 * on the home screen.
2807 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002808 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002809 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002810 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2811 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002812
2813 if (mLauncherCallbacks != null) {
2814 mLauncherCallbacks.onClickWallpaperPicker(v);
2815 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002816 }
2817
2818 /**
2819 * Event handler for a click on the settings button that appears after a long press
2820 * on the home screen.
2821 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002822 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002823 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002824 if (mLauncherCallbacks != null) {
2825 mLauncherCallbacks.onClickSettingsButton(v);
2826 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002827 }
2828
Michael Jurka5130e402011-10-13 04:55:35 -07002829 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002830 // Provide the same haptic feedback that the system offers for virtual keys.
2831 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002832 }
2833
Adam Cohen61f560d2013-09-30 15:58:20 -07002834 public void performHapticFeedbackOnTouchDown(View v) {
2835 // Provide the same haptic feedback that the system offers for virtual keys.
2836 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2837 }
2838
2839 public View.OnTouchListener getHapticFeedbackTouchListener() {
2840 if (mHapticFeedbackTouchListener == null) {
2841 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002842 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002843 @Override
2844 public boolean onTouch(View v, MotionEvent event) {
2845 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2846 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2847 }
2848 return false;
2849 }
2850 };
2851 }
2852 return mHapticFeedbackTouchListener;
2853 }
2854
Adam Cohen9211d422014-10-07 18:14:53 -07002855 public void onDragStarted(View view) {
2856 if (isOnCustomContent()) {
2857 // Custom content screen doesn't participate in drag and drop. If on custom
2858 // content screen, move to default.
2859 moveWorkspaceToDefaultScreen();
2860 }
2861
2862 if (mLauncherCallbacks != null) {
2863 mLauncherCallbacks.onDragStarted(view);
2864 }
2865 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002866
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002867 /**
2868 * Called when the user stops interacting with the launcher.
2869 * This implies that the user is now on the homescreen and is not doing housekeeping.
2870 */
Adam Cohen9211d422014-10-07 18:14:53 -07002871 protected void onInteractionEnd() {
2872 if (mLauncherCallbacks != null) {
2873 mLauncherCallbacks.onInteractionEnd();
2874 }
2875 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002876
2877 /**
2878 * Called when the user starts interacting with the launcher.
2879 * The possible interactions are:
2880 * - open all apps
2881 * - reorder an app shortcut, or a widget
2882 * - open the overview mode.
2883 * This is a good time to stop doing things that only make sense
2884 * when the user is on the homescreen and not doing housekeeping.
2885 */
Adam Cohen9211d422014-10-07 18:14:53 -07002886 protected void onInteractionBegin() {
2887 if (mLauncherCallbacks != null) {
2888 mLauncherCallbacks.onInteractionBegin();
2889 }
2890 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002891
Winson Chungcd99cd32015-04-29 11:03:24 -07002892 /** Updates the interaction state. */
2893 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002894 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002895 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002896 boolean fromStateWithOverlay;
2897 boolean toStateWithOverlay;
2898 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2899 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2900 toStateWithOverlay = toState != Workspace.State.NORMAL;
2901 } else {
2902 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2903 fromState != Workspace.State.NORMAL_HIDDEN;
2904 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2905 toState != Workspace.State.NORMAL_HIDDEN;
2906 }
2907 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002908 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002909 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002910 onInteractionEnd();
2911 }
2912 }
2913
Kenny Guyf07af7b2014-07-31 11:39:16 +01002914 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002915 try {
2916 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002917 launcherApps.showAppDetailsForProfile(componentName, user);
2918 } catch (SecurityException e) {
2919 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2920 Log.e(TAG, "Launcher does not have permission to launch settings");
2921 } catch (ActivityNotFoundException e) {
2922 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2923 Log.e(TAG, "Unable to launch settings");
2924 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002925 }
2926
Michael Jurka1e2f4652013-07-08 18:03:46 -07002927 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002928 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2929 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002930 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002931 // System applications cannot be installed. For now, show a toast explaining that.
2932 // We may give them the option of disabling apps this way.
2933 int messageId = R.string.uninstall_system_app_text;
2934 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002935 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002936 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002937 String packageName = componentName.getPackageName();
2938 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002939 Intent intent = new Intent(
2940 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002941 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2942 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002943 if (user != null) {
2944 user.addToIntent(intent, Intent.EXTRA_USER);
2945 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002946 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002947 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002948 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002949 }
2950
Michael Jurka86a720e2012-05-09 11:23:23 -07002951 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002952 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002954 // Only launch using the new animation if the shortcut has not opted out (this is a
2955 // private contract between launcher and may be ignored in the future).
2956 boolean useLaunchAnimation = (v != null) &&
2957 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002958 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2959 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002960
Kenny Guy1317e2d2014-05-08 18:52:50 +01002961 UserHandleCompat user = null;
2962 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2963 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2964 user = userManager.getUserForSerialNumber(serialNumber);
2965 }
2966
2967 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002968 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002969 ActivityOptions opts = null;
2970 if (sClipRevealMethod != null) {
2971 // TODO: call method directly when Launcher3 can depend on M APIs
2972 int left = 0, top = 0;
2973 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2974 if (v instanceof TextView) {
2975 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002976 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2977 if (icon != null) {
2978 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002979 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002980 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002981 width = bounds.width();
2982 height = bounds.height();
2983 }
2984 }
2985 try {
2986 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2987 left, top, width, height);
2988 } catch (IllegalAccessException e) {
2989 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2990 sClipRevealMethod = null;
2991 } catch (InvocationTargetException e) {
2992 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2993 sClipRevealMethod = null;
2994 }
2995 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002996 if (opts == null && !Utilities.isLmpOrAbove()) {
2997 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002998 v.getMeasuredWidth(), v.getMeasuredHeight());
2999 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003000 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07003001 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003002
3003 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3004 // Could be launching some bookkeeping activity
3005 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003006 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003007 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003008 launcherApps.startActivityForProfile(intent.getComponent(), user,
3009 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003010 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003011 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 } catch (SecurityException e) {
3013 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003014 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003016 "or use the exported attribute for this activity. "
3017 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003018 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003019 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003021
Michael Jurka86a720e2012-05-09 11:23:23 -07003022 boolean startActivitySafely(View v, Intent intent, Object tag) {
3023 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003024 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3025 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3026 return false;
3027 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003028 try {
3029 success = startActivity(v, intent, tag);
3030 } catch (ActivityNotFoundException e) {
3031 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3032 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3033 }
3034 return success;
3035 }
3036
Adam Cohen268c4752012-06-06 17:47:33 -07003037 /**
3038 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3039 * in the DragLayer in the exact absolute location of the original FolderIcon.
3040 */
3041 private void copyFolderIconToImage(FolderIcon fi) {
3042 final int width = fi.getMeasuredWidth();
3043 final int height = fi.getMeasuredHeight();
3044
3045 // Lazy load ImageView, Bitmap and Canvas
3046 if (mFolderIconImageView == null) {
3047 mFolderIconImageView = new ImageView(this);
3048 }
3049 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3050 mFolderIconBitmap.getHeight() != height) {
3051 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3052 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3053 }
3054
3055 DragLayer.LayoutParams lp;
3056 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3057 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3058 } else {
3059 lp = new DragLayer.LayoutParams(width, height);
3060 }
3061
Adam Cohen307fe232012-08-16 17:55:58 -07003062 // The layout from which the folder is being opened may be scaled, adjust the starting
3063 // view size by this scale factor.
3064 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003065 lp.customPosition = true;
3066 lp.x = mRectForFolderAnimation.left;
3067 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003068 lp.width = (int) (scale * width);
3069 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003070
3071 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3072 fi.draw(mFolderIconCanvas);
3073 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003074 if (fi.getFolder() != null) {
3075 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3076 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003077 }
Adam Cohen268c4752012-06-06 17:47:33 -07003078 // Just in case this image view is still in the drag layer from a previous animation,
3079 // we remove it and re-add it.
3080 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3081 mDragLayer.removeView(mFolderIconImageView);
3082 }
3083 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003084 if (fi.getFolder() != null) {
3085 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003086 }
Adam Cohen268c4752012-06-06 17:47:33 -07003087 }
3088
Adam Cohen2801caf2011-05-13 20:57:39 -07003089 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003090 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003091 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3092 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3093 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3094
Adam Cohenc51934b2011-07-26 21:07:43 -07003095 FolderInfo info = (FolderInfo) fi.getTag();
3096 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3097 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003098 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3099 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003100 }
3101
Adam Cohen268c4752012-06-06 17:47:33 -07003102 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3103 copyFolderIconToImage(fi);
3104 fi.setVisibility(View.INVISIBLE);
3105
Michael Jurka2ecf9952012-06-18 12:52:28 -07003106 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003107 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003108 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003109 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3110 }
3111 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003112 oa.start();
3113 }
3114
Adam Cohen268c4752012-06-06 17:47:33 -07003115 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003116 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003117 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3118 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3119 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3120
Adam Cohen268c4752012-06-06 17:47:33 -07003121 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003122
Adam Cohen268c4752012-06-06 17:47:33 -07003123 // We remove and re-draw the FolderIcon in-case it has changed
3124 mDragLayer.removeView(mFolderIconImageView);
3125 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003126 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003127 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003128 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003129 oa.addListener(new AnimatorListenerAdapter() {
3130 @Override
3131 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003132 if (cl != null) {
3133 cl.clearFolderLeaveBehind();
3134 // Remove the ImageView copy of the FolderIcon and make the original visible.
3135 mDragLayer.removeView(mFolderIconImageView);
3136 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003137 }
3138 }
3139 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003140 oa.start();
3141 }
3142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003144 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 * is animated relative to the specified View. If the View is null, no animation
3146 * is played.
3147 *
3148 * @param folderInfo The FolderInfo describing the folder to open.
3149 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003150 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003151 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003152 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3153 if (openFolder != null && openFolder != folder) {
3154 // Close any open folder before opening a folder.
3155 closeFolder();
3156 }
3157
Adam Cohena9cf38f2011-05-02 15:36:58 -07003158 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003159
Adam Cohena9cf38f2011-05-02 15:36:58 -07003160 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003161
Sunny Goyalf4066152015-04-15 09:42:19 -07003162 // While the folder is open, the position of the icon cannot change.
3163 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3164
Adam Cohen4554ee12011-08-03 16:13:21 -07003165 // Just verify that the folder hasn't already been added to the DragLayer.
3166 // There was a one-off crash where the folder had a parent already.
3167 if (folder.getParent() == null) {
3168 mDragLayer.addView(folder);
3169 mDragController.addDropTarget((DropTarget) folder);
3170 } else {
3171 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3172 folder.getParent() + ").");
3173 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003174 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003175 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003176
3177 // Notify the accessibility manager that this folder "window" has appeared and occluded
3178 // the workspace items
3179 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3180 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003181 }
3182
3183 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003184 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003185 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003186 if (folder.isEditingName()) {
3187 folder.dismissEditingName();
3188 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003189 closeFolder(folder);
3190 }
3191 }
3192
Sunny Goyal83a8f042015-05-19 12:52:12 -07003193 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003194 folder.getInfo().opened = false;
3195
3196 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3197 if (parent != null) {
3198 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3199 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003200 if (fi != null) {
3201 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3202 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003203 }
3204 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003205
3206 // Notify the accessibility manager that this folder "window" has disappeard and no
3207 // longer occludeds the workspace items
3208 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003209 }
3210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003212 if (!isDraggingEnabled()) return false;
3213 if (isWorkspaceLocked()) return false;
3214 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003215
Adam Cohen1697b792013-09-17 19:08:21 -07003216 if (v instanceof Workspace) {
3217 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003218 if (!mWorkspace.isTouchActive()) {
3219 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003220 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3221 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3222 return true;
3223 } else {
3224 return false;
3225 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003226 } else {
3227 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003228 }
Adam Cohen1697b792013-09-17 19:08:21 -07003229 }
3230
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003231 CellLayout.CellInfo longClickCellInfo = null;
3232 View itemUnderLongClick = null;
3233 if (v.getTag() instanceof ItemInfo) {
3234 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003235 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003236 itemUnderLongClick = longClickCellInfo.cell;
3237 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 }
3239
Winson Chung3d503fb2011-07-13 17:25:49 -07003240 // The hotseat touch handling does not go through Workspace, and we always allow long press
3241 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003242 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003243 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003244 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003245 // User long pressed on empty space
3246 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3247 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003248 if (mWorkspace.isInOverviewMode()) {
3249 mWorkspace.startReordering(v);
3250 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003251 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003254 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3255 mHotseat.getOrderInHotseat(
3256 longClickCellInfo.cellX,
3257 longClickCellInfo.cellY));
3258 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003259 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003260 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003261 }
3262 }
3263 }
3264 return true;
3265 }
3266
Winson Chung3d503fb2011-07-13 17:25:49 -07003267 boolean isHotseatLayout(View layout) {
3268 return mHotseat != null && layout != null &&
3269 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3270 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003271
Winson Chung3d503fb2011-07-13 17:25:49 -07003272 /**
3273 * Returns the CellLayout of the specified container at the specified screen.
3274 */
Sunny Goyal92820592015-03-02 11:31:35 -08003275 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003276 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3277 if (mHotseat != null) {
3278 return mHotseat.getLayout();
3279 } else {
3280 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003281 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003282 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003283 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003284 }
3285 }
3286
Winson Chungb745afb2015-03-02 11:51:23 -08003287 /**
3288 * For overridden classes.
3289 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003290 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003291 return isAppsViewVisible();
3292 }
3293
3294 public boolean isAppsViewVisible() {
3295 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3296 }
3297
3298 public boolean isWidgetsViewVisible() {
3299 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003300 }
3301
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003302 private void setWorkspaceBackground(int background) {
3303 switch (background) {
3304 case WORKSPACE_BACKGROUND_TRANSPARENT:
3305 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3306 break;
3307 case WORKSPACE_BACKGROUND_BLACK:
3308 getWindow().setBackgroundDrawable(null);
3309 break;
3310 default:
3311 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3312 }
Craig Mautner360310b2012-10-26 15:13:08 -07003313 }
3314
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003315 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003316 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3317 int curflags = getWindow().getAttributes().flags
3318 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3319 if (wpflags != curflags) {
3320 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3321 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003322 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003323 }
3324
Michael Jurkae326f182011-11-21 14:05:46 -08003325 @Override
3326 public void onTrimMemory(int level) {
3327 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003328 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3329 // The widget preview db can result in holding onto over
3330 // 3MB of memory for caching which isn't necessary.
3331 SQLiteDatabase.releaseMemory();
3332
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003333 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003334 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003335 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003336 if (mLauncherCallbacks != null) {
3337 mLauncherCallbacks.onTrimMemory(level);
3338 }
Michael Jurkae326f182011-11-21 14:05:46 -08003339 }
3340
Winson Chungb745afb2015-03-02 11:51:23 -08003341 @Override
3342 public void onStateTransitionHideSearchBar() {
3343 // Hide the search bar
3344 if (mSearchDropTargetBar != null) {
3345 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3346 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003347 }
3348
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003349 public void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003350 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3351 true);
Adam Cohened307df2013-10-02 09:37:31 -07003352 }
3353
Sunny Goyal83a8f042015-05-19 12:52:12 -07003354 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003355 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3356 onCompleteRunnable, true);
3357 }
3358
3359 protected void showWorkspace(int snapToPage, boolean animated) {
3360 showWorkspace(snapToPage, animated, null, true);
3361 }
3362
3363 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3364 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003365 boolean changed = mState != State.WORKSPACE ||
3366 mWorkspace.getState() != Workspace.State.NORMAL;
3367 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003368 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003370 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003371 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003372
Winson Chungc7d2b602012-05-16 17:02:20 -07003373 // Show the search bar (only animate if we were showing the drop target bar in spring
3374 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003375 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003376 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003377 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003378
Michael Jurkab3e22d92011-10-31 15:58:33 -07003379 // Set focus to the AppsCustomize button
3380 if (mAllAppsButton != null) {
3381 mAllAppsButton.requestFocus();
3382 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003383 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003384
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003385 // Change the state *after* we've called all the transition code
3386 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003387
Winson Chung5fb63472011-02-02 17:03:37 -08003388 // Resume the auto-advance of widgets
3389 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003390 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003391
Winson Chung0f785722015-04-08 10:27:49 -07003392 if (changed) {
3393 // Send an accessibility event to announce the context change
3394 getWindow().getDecorView()
3395 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003396 if (notifyLauncherCallbacks) {
3397 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003398 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003399 mLauncherCallbacks.onAllAppsHidden();
3400 }
3401 }
Winson Chung0f785722015-04-08 10:27:49 -07003402 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003403 }
3404
Adam Cohened307df2013-10-02 09:37:31 -07003405 void showOverviewMode(boolean animated) {
3406 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003407 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003408 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3409 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003410 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003411 }
3412
Winson Chungb745afb2015-03-02 11:51:23 -08003413 /**
3414 * Shows the apps view.
3415 */
Winson Chung4ac30062015-05-08 17:34:17 -07003416 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003417 if (resetListToTop) {
3418 mAppsView.scrollToTop();
3419 }
Winson Chung4ac30062015-05-08 17:34:17 -07003420 if (updatePredictedApps) {
3421 tryAndUpdatePredictedApps();
3422 }
Winson Chungb745afb2015-03-02 11:51:23 -08003423 showAppsOrWidgets(animated, State.APPS);
3424 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425
Winson Chungb745afb2015-03-02 11:51:23 -08003426 /**
3427 * Shows the widgets view.
3428 */
3429 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003430 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003431 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003432 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003433 }
Winson Chungb745afb2015-03-02 11:51:23 -08003434 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003435
3436 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003437 @Override
3438 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003439 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003440 }
3441 });
Winson Chungb745afb2015-03-02 11:51:23 -08003442 }
3443
3444 /**
3445 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003446 *
3447 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003448 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003449 // TODO: calling method should use the return value so that when {@code false} is returned
3450 // the workspace transition doesn't fall into invalid state.
3451 private boolean showAppsOrWidgets(boolean animated, State toState) {
3452 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3453 mState != State.WIDGETS_SPRING_LOADED) {
3454 return false;
3455 }
3456 if (toState != State.APPS && toState != State.WIDGETS) {
3457 return false;
3458 }
Winson Chungb745afb2015-03-02 11:51:23 -08003459
3460 if (toState == State.APPS) {
3461 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003462 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003463 mLauncherCallbacks.onAllAppsShown();
3464 }
Winson Chungb745afb2015-03-02 11:51:23 -08003465 } else {
3466 mStateTransitionAnimation.startAnimationToWidgets(animated);
3467 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003468
Michael Jurkab3e22d92011-10-31 15:58:33 -07003469 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003470 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003471
3472 // Pause the auto-advance of widgets until we are out of AllApps
3473 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003474 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003475 closeFolder();
3476
3477 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003478 getWindow().getDecorView()
3479 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003480 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003481 }
3482
Winson Chungcd99cd32015-04-29 11:03:24 -07003483 /**
3484 * Updates the workspace and interaction state on state change, and return the animation to this
3485 * new state.
3486 */
3487 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3488 boolean animated, HashMap<View, Integer> layerViews) {
3489 Workspace.State fromState = mWorkspace.getState();
3490 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3491 updateInteraction(fromState, toState);
3492 return anim;
3493 }
3494
Hyunyoung Song3f471442015-04-08 19:01:34 -07003495 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003496 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003497 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3498 mState == State.WIDGETS_SPRING_LOADED) {
3499 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003500 }
Winson Chungb745afb2015-03-02 11:51:23 -08003501
3502 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003503 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3504 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003505 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003506 }
Adam Cohen7777d962011-08-18 18:58:38 -07003507
Hyunyoung Song3f471442015-04-08 19:01:34 -07003508 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003509 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003510 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003511
Winson Chungcd99cd32015-04-29 11:03:24 -07003512 if (successfulDrop) {
3513 // We need to trigger all apps hidden to notify search to update itself before the
3514 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003515 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003516 mLauncherCallbacks.onAllAppsHidden();
3517 }
3518 }
3519
Winson Chunge7a03942011-08-05 15:05:12 -07003520 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003521 @Override
3522 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003523 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003524 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3525 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003526 // Before we show workspace, hide all apps again because
3527 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3528 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003529 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003530 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003531 } else {
3532 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003533 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003534 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003535 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003536 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003537
Michael Jurkad3ef3062010-11-23 16:23:58 -08003538 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003539 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003540 showAppsView(true /* animated */, false /* resetListToTop */,
3541 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003542 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003543 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003544 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003545 }
3546
Winson Chung4ac30062015-05-08 17:34:17 -07003547 /**
3548 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3549 * resumed.
3550 */
3551 private void tryAndUpdatePredictedApps() {
3552 if (mLauncherCallbacks != null) {
3553 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3554 if (!apps.isEmpty()) {
3555 mAppsView.setPredictedApps(apps);
3556 }
3557 }
3558 }
3559
Michael Jurkab3e22d92011-10-31 15:58:33 -07003560 void lockAllApps() {
3561 // TODO
3562 }
3563
3564 void unlockAllApps() {
3565 // TODO
3566 }
3567
Sunny Goyal594d76d2014-11-06 10:12:54 -08003568 protected void disableVoiceButtonProxy(boolean disable) {
3569 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003570 }
3571
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003572 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003573 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3574 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003575 }
3576
Adam Cohen24ce0b32014-01-14 16:18:14 -08003577 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003578 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3579 if (searchProvider == null) {
3580 return null;
3581 }
3582
3583 Bundle opts = new Bundle();
3584 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003585 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003586
3587 SharedPreferences sp = getSharedPreferences(
3588 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3589 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003590 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003591 if (!searchProvider.provider.flattenToString().equals(
3592 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003593 || (widgetInfo == null)
3594 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003595 // A valid widget is not already bound.
3596 if (widgetId > -1) {
3597 mAppWidgetHost.deleteAppWidgetId(widgetId);
3598 widgetId = -1;
3599 }
3600
3601 // Try to bind a new widget
3602 widgetId = mAppWidgetHost.allocateAppWidgetId();
3603
3604 if (!AppWidgetManagerCompat.getInstance(this)
3605 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3606 mAppWidgetHost.deleteAppWidgetId(widgetId);
3607 widgetId = -1;
3608 }
3609
3610 sp.edit()
3611 .putInt(QSB_WIDGET_ID, widgetId)
3612 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3613 .commit();
3614 }
3615
3616 if (widgetId != -1) {
3617 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3618 mQsb.updateAppWidgetOptions(opts);
3619 mQsb.setPadding(0, 0, 0, 0);
3620 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003621 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003622 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003623 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003624 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003625 }
3626
Sunny Goyal22235bc2015-04-03 09:23:43 -07003627 private void reinflateQSBIfNecessary() {
3628 if (mQsb instanceof LauncherAppWidgetHostView &&
3629 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3630 mSearchDropTargetBar.removeView(mQsb);
3631 mQsb = null;
3632 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3633 }
3634 }
3635
Michael Jurkad7c28052012-04-27 15:43:36 -07003636 @Override
3637 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003638 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003639 final List<CharSequence> text = event.getText();
3640 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003641 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003642 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003643 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003644 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003645 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003646 } else {
3647 text.add(getString(R.string.all_apps_home_button_label));
3648 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003649 return result;
3650 }
3651
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003652 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003653 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003654 */
Adam Cohen091440a2015-03-18 14:16:05 -07003655 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003656 @Override
3657 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003658 closeSystemDialogs();
3659 }
3660 }
3661
3662 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003663 * Receives notifications whenever the appwidgets are reset.
3664 */
3665 private class AppWidgetResetObserver extends ContentObserver {
3666 public AppWidgetResetObserver() {
3667 super(new Handler());
3668 }
3669
3670 @Override
3671 public void onChange(boolean selfChange) {
3672 onAppWidgetReset();
3673 }
3674 }
3675
3676 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003677 * If the activity is currently paused, signal that we need to run the passed Runnable
3678 * in onResume.
3679 *
3680 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003681 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3682 * wrong when we're not running, and if the activity comes back to what the configuration was
3683 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003684 *
3685 * Implementation of the method from LauncherModel.Callbacks.
3686 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003687 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003688 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003689 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003690 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003691 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003692 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003693 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003694 }
3695 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003696 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003697 return true;
3698 } else {
3699 return false;
3700 }
3701 }
3702
Michael Jurkac402cd92013-05-20 15:49:32 +02003703 private boolean waitUntilResume(Runnable run) {
3704 return waitUntilResume(run, false);
3705 }
3706
Michael Jurka1e2f4652013-07-08 18:03:46 -07003707 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003708 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003709 }
3710
Michael Jurka7607c2f2013-04-03 14:33:19 -07003711 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003712 * If the activity is currently paused, signal that we need to re-run the loader
3713 * in onResume.
3714 *
3715 * This needs to be called from incoming places where resources might have been loaded
3716 * while we are paused. That is becaues the Configuration might be wrong
3717 * when we're not running, and if it comes back to what it was when we
3718 * were paused, we are not restarted.
3719 *
3720 * Implementation of the method from LauncherModel.Callbacks.
3721 *
3722 * @return true if we are currently paused. The caller might be able to
3723 * skip some work in that case since we will come back again.
3724 */
3725 public boolean setLoadOnResume() {
3726 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003727 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003728 mOnResumeNeedsLoad = true;
3729 return true;
3730 } else {
3731 return false;
3732 }
3733 }
3734
3735 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003737 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003738 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003739 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003740 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003741 } else {
3742 return SCREEN_COUNT / 2;
3743 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003744 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003745
Joe Onorato9c1289c2009-08-17 11:03:03 -04003746 /**
3747 * Refreshes the shortcuts shown on the workspace.
3748 *
3749 * Implementation of the method from LauncherModel.Callbacks.
3750 */
3751 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003752 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003753
Michael Jurka7607c2f2013-04-03 14:33:19 -07003754 // If we're starting binding all over again, clear any bind calls we'd postponed in
3755 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3756 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003757 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003758
Winson Chungd64d1762013-08-20 14:37:16 -07003759 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003760 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003761 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003762
Michael Jurka05bf6442011-11-30 20:28:53 -08003763 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003764 if (mHotseat != null) {
3765 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003766 }
3767 }
3768
Adam Cohendcd297f2013-06-18 13:13:40 -07003769 @Override
3770 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003771 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003772
Adam Cohen5084cba2013-09-03 12:01:16 -07003773 // If there are no screens, we need to have an empty screen
3774 if (orderedScreenIds.size() == 0) {
3775 mWorkspace.addExtraEmptyScreen();
3776 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003777
3778 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003779 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003780 if (hasCustomContentToLeft()) {
3781 mWorkspace.createCustomContentContainer();
3782 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003783 }
Winson Chung64359a52013-07-08 17:17:08 -07003784 }
3785
3786 @Override
3787 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003788 // Log to disk
3789 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3790 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3791 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003792 int count = orderedScreenIds.size();
3793 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003794 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003795 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003796 }
3797
Adam Cohen39a06042013-07-19 14:30:12 -07003798 private boolean shouldShowWeightWatcher() {
3799 String spKey = LauncherAppState.getSharedPreferencesKey();
3800 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003801 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003802
3803 return show;
3804 }
3805
3806 private void toggleShowWeightWatcher() {
3807 String spKey = LauncherAppState.getSharedPreferencesKey();
3808 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3809 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3810
3811 show = !show;
3812
3813 SharedPreferences.Editor editor = sp.edit();
3814 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3815 editor.commit();
3816
3817 if (mWeightWatcher != null) {
3818 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3819 }
3820 }
3821
Winson Chungd64d1762013-08-20 14:37:16 -07003822 public void bindAppsAdded(final ArrayList<Long> newScreens,
3823 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003824 final ArrayList<ItemInfo> addAnimated,
3825 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003826 Runnable r = new Runnable() {
3827 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003828 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003829 }
3830 };
3831 if (waitUntilResume(r)) {
3832 return;
3833 }
3834
Winson Chungd64d1762013-08-20 14:37:16 -07003835 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003836 if (newScreens != null) {
3837 bindAddScreens(newScreens);
3838 }
Winson Chungd64d1762013-08-20 14:37:16 -07003839
3840 // We add the items without animation on non-visible pages, and with
3841 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003842 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003843 bindItems(addNotAnimated, 0,
3844 addNotAnimated.size(), false);
3845 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003846 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003847 bindItems(addAnimated, 0,
3848 addAnimated.size(), true);
3849 }
Winson Chungc58497e2013-09-03 17:48:37 -07003850
Winson Chung87412982013-10-03 18:34:14 -07003851 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003852 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003853
Winson Chungb745afb2015-03-02 11:51:23 -08003854 if (addedApps != null && mAppsView != null) {
3855 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003856 }
Winson Chungd64d1762013-08-20 14:37:16 -07003857 }
3858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003859 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 * Bind the items start-end from the list.
3861 *
3862 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003863 */
Winson Chung64359a52013-07-08 17:17:08 -07003864 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3865 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003866 Runnable r = new Runnable() {
3867 public void run() {
3868 bindItems(shortcuts, start, end, forceAnimateIcons);
3869 }
3870 };
3871 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003872 return;
3873 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003874
Winson Chungf0c6ae02012-03-21 16:10:31 -07003875 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003876 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3877 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003878 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003879 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003880 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003881 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003882 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003883
3884 // Short circuit if we are loading dock items for a configuration which has no dock
3885 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3886 mHotseat == null) {
3887 continue;
3888 }
3889
Joe Onorato9c1289c2009-08-17 11:03:03 -04003890 switch (item.itemType) {
3891 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3892 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003893 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003894 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003895
Winson Chung64359a52013-07-08 17:17:08 -07003896 /*
3897 * TODO: FIX collision case
3898 */
Winson Chungce376632013-07-11 16:12:41 -07003899 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3900 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3901 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003902 View v = cl.getChildAt(item.cellX, item.cellY);
3903 Object tag = v.getTag();
3904 String desc = "Collision while binding workspace item: " + item
3905 + ". Collides with " + tag;
3906 if (LauncherAppState.isDogfoodBuild()) {
3907 throw (new RuntimeException(desc));
3908 } else {
3909 Log.d(TAG, desc);
3910 }
Winson Chungce376632013-07-11 16:12:41 -07003911 }
Winson Chung64359a52013-07-08 17:17:08 -07003912 }
3913
Adam Cohendcd297f2013-06-18 13:13:40 -07003914 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3915 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003916 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003917 // Animate all the applications up now
3918 shortcut.setAlpha(0f);
3919 shortcut.setScaleX(0f);
3920 shortcut.setScaleY(0f);
3921 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003922 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003923 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003924 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003925 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003926 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003927 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003928 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003929 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3930 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003931 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003932 default:
3933 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003935 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003936
Winson Chung997a9232013-07-24 15:33:46 -07003937 if (animateIcons) {
3938 // Animate to the correct page
3939 if (newShortcutsScreenId > -1) {
3940 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003941 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003942 final Runnable startBounceAnimRunnable = new Runnable() {
3943 public void run() {
3944 anim.playTogether(bounceAnims);
3945 anim.start();
3946 }
3947 };
Winson Chung997a9232013-07-24 15:33:46 -07003948 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003949 // We post the animation slightly delayed to prevent slowdowns
3950 // when we are loading right after we return to launcher.
3951 mWorkspace.postDelayed(new Runnable() {
3952 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003953 if (mWorkspace != null) {
3954 mWorkspace.snapToPage(newScreenIndex);
3955 mWorkspace.postDelayed(startBounceAnimRunnable,
3956 NEW_APPS_ANIMATION_DELAY);
3957 }
Winson Chung94d67682013-09-25 16:29:40 -07003958 }
3959 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003960 } else {
3961 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003962 }
3963 }
Winson Chung64359a52013-07-08 17:17:08 -07003964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003966 }
3967
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968 /**
3969 * Implementation of the method from LauncherModel.Callbacks.
3970 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003971 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003972 Runnable r = new Runnable() {
3973 public void run() {
3974 bindFolders(folders);
3975 }
3976 };
3977 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003978 return;
3979 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003980 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003982
3983 /**
3984 * Add the views for a widget to the workspace.
3985 *
3986 * Implementation of the method from LauncherModel.Callbacks.
3987 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003988 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003989 Runnable r = new Runnable() {
3990 public void run() {
3991 bindAppWidget(item);
3992 }
3993 };
3994 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003995 return;
3996 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003997
Daniel Sandler843e8602010-06-07 14:59:01 -04003998 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3999 if (DEBUG_WIDGETS) {
4000 Log.d(TAG, "bindAppWidget: " + item);
4001 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002 final Workspace workspace = mWorkspace;
4003
Adam Cohen59400422014-03-05 18:07:04 -08004004 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00004005 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08004006
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004007 if (!mIsSafeModeEnabled
4008 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4009 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004010 if (appWidgetInfo == null) {
4011 if (DEBUG_WIDGETS) {
4012 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4013 + " belongs to component " + item.providerName
4014 + ", as the povider is null");
4015 }
4016 LauncherModel.deleteItemFromDatabase(this, item);
4017 return;
4018 }
4019 // Note: This assumes that the id remap broadcast is received before this step.
4020 // If that is not the case, the id remap will be ignored and user may see the
4021 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07004022 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004023 pendingInfo.spanX = item.spanX;
4024 pendingInfo.spanY = item.spanY;
4025 pendingInfo.minSpanX = item.minSpanX;
4026 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004027 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004028 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004029
Sunny Goyalff572272014-07-23 13:58:07 -07004030 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004031 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4032 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004033
4034 // TODO consider showing a permission dialog when the widget is clicked.
4035 if (!success) {
4036 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4037 if (DEBUG_WIDGETS) {
4038 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4039 + " belongs to component " + item.providerName
4040 + ", as the launcher is unable to bing a new widget id");
4041 }
4042 LauncherModel.deleteItemFromDatabase(this, item);
4043 return;
4044 }
4045
4046 item.appWidgetId = newWidgetId;
4047
4048 // If the widget has a configure activity, it is still needs to set it up, otherwise
4049 // the widget is ready to go.
4050 item.restoreStatus = (appWidgetInfo.configure == null)
4051 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4052 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4053
4054 LauncherModel.updateItemInDatabase(this, item);
4055 }
4056
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004057 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004058 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004059 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004060 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4061 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004062 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004063
Sunny Goyal651077b2014-06-30 14:15:31 -07004064 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4065 } else {
4066 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004067 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4068 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004069 view.updateIcon(mIconCache);
4070 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004071 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004072 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004073 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004076 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004077
Adam Cohendcd297f2013-06-18 13:13:40 -07004078 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004079 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004080 if (!item.isCustomWidget()) {
4081 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4082 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004083
Joe Onorato9c1289c2009-08-17 11:03:03 -04004084 workspace.requestLayout();
4085
Daniel Sandler843e8602010-06-07 14:59:01 -04004086 if (DEBUG_WIDGETS) {
4087 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4088 + (SystemClock.uptimeMillis()-start) + "ms");
4089 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004090 }
4091
Sunny Goyalff572272014-07-23 13:58:07 -07004092 /**
4093 * Restores a pending widget.
4094 *
4095 * @param appWidgetId The app widget id
4096 * @param cellInfo The position on screen where to create the widget.
4097 */
4098 private void completeRestoreAppWidget(final int appWidgetId) {
4099 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4100 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4101 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4102 return;
4103 }
4104
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004105 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004106 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4107
4108 mWorkspace.reinflateWidgetsIfNecessary();
4109 LauncherModel.updateItemInDatabase(this, info);
4110 }
4111
Adam Cohen1462de32012-07-24 22:34:36 -07004112 public void onPageBoundSynchronously(int page) {
4113 mSynchronouslyBoundPages.add(page);
4114 }
4115
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116 /**
4117 * Callback saying that there aren't any more items to bind.
4118 *
4119 * Implementation of the method from LauncherModel.Callbacks.
4120 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004121 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004122 Runnable r = new Runnable() {
4123 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004124 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004125 }
4126 };
4127 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004128 return;
4129 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004130 if (mSavedState != null) {
4131 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004132 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004133 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004134 mSavedState = null;
4135 }
4136
Adam Cohen1462de32012-07-24 22:34:36 -07004137 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004138
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004139 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004140 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004141
4142 // If we received the result of any pending adds while the loader was running (e.g. the
4143 // widget configuration forced an orientation change), process them now.
4144 if (sPendingAddItem != null) {
4145 final long screenId = completeAdd(sPendingAddItem);
4146
4147 // TODO: this moves the user to the page where the pending item was added. Ideally,
4148 // the screen would be guaranteed to exist after bind, and the page would be set through
4149 // the workspace restore process.
4150 mWorkspace.post(new Runnable() {
4151 @Override
4152 public void run() {
4153 mWorkspace.snapToScreenId(screenId);
4154 }
4155 });
4156 sPendingAddItem = null;
4157 }
4158
Sunny Goyal756adbc2015-04-16 15:20:51 -07004159 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004160
4161 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004162 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004163 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004164 }
4165
Adam Cohen3ed316a2014-07-23 18:21:20 -07004166 private void sendLoadingCompleteBroadcastIfNecessary() {
4167 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4168 String permission =
4169 getResources().getString(R.string.receive_first_load_broadcast_permission);
4170 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4171 sendBroadcast(intent, permission);
4172 SharedPreferences.Editor editor = mSharedPrefs.edit();
4173 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4174 editor.apply();
4175 }
4176 }
4177
Winson Chunga0b7e862013-09-05 16:03:15 -07004178 public boolean isAllAppsButtonRank(int rank) {
4179 if (mHotseat != null) {
4180 return mHotseat.isAllAppsButtonRank(rank);
4181 }
4182 return false;
4183 }
4184
Winson Chunga2413752012-04-03 14:22:34 -07004185 private boolean canRunNewAppsAnimation() {
4186 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4187 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4188 }
4189
Winson Chungc9168342013-06-26 14:54:55 -07004190 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4191 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4192 PropertyValuesHolder.ofFloat("alpha", 1f),
4193 PropertyValuesHolder.ofFloat("scaleX", 1f),
4194 PropertyValuesHolder.ofFloat("scaleY", 1f));
4195 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4196 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4197 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4198 return bounceAnim;
4199 }
4200
Adam Cohen27772732013-11-11 14:00:56 +00004201 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004202 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004203 }
4204
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004205 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004206 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004207 }
4208
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004209 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004210 if (mSearchDropTargetBar == null) {
4211 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004212 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004213 if (mQsb != null) {
4214 mSearchDropTargetBar.removeView(mQsb);
4215 mQsb = null;
4216 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004217 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004218 }
4219
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004220 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004221 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4222 * multiple calls to bind the same list.)
4223 */
4224 @Thunk ArrayList<AppInfo> mTmpAppsList;
4225 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4226 public void run() {
4227 bindAllApplications(mTmpAppsList);
4228 mTmpAppsList = null;
4229 }
4230 };
4231
4232 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004233 * Add the icons for all apps.
4234 *
4235 * Implementation of the method from LauncherModel.Callbacks.
4236 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004237 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004238 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4239 mTmpAppsList = apps;
4240 return;
4241 }
4242
Winson Chungb745afb2015-03-02 11:51:23 -08004243 if (mAppsView != null) {
4244 mAppsView.setApps(apps);
4245 }
Adam Cohen9211d422014-10-07 18:14:53 -07004246 if (mLauncherCallbacks != null) {
4247 mLauncherCallbacks.bindAllApplications(apps);
4248 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004249 }
4250
4251 /**
4252 * A package was updated.
4253 *
4254 * Implementation of the method from LauncherModel.Callbacks.
4255 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004256 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004257 Runnable r = new Runnable() {
4258 public void run() {
4259 bindAppsUpdated(apps);
4260 }
4261 };
4262 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004263 return;
4264 }
4265
Winson Chungb745afb2015-03-02 11:51:23 -08004266 if (mAppsView != null) {
4267 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004268 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004269 }
4270
Sunny Goyal4390ace2014-10-13 11:33:11 -07004271 @Override
4272 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4273 Runnable r = new Runnable() {
4274 public void run() {
4275 bindWidgetsRestored(widgets);
4276 }
4277 };
4278 if (waitUntilResume(r)) {
4279 return;
4280 }
4281 mWorkspace.widgetsRestored(widgets);
4282 }
4283
Joe Onorato9c1289c2009-08-17 11:03:03 -04004284 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004285 * Some shortcuts were updated in the background.
4286 *
4287 * Implementation of the method from LauncherModel.Callbacks.
4288 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004289 @Override
4290 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4291 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004292 Runnable r = new Runnable() {
4293 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004294 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004295 }
4296 };
4297 if (waitUntilResume(r)) {
4298 return;
4299 }
4300
Sunny Goyal4390ace2014-10-13 11:33:11 -07004301 if (!updated.isEmpty()) {
4302 mWorkspace.updateShortcuts(updated);
4303 }
4304
4305 if (!removed.isEmpty()) {
4306 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4307 for (ShortcutInfo si : removed) {
4308 removedComponents.add(si.getTargetComponent());
4309 }
4310 mWorkspace.removeItemsByComponentName(removedComponents, user);
4311 // Notify the drag controller
4312 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004313 }
4314 }
4315
4316 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004317 * Update the state of a package, typically related to install state.
4318 *
4319 * Implementation of the method from LauncherModel.Callbacks.
4320 */
Sunny Goyale755d462014-07-22 13:48:29 -07004321 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004322 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4323 Runnable r = new Runnable() {
4324 public void run() {
4325 bindRestoreItemsChange(updates);
4326 }
4327 };
4328 if (waitUntilResume(r)) {
4329 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004330 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004331
Sunny Goyal756adbc2015-04-16 15:20:51 -07004332 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004333 }
4334
4335 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004336 * A package was uninstalled. We take both the super set of packageNames
4337 * in addition to specific applications to remove, the reason being that
4338 * this can be called when a package is updated as well. In that scenario,
4339 * we only remove specific components from the workspace, where as
4340 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004341 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004342 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004343 * Implementation of the method from LauncherModel.Callbacks.
4344 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004345 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004346 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004347 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004348 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004349 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004350 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004351 }
Winson Chungd64d1762013-08-20 14:37:16 -07004352 };
4353 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004354 return;
4355 }
4356
Sunny Goyal1a745e82014-10-02 15:58:31 -07004357 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004358 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4359 for (AppInfo info : appInfos) {
4360 removedComponents.add(info.componentName);
4361 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004362 if (!packageNames.isEmpty()) {
4363 mWorkspace.removeItemsByPackageName(packageNames, user);
4364 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004365 if (!removedComponents.isEmpty()) {
4366 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004367 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004368 // Notify the drag controller
4369 mDragController.onAppsRemoved(packageNames, removedComponents);
4370
Sunny Goyal1a745e82014-10-02 15:58:31 -07004371 } else {
4372 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004373 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004374
Winson Chungdf95eb12013-10-16 14:57:07 -07004375 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004376 if (mAppsView != null) {
4377 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004378 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004379 }
Joe Onoratobe386092009-11-17 17:32:16 -08004380
Michael Jurkac402cd92013-05-20 15:49:32 +02004381 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004382 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004383 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004384 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004385 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004386
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004387 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004388 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004389 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004390 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004391 return;
4392 }
4393
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004394 if (mWidgetsView != null && model != null) {
4395 mWidgetsView.addWidgets(model);
4396 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004397 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004398 }
4399
Winson Chung400438b2011-01-16 17:53:48 -08004400 private int mapConfigurationOriActivityInfoOri(int configOri) {
4401 final Display d = getWindowManager().getDefaultDisplay();
4402 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4403 switch (d.getRotation()) {
4404 case Surface.ROTATION_0:
4405 case Surface.ROTATION_180:
4406 // We are currently in the same basic orientation as the natural orientation
4407 naturalOri = configOri;
4408 break;
4409 case Surface.ROTATION_90:
4410 case Surface.ROTATION_270:
4411 // We are currently in the other basic orientation to the natural orientation
4412 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4413 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4414 break;
4415 }
4416
4417 int[] oriMap = {
4418 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4419 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4420 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4421 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4422 };
4423 // Since the map starts at portrait, we need to offset if this device's natural orientation
4424 // is landscape.
4425 int indexOffset = 0;
4426 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4427 indexOffset = 1;
4428 }
4429 return oriMap[(d.getRotation() + indexOffset) % 4];
4430 }
Adam Cohen446e9402011-09-15 18:21:21 -07004431
Winson Chung4b919f82012-05-01 10:44:08 -07004432 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004433 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004434 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4435 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4436 .getConfiguration().orientation));
4437 } else {
4438 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4439 }
Winson Chung4b919f82012-05-01 10:44:08 -07004440 }
4441 }
4442 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004443 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004444 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004445 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004446 } else {
4447 mHandler.postDelayed(new Runnable() {
4448 public void run() {
4449 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4450 }
4451 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004452 }
Winson Chung4b919f82012-05-01 10:44:08 -07004453 }
Winson Chung400438b2011-01-16 17:53:48 -08004454 }
4455
Winson Chunge43a1e72014-01-15 10:33:02 -08004456 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004457 if (mLauncherCallbacks != null) {
4458 return mLauncherCallbacks.isLauncherPreinstalled();
4459 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004460 PackageManager pm = getPackageManager();
4461 try {
4462 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4463 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4464 return true;
4465 } else {
4466 return false;
4467 }
4468 } catch (NameNotFoundException e) {
4469 e.printStackTrace();
4470 return false;
4471 }
4472 }
4473
Adam Cohenea90f832014-05-21 15:03:34 -07004474 /**
4475 * This method indicates whether or not we should suggest default wallpaper dimensions
4476 * when our wallpaper cropper was not yet used to set a wallpaper.
4477 */
4478 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004479 if (mLauncherCallbacks != null) {
4480 return mLauncherCallbacks.overrideWallpaperDimensions();
4481 }
Adam Cohenea90f832014-05-21 15:03:34 -07004482 return true;
4483 }
4484
Adam Cohen432609a2014-03-13 17:03:22 -07004485 /**
4486 * To be overridden by subclasses to indicate that there is an activity to launch
4487 * before showing the standard launcher experience.
4488 */
4489 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004490 if (mLauncherCallbacks != null) {
4491 return mLauncherCallbacks.hasFirstRunActivity();
4492 }
Adam Cohen432609a2014-03-13 17:03:22 -07004493 return false;
4494 }
4495
4496 /**
4497 * To be overridden by subclasses to launch any first run activity
4498 */
4499 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004500 if (mLauncherCallbacks != null) {
4501 return mLauncherCallbacks.getFirstRunActivity();
4502 }
Adam Cohen432609a2014-03-13 17:03:22 -07004503 return null;
4504 }
4505
Winson Chung2826a402015-04-17 16:18:53 -07004506 /**
4507 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004508 */
4509 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004510 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4511 return false;
4512 }
4513
Winson Chung2826a402015-04-17 16:18:53 -07004514 if (mLauncherCallbacks != null) {
4515 return mLauncherCallbacks.overrideAllAppsSearch();
4516 }
4517 return false;
4518 }
4519
Winson Chunga6945242014-01-08 14:04:34 -08004520 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004521 return !ActivityManager.isRunningInTestHarness() &&
4522 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004523 }
4524
Adam Cohen4a9423d2014-03-28 14:22:31 -07004525 protected boolean hasRunFirstRunActivity() {
4526 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4527 }
4528
Adam Cohen432609a2014-03-13 17:03:22 -07004529 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004530 if (shouldRunFirstRunActivity() &&
4531 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004532 Intent firstRunIntent = getFirstRunActivity();
4533 if (firstRunIntent != null) {
4534 startActivity(firstRunIntent);
4535 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004536 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004537 }
4538 }
Adam Cohen432609a2014-03-13 17:03:22 -07004539 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004540 }
4541
4542 private void markFirstRunActivityShown() {
4543 SharedPreferences.Editor editor = mSharedPrefs.edit();
4544 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4545 editor.apply();
4546 }
4547
Adam Cohen432609a2014-03-13 17:03:22 -07004548 /**
4549 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4550 * screen that must be displayed and dismissed.
4551 */
4552 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004553 if (mLauncherCallbacks != null) {
4554 return mLauncherCallbacks.hasDismissableIntroScreen();
4555 }
Adam Cohen432609a2014-03-13 17:03:22 -07004556 return false;
4557 }
4558
4559 /**
4560 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4561 */
4562 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004563 if (mLauncherCallbacks != null) {
4564 return mLauncherCallbacks.getIntroScreen();
4565 }
Adam Cohen432609a2014-03-13 17:03:22 -07004566 return null;
4567 }
4568
4569 /**
4570 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4571 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4572 */
4573 private boolean shouldShowIntroScreen() {
4574 return hasDismissableIntroScreen() &&
4575 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4576 }
4577
4578 protected void showIntroScreen() {
4579 View introScreen = getIntroScreen();
4580 changeWallpaperVisiblity(false);
4581 if (introScreen != null) {
4582 mDragLayer.showOverlayView(introScreen);
4583 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004584 if (mLauncherOverlayContainer != null) {
4585 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4586 }
Adam Cohen432609a2014-03-13 17:03:22 -07004587 }
4588
4589 public void dismissIntroScreen() {
4590 markIntroScreenDismissed();
4591 if (showFirstRunActivity()) {
4592 // We delay hiding the intro view until the first run activity is showing. This
4593 // avoids a blip.
4594 mWorkspace.postDelayed(new Runnable() {
4595 @Override
4596 public void run() {
4597 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004598 if (mLauncherOverlayContainer != null) {
4599 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4600 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004601 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004602 }
4603 }, ACTIVITY_START_DELAY);
4604 } else {
4605 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004606 if (mLauncherOverlayContainer != null) {
4607 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4608 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004609 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004610 }
4611 changeWallpaperVisiblity(true);
4612 }
4613
4614 private void markIntroScreenDismissed() {
4615 SharedPreferences.Editor editor = mSharedPrefs.edit();
4616 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4617 editor.apply();
4618 }
4619
Adam Cohen091440a2015-03-18 14:16:05 -07004620 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004621 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4622 // on the device, then we always show the first run cling experience (or if there is no
4623 // launcher2). Otherwise, we prompt the user upon started for migration
4624 LauncherClings launcherClings = new LauncherClings(this);
4625 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4626 if (mModel.canMigrateFromOldLauncherDb(this)) {
4627 launcherClings.showMigrationCling();
4628 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004629 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004630 }
4631 }
4632 }
4633
Winson Chunga6945242014-01-08 14:04:34 -08004634 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004635 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4636 if (mHotseat != null) mHotseat.setAlpha(1f);
4637 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4638 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004639 }
4640
4641 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004642 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4643 if (mHotseat != null) mHotseat.setAlpha(0f);
4644 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4645 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004646 }
4647
Mathew Inwood72fbec12013-11-19 15:45:07 +00004648 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004649 // Called from search suggestion, not supported in other profiles.
4650 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4651 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4652 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4653 myUser);
4654 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004655 return null;
4656 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004657 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004658 }
4659
4660 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4661 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004662 // Called from search suggestion, not supported in other profiles.
4663 return createShortcutDragInfo(shortcutIntent, caption, icon,
4664 UserHandleCompat.myUserHandle());
4665 }
4666
4667 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4668 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004669 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004670 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004671 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004672 }
4673
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004674 protected void moveWorkspaceToDefaultScreen() {
4675 mWorkspace.moveToDefaultScreen(false);
4676 }
4677
Mathew Inwood72fbec12013-11-19 15:45:07 +00004678 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4679 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004680 mWorkspace.onExternalDragStartedWithItem(dragView);
4681 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004682 }
4683
Anjali Koppalf5d29132014-02-28 13:33:27 -08004684 @Override
4685 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004686 if (mLauncherCallbacks != null) {
4687 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4688 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004689 }
4690
Winson Chung80baf5a2010-08-09 16:03:15 -07004691 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004692 * Prints out out state for debugging.
4693 */
4694 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004695 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004696 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004697 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4698 Log.d(TAG, "mRestoring=" + mRestoring);
4699 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4700 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004701 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004702 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004703 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004704
Daniel Sandler325dc232013-06-05 22:57:57 -04004705 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004706 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004707
4708 @Override
4709 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4710 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004711 synchronized (sDumpLogs) {
4712 writer.println(" ");
4713 writer.println("Debug logs: ");
4714 for (int i = 0; i < sDumpLogs.size(); i++) {
4715 writer.println(" " + sDumpLogs.get(i));
4716 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004717 }
Adam Cohen9211d422014-10-07 18:14:53 -07004718 if (mLauncherCallbacks != null) {
4719 mLauncherCallbacks.dump(prefix, fd, writer, args);
4720 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004721 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004722
4723 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004724 if (DEBUG_DUMP_LOG) {
4725 synchronized (sDumpLogs) {
4726 Log.d(TAG, "");
4727 Log.d(TAG, "*********************");
4728 Log.d(TAG, "Launcher debug logs: ");
4729 for (int i = 0; i < sDumpLogs.size(); i++) {
4730 Log.d(TAG, " " + sDumpLogs.get(i));
4731 }
4732 Log.d(TAG, "*********************");
4733 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004734 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004735 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004736 }
4737
4738 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004739 addDumpLog(tag, log, null, debugLog);
4740 }
4741
4742 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004743 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004744 if (e != null) {
4745 Log.d(tag, log, e);
4746 } else {
4747 Log.d(tag, log);
4748 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004749 }
Winson Chungede41292013-09-19 16:27:36 -07004750 if (DEBUG_DUMP_LOG) {
4751 sDateStamp.setTime(System.currentTimeMillis());
4752 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004753 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4754 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004755 }
Winson Chungede41292013-09-19 16:27:36 -07004756 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004757 }
4758
Adam Cohen59400422014-03-05 18:07:04 -08004759 public static CustomAppWidget getCustomAppWidget(String name) {
4760 return sCustomAppWidgets.get(name);
4761 }
4762
4763 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4764 return sCustomAppWidgets;
4765 }
4766
Winson Chungede41292013-09-19 16:27:36 -07004767 public void dumpLogsToLocalData() {
4768 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004769 new AsyncTask<Void, Void, Void>() {
4770 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004771 boolean success = false;
4772 sDateStamp.setTime(sRunStart);
4773 String FILENAME = sDateStamp.getMonth() + "-"
4774 + sDateStamp.getDay() + "_"
4775 + sDateStamp.getHours() + "-"
4776 + sDateStamp.getMinutes() + "_"
4777 + sDateStamp.getSeconds() + ".txt";
4778
4779 FileOutputStream fos = null;
4780 File outFile = null;
4781 try {
4782 outFile = new File(getFilesDir(), FILENAME);
4783 outFile.createNewFile();
4784 fos = new FileOutputStream(outFile);
4785 } catch (Exception e) {
4786 e.printStackTrace();
4787 }
4788 if (fos != null) {
4789 PrintWriter writer = new PrintWriter(fos);
4790
4791 writer.println(" ");
4792 writer.println("Debug logs: ");
4793 synchronized (sDumpLogs) {
4794 for (int i = 0; i < sDumpLogs.size(); i++) {
4795 writer.println(" " + sDumpLogs.get(i));
4796 }
4797 }
4798 writer.close();
4799 }
4800 try {
4801 if (fos != null) {
4802 fos.close();
4803 success = true;
4804 }
4805 } catch (IOException e) {
4806 e.printStackTrace();
4807 }
Michael Jurka43467462013-11-14 12:03:58 +01004808 return null;
Winson Chungede41292013-09-19 16:27:36 -07004809 }
Michael Jurka43467462013-11-14 12:03:58 +01004810 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004811 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004813}
Michael Jurka2763be32011-02-24 11:19:57 -08004814
Dan Sandlerd5024042014-01-09 15:01:33 -05004815interface DebugIntents {
4816 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4817 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004818}