blob: ee8be6267318409c4cd6abeafa5585e26748f8ce [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;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080046import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
91import android.widget.FrameLayout;
92import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale2df0622015-04-24 11:27:00 -0700103import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700104import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700105import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700106import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700107import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700108
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.DataInputStream;
110import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700111import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700116import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800117import java.lang.reflect.InvocationTargetException;
118import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700121import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700124import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700125import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000126import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128/**
129 * Default launcher application.
130 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100131public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700132 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800133 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
134 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700135 static final String TAG = "Launcher";
Hyunyoung Song3f471442015-04-08 19:01:34 -0700136 static final boolean LOGD = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Daniel Sandlerf061f822013-06-27 13:59:36 -0400138 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700139 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700140 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700142 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700143
Dan Sandlerd5024042014-01-09 15:01:33 -0500144 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700149 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Michael Jurka8b805b12012-04-18 14:23:14 -0700151 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700152 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700153
Mathew Inwood876a8462013-06-14 14:12:41 +0100154 /**
155 * IntentStarter uses request codes starting with this. This must be greater than all activity
156 * request codes used internally.
157 */
158 protected static final int REQUEST_LAST = 100;
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
161
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800162 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Michael Jurka0a457bf2012-11-19 14:05:05 -0800164 // To turn on these properties, type
165 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800166 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chung2672ff92012-05-04 16:22:30 -0700168 // The Intent extra that defines whether to ignore the launch animation
169 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400170 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
173 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700174 // Type: int
175 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
178 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: boolean
185 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
186 // Type: long
187 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: int[]
197 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Adam Cohen432609a2014-03-13 17:03:22 -0700199 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800200 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
201
Adam Cohen3ed316a2014-07-23 18:21:20 -0700202 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
203 static final String ACTION_FIRST_LOAD_COMPLETE =
204 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
205
Adam Cohen39a06042013-07-19 14:30:12 -0700206 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700207 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700208
Sunny Goyal594d76d2014-11-06 10:12:54 -0800209 private static final String QSB_WIDGET_ID = "qsb_widget_id";
210 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
211
Winson Chunga6945242014-01-08 14:04:34 -0800212 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
213
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700214 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800215 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700219 private boolean mIsSafeModeEnabled;
220
Adam Cohenc2d6e892014-10-16 09:49:24 -0700221 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
222 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700223 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700226 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
227 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700228 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000230 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
231 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
232
Winson Chunga2413752012-04-03 14:22:34 -0700233 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700234 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
235 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700237
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800238 private final BroadcastReceiver mCloseSystemDialogsReceiver
239 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800240 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 private LayoutInflater mInflater;
243
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700245 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800246 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700247 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800248 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700249 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700250
Sunny Goyalffe83f12014-08-14 17:39:34 -0700251 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700252 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Adam Cohen091440a2015-03-18 14:16:05 -0700254 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800255 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800256 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700257
Michael Jurka0280c3b2010-09-17 15:00:07 -0700258 private int[] mTmpAddItemCellCoordinates = new int[2];
259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private FolderInfo mFolderInfo;
261
Winson Chung3d503fb2011-07-13 17:25:49 -0700262 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800263 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700264
Michael Jurka838a4ca2011-02-07 13:33:06 -0800265 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700266
Winson Chungc51db6a2011-10-05 11:44:49 -0700267 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700268
269 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700270 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700271
272 // Main container view for the widget tray screen.
273 private WidgetsContainerView mWidgetsView;
274
Winson Chungf0ea4d32011-06-06 14:27:16 -0700275 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800276 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700279 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
280 // scroll issues (because the workspace may not have been measured yet) and extra work.
281 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
282 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
284 private SpannableStringBuilder mDefaultKeySsb = null;
285
Adam Cohen091440a2015-03-18 14:16:05 -0700286 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800288 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 private boolean mRestoring;
290 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700291 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292
Michael Jurka1e2f4652013-07-08 18:03:46 -0700293 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700294 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 private Bundle mSavedInstanceState;
297
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800299 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700300 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800301 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700302 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800303 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400304
Adam Cohen091440a2015-03-18 14:16:05 -0700305 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700306
Sunny Goyale2df0622015-04-24 11:27:00 -0700307 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700308
Adam Cohen61f560d2013-09-30 15:58:20 -0700309 private View.OnTouchListener mHapticFeedbackTouchListener;
310
Adam Cohended9f8d2010-11-03 13:25:16 -0700311 // Related to the auto-advancing of widgets
312 private final int ADVANCE_MSG = 1;
313 private final int mAdvanceInterval = 20000;
314 private final int mAdvanceStagger = 250;
315 private long mAutoAdvanceSentTime;
316 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700317 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700318 new HashMap<View, AppWidgetProviderInfo>();
319
Winson Chung400438b2011-01-16 17:53:48 -0800320 // Determines how long to wait after a rotation before restoring the screen orientation to
321 // match the sensor state.
322 private final int mRestoreScreenOrientationDelay = 500;
323
Adam Cohen091440a2015-03-18 14:16:05 -0700324 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700325
Adam Cohen1462de32012-07-24 22:34:36 -0700326 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700327 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700328
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700329 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700330 static Date sDateStamp = new Date();
331 static DateFormat sDateFormat =
332 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
333 static long sRunStart = System.currentTimeMillis();
334 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700335
Winson Chung46353de2012-02-16 14:05:10 -0800336 // We only want to get the SharedPreferences once since it does an FS stat each time we get
337 // it from the context.
338 private SharedPreferences mSharedPrefs;
339
Winson Chungf0c6ae02012-03-21 16:10:31 -0700340 // Holds the page that we need to animate to, and the icon views that we need to animate up
341 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700342 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700343 private Bitmap mFolderIconBitmap;
344 private Canvas mFolderIconCanvas;
345 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700346
Michael Jurkaddd62e92011-02-16 17:49:14 -0800347 private BubbleTextView mWaitingForResume;
348
Adam Cohen59400422014-03-05 18:07:04 -0800349 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
350 new HashMap<String, CustomAppWidget>();
351
352 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
353 static {
354 if (ENABLE_CUSTOM_WIDGET_TEST) {
355 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
356 }
357 }
358
Chet Haasea8f996d2015-02-17 12:56:04 -0800359 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
360 private static Method sClipRevealMethod = null;
361 static {
362 Class<?> activityOptionsClass = ActivityOptions.class;
363 try {
364 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
365 View.class, int.class, int.class, int.class, int.class);
366 } catch (Exception e) {
367 // Earlier version
368 }
369 }
370
Adam Cohen091440a2015-03-18 14:16:05 -0700371 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700372 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700373 if (mWorkspace != null) {
374 mWorkspace.buildPageHardwareLayers();
375 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700376 }
377 };
378
Adam Cohendb364c32014-05-20 14:23:40 -0700379 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380
Adam Cohen091440a2015-03-18 14:16:05 -0700381 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800382 int requestCode;
383 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700384 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700385 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800386 int cellX;
387 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700388 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800389 }
390
Daniel Sandlerff02d492013-08-05 02:12:05 -0400391 private Stats mStats;
392
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700393 FocusIndicatorView mFocusHandler;
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 @Override
396 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700397 if (DEBUG_STRICT_MODE) {
398 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
399 .detectDiskReads()
400 .detectDiskWrites()
401 .detectNetwork() // or .detectAll() for all detectable problems
402 .penaltyLog()
403 .build());
404 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
405 .detectLeakedSqlLiteObjects()
406 .detectLeakedClosableObjects()
407 .penaltyLog()
408 .penaltyDeath()
409 .build());
410 }
411
Adam Cohen9211d422014-10-07 18:14:53 -0700412 if (mLauncherCallbacks != null) {
413 mLauncherCallbacks.preOnCreate();
414 }
415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400417
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400418 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400419 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700420 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700421
Winson Chung5f8afe62013-08-12 16:19:28 -0700422 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700423 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700424
425 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400426 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700427 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700428 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800429 mModel = app.setLauncher(this);
430 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700431 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400432 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800434 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Daniel Sandlerff02d492013-08-05 02:12:05 -0400436 mStats = new Stats(this);
437
Sunny Goyalffe83f12014-08-14 17:39:34 -0700438 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700439
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
441 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700442
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700443 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
444 // this also ensures that any synchronous binding below doesn't re-trigger another
445 // LauncherModel load.
446 mPaused = false;
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200449 android.os.Debug.startMethodTracing(
450 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 }
452
453 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100457 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800459 registerContentObservers();
460
Joe Onorato7c312c12009-08-13 21:36:53 -0700461 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700462
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 mSavedState = savedInstanceState;
464 restoreState(mSavedState);
465
466 if (PROFILE_STARTUP) {
467 android.os.Debug.stopMethodTracing();
468 }
469
470 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700471 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700472 // If the user leaves launcher, then we should just load items asynchronously when
473 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500474 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 } else {
476 // We only load the page synchronously if the user rotates (or triggers a
477 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800478 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 }
481
482 // For handling default keys
483 mDefaultKeySsb = new SpannableStringBuilder();
484 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800485
486 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
487 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800488
Adam Cohenf9426d52012-06-04 17:26:21 -0700489 // On large interfaces, we want the screen to auto-rotate based on the current orientation
490 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700494 if (mLauncherCallbacks.hasLauncherOverlay()) {
495 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700496 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
497 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
498 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700499 mWorkspace.setLauncherOverlay(mLauncherOverlay);
500 }
Adam Cohen9211d422014-10-07 18:14:53 -0700501 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700502
503 if (shouldShowIntroScreen()) {
504 showIntroScreen();
505 } else {
506 showFirstRunActivity();
507 showFirstRunClings();
508 }
Adam Cohen9211d422014-10-07 18:14:53 -0700509 }
510
511 private LauncherCallbacks mLauncherCallbacks;
512
513 public void onPostCreate(Bundle savedInstanceState) {
514 super.onPostCreate(savedInstanceState);
515 if (mLauncherCallbacks != null) {
516 mLauncherCallbacks.onPostCreate(savedInstanceState);
517 }
518 }
519
520 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
521 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700522 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
523 @Override
524 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700525 if (LOGD) {
526 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
527 }
Winson Chung0f785722015-04-08 10:27:49 -0700528 mAppsView.setFixedBounds(Launcher.this, bounds);
529 }
530
531 @Override
532 public void dismissAllApps() {
Winson Chungcd99cd32015-04-29 11:03:24 -0700533 // Dismiss All Apps if we aren't already paused/invisible
534 if (!mPaused) {
535 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
536 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
537 }
Winson Chung0f785722015-04-08 10:27:49 -0700538 }
539 });
Adam Cohen9211d422014-10-07 18:14:53 -0700540 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700541 }
542
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700543 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700544 public void onLauncherProviderChange() {
545 if (mLauncherCallbacks != null) {
546 mLauncherCallbacks.onLauncherProviderChange();
547 }
548 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700549
Adam Cohen9211d422014-10-07 18:14:53 -0700550 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700551 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700552 if (mLauncherCallbacks != null) {
553 return mLauncherCallbacks.hasCustomContentToLeft();
554 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700555 return false;
556 }
557
Dave Hawkeya8881582013-09-17 15:55:33 -0600558 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500559 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600560 * {@link #addToCustomContentPage}. This will only be invoked if
561 * {@link #hasCustomContentToLeft()} is {@code true}.
562 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500563 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700564 if (mLauncherCallbacks != null) {
565 mLauncherCallbacks.populateCustomContentContainer();
566 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600567 }
568
569 /**
570 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
571 * ensure the custom content page is added or removed if necessary.
572 */
573 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600574 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600575 // Not bound yet, wait for bindScreens to be called.
576 return;
577 }
578
579 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
580 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500581 mWorkspace.createCustomContentContainer();
582 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600583 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
584 mWorkspace.removeCustomContentPage();
585 }
586 }
587
Adam Cohen091440a2015-03-18 14:16:05 -0700588 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700589 if (sLocaleConfiguration == null) {
590 new AsyncTask<Void, Void, LocaleConfiguration>() {
591 @Override
592 protected LocaleConfiguration doInBackground(Void... unused) {
593 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
594 readConfiguration(Launcher.this, localeConfiguration);
595 return localeConfiguration;
596 }
597
598 @Override
599 protected void onPostExecute(LocaleConfiguration result) {
600 sLocaleConfiguration = result;
601 checkForLocaleChange(); // recursive, but now with a locale configuration
602 }
603 }.execute();
604 return;
605 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700606
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 final Configuration configuration = getResources().getConfiguration();
608
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700609 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 final String locale = configuration.locale.toString();
611
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700612 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 final int mcc = configuration.mcc;
614
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700615 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 final int mnc = configuration.mnc;
617
Romain Guy84f296c2009-11-04 15:00:44 -0800618 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619
Romain Guy84f296c2009-11-04 15:00:44 -0800620 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700621 sLocaleConfiguration.locale = locale;
622 sLocaleConfiguration.mcc = mcc;
623 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700624
Joe Onorato0589f0f2010-02-08 13:44:00 -0800625 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700626
627 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100628 new AsyncTask<Void, Void, Void>() {
629 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700630 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100631 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700632 }
Michael Jurka43467462013-11-14 12:03:58 +0100633 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700634 }
635 }
636
Adam Cohen091440a2015-03-18 14:16:05 -0700637 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700638 public String locale;
639 public int mcc = -1;
640 public int mnc = -1;
641 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700642
Adam Cohen091440a2015-03-18 14:16:05 -0700643 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700644 DataInputStream in = null;
645 try {
Helena Josol28db2802014-10-09 17:04:09 +0100646 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700647 configuration.locale = in.readUTF();
648 configuration.mcc = in.readInt();
649 configuration.mnc = in.readInt();
650 } catch (FileNotFoundException e) {
651 // Ignore
652 } catch (IOException e) {
653 // Ignore
654 } finally {
655 if (in != null) {
656 try {
657 in.close();
658 } catch (IOException e) {
659 // Ignore
660 }
661 }
662 }
663 }
664
Adam Cohen091440a2015-03-18 14:16:05 -0700665 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700666 DataOutputStream out = null;
667 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100668 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100669 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700670 out.writeUTF(configuration.locale);
671 out.writeInt(configuration.mcc);
672 out.writeInt(configuration.mnc);
673 out.flush();
674 } catch (FileNotFoundException e) {
675 // Ignore
676 } catch (IOException e) {
677 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100678 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700679 } finally {
680 if (out != null) {
681 try {
682 out.close();
683 } catch (IOException e) {
684 // Ignore
685 }
686 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 }
688 }
689
Anton Hanssona2f665f2013-09-26 12:18:32 +0100690 public Stats getStats() {
691 return mStats;
692 }
693
Bjorn Bringertc459e522013-06-07 19:36:01 +0100694 public LayoutInflater getInflater() {
695 return mInflater;
696 }
697
Hyunyoung Song3f471442015-04-08 19:01:34 -0700698 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700699 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
700 // that is subsequently removed from the workspace in startBinding().
701 return !mModel.isLoadingWorkspace();
702 }
703
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800704 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000705 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100706 if (Build.VERSION.SDK_INT >= 17) {
707 return View.generateViewId();
708 } else {
709 // View.generateViewId() is not available. The following fallback logic is a copy
710 // of its implementation.
711 for (;;) {
712 final int result = sNextGeneratedId.get();
713 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
714 int newValue = result + 1;
715 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
716 if (sNextGeneratedId.compareAndSet(result, newValue)) {
717 return result;
718 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000719 }
720 }
721 }
722
723 public int getViewIdForItem(ItemInfo info) {
724 // This cast is safe given the > 2B range for int.
725 int itemId = (int) info.id;
726 if (mItemIdToViewId.containsKey(itemId)) {
727 return mItemIdToViewId.get(itemId);
728 }
729 int viewId = generateViewId();
730 mItemIdToViewId.put(itemId, viewId);
731 return viewId;
732 }
733
734 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700735 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
736 * a configuration step, this allows the proper animations to run after other transitions.
737 */
Adam Cohendb364c32014-05-20 14:23:40 -0700738 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700739 long screenId = args.screenId;
740 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
741 // When the screen id represents an actual screen (as opposed to a rank) we make sure
742 // that the drop page actually exists.
743 screenId = ensurePendingDropLayoutExists(args.screenId);
744 }
Adam Cohendb364c32014-05-20 14:23:40 -0700745
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800746 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800747 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700748 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700749 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700750 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800751 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700752 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700753 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700754 case REQUEST_RECONFIGURE_APPWIDGET:
755 completeRestoreAppWidget(args.appWidgetId);
756 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800757 }
Michael Jurka27614d22012-04-02 06:40:22 -0700758 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
759 // if you turned the screen off and then back while in All Apps, Launcher would not
760 // return to the workspace. Clearing mAddInfo.container here fixes this issue
761 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700762 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800763 }
764
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800765 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700767 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700768 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700769 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800770 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700771
Adam Cohenad4e15c2013-10-17 16:21:35 -0700772 Runnable exitSpringLoaded = new Runnable() {
773 @Override
774 public void run() {
775 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
776 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
777 }
778 };
779
Michael Jurka8b805b12012-04-18 14:23:14 -0700780 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
783 if (resultCode == RESULT_CANCELED) {
784 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700785 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700787 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800788 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700790 }
791 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200792 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
793 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700794 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200795 }
796 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700797 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200798
Adam Cohened66b2b2012-01-23 17:28:51 -0800799 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
800 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700801
Adam Cohendb364c32014-05-20 14:23:40 -0700802 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800803 // We have special handling for widgets
804 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800805 final int appWidgetId;
806 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
807 : -1;
808 if (widgetId < 0) {
809 appWidgetId = pendingAddWidgetId;
810 } else {
811 appWidgetId = widgetId;
812 }
813
Adam Cohenad4e15c2013-10-17 16:21:35 -0700814 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800815 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700816 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
817 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700818 result = RESULT_CANCELED;
819 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700820 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700821 @Override
822 public void run() {
823 exitSpringLoadedDragModeDelayed(false, 0, null);
824 }
825 };
Adam Cohendb364c32014-05-20 14:23:40 -0700826 if (workspaceLocked) {
827 // No need to remove the empty screen if we're mid-binding, as the
828 // the bind will not add the empty screen.
829 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
830 } else {
831 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
832 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
833 }
Winson Chung5aaab772012-04-27 15:24:02 -0700834 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700835 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700836 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
837 // When the screen id represents an actual screen (as opposed to a rank)
838 // we make sure that the drop page actually exists.
839 mPendingAddInfo.screenId =
840 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
841 }
Adam Cohendb364c32014-05-20 14:23:40 -0700842 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
843
844 dropLayout.setDropPending(true);
845 final Runnable onComplete = new Runnable() {
846 @Override
847 public void run() {
848 completeTwoStageWidgetDrop(resultCode, appWidgetId);
849 dropLayout.setDropPending(false);
850 }
851 };
852 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
853 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
854 } else {
855 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
856 mPendingAddInfo);
857 sPendingAddItem = args;
858 }
Winson Chung5aaab772012-04-27 15:24:02 -0700859 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800860 return;
861 }
862
Sunny Goyalff572272014-07-23 13:58:07 -0700863 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
864 if (resultCode == RESULT_OK) {
865 // Update the widget view.
866 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
867 pendingAddWidgetId, mPendingAddInfo);
868 if (workspaceLocked) {
869 sPendingAddItem = args;
870 } else {
871 completeAdd(args);
872 }
873 }
874 // Leave the widget in the pending state if the user canceled the configure.
875 return;
876 }
877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 // The pattern used here is that a user PICKs a specific application,
879 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
882 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700883 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700884 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
885 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800886 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700887 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800888 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700889 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700890 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
891 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
Adam Cohen00074722013-10-11 17:46:09 -0700893 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700894 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700895 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800898
899 }
900
901 @Override
902 protected void onActivityResult(
903 final int requestCode, final int resultCode, final Intent data) {
904 handleActivityResult(requestCode, resultCode, data);
905 if (mLauncherCallbacks != null) {
906 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
907 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800908 }
909
Adam Cohendb364c32014-05-20 14:23:40 -0700910 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
911 appWidgetId, ItemInfo info) {
912 PendingAddArguments args = new PendingAddArguments();
913 args.requestCode = requestCode;
914 args.intent = data;
915 args.container = info.container;
916 args.screenId = info.screenId;
917 args.cellX = info.cellX;
918 args.cellY = info.cellY;
919 args.appWidgetId = appWidgetId;
920 return args;
921 }
922
923 /**
924 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
925 *
926 * @param screenId the screen id to check
927 * @return the new screen, or screenId if it exists
928 */
929 private long ensurePendingDropLayoutExists(long screenId) {
930 CellLayout dropLayout =
931 (CellLayout) mWorkspace.getScreenWithId(screenId);
932 if (dropLayout == null) {
933 // it's possible that the add screen was removed because it was
934 // empty and a re-bind occurred
935 mWorkspace.addExtraEmptyScreen();
936 return mWorkspace.commitExtraEmptyScreen();
937 } else {
938 return screenId;
939 }
940 }
941
Adam Cohen091440a2015-03-18 14:16:05 -0700942 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700943 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700944 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800945 Runnable onCompleteRunnable = null;
946 int animationType = 0;
947
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700948 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 if (resultCode == RESULT_OK) {
950 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
951 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700952 mPendingAddWidgetInfo);
953 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800954 onCompleteRunnable = new Runnable() {
955 @Override
956 public void run() {
957 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700958 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700959 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
960 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800961 }
962 };
963 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800964 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800965 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800966 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700967 if (mDragLayer.getAnimatedView() != null) {
968 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
969 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
970 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700971 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700972 // The animated view may be null in the case of a rotation during widget configuration
973 onCompleteRunnable.run();
974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976
977 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700978 protected void onStop() {
979 super.onStop();
980 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700981
982 if (mLauncherCallbacks != null) {
983 mLauncherCallbacks.onStop();
984 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700985 }
986
987 @Override
988 protected void onStart() {
989 super.onStart();
990 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700991
992 if (mLauncherCallbacks != null) {
993 mLauncherCallbacks.onStart();
994 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700995 }
996
997 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200999 long startTime = 0;
1000 if (DEBUG_RESUME_TIME) {
1001 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001002 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001003 }
Adam Cohen9211d422014-10-07 18:14:53 -07001004
1005 if (mLauncherCallbacks != null) {
1006 mLauncherCallbacks.preOnResume();
1007 }
1008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001010
Winson Chung4a2afa32012-07-19 14:53:05 -07001011 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001012 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001013 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001014 } else if (mOnResumeState == State.APPS) {
1015 showAppsView(false /* animated */, false /* resetListToTop */);
1016 } else if (mOnResumeState == State.WIDGETS) {
1017 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001018 }
1019 mOnResumeState = State.NONE;
1020
Winson Chungcd99cd32015-04-29 11:03:24 -07001021 // Restore the apps state if we are in all apps
1022 if (mState == State.APPS) {
1023 if (mLauncherCallbacks != null) {
1024 mLauncherCallbacks.onAllAppsShown();
1025 }
1026 }
1027
Craig Mautner360310b2012-10-26 15:13:08 -07001028 // Background was set to gradient in onPause(), restore to black if in all apps.
1029 setWorkspaceBackground(mState == State.WORKSPACE);
1030
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001031 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001032 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001033 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001034 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001036 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001038 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001039 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1040 // execute them here
1041 long startTimeCallbacks = 0;
1042 if (DEBUG_RESUME_TIME) {
1043 startTimeCallbacks = System.currentTimeMillis();
1044 }
1045
Michael Jurka1e2f4652013-07-08 18:03:46 -07001046 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1047 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001048 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001049 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001050 if (DEBUG_RESUME_TIME) {
1051 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1052 (System.currentTimeMillis() - startTimeCallbacks));
1053 }
Michael Jurka447bf842013-05-15 14:52:15 +02001054 }
Michael Jurka54554252013-08-01 12:52:23 +02001055 if (mOnResumeCallbacks.size() > 0) {
1056 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1057 mOnResumeCallbacks.get(i).run();
1058 }
1059 mOnResumeCallbacks.clear();
1060 }
Winson Chunge4e50662012-01-23 14:45:13 -08001061
1062 // Reset the pressed state of icons that were locked in the press state while activities
1063 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001064 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001065 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001066 mWaitingForResume.setStayPressed(false);
1067 }
Winson Chung82963d52013-10-09 11:20:57 -07001068
Adam Cohen06dff352012-06-01 17:17:08 -07001069 // It is possible that widgets can receive updates while launcher is not in the foreground.
1070 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1071 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1072 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001073 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001074 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001075
Michael Jurka447bf842013-05-15 14:52:15 +02001076 if (DEBUG_RESUME_TIME) {
1077 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1078 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001079
1080 if (mWorkspace.getCustomContentCallbacks() != null) {
1081 // If we are resuming and the custom content is the current page, we call onShow().
1082 // It is also poassible that onShow will instead be called slightly after first layout
1083 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1084 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001085 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001086 }
1087 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001088 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001089 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001090
Sunny Goyal756adbc2015-04-16 15:20:51 -07001091 if (!isWorkspaceLoading()) {
1092 // Process any items that were added while Launcher was away.
1093 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1094 }
Adam Cohen9211d422014-10-07 18:14:53 -07001095
1096 if (mLauncherCallbacks != null) {
1097 mLauncherCallbacks.onResume();
1098 }
Adam Cohen06dff352012-06-01 17:17:08 -07001099 }
1100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001102 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001103 // Ensure that items added to Launcher are queued until Launcher returns
1104 InstallShortcutReceiver.enableInstallQueue();
1105
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001106 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001107 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001108 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001109 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001110
1111 // We call onHide() aggressively. The custom content callbacks should be able to
1112 // debounce excess onHide calls.
1113 if (mWorkspace.getCustomContentCallbacks() != null) {
1114 mWorkspace.getCustomContentCallbacks().onHide();
1115 }
Romain Guycbb89e42009-06-08 15:52:54 -07001116
Adam Cohen9211d422014-10-07 18:14:53 -07001117 if (mLauncherCallbacks != null) {
1118 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001119 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001120 }
1121
1122 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001123 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1124 // by a onResume or by scrolling otherwise.
1125 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001126
1127 // Custom content is completely hidden
1128 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001129
1130 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1131 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001132
1133 // Indicates whether the user is allowed to scroll away from the custom content.
1134 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001135 }
1136
Adam Cohenc2d6e892014-10-16 09:49:24 -07001137 public interface LauncherOverlay {
1138
1139 /**
1140 * Touch interaction leading to overscroll has begun
1141 */
1142 public void onScrollInteractionBegin();
1143
1144 /**
1145 * Touch interaction related to overscroll has ended
1146 */
1147 public void onScrollInteractionEnd();
1148
1149 /**
1150 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1151 * screen (or in the case of RTL, the rightmost screen).
1152 */
1153 public void onScrollChange(int progress, boolean rtl);
1154
1155 /**
1156 * Screen has stopped scrolling
1157 */
1158 public void onScrollSettled();
1159
1160 /**
1161 * This method can be called by the Launcher in order to force the LauncherOverlay
1162 * to exit fully immersive mode.
1163 */
1164 public void forceExitFullImmersion();
1165 }
1166
Winson Chung0f785722015-04-08 10:27:49 -07001167 public interface LauncherAppsCallbacks {
1168 /**
1169 * Updates launcher to the available space that AllApps can take so as not to overlap with
1170 * any other views.
1171 */
1172 public void onAllAppsBoundsChanged(Rect bounds);
1173
1174 /**
1175 * Called to dismiss all apps if it is showing.
1176 */
1177 public void dismissAllApps();
1178 }
1179
Adam Cohenc2d6e892014-10-16 09:49:24 -07001180 public interface LauncherOverlayCallbacks {
1181 /**
1182 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1183 * however it doesn't modify any state within the launcher.
1184 */
1185 public boolean canEnterFullImmersion();
1186
1187 /**
1188 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1189 * eg. by occupying the full screen and handling all touch events.
1190 *
1191 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1192 * case, Launcher will modify any necessary state and assumes the overlay is
1193 * handling all interaction. If false, the LauncherOverlay should cancel any
1194 *
1195 */
1196 public boolean enterFullImmersion();
1197
1198 /**
1199 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1200 * full control over UI and state.
1201 */
1202 public void exitFullImmersion();
1203 }
1204
1205 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1206
1207 @Override
1208 public boolean canEnterFullImmersion() {
1209 return mState == State.WORKSPACE;
1210 }
1211
1212 @Override
1213 public boolean enterFullImmersion() {
1214 if (mState == State.WORKSPACE) {
1215 // When fully immersed, disregard any touches which fall through.
1216 mDragLayer.setBlockTouch(true);
1217 return true;
1218 }
1219 return false;
1220 }
1221
1222 @Override
1223 public void exitFullImmersion() {
1224 mDragLayer.setBlockTouch(false);
1225 }
1226 }
1227
Jorim Jaggid017f882014-01-14 17:08:48 -08001228 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001229 if (mLauncherCallbacks != null) {
1230 return mLauncherCallbacks.hasSettings();
1231 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001232 return false;
1233 }
1234
Adam Cohen9211d422014-10-07 18:14:53 -07001235 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001236 CustomContentCallbacks callbacks, String description) {
1237 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001238 }
1239
Adam Cohenedb40762013-07-18 16:45:45 -07001240 // The custom content needs to offset its content to account for the QSB
1241 public int getTopOffsetForCustomContent() {
1242 return mWorkspace.getPaddingTop();
1243 }
1244
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001245 @Override
1246 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001248 if (mModel.isCurrentCallbacks(this)) {
1249 mModel.stopLoader();
1250 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001251 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1252
Romain Guy13c2e7b2010-03-10 19:45:00 -08001253 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001254 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001255
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001256 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001257 @Override
1258 public void onWindowFocusChanged(boolean hasFocus) {
1259 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001260 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001261
1262 if (mLauncherCallbacks != null) {
1263 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1264 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001265 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 private boolean acceptFilter() {
1268 final InputMethodManager inputManager = (InputMethodManager)
1269 getSystemService(Context.INPUT_METHOD_SERVICE);
1270 return !inputManager.isFullscreenMode();
1271 }
1272
1273 @Override
1274 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001275 final int uniChar = event.getUnicodeChar();
1276 final boolean handled = super.onKeyDown(keyCode, event);
1277 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1278 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1280 keyCode, event);
1281 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001282 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001283 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001284 // showSearchDialog()
1285 // If there are multiple keystrokes before the search dialog takes focus,
1286 // onSearchRequested() will be called for every keystroke,
1287 // but it is idempotent, so it's fine.
1288 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 }
1290 }
1291
Joe Onorato8a9625e2010-01-28 15:55:35 -08001292 // Eat the long press event so the keyboard doesn't come up.
1293 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1294 return true;
1295 }
1296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 return handled;
1298 }
1299
Karl Rosaen138a0412009-04-23 19:00:21 -07001300 private String getTypedText() {
1301 return mDefaultKeySsb.toString();
1302 }
1303
1304 private void clearTypedText() {
1305 mDefaultKeySsb.clear();
1306 mDefaultKeySsb.clearSpans();
1307 Selection.setSelection(mDefaultKeySsb, 0);
1308 }
1309
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001311 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1312 * State
1313 */
1314 private static State intToState(int stateOrdinal) {
1315 State state = State.WORKSPACE;
1316 final State[] stateValues = State.values();
1317 for (int i = 0; i < stateValues.length; i++) {
1318 if (stateValues[i].ordinal() == stateOrdinal) {
1319 state = stateValues[i];
1320 break;
1321 }
1322 }
1323 return state;
1324 }
1325
1326 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 * Restores the previous state, if it exists.
1328 *
1329 * @param savedState The previous state.
1330 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001331 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 private void restoreState(Bundle savedState) {
1333 if (savedState == null) {
1334 return;
1335 }
1336
Michael Jurka883f55b2010-10-21 15:47:14 -07001337 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001338 if (state == State.APPS || state == State.WIDGETS) {
1339 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001340 }
1341
Adam Cohen21cd0022013-10-09 18:57:02 -07001342 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1343 PagedView.INVALID_RESTORE_PAGE);
1344 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001345 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
Winson Chung3d503fb2011-07-13 17:25:49 -07001348 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001349 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001350
Winson Chung3d503fb2011-07-13 17:25:49 -07001351 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1352 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001353 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1355 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001356 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1357 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001358 AppWidgetProviderInfo info = savedState.getParcelable(
1359 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1360 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001361 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001362 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 mRestoring = true;
1364 }
1365
1366 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1367 if (renameFolder) {
1368 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001369 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 mRestoring = true;
1371 }
Winson Chunga12a2502010-12-20 14:41:35 -08001372
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001373 mItemIdToViewId = (HashMap<Integer, Integer>)
1374 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
1376
1377 /**
1378 * Finds all the views we need and configure them properly.
1379 */
1380 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001381 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001382
Craig Mautner360310b2012-10-26 15:13:08 -07001383 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001384 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001385 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1386 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001387 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001388 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001389
John Spurlock77e1f472013-09-11 10:09:51 -04001390 mLauncherView.setSystemUiVisibility(
1391 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001392 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393
Winson Chung4c98d922011-05-31 16:50:48 -07001394 // Setup the drag layer
1395 mDragLayer.setup(this, dragController);
1396
Winson Chung3d503fb2011-07-13 17:25:49 -07001397 // Setup the hotseat
1398 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1399 if (mHotseat != null) {
1400 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001401 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001402 }
1403
Jorim Jaggid017f882014-01-14 17:08:48 -08001404 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001405 View widgetButton = findViewById(R.id.widget_button);
1406 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001407 @Override
1408 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001409 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001410 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001411 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001412 }
1413 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001414 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1415
1416 View wallpaperButton = findViewById(R.id.wallpaper_button);
1417 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418 @Override
1419 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001420 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001421 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001422 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001423 }
1424 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001425 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1426
1427 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001428 if (hasSettings()) {
1429 settingsButton.setOnClickListener(new OnClickListener() {
1430 @Override
1431 public void onClick(View arg0) {
1432 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001433 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001434 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001435 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001436 });
1437 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438 } else {
1439 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001440 }
1441
Adam Cohendbdff6b2013-09-18 19:09:15 -07001442 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001443
Winson Chung4c98d922011-05-31 16:50:48 -07001444 // Setup the workspace
1445 mWorkspace.setHapticFeedbackEnabled(false);
1446 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001447 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001448 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001449
Winson Chungf0ea4d32011-06-06 14:27:16 -07001450 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001451 mSearchDropTargetBar = (SearchDropTargetBar)
1452 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001453
Winson Chungb745afb2015-03-02 11:51:23 -08001454 // Setup Apps
1455 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung0f785722015-04-08 10:27:49 -07001456 if (mLauncherCallbacks != null && mLauncherCallbacks.overrideAllAppsSearch()) {
1457 mAppsView.hideSearchBar();
1458 }
Winson Chungb745afb2015-03-02 11:51:23 -08001459
Winson Chungf0ea4d32011-06-06 14:27:16 -07001460 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001461 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001462
Winson Chung3d503fb2011-07-13 17:25:49 -07001463 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001464 dragController.setDragScoller(mWorkspace);
1465 dragController.setScrollView(mDragLayer);
1466 dragController.setMoveTarget(mWorkspace);
1467 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001468 if (mSearchDropTargetBar != null) {
1469 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001470 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001471 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001472
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001473 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001474 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001475 mWeightWatcher = new WeightWatcher(this);
1476 mWeightWatcher.setAlpha(0.5f);
1477 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001478 new FrameLayout.LayoutParams(
1479 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001480 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001481 Gravity.BOTTOM)
1482 );
Adam Cohen39a06042013-07-19 14:30:12 -07001483
1484 boolean show = shouldShowWeightWatcher();
1485 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001486 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
1489 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001490 * Sets the all apps button. This method is called from {@link Hotseat}.
1491 */
1492 public void setAllAppsButton(View allAppsButton) {
1493 mAllAppsButton = allAppsButton;
1494 }
1495
1496 public View getAllAppsButton() {
1497 return mAllAppsButton;
1498 }
1499
1500 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 * Creates a view representing a shortcut.
1502 *
1503 * @param info The data structure describing the shortcut.
1504 *
1505 * @return A View inflated from R.layout.application.
1506 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001507 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001509 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
1511
1512 /**
1513 * Creates a view representing a shortcut inflated from the specified resource.
1514 *
1515 * @param layoutResId The id of the XML layout used to create the shortcut.
1516 * @param parent The group the shortcut belongs to.
1517 * @param info The data structure describing the shortcut.
1518 *
1519 * @return A View inflated from layoutResId.
1520 */
Adam Cohen59400422014-03-05 18:07:04 -08001521 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001522 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001523 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001525 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 return favorite;
1527 }
1528
1529 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 * Add a shortcut to the workspace.
1531 *
1532 * @param data The intent describing the shortcut.
1533 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001535 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001536 int cellY) {
1537 int[] cellXY = mTmpAddItemCellCoordinates;
1538 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001539 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001540
Michael Jurkad3ef3062010-11-23 16:23:58 -08001541 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001542
Sunny Goyal2350bc92014-10-14 16:42:54 -07001543 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001544 if (info == null) {
1545 return;
1546 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001547 final View view = createShortcut(info);
1548
Adam Cohenfbba09b2011-07-18 21:43:05 -07001549 // First we check if we already know the exact location where we want to add this item.
1550 if (cellX >= 0 && cellY >= 0) {
1551 cellXY[0] = cellX;
1552 cellXY[1] = cellY;
1553 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001554
1555 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001556 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001557 true, null,null)) {
1558 return;
1559 }
1560 DragObject dragObject = new DragObject();
1561 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001562 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1563 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001564 return;
1565 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001566 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001567 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001568 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001569 foundCellSpan = (result != null);
1570 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001571 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001572 }
1573
1574 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001575 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001576 return;
1577 }
1578
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001579 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580
1581 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001582 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001583 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585 }
1586
Adam Cohen2f093b62012-04-30 18:59:53 -07001587 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1588 int minHeight) {
1589 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001590 // We want to account for the extra amount of padding that we are adding to the widget
1591 // to ensure that it gets the full amount of space that it has requested
1592 int requiredWidth = minWidth + padding.left + padding.right;
1593 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001594 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001595 }
1596
Adam Cohen2f093b62012-04-30 18:59:53 -07001597 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1598 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001599 }
1600
Adam Cohen2f093b62012-04-30 18:59:53 -07001601 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1602 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001603 }
1604
Adam Cohen2f093b62012-04-30 18:59:53 -07001605 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1606 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001607 }
1608
Adam Cohen2f093b62012-04-30 18:59:53 -07001609 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1610 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001611 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001612 }
1613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001615 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001617 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 */
Adam Cohen091440a2015-03-18 14:16:05 -07001619 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001620 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1621
1622 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001623 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001624 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1625 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001626 }
Romain Guycbb89e42009-06-08 15:52:54 -07001627
Adam Cohen59400422014-03-05 18:07:04 -08001628 if (appWidgetInfo.isCustomWidget) {
1629 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001630 }
1631
Adam Cohen59400422014-03-05 18:07:04 -08001632 LauncherAppWidgetInfo launcherInfo;
1633 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1634 launcherInfo.spanX = info.spanX;
1635 launcherInfo.spanY = info.spanY;
1636 launcherInfo.minSpanX = info.minSpanX;
1637 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001638 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001639
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001641 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642
1643 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001644 if (hostView == null) {
1645 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001646 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1647 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001648 } else {
1649 // The AppWidgetHostView has already been inflated and instantiated
1650 launcherInfo.hostView = hostView;
1651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001653 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001654 launcherInfo.notifyWidgetSizeChanged(this);
1655
Adam Cohen59400422014-03-05 18:07:04 -08001656 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1657 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001658
1659 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001661 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 }
Romain Guycbb89e42009-06-08 15:52:54 -07001663
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1665 @Override
1666 public void onReceive(Context context, Intent intent) {
1667 final String action = intent.getAction();
1668 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1669 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001670 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001671 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001672
Winson Chungc100e8e2011-08-09 16:02:43 -07001673 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001674 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001675 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001676 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001677 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001678 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1680 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001681 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001682 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1683 mModel.resetLoadedState(false, true);
1684 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1685 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1686 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1687 mModel.resetLoadedState(false, true);
1688 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1689 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1690 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001691 }
1692 }
1693 };
1694
1695 @Override
1696 public void onAttachedToWindow() {
1697 super.onAttachedToWindow();
1698
1699 // Listen for broadcasts related to user-presence
1700 final IntentFilter filter = new IntentFilter();
1701 filter.addAction(Intent.ACTION_SCREEN_OFF);
1702 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001703 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001704 if (ENABLE_DEBUG_INTENTS) {
1705 filter.addAction(DebugIntents.DELETE_DATABASE);
1706 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1707 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001709 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001710 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001711 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 mVisible = true;
1713 }
1714
Adam Cohen0b395352014-06-09 22:54:36 +00001715 /**
1716 * Sets up transparent navigation and status bars in LMP.
1717 * This method is a no-op for other platform versions.
1718 */
Sunny Goyald0091012015-01-20 15:55:34 -08001719 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001720 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001721 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001722 Window window = getWindow();
1723 window.getAttributes().systemUiVisibility |=
1724 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1725 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1726 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1727 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1728 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1729 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1730 window.setStatusBarColor(Color.TRANSPARENT);
1731 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001732 }
1733 }
1734
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 @Override
1736 public void onDetachedFromWindow() {
1737 super.onDetachedFromWindow();
1738 mVisible = false;
1739
Adam Cohend113e0c2010-11-11 10:48:05 -08001740 if (mAttached) {
1741 unregisterReceiver(mReceiver);
1742 mAttached = false;
1743 }
Winson Chungb745afb2015-03-02 11:51:23 -08001744 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 }
1746
1747 public void onWindowVisibilityChanged(int visibility) {
1748 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001749 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001750 // The following code used to be in onResume, but it turns out onResume is called when
1751 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1752 // is a more appropriate event to handle
1753 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001754 if (!mWorkspaceLoading) {
1755 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001756 // We want to let Launcher draw itself at least once before we force it to build
1757 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001758 // apps is nice and speedy.
1759 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001760 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001761 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001762 if (mStarted) return;
1763 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001764 // We delay the layer building a bit in order to give
1765 // other message processing a time to run. In particular
1766 // this avoids a delay in hiding the IME if it was
1767 // currently shown, because doing that may involve
1768 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001769 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001770 final ViewTreeObserver.OnDrawListener listener = this;
1771 mWorkspace.post(new Runnable() {
1772 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001773 if (mWorkspace != null &&
1774 mWorkspace.getViewTreeObserver() != null) {
1775 mWorkspace.getViewTreeObserver().
1776 removeOnDrawListener(listener);
1777 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001778 }
1779 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001780 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001781 }
1782 });
1783 }
1784 clearTypedText();
1785 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001786 }
1787
Adam Cohen091440a2015-03-18 14:16:05 -07001788 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001789 mHandler.removeMessages(ADVANCE_MSG);
1790 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1791 mHandler.sendMessageDelayed(msg, delay);
1792 mAutoAdvanceSentTime = System.currentTimeMillis();
1793 }
1794
Adam Cohen091440a2015-03-18 14:16:05 -07001795 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001796 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1797 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1798 mAutoAdvanceRunning = autoAdvanceRunning;
1799 if (autoAdvanceRunning) {
1800 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1801 sendAdvanceMessage(delay);
1802 } else {
1803 if (!mWidgetsToAdvance.isEmpty()) {
1804 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1805 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1806 }
1807 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001808 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001809 }
1810 }
1811 }
1812
1813 private final Handler mHandler = new Handler() {
1814 @Override
1815 public void handleMessage(Message msg) {
1816 if (msg.what == ADVANCE_MSG) {
1817 int i = 0;
1818 for (View key: mWidgetsToAdvance.keySet()) {
1819 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1820 final int delay = mAdvanceStagger * i;
1821 if (v instanceof Advanceable) {
1822 postDelayed(new Runnable() {
1823 public void run() {
1824 ((Advanceable) v).advance();
1825 }
1826 }, delay);
1827 }
1828 i++;
1829 }
1830 sendAdvanceMessage(mAdvanceInterval);
1831 }
1832 }
1833 };
1834
1835 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001836 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001837 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1838 if (v instanceof Advanceable) {
1839 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001840 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001841 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001842 }
1843 }
1844
1845 void removeWidgetToAutoAdvance(View hostView) {
1846 if (mWidgetsToAdvance.containsKey(hostView)) {
1847 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001848 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001849 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001850 }
1851
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001853 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001854 launcherInfo.hostView = null;
1855 }
1856
Hyunyoung Song3f471442015-04-08 19:01:34 -07001857 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001858 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1859 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001860 }
1861
Winson Chunga6945242014-01-08 14:04:34 -08001862 public DragLayer getDragLayer() {
1863 return mDragLayer;
1864 }
1865
Winson Chungb745afb2015-03-02 11:51:23 -08001866 public AppsContainerView getAppsView() {
1867 return mAppsView;
1868 }
1869
Hyunyoung Song3f471442015-04-08 19:01:34 -07001870 public WidgetsContainerView getWidgetsView() {
1871 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001872 }
1873
Winson Chunga6945242014-01-08 14:04:34 -08001874 public Workspace getWorkspace() {
1875 return mWorkspace;
1876 }
1877
1878 public Hotseat getHotseat() {
1879 return mHotseat;
1880 }
1881
Jorim Jaggid017f882014-01-14 17:08:48 -08001882 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001883 return mOverviewPanel;
1884 }
1885
1886 public SearchDropTargetBar getSearchBar() {
1887 return mSearchDropTargetBar;
1888 }
1889
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001890 public LauncherAppWidgetHost getAppWidgetHost() {
1891 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 }
Romain Guycbb89e42009-06-08 15:52:54 -07001893
Winson Chunga9abd0e2010-10-27 17:18:37 -07001894 public LauncherModel getModel() {
1895 return mModel;
1896 }
1897
Winson Chunga6945242014-01-08 14:04:34 -08001898 protected SharedPreferences getSharedPrefs() {
1899 return mSharedPrefs;
1900 }
1901
Bjorn Bringertc459e522013-06-07 19:36:01 +01001902 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001903 getWindow().closeAllPanels();
1904
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001905 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001906 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001907 }
1908
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 @Override
1910 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001911 long startTime = 0;
1912 if (DEBUG_RESUME_TIME) {
1913 startTime = System.currentTimeMillis();
1914 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 super.onNewIntent(intent);
1916
1917 // Close the menu
1918 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001919 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001920 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001921
Adam Cohened307df2013-10-02 09:37:31 -07001922 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1923 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1924 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001925
Adam Cohen6fecd412013-10-02 17:41:50 -07001926 if (mWorkspace == null) {
1927 // Can be cases where mWorkspace is null, this prevents a NPE
1928 return;
1929 }
1930 Folder openFolder = mWorkspace.getOpenFolder();
1931 // In all these cases, only animate if we're already on home
1932 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001933
1934 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1935 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001936 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001937 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001938 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001939 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001940
Adam Cohen6fecd412013-10-02 17:41:50 -07001941 closeFolder();
1942 exitSpringLoadedDragMode();
1943
1944 // If we are already on home, then just animate back to the workspace,
1945 // otherwise, just wait until onResume to set the state back to Workspace
1946 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001947 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001948 } else {
1949 mOnResumeState = State.WORKSPACE;
1950 }
1951
1952 final View v = getWindow().peekDecorView();
1953 if (v != null && v.getWindowToken() != null) {
1954 InputMethodManager imm = (InputMethodManager)getSystemService(
1955 INPUT_METHOD_SERVICE);
1956 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1957 }
1958
Winson Chungb745afb2015-03-02 11:51:23 -08001959 // Reset the apps view
1960 if (!alreadyOnHome && mAppsView != null) {
1961 mAppsView.scrollToTop();
1962 }
1963
Hyunyoung Song3f471442015-04-08 19:01:34 -07001964 // Reset the widgets view
1965 if (!alreadyOnHome && mWidgetsView != null) {
1966 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001967 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001968
Adam Cohen9211d422014-10-07 18:14:53 -07001969 if (mLauncherCallbacks != null) {
1970 mLauncherCallbacks.onHomeIntent();
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
Adam Cohened307df2013-10-02 09:37:31 -07001973
Michael Jurka447bf842013-05-15 14:52:15 +02001974 if (DEBUG_RESUME_TIME) {
1975 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977
Adam Cohen9211d422014-10-07 18:14:53 -07001978 if (mLauncherCallbacks != null) {
1979 mLauncherCallbacks.onNewIntent(intent);
1980 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001981 }
1982
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001984 public void onRestoreInstanceState(Bundle state) {
1985 super.onRestoreInstanceState(state);
1986 for (int page: mSynchronouslyBoundPages) {
1987 mWorkspace.restoreInstanceStateForChild(page);
1988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990
1991 @Override
1992 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001993 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001994 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1995 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001996 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001997 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998
Michael Jurka883f55b2010-10-21 15:47:14 -07001999 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002000 // We close any open folder since it will not be re-opened, and we need to make sure
2001 // this state is reflected.
2002 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003
Adam Cohendcd297f2013-06-18 13:13:40 -07002004 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002005 mWaitingForResult) {
2006 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002007 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002008 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2009 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002010 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2011 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2012 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002013 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
2015
2016 if (mFolderInfo != null && mWaitingForResult) {
2017 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2018 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2019 }
Michael Jurka946ad472010-07-09 18:05:18 -07002020
Hyunyoung Song3f471442015-04-08 19:01:34 -07002021 // Save the current widgets tray?
2022 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002023 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002024
2025 if (mLauncherCallbacks != null) {
2026 mLauncherCallbacks.onSaveInstanceState(outState);
2027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 }
2029
2030 @Override
2031 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002033
Winson Chunge7a03942011-08-05 15:05:12 -07002034 // Remove all pending runnables
2035 mHandler.removeMessages(ADVANCE_MSG);
2036 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002037 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002038
Winson Chungcd2b0142011-06-08 16:02:26 -07002039 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002040 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002041
2042 // It's possible to receive onDestroy after a new Launcher activity has
2043 // been created. In this case, don't interfere with the new Launcher.
2044 if (mModel.isCurrentCallbacks(this)) {
2045 mModel.stopLoader();
2046 app.setLauncher(null);
2047 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002048
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002050 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002052 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002054 mAppWidgetHost = null;
2055
2056 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057
2058 TextKeyListener.getInstance().release();
2059
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002060 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002061 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002062
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002063 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002064 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002065 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002066 mWorkspace = null;
2067 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002068
Michael Jurka2ecf9952012-06-18 12:52:28 -07002069 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002070
2071 if (mLauncherCallbacks != null) {
2072 mLauncherCallbacks.onDestroy();
2073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
Adam Cohena9cf38f2011-05-02 15:36:58 -07002076 public DragController getDragController() {
2077 return mDragController;
2078 }
2079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 @Override
2081 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002082 onStartForResult(requestCode);
2083 super.startActivityForResult(intent, requestCode);
2084 }
2085
2086 @Override
2087 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2088 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2089 onStartForResult(requestCode);
2090 try {
2091 super.startIntentSenderForResult(intent, requestCode,
2092 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2093 } catch (IntentSender.SendIntentException e) {
2094 throw new ActivityNotFoundException();
2095 }
2096 }
2097
2098 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002099 if (requestCode >= 0) {
2100 setWaitingForResult(true);
2101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 }
2103
Winson Chung70d72102011-08-12 11:24:35 -07002104 /**
2105 * Indicates that we want global search for this activity by setting the globalSearch
2106 * argument for {@link #startSearch} to true.
2107 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002109 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002111
Karl Rosaen138a0412009-04-23 19:00:21 -07002112 if (initialQuery == null) {
2113 // Use any text typed in the launcher as the initial query
2114 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 if (appSearchData == null) {
2117 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002118 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 }
Winson Chungadf0c182012-08-23 14:59:07 -07002120 Rect sourceBounds = new Rect();
2121 if (mSearchDropTargetBar != null) {
2122 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2123 }
Romain Guycbb89e42009-06-08 15:52:54 -07002124
Ian Parkinson047036e2014-09-01 15:40:53 +01002125 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002126 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002127 if (clearTextImmediately) {
2128 clearTypedText();
2129 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002130
2131 // We need to show the workspace after starting the search
2132 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002133 }
2134
Ian Parkinson047036e2014-09-01 15:40:53 +01002135 /**
2136 * Start a text search.
2137 *
2138 * @return {@code true} if the search will start immediately, so any further keypresses
2139 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2140 * to buffer keypresses.
2141 */
2142 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002143 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002144 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2145 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2146 sourceBounds);
2147 }
2148
Michael Jurkaa33411c2012-06-14 16:18:21 -07002149 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002150 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002151 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002152 }
2153
2154 /**
2155 * Starts the global search activity. This code is a copied from SearchManager
2156 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002157 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002158 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002159 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002160 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2161 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2162 if (globalSearchActivity == null) {
2163 Log.w(TAG, "No global search activity found.");
2164 return;
2165 }
2166 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2167 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2168 intent.setComponent(globalSearchActivity);
2169 // Make sure that we have a Bundle to put source in
2170 if (appSearchData == null) {
2171 appSearchData = new Bundle();
2172 } else {
2173 appSearchData = new Bundle(appSearchData);
2174 }
Adam Cohen9211d422014-10-07 18:14:53 -07002175 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002176 if (!appSearchData.containsKey("source")) {
2177 appSearchData.putString("source", getPackageName());
2178 }
2179 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2180 if (!TextUtils.isEmpty(initialQuery)) {
2181 intent.putExtra(SearchManager.QUERY, initialQuery);
2182 }
2183 if (selectInitialQuery) {
2184 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2185 }
2186 intent.setSourceBounds(sourceBounds);
2187 try {
2188 startActivity(intent);
2189 } catch (ActivityNotFoundException ex) {
2190 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 }
2193
Yura4f93ec62014-02-04 14:15:21 +00002194 public boolean isOnCustomContent() {
2195 return mWorkspace.isOnOrMovingToCustomContent();
2196 }
2197
Winson Chung70d72102011-08-12 11:24:35 -07002198 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002199 public boolean onPrepareOptionsMenu(Menu menu) {
2200 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002201 if (!isOnCustomContent()) {
2202 // Close any open folders
2203 closeFolder();
2204 // Stop resizing any widgets
2205 mWorkspace.exitWidgetResizeMode();
2206 if (!mWorkspace.isInOverviewMode()) {
2207 // Show the overview mode
2208 showOverviewMode(true);
2209 } else {
2210 showWorkspace(true);
2211 }
Winson Chunge0298742014-01-17 12:03:00 -08002212 }
Adam Cohen9211d422014-10-07 18:14:53 -07002213 if (mLauncherCallbacks != null) {
2214 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2215 }
2216
Michael Jurkab94f3f82013-09-11 18:08:54 +02002217 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002220 @Override
2221 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002222 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002223 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002224 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002225 }
2226
Joe Onorato9c1289c2009-08-17 11:03:03 -04002227 public boolean isWorkspaceLocked() {
2228 return mWorkspaceLoading || mWaitingForResult;
2229 }
2230
Adam Cohen517a7f52014-03-01 12:12:59 -08002231 public boolean isWorkspaceLoading() {
2232 return mWorkspaceLoading;
2233 }
2234
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002235 private void setWorkspaceLoading(boolean value) {
2236 boolean isLocked = isWorkspaceLocked();
2237 mWorkspaceLoading = value;
2238 if (isLocked != isWorkspaceLocked()) {
2239 onWorkspaceLockedChanged();
2240 }
2241 }
2242
2243 private void setWaitingForResult(boolean value) {
2244 boolean isLocked = isWorkspaceLocked();
2245 mWaitingForResult = value;
2246 if (isLocked != isWorkspaceLocked()) {
2247 onWorkspaceLockedChanged();
2248 }
2249 }
2250
Adam Cohen9211d422014-10-07 18:14:53 -07002251 protected void onWorkspaceLockedChanged() {
2252 if (mLauncherCallbacks != null) {
2253 mLauncherCallbacks.onWorkspaceLockedChanged();
2254 }
2255 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002256
Michael Jurka0280c3b2010-09-17 15:00:07 -07002257 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002260 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2261 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002262 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002263 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002265
Sunny Goyale6b63a32015-01-16 16:14:29 -08002266 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002267 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002268 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2269 }
2270
Sunny Goyale6b63a32015-01-16 16:14:29 -08002271 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002272 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2273 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002274 if (appWidgetInfo.configure != null) {
2275 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002276 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002277
Bjorn Bringert7984c942009-12-09 15:38:25 +00002278 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002279 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2280 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002282 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002283 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002284 Runnable onComplete = new Runnable() {
2285 @Override
2286 public void run() {
2287 // Exit spring loaded mode if necessary after adding the widget
2288 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2289 null);
2290 }
2291 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002292 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002293 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002294 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 }
2296 }
Romain Guycbb89e42009-06-08 15:52:54 -07002297
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002298 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002299 // Close any folders that may be open.
2300 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002301 mWorkspace.moveToCustomContentScreen(animate);
2302 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002303
2304 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2305 int[] cell, int spanX, int spanY) {
2306 switch (info.itemType) {
2307 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2308 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2309 int span[] = new int[2];
2310 span[0] = spanX;
2311 span[1] = spanY;
2312 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2313 container, screenId, cell, span);
2314 break;
2315 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2316 processShortcutFromDrop(info.componentName, container, screenId, cell);
2317 break;
2318 default:
2319 throw new IllegalStateException("Unknown item type: " + info.itemType);
2320 }
2321 }
2322
Adam Cohenfbba09b2011-07-18 21:43:05 -07002323 /**
2324 * Process a shortcut drop.
2325 *
2326 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002327 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002330 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2331 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002332 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002335 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336
2337 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002338 mPendingAddInfo.cellX = cell[0];
2339 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002340 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002341
2342 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2343 createShortcutIntent.setComponent(componentName);
2344 processShortcut(createShortcutIntent);
2345 }
2346
Adam Cohenfbba09b2011-07-18 21:43:05 -07002347 /**
2348 * Process a widget drop.
2349 *
2350 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002351 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002352 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002353 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002354 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2355 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002356 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002357 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002358 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002359 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002360 mPendingAddInfo.minSpanX = info.minSpanX;
2361 mPendingAddInfo.minSpanY = info.minSpanY;
2362
Adam Cohenfbba09b2011-07-18 21:43:05 -07002363 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002364 mPendingAddInfo.cellX = cell[0];
2365 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002366 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002367 if (span != null) {
2368 mPendingAddInfo.spanX = span[0];
2369 mPendingAddInfo.spanY = span[1];
2370 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002371
Adam Cohened66b2b2012-01-23 17:28:51 -08002372 AppWidgetHostView hostView = info.boundWidget;
2373 int appWidgetId;
2374 if (hostView != null) {
2375 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002376 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002377 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002378 // In this case, we either need to start an activity to get permission to bind
2379 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002380 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002381 Bundle options = info.bindOptions;
2382
Sunny Goyalffe83f12014-08-14 17:39:34 -07002383 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2384 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002385 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002386 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002387 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002388 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002389 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2390 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2391 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002392 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2393 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002394 // TODO: we need to make sure that this accounts for the options bundle.
2395 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002396 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2397 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002398 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002399 }
2400
Joe Onoratodeb98af2010-02-19 14:59:39 -08002401 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002402 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 }
2404
Winson Chung24ab2f12010-09-16 14:10:47 -07002405 void processWallpaper(Intent intent) {
2406 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2407 }
2408
Adam Cohendcd297f2013-06-18 13:13:40 -07002409 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002410 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002411 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 folderInfo.title = getText(R.string.folder_name);
2413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002415 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2416 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002417 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418
2419 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002420 FolderIcon newFolder =
2421 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002422 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002423 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002424 // Force measure the new folder icon
2425 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2426 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002427 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 }
Romain Guycbb89e42009-06-08 15:52:54 -07002429
Joe Onorato9c1289c2009-08-17 11:03:03 -04002430 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002431 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002432 }
2433
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002434 /**
2435 * Registers various content observers. The current implementation registers
2436 * only a favorites observer to keep track of the favorites applications.
2437 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002438 private void registerContentObservers() {
2439 ContentResolver resolver = getContentResolver();
2440 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2441 true, mWidgetObserver);
2442 }
2443
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 @Override
2445 public boolean dispatchKeyEvent(KeyEvent event) {
2446 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2447 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002449 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002450 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002451 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002452 dumpState();
2453 return true;
2454 }
2455 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002456 }
2457 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2458 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002459 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 return true;
2461 }
2462 }
2463
2464 return super.dispatchKeyEvent(event);
2465 }
2466
Joe Onorato88ec0992009-11-19 13:16:06 -08002467 @Override
2468 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002469 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2470 return;
2471 }
2472
Winson Chung3d9490a2015-03-24 17:38:42 -07002473 LauncherAccessibilityDelegate delegate =
2474 LauncherAppState.getInstance().getAccessibilityDelegate();
2475 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002476 return;
2477 }
2478
Winson Chungb745afb2015-03-02 11:51:23 -08002479 if (isAppsViewVisible()) {
2480 showWorkspace(true);
2481 } else if (isWidgetsViewVisible()) {
2482 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002483 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002484 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002485 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002486 Folder openFolder = mWorkspace.getOpenFolder();
2487 if (openFolder.isEditingName()) {
2488 openFolder.dismissEditingName();
2489 } else {
2490 closeFolder();
2491 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002492 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002493 mWorkspace.exitWidgetResizeMode();
2494
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002495 // Back button is a no-op here, but give at least some feedback for the button press
2496 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002497 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002498 }
2499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002501 * Re-listen when widgets are reset.
2502 */
Adam Cohen091440a2015-03-18 14:16:05 -07002503 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002504 if (mAppWidgetHost != null) {
2505 mAppWidgetHost.startListening();
2506 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002507 }
2508
2509 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 * Launches the intent referred by the clicked shortcut.
2511 *
2512 * @param v The view representing the clicked shortcut.
2513 */
2514 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002515 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2516 // view has detached (it's possible for this to happen if the view is removed mid touch).
2517 if (v.getWindowToken() == null) {
2518 return;
2519 }
2520
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002521 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002522 return;
2523 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002524
Adam Cohen1697b792013-09-17 19:08:21 -07002525 if (v instanceof Workspace) {
2526 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002527 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002528 }
2529 return;
2530 }
2531
2532 if (v instanceof CellLayout) {
2533 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002534 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002535 }
2536 }
2537
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002539 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002540 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002542 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002543 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002544 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002545 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002546 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002547 } else if (tag instanceof AppInfo) {
2548 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002549 } else if (tag instanceof LauncherAppWidgetInfo) {
2550 if (v instanceof PendingAppWidgetHostView) {
2551 onClickPendingWidget((PendingAppWidgetHostView) v);
2552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 }
2554 }
2555
Sunny Goyal508da152014-08-14 10:53:27 -07002556 public void onClickPagedViewIcon(View v) {
2557 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002558 if (mLauncherCallbacks != null) {
2559 mLauncherCallbacks.onClickPagedViewIcon(v);
2560 }
Sunny Goyal508da152014-08-14 10:53:27 -07002561 }
2562
Michael Jurka0e260592010-06-30 17:07:39 -07002563 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002564 return false;
2565 }
2566
Michael Jurkaaf442092010-06-10 17:01:57 -07002567 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002568 * Event handler for the app widget view which has not fully restored.
2569 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002570 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002571 if (mIsSafeModeEnabled) {
2572 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2573 return;
2574 }
2575
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002576 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002577 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002578 int widgetId = info.appWidgetId;
2579 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2580 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002581 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2582 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002583 mPendingAddInfo.copyFrom(info);
2584 mPendingAddWidgetId = widgetId;
2585
Sunny Goyalffe83f12014-08-14 17:39:34 -07002586 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2587 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002588 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002589 } else if (info.installProgress < 0) {
2590 // The install has not been queued
2591 final String packageName = info.providerName.getPackageName();
2592 showBrokenAppInstallDialog(packageName,
2593 new DialogInterface.OnClickListener() {
2594 public void onClick(DialogInterface dialog, int id) {
2595 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2596 }
2597 });
2598 } else {
2599 // Download has started.
2600 final String packageName = info.providerName.getPackageName();
2601 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002602 }
2603 }
2604
2605 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002606 * Event handler for the "grid" button that appears on the home screen, which
2607 * enters all apps mode.
2608 *
2609 * @param v The view that was clicked.
2610 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002611 protected void onClickAllAppsButton(View v) {
2612 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002613 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002614 showWorkspace(true);
2615 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002616 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002617 }
2618 }
2619
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002620 private void showBrokenAppInstallDialog(final String packageName,
2621 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002622 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002623 .setTitle(R.string.abandoned_promises_title)
2624 .setMessage(R.string.abandoned_promise_explanation)
2625 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2626 .setNeutralButton(R.string.abandoned_clean_this,
2627 new DialogInterface.OnClickListener() {
2628 public void onClick(DialogInterface dialog, int id) {
2629 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2630 mWorkspace.removeAbandonedPromise(packageName, user);
2631 }
2632 })
2633 .create().show();
2634 return;
2635 }
2636
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002637 /**
2638 * Event handler for an app shortcut click.
2639 *
2640 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2641 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002642 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2644 Object tag = v.getTag();
2645 if (!(tag instanceof ShortcutInfo)) {
2646 throw new IllegalArgumentException("Input must be a Shortcut");
2647 }
2648
2649 // Open shortcut
2650 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002651
2652 if (shortcut.isDisabled != 0) {
2653 int error = R.string.activity_not_available;
2654 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2655 error = R.string.safemode_shortcut_error;
2656 }
2657 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2658 return;
2659 }
2660
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002661 final Intent intent = shortcut.intent;
2662
2663 // Check for special shortcuts
2664 if (intent.getComponent() != null) {
2665 final String shortcutClass = intent.getComponent().getClassName();
2666
2667 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2668 MemoryDumpActivity.startDump(this);
2669 return;
2670 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2671 toggleShowWeightWatcher();
2672 return;
2673 }
2674 }
2675
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002677 if ((v instanceof BubbleTextView)
2678 && shortcut.isPromise()
2679 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002680 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002681 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 new DialogInterface.OnClickListener() {
2683 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002684 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 return;
2688 }
2689
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002690 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002691 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002692
2693 if (mLauncherCallbacks != null) {
2694 mLauncherCallbacks.onClickAppShortcut(v);
2695 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002696 }
2697
Adam Cohen091440a2015-03-18 14:16:05 -07002698 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002699 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002700 final ShortcutInfo shortcut;
2701 final Intent intent;
2702 if (tag instanceof ShortcutInfo) {
2703 shortcut = (ShortcutInfo) tag;
2704 intent = shortcut.intent;
2705 int[] pos = new int[2];
2706 v.getLocationOnScreen(pos);
2707 intent.setSourceBounds(new Rect(pos[0], pos[1],
2708 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002709
Sunny Goyal508da152014-08-14 10:53:27 -07002710 } else if (tag instanceof AppInfo) {
2711 shortcut = null;
2712 intent = ((AppInfo) tag).intent;
2713 } else {
2714 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2715 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716
2717 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002718 mStats.recordLaunch(intent, shortcut);
2719
2720 if (success && v instanceof BubbleTextView) {
2721 mWaitingForResume = (BubbleTextView) v;
2722 mWaitingForResume.setStayPressed(true);
2723 }
2724 }
2725
2726 /**
2727 * Event handler for a folder icon click.
2728 *
2729 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2730 */
2731 protected void onClickFolderIcon(View v) {
2732 if (LOGD) Log.d(TAG, "onClickFolder");
2733 if (!(v instanceof FolderIcon)){
2734 throw new IllegalArgumentException("Input must be a FolderIcon");
2735 }
2736
2737 FolderIcon folderIcon = (FolderIcon) v;
2738 final FolderInfo info = folderIcon.getFolderInfo();
2739 Folder openFolder = mWorkspace.getFolderForTag(info);
2740
2741 // If the folder info reports that the associated folder is open, then verify that
2742 // it is actually opened. There have been a few instances where this gets out of sync.
2743 if (info.opened && openFolder == null) {
2744 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2745 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2746 info.opened = false;
2747 }
2748
2749 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2750 // Close any open folder
2751 closeFolder();
2752 // Open the requested folder
2753 openFolder(folderIcon);
2754 } else {
2755 // Find the open folder...
2756 int folderScreen;
2757 if (openFolder != null) {
2758 folderScreen = mWorkspace.getPageForView(openFolder);
2759 // .. and close it
2760 closeFolder(openFolder);
2761 if (folderScreen != mWorkspace.getCurrentPage()) {
2762 // Close any folder open on the current screen
2763 closeFolder();
2764 // Pull the folder onto this screen
2765 openFolder(folderIcon);
2766 }
2767 }
2768 }
Adam Cohen9211d422014-10-07 18:14:53 -07002769
2770 if (mLauncherCallbacks != null) {
2771 mLauncherCallbacks.onClickFolderIcon(v);
2772 }
Michael Jurka5130e402011-10-13 04:55:35 -07002773 }
2774
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002775 /**
2776 * Event handler for the (Add) Widgets button that appears after a long press
2777 * on the home screen.
2778 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002779 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002780 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002781 if (mIsSafeModeEnabled) {
2782 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2783 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002784 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002785 if (mLauncherCallbacks != null) {
2786 mLauncherCallbacks.onClickAddWidgetButton(view);
2787 }
Adam Cohen9211d422014-10-07 18:14:53 -07002788 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002789 }
2790
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002791 /**
2792 * Event handler for the wallpaper picker button that appears after a long press
2793 * on the home screen.
2794 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002795 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002796 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002797 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2798 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002799
2800 if (mLauncherCallbacks != null) {
2801 mLauncherCallbacks.onClickWallpaperPicker(v);
2802 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002803 }
2804
2805 /**
2806 * Event handler for a click on the settings button that appears after a long press
2807 * on the home screen.
2808 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002809 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002810 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002811 if (mLauncherCallbacks != null) {
2812 mLauncherCallbacks.onClickSettingsButton(v);
2813 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002814 }
2815
Michael Jurka5130e402011-10-13 04:55:35 -07002816 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002817 // Provide the same haptic feedback that the system offers for virtual keys.
2818 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002819 }
2820
Adam Cohen61f560d2013-09-30 15:58:20 -07002821 public void performHapticFeedbackOnTouchDown(View v) {
2822 // Provide the same haptic feedback that the system offers for virtual keys.
2823 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2824 }
2825
2826 public View.OnTouchListener getHapticFeedbackTouchListener() {
2827 if (mHapticFeedbackTouchListener == null) {
2828 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2829 @Override
2830 public boolean onTouch(View v, MotionEvent event) {
2831 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2832 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2833 }
2834 return false;
2835 }
2836 };
2837 }
2838 return mHapticFeedbackTouchListener;
2839 }
2840
Adam Cohen9211d422014-10-07 18:14:53 -07002841 public void onDragStarted(View view) {
2842 if (isOnCustomContent()) {
2843 // Custom content screen doesn't participate in drag and drop. If on custom
2844 // content screen, move to default.
2845 moveWorkspaceToDefaultScreen();
2846 }
2847
2848 if (mLauncherCallbacks != null) {
2849 mLauncherCallbacks.onDragStarted(view);
2850 }
2851 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002852
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002853 /**
2854 * Called when the user stops interacting with the launcher.
2855 * This implies that the user is now on the homescreen and is not doing housekeeping.
2856 */
Adam Cohen9211d422014-10-07 18:14:53 -07002857 protected void onInteractionEnd() {
2858 if (mLauncherCallbacks != null) {
2859 mLauncherCallbacks.onInteractionEnd();
2860 }
2861 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002862
2863 /**
2864 * Called when the user starts interacting with the launcher.
2865 * The possible interactions are:
2866 * - open all apps
2867 * - reorder an app shortcut, or a widget
2868 * - open the overview mode.
2869 * This is a good time to stop doing things that only make sense
2870 * when the user is on the homescreen and not doing housekeeping.
2871 */
Adam Cohen9211d422014-10-07 18:14:53 -07002872 protected void onInteractionBegin() {
2873 if (mLauncherCallbacks != null) {
2874 mLauncherCallbacks.onInteractionBegin();
2875 }
2876 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002877
Winson Chungcd99cd32015-04-29 11:03:24 -07002878 /** Updates the interaction state. */
2879 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
2880 // Only update the interacting state if we are transitioning to/from a view without an
2881 // overlay
2882 boolean fromStateWithoutOverlay = fromState != Workspace.State.NORMAL &&
2883 fromState != Workspace.State.NORMAL_HIDDEN;
2884 boolean toStateWithoutOverlay = toState != Workspace.State.NORMAL &&
2885 toState != Workspace.State.NORMAL_HIDDEN;
2886 if (toStateWithoutOverlay) {
2887 onInteractionBegin();
2888 } else if (fromStateWithoutOverlay) {
2889 onInteractionEnd();
2890 }
2891 }
2892
Kenny Guyf07af7b2014-07-31 11:39:16 +01002893 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002894 try {
2895 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002896 launcherApps.showAppDetailsForProfile(componentName, user);
2897 } catch (SecurityException e) {
2898 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2899 Log.e(TAG, "Launcher does not have permission to launch settings");
2900 } catch (ActivityNotFoundException e) {
2901 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2902 Log.e(TAG, "Unable to launch settings");
2903 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002904 }
2905
Michael Jurka1e2f4652013-07-08 18:03:46 -07002906 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002907 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2908 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002909 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002910 // System applications cannot be installed. For now, show a toast explaining that.
2911 // We may give them the option of disabling apps this way.
2912 int messageId = R.string.uninstall_system_app_text;
2913 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002914 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002915 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002916 String packageName = componentName.getPackageName();
2917 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002918 Intent intent = new Intent(
2919 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002920 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2921 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002922 if (user != null) {
2923 user.addToIntent(intent, Intent.EXTRA_USER);
2924 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002925 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002926 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002927 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002928 }
2929
Michael Jurka86a720e2012-05-09 11:23:23 -07002930 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002931 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002933 // Only launch using the new animation if the shortcut has not opted out (this is a
2934 // private contract between launcher and may be ignored in the future).
2935 boolean useLaunchAnimation = (v != null) &&
2936 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002937 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2938 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002939
Kenny Guy1317e2d2014-05-08 18:52:50 +01002940 UserHandleCompat user = null;
2941 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2942 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2943 user = userManager.getUserForSerialNumber(serialNumber);
2944 }
2945
2946 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002947 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002948 ActivityOptions opts = null;
2949 if (sClipRevealMethod != null) {
2950 // TODO: call method directly when Launcher3 can depend on M APIs
2951 int left = 0, top = 0;
2952 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2953 if (v instanceof TextView) {
2954 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002955 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2956 if (icon != null) {
2957 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002958 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002959 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002960 width = bounds.width();
2961 height = bounds.height();
2962 }
2963 }
2964 try {
2965 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2966 left, top, width, height);
2967 } catch (IllegalAccessException e) {
2968 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2969 sClipRevealMethod = null;
2970 } catch (InvocationTargetException e) {
2971 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2972 sClipRevealMethod = null;
2973 }
2974 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002975 if (opts == null && !Utilities.isLmpOrAbove()) {
2976 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002977 v.getMeasuredWidth(), v.getMeasuredHeight());
2978 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002979 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002980 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002981
2982 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2983 // Could be launching some bookkeeping activity
2984 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002985 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002986 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002987 launcherApps.startActivityForProfile(intent.getComponent(), user,
2988 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002989 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002990 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002991 } catch (SecurityException e) {
2992 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002993 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002994 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002995 "or use the exported attribute for this activity. "
2996 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002998 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002999 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003000
Michael Jurka86a720e2012-05-09 11:23:23 -07003001 boolean startActivitySafely(View v, Intent intent, Object tag) {
3002 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003003 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3004 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3005 return false;
3006 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003007 try {
3008 success = startActivity(v, intent, tag);
3009 } catch (ActivityNotFoundException e) {
3010 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3011 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3012 }
3013 return success;
3014 }
3015
Adam Cohen268c4752012-06-06 17:47:33 -07003016 /**
3017 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3018 * in the DragLayer in the exact absolute location of the original FolderIcon.
3019 */
3020 private void copyFolderIconToImage(FolderIcon fi) {
3021 final int width = fi.getMeasuredWidth();
3022 final int height = fi.getMeasuredHeight();
3023
3024 // Lazy load ImageView, Bitmap and Canvas
3025 if (mFolderIconImageView == null) {
3026 mFolderIconImageView = new ImageView(this);
3027 }
3028 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3029 mFolderIconBitmap.getHeight() != height) {
3030 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3031 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3032 }
3033
3034 DragLayer.LayoutParams lp;
3035 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3036 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3037 } else {
3038 lp = new DragLayer.LayoutParams(width, height);
3039 }
3040
Adam Cohen307fe232012-08-16 17:55:58 -07003041 // The layout from which the folder is being opened may be scaled, adjust the starting
3042 // view size by this scale factor.
3043 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003044 lp.customPosition = true;
3045 lp.x = mRectForFolderAnimation.left;
3046 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003047 lp.width = (int) (scale * width);
3048 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003049
3050 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3051 fi.draw(mFolderIconCanvas);
3052 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003053 if (fi.getFolder() != null) {
3054 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3055 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003056 }
Adam Cohen268c4752012-06-06 17:47:33 -07003057 // Just in case this image view is still in the drag layer from a previous animation,
3058 // we remove it and re-add it.
3059 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3060 mDragLayer.removeView(mFolderIconImageView);
3061 }
3062 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003063 if (fi.getFolder() != null) {
3064 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003065 }
Adam Cohen268c4752012-06-06 17:47:33 -07003066 }
3067
Adam Cohen2801caf2011-05-13 20:57:39 -07003068 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003069 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003070 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3071 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3072 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3073
Adam Cohenc51934b2011-07-26 21:07:43 -07003074 FolderInfo info = (FolderInfo) fi.getTag();
3075 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3076 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003077 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3078 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003079 }
3080
Adam Cohen268c4752012-06-06 17:47:33 -07003081 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3082 copyFolderIconToImage(fi);
3083 fi.setVisibility(View.INVISIBLE);
3084
Michael Jurka2ecf9952012-06-18 12:52:28 -07003085 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003086 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003087 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003088 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3089 }
3090 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003091 oa.start();
3092 }
3093
Adam Cohen268c4752012-06-06 17:47:33 -07003094 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003095 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003096 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3097 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3098 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3099
Adam Cohen268c4752012-06-06 17:47:33 -07003100 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003101
Adam Cohen268c4752012-06-06 17:47:33 -07003102 // We remove and re-draw the FolderIcon in-case it has changed
3103 mDragLayer.removeView(mFolderIconImageView);
3104 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003105 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003106 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003107 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003108 oa.addListener(new AnimatorListenerAdapter() {
3109 @Override
3110 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003111 if (cl != null) {
3112 cl.clearFolderLeaveBehind();
3113 // Remove the ImageView copy of the FolderIcon and make the original visible.
3114 mDragLayer.removeView(mFolderIconImageView);
3115 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003116 }
3117 }
3118 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003119 oa.start();
3120 }
3121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003123 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124 * is animated relative to the specified View. If the View is null, no animation
3125 * is played.
3126 *
3127 * @param folderInfo The FolderInfo describing the folder to open.
3128 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003129 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003130 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003131 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3132 if (openFolder != null && openFolder != folder) {
3133 // Close any open folder before opening a folder.
3134 closeFolder();
3135 }
3136
Adam Cohena9cf38f2011-05-02 15:36:58 -07003137 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138
Adam Cohena9cf38f2011-05-02 15:36:58 -07003139 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003140
Sunny Goyalf4066152015-04-15 09:42:19 -07003141 // While the folder is open, the position of the icon cannot change.
3142 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3143
Adam Cohen4554ee12011-08-03 16:13:21 -07003144 // Just verify that the folder hasn't already been added to the DragLayer.
3145 // There was a one-off crash where the folder had a parent already.
3146 if (folder.getParent() == null) {
3147 mDragLayer.addView(folder);
3148 mDragController.addDropTarget((DropTarget) folder);
3149 } else {
3150 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3151 folder.getParent() + ").");
3152 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003153 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003154 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003155
3156 // Notify the accessibility manager that this folder "window" has appeared and occluded
3157 // the workspace items
3158 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3159 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003160 }
3161
3162 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003163 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003164 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003165 if (folder.isEditingName()) {
3166 folder.dismissEditingName();
3167 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003168 closeFolder(folder);
3169 }
3170 }
3171
3172 void closeFolder(Folder folder) {
3173 folder.getInfo().opened = false;
3174
3175 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3176 if (parent != null) {
3177 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3178 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003179 if (fi != null) {
3180 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3181 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003182 }
3183 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003184
3185 // Notify the accessibility manager that this folder "window" has disappeard and no
3186 // longer occludeds the workspace items
3187 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 }
3189
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003191 if (!isDraggingEnabled()) return false;
3192 if (isWorkspaceLocked()) return false;
3193 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194
Adam Cohen1697b792013-09-17 19:08:21 -07003195 if (v instanceof Workspace) {
3196 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003197 if (!mWorkspace.isTouchActive()) {
3198 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003199 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3200 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3201 return true;
3202 } else {
3203 return false;
3204 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003205 } else {
3206 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003207 }
Adam Cohen1697b792013-09-17 19:08:21 -07003208 }
3209
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003210 CellLayout.CellInfo longClickCellInfo = null;
3211 View itemUnderLongClick = null;
3212 if (v.getTag() instanceof ItemInfo) {
3213 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003214 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003215 itemUnderLongClick = longClickCellInfo.cell;
3216 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003217 }
3218
Winson Chung3d503fb2011-07-13 17:25:49 -07003219 // The hotseat touch handling does not go through Workspace, and we always allow long press
3220 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003221 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003222 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003223 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003224 // User long pressed on empty space
3225 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3226 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003227 if (mWorkspace.isInOverviewMode()) {
3228 mWorkspace.startReordering(v);
3229 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003230 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003233 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3234 mHotseat.getOrderInHotseat(
3235 longClickCellInfo.cellX,
3236 longClickCellInfo.cellY));
3237 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003239 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003240 }
3241 }
3242 }
3243 return true;
3244 }
3245
Winson Chung3d503fb2011-07-13 17:25:49 -07003246 boolean isHotseatLayout(View layout) {
3247 return mHotseat != null && layout != null &&
3248 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3249 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003250
Winson Chung3d503fb2011-07-13 17:25:49 -07003251 /**
3252 * Returns the CellLayout of the specified container at the specified screen.
3253 */
Sunny Goyal92820592015-03-02 11:31:35 -08003254 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003255 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3256 if (mHotseat != null) {
3257 return mHotseat.getLayout();
3258 } else {
3259 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003260 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003261 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003262 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003263 }
3264 }
3265
Winson Chungb745afb2015-03-02 11:51:23 -08003266 /**
3267 * For overridden classes.
3268 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003269 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003270 return isAppsViewVisible();
3271 }
3272
3273 public boolean isAppsViewVisible() {
3274 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3275 }
3276
3277 public boolean isWidgetsViewVisible() {
3278 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003279 }
3280
Craig Mautner360310b2012-10-26 15:13:08 -07003281 private void setWorkspaceBackground(boolean workspace) {
3282 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003283 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003284 }
3285
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003286 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003287 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3288 int curflags = getWindow().getAttributes().flags
3289 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3290 if (wpflags != curflags) {
3291 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3292 }
Craig Mautner360310b2012-10-26 15:13:08 -07003293 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003294 }
3295
Michael Jurkae326f182011-11-21 14:05:46 -08003296 @Override
3297 public void onTrimMemory(int level) {
3298 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003299 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3300 // The widget preview db can result in holding onto over
3301 // 3MB of memory for caching which isn't necessary.
3302 SQLiteDatabase.releaseMemory();
3303
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003304 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003305 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003306 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003307 if (mLauncherCallbacks != null) {
3308 mLauncherCallbacks.onTrimMemory(level);
3309 }
Michael Jurkae326f182011-11-21 14:05:46 -08003310 }
3311
Winson Chungb745afb2015-03-02 11:51:23 -08003312 @Override
3313 public void onStateTransitionHideSearchBar() {
3314 // Hide the search bar
3315 if (mSearchDropTargetBar != null) {
3316 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3317 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003318 }
3319
Winson Chungb745afb2015-03-02 11:51:23 -08003320 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003321 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3322 true);
Adam Cohened307df2013-10-02 09:37:31 -07003323 }
3324
Winson Chungdc61c4d2015-04-20 18:26:57 -07003325 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3326 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3327 onCompleteRunnable, true);
3328 }
3329
3330 protected void showWorkspace(int snapToPage, boolean animated) {
3331 showWorkspace(snapToPage, animated, null, true);
3332 }
3333
3334 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3335 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003336 boolean changed = mState != State.WORKSPACE ||
3337 mWorkspace.getState() != Workspace.State.NORMAL;
3338 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003339 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003340 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003341 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003342 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003343
Winson Chungc7d2b602012-05-16 17:02:20 -07003344 // Show the search bar (only animate if we were showing the drop target bar in spring
3345 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003346 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003347 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003348 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003349
Michael Jurkab3e22d92011-10-31 15:58:33 -07003350 // Set focus to the AppsCustomize button
3351 if (mAllAppsButton != null) {
3352 mAllAppsButton.requestFocus();
3353 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003354 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003355
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003356 // Change the state *after* we've called all the transition code
3357 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003358
Winson Chung5fb63472011-02-02 17:03:37 -08003359 // Resume the auto-advance of widgets
3360 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003361 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003362
Winson Chung0f785722015-04-08 10:27:49 -07003363 if (changed) {
3364 // Send an accessibility event to announce the context change
3365 getWindow().getDecorView()
3366 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003367 if (notifyLauncherCallbacks) {
3368 // Dismiss all apps when the workspace is shown
3369 if (mLauncherCallbacks != null) {
3370 mLauncherCallbacks.onAllAppsHidden();
3371 }
3372 }
Winson Chung0f785722015-04-08 10:27:49 -07003373 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003374 }
3375
Adam Cohened307df2013-10-02 09:37:31 -07003376 void showOverviewMode(boolean animated) {
3377 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003378 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003379 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3380 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003381 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003382 }
3383
Winson Chungb745afb2015-03-02 11:51:23 -08003384 /**
3385 * Shows the apps view.
3386 */
3387 void showAppsView(boolean animated, boolean resetListToTop) {
3388 if (resetListToTop) {
3389 mAppsView.scrollToTop();
3390 }
3391 showAppsOrWidgets(animated, State.APPS);
3392 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003393
Winson Chungb745afb2015-03-02 11:51:23 -08003394 /**
3395 * Shows the widgets view.
3396 */
3397 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003398 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003399 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003400 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003401 }
Winson Chungb745afb2015-03-02 11:51:23 -08003402 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003403
3404 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003405 @Override
3406 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003407 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003408 }
3409 });
Winson Chungb745afb2015-03-02 11:51:23 -08003410 }
3411
3412 /**
3413 * Sets up the transition to show the apps/widgets view.
3414 */
3415 private void showAppsOrWidgets(boolean animated, State toState) {
3416 if (mState != State.WORKSPACE) return;
3417 if (toState != State.APPS && toState != State.WIDGETS) return;
3418
3419 if (toState == State.APPS) {
3420 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung0f785722015-04-08 10:27:49 -07003421 if (mLauncherCallbacks != null) {
3422 mLauncherCallbacks.onAllAppsShown();
3423 }
Winson Chungb745afb2015-03-02 11:51:23 -08003424 } else {
3425 mStateTransitionAnimation.startAnimationToWidgets(animated);
3426 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003427
Michael Jurkab3e22d92011-10-31 15:58:33 -07003428 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003429 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003430
3431 // Pause the auto-advance of widgets until we are out of AllApps
3432 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003433 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003434 closeFolder();
3435
3436 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003437 getWindow().getDecorView()
3438 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003439 }
3440
Winson Chungcd99cd32015-04-29 11:03:24 -07003441 /**
3442 * Updates the workspace and interaction state on state change, and return the animation to this
3443 * new state.
3444 */
3445 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3446 boolean animated, HashMap<View, Integer> layerViews) {
3447 Workspace.State fromState = mWorkspace.getState();
3448 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3449 updateInteraction(fromState, toState);
3450 return anim;
3451 }
3452
Hyunyoung Song3f471442015-04-08 19:01:34 -07003453 public void enterSpringLoadedDragMode() {
3454 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3455 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003456 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3457 mState == State.WIDGETS_SPRING_LOADED) {
3458 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003459 }
Winson Chungb745afb2015-03-02 11:51:23 -08003460
3461 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003462 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3463 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003464 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003465 }
Adam Cohen7777d962011-08-18 18:58:38 -07003466
Hyunyoung Song3f471442015-04-08 19:01:34 -07003467 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003468 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003469 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003470
Winson Chungcd99cd32015-04-29 11:03:24 -07003471 if (successfulDrop) {
3472 // We need to trigger all apps hidden to notify search to update itself before the
3473 // delayed call to showWorkspace below
3474 if (mLauncherCallbacks != null) {
3475 mLauncherCallbacks.onAllAppsHidden();
3476 }
3477 }
3478
Winson Chunge7a03942011-08-05 15:05:12 -07003479 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003480 @Override
3481 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003482 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003483 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3484 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003485 // Before we show workspace, hide all apps again because
3486 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3487 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003488 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003489 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003490 } else {
3491 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003492 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003493 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003494 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003495 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003496
Michael Jurkad3ef3062010-11-23 16:23:58 -08003497 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003498 if (mState == State.APPS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003499 showAppsView(true, false);
Winson Chungb745afb2015-03-02 11:51:23 -08003500 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003501 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003502 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003503 }
3504
Michael Jurkab3e22d92011-10-31 15:58:33 -07003505 void lockAllApps() {
3506 // TODO
3507 }
3508
3509 void unlockAllApps() {
3510 // TODO
3511 }
3512
Sunny Goyal594d76d2014-11-06 10:12:54 -08003513 protected void disableVoiceButtonProxy(boolean disable) {
3514 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003515 }
3516
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003517 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003518 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3519 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003520 }
3521
Adam Cohen24ce0b32014-01-14 16:18:14 -08003522 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003523 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3524 if (searchProvider == null) {
3525 return null;
3526 }
3527
3528 Bundle opts = new Bundle();
3529 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003530 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003531
3532 SharedPreferences sp = getSharedPreferences(
3533 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3534 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003535 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003536 if (!searchProvider.provider.flattenToString().equals(
3537 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003538 || (widgetInfo == null)
3539 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003540 // A valid widget is not already bound.
3541 if (widgetId > -1) {
3542 mAppWidgetHost.deleteAppWidgetId(widgetId);
3543 widgetId = -1;
3544 }
3545
3546 // Try to bind a new widget
3547 widgetId = mAppWidgetHost.allocateAppWidgetId();
3548
3549 if (!AppWidgetManagerCompat.getInstance(this)
3550 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3551 mAppWidgetHost.deleteAppWidgetId(widgetId);
3552 widgetId = -1;
3553 }
3554
3555 sp.edit()
3556 .putInt(QSB_WIDGET_ID, widgetId)
3557 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3558 .commit();
3559 }
3560
3561 if (widgetId != -1) {
3562 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3563 mQsb.updateAppWidgetOptions(opts);
3564 mQsb.setPadding(0, 0, 0, 0);
3565 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003566 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003567 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003568 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003569 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003570 }
3571
Sunny Goyal22235bc2015-04-03 09:23:43 -07003572 private void reinflateQSBIfNecessary() {
3573 if (mQsb instanceof LauncherAppWidgetHostView &&
3574 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3575 mSearchDropTargetBar.removeView(mQsb);
3576 mQsb = null;
3577 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3578 }
3579 }
3580
Michael Jurkad7c28052012-04-27 15:43:36 -07003581 @Override
3582 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003583 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003584 final List<CharSequence> text = event.getText();
3585 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003586 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003587 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003588 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003589 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003590 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003591 } else {
3592 text.add(getString(R.string.all_apps_home_button_label));
3593 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003594 return result;
3595 }
3596
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003597 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003598 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003599 */
Adam Cohen091440a2015-03-18 14:16:05 -07003600 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003601 @Override
3602 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003603 closeSystemDialogs();
3604 }
3605 }
3606
3607 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003608 * Receives notifications whenever the appwidgets are reset.
3609 */
3610 private class AppWidgetResetObserver extends ContentObserver {
3611 public AppWidgetResetObserver() {
3612 super(new Handler());
3613 }
3614
3615 @Override
3616 public void onChange(boolean selfChange) {
3617 onAppWidgetReset();
3618 }
3619 }
3620
3621 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003622 * If the activity is currently paused, signal that we need to run the passed Runnable
3623 * in onResume.
3624 *
3625 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003626 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3627 * wrong when we're not running, and if the activity comes back to what the configuration was
3628 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003629 *
3630 * Implementation of the method from LauncherModel.Callbacks.
3631 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003632 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003633 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003634 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003635 if (mPaused) {
3636 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003637 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003638 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003639 }
3640 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003641 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003642 return true;
3643 } else {
3644 return false;
3645 }
3646 }
3647
Michael Jurkac402cd92013-05-20 15:49:32 +02003648 private boolean waitUntilResume(Runnable run) {
3649 return waitUntilResume(run, false);
3650 }
3651
Michael Jurka1e2f4652013-07-08 18:03:46 -07003652 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003653 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003654 }
3655
Michael Jurka7607c2f2013-04-03 14:33:19 -07003656 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003657 * If the activity is currently paused, signal that we need to re-run the loader
3658 * in onResume.
3659 *
3660 * This needs to be called from incoming places where resources might have been loaded
3661 * while we are paused. That is becaues the Configuration might be wrong
3662 * when we're not running, and if it comes back to what it was when we
3663 * were paused, we are not restarted.
3664 *
3665 * Implementation of the method from LauncherModel.Callbacks.
3666 *
3667 * @return true if we are currently paused. The caller might be able to
3668 * skip some work in that case since we will come back again.
3669 */
3670 public boolean setLoadOnResume() {
3671 if (mPaused) {
3672 Log.i(TAG, "setLoadOnResume");
3673 mOnResumeNeedsLoad = true;
3674 return true;
3675 } else {
3676 return false;
3677 }
3678 }
3679
3680 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003681 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003682 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003684 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003685 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003686 } else {
3687 return SCREEN_COUNT / 2;
3688 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003690
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 /**
3692 * Refreshes the shortcuts shown on the workspace.
3693 *
3694 * Implementation of the method from LauncherModel.Callbacks.
3695 */
3696 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003697 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003698
Michael Jurka7607c2f2013-04-03 14:33:19 -07003699 // If we're starting binding all over again, clear any bind calls we'd postponed in
3700 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3701 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003702 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003703
Winson Chungd64d1762013-08-20 14:37:16 -07003704 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003705 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003706 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003707
Michael Jurka05bf6442011-11-30 20:28:53 -08003708 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003709 if (mHotseat != null) {
3710 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003711 }
3712 }
3713
Adam Cohendcd297f2013-06-18 13:13:40 -07003714 @Override
3715 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003716 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003717
Adam Cohen5084cba2013-09-03 12:01:16 -07003718 // If there are no screens, we need to have an empty screen
3719 if (orderedScreenIds.size() == 0) {
3720 mWorkspace.addExtraEmptyScreen();
3721 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003722
3723 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003724 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003725 if (hasCustomContentToLeft()) {
3726 mWorkspace.createCustomContentContainer();
3727 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003728 }
Winson Chung64359a52013-07-08 17:17:08 -07003729 }
3730
3731 @Override
3732 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003733 // Log to disk
3734 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3735 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3736 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003737 int count = orderedScreenIds.size();
3738 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003739 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003740 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003741 }
3742
Adam Cohen39a06042013-07-19 14:30:12 -07003743 private boolean shouldShowWeightWatcher() {
3744 String spKey = LauncherAppState.getSharedPreferencesKey();
3745 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003746 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003747
3748 return show;
3749 }
3750
3751 private void toggleShowWeightWatcher() {
3752 String spKey = LauncherAppState.getSharedPreferencesKey();
3753 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3754 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3755
3756 show = !show;
3757
3758 SharedPreferences.Editor editor = sp.edit();
3759 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3760 editor.commit();
3761
3762 if (mWeightWatcher != null) {
3763 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3764 }
3765 }
3766
Winson Chungd64d1762013-08-20 14:37:16 -07003767 public void bindAppsAdded(final ArrayList<Long> newScreens,
3768 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003769 final ArrayList<ItemInfo> addAnimated,
3770 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003771 Runnable r = new Runnable() {
3772 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003773 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003774 }
3775 };
3776 if (waitUntilResume(r)) {
3777 return;
3778 }
3779
Winson Chungd64d1762013-08-20 14:37:16 -07003780 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003781 if (newScreens != null) {
3782 bindAddScreens(newScreens);
3783 }
Winson Chungd64d1762013-08-20 14:37:16 -07003784
3785 // We add the items without animation on non-visible pages, and with
3786 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003787 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003788 bindItems(addNotAnimated, 0,
3789 addNotAnimated.size(), false);
3790 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003791 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003792 bindItems(addAnimated, 0,
3793 addAnimated.size(), true);
3794 }
Winson Chungc58497e2013-09-03 17:48:37 -07003795
Winson Chung87412982013-10-03 18:34:14 -07003796 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003797 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003798
Winson Chungb745afb2015-03-02 11:51:23 -08003799 if (addedApps != null && mAppsView != null) {
3800 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003801 }
Winson Chungd64d1762013-08-20 14:37:16 -07003802 }
3803
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003804 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003805 * Bind the items start-end from the list.
3806 *
3807 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003808 */
Winson Chung64359a52013-07-08 17:17:08 -07003809 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3810 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003811 Runnable r = new Runnable() {
3812 public void run() {
3813 bindItems(shortcuts, start, end, forceAnimateIcons);
3814 }
3815 };
3816 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003817 return;
3818 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003819
Winson Chungf0c6ae02012-03-21 16:10:31 -07003820 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003821 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3822 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003823 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003824 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003825 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003826 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003827 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003828
3829 // Short circuit if we are loading dock items for a configuration which has no dock
3830 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3831 mHotseat == null) {
3832 continue;
3833 }
3834
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 switch (item.itemType) {
3836 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3837 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003838 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003839 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003840
Winson Chung64359a52013-07-08 17:17:08 -07003841 /*
3842 * TODO: FIX collision case
3843 */
Winson Chungce376632013-07-11 16:12:41 -07003844 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3845 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3846 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003847 View v = cl.getChildAt(item.cellX, item.cellY);
3848 Object tag = v.getTag();
3849 String desc = "Collision while binding workspace item: " + item
3850 + ". Collides with " + tag;
3851 if (LauncherAppState.isDogfoodBuild()) {
3852 throw (new RuntimeException(desc));
3853 } else {
3854 Log.d(TAG, desc);
3855 }
Winson Chungce376632013-07-11 16:12:41 -07003856 }
Winson Chung64359a52013-07-08 17:17:08 -07003857 }
3858
Adam Cohendcd297f2013-06-18 13:13:40 -07003859 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3860 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003861 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003862 // Animate all the applications up now
3863 shortcut.setAlpha(0f);
3864 shortcut.setScaleX(0f);
3865 shortcut.setScaleY(0f);
3866 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003867 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003870 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003871 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003872 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003873 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003874 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3875 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003876 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003877 default:
3878 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003880 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003881
Winson Chung997a9232013-07-24 15:33:46 -07003882 if (animateIcons) {
3883 // Animate to the correct page
3884 if (newShortcutsScreenId > -1) {
3885 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003886 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003887 final Runnable startBounceAnimRunnable = new Runnable() {
3888 public void run() {
3889 anim.playTogether(bounceAnims);
3890 anim.start();
3891 }
3892 };
Winson Chung997a9232013-07-24 15:33:46 -07003893 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003894 // We post the animation slightly delayed to prevent slowdowns
3895 // when we are loading right after we return to launcher.
3896 mWorkspace.postDelayed(new Runnable() {
3897 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003898 if (mWorkspace != null) {
3899 mWorkspace.snapToPage(newScreenIndex);
3900 mWorkspace.postDelayed(startBounceAnimRunnable,
3901 NEW_APPS_ANIMATION_DELAY);
3902 }
Winson Chung94d67682013-09-25 16:29:40 -07003903 }
3904 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003905 } else {
3906 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003907 }
3908 }
Winson Chung64359a52013-07-08 17:17:08 -07003909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003910 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003911 }
3912
Joe Onorato9c1289c2009-08-17 11:03:03 -04003913 /**
3914 * Implementation of the method from LauncherModel.Callbacks.
3915 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003916 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003917 Runnable r = new Runnable() {
3918 public void run() {
3919 bindFolders(folders);
3920 }
3921 };
3922 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003923 return;
3924 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003925 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003926 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003927
3928 /**
3929 * Add the views for a widget to the workspace.
3930 *
3931 * Implementation of the method from LauncherModel.Callbacks.
3932 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003933 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003934 Runnable r = new Runnable() {
3935 public void run() {
3936 bindAppWidget(item);
3937 }
3938 };
3939 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003940 return;
3941 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003942
Daniel Sandler843e8602010-06-07 14:59:01 -04003943 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3944 if (DEBUG_WIDGETS) {
3945 Log.d(TAG, "bindAppWidget: " + item);
3946 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003947 final Workspace workspace = mWorkspace;
3948
Adam Cohen59400422014-03-05 18:07:04 -08003949 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003950 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003951
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003952 if (!mIsSafeModeEnabled
3953 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3954 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003955 if (appWidgetInfo == null) {
3956 if (DEBUG_WIDGETS) {
3957 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3958 + " belongs to component " + item.providerName
3959 + ", as the povider is null");
3960 }
3961 LauncherModel.deleteItemFromDatabase(this, item);
3962 return;
3963 }
3964 // Note: This assumes that the id remap broadcast is received before this step.
3965 // If that is not the case, the id remap will be ignored and user may see the
3966 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003967 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003968 pendingInfo.spanX = item.spanX;
3969 pendingInfo.spanY = item.spanY;
3970 pendingInfo.minSpanX = item.minSpanX;
3971 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003972 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003973 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003974
Sunny Goyalff572272014-07-23 13:58:07 -07003975 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003976 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3977 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003978
3979 // TODO consider showing a permission dialog when the widget is clicked.
3980 if (!success) {
3981 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3982 if (DEBUG_WIDGETS) {
3983 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3984 + " belongs to component " + item.providerName
3985 + ", as the launcher is unable to bing a new widget id");
3986 }
3987 LauncherModel.deleteItemFromDatabase(this, item);
3988 return;
3989 }
3990
3991 item.appWidgetId = newWidgetId;
3992
3993 // If the widget has a configure activity, it is still needs to set it up, otherwise
3994 // the widget is ready to go.
3995 item.restoreStatus = (appWidgetInfo.configure == null)
3996 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3997 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3998
3999 LauncherModel.updateItemInDatabase(this, item);
4000 }
4001
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004002 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004003 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004004 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004005 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4006 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004007 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004008
Sunny Goyal651077b2014-06-30 14:15:31 -07004009 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4010 } else {
4011 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004012 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4013 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004014 view.updateIcon(mIconCache);
4015 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004016 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004017 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004018 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004019
Joe Onorato9c1289c2009-08-17 11:03:03 -04004020 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004021 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004022
Adam Cohendcd297f2013-06-18 13:13:40 -07004023 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004024 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004025 if (!item.isCustomWidget()) {
4026 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4027 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004028
Joe Onorato9c1289c2009-08-17 11:03:03 -04004029 workspace.requestLayout();
4030
Daniel Sandler843e8602010-06-07 14:59:01 -04004031 if (DEBUG_WIDGETS) {
4032 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4033 + (SystemClock.uptimeMillis()-start) + "ms");
4034 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004035 }
4036
Sunny Goyalff572272014-07-23 13:58:07 -07004037 /**
4038 * Restores a pending widget.
4039 *
4040 * @param appWidgetId The app widget id
4041 * @param cellInfo The position on screen where to create the widget.
4042 */
4043 private void completeRestoreAppWidget(final int appWidgetId) {
4044 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4045 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4046 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4047 return;
4048 }
4049
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004050 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004051 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4052
4053 mWorkspace.reinflateWidgetsIfNecessary();
4054 LauncherModel.updateItemInDatabase(this, info);
4055 }
4056
Adam Cohen1462de32012-07-24 22:34:36 -07004057 public void onPageBoundSynchronously(int page) {
4058 mSynchronouslyBoundPages.add(page);
4059 }
4060
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061 /**
4062 * Callback saying that there aren't any more items to bind.
4063 *
4064 * Implementation of the method from LauncherModel.Callbacks.
4065 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004066 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004067 Runnable r = new Runnable() {
4068 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004069 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004070 }
4071 };
4072 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004073 return;
4074 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075 if (mSavedState != null) {
4076 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004077 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004078 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004079 mSavedState = null;
4080 }
4081
Adam Cohen1462de32012-07-24 22:34:36 -07004082 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004083
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004084 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004085 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004086
4087 // If we received the result of any pending adds while the loader was running (e.g. the
4088 // widget configuration forced an orientation change), process them now.
4089 if (sPendingAddItem != null) {
4090 final long screenId = completeAdd(sPendingAddItem);
4091
4092 // TODO: this moves the user to the page where the pending item was added. Ideally,
4093 // the screen would be guaranteed to exist after bind, and the page would be set through
4094 // the workspace restore process.
4095 mWorkspace.post(new Runnable() {
4096 @Override
4097 public void run() {
4098 mWorkspace.snapToScreenId(screenId);
4099 }
4100 });
4101 sPendingAddItem = null;
4102 }
4103
Sunny Goyal756adbc2015-04-16 15:20:51 -07004104 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004105
4106 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004107 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004108 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004109 }
4110
Adam Cohen3ed316a2014-07-23 18:21:20 -07004111 private void sendLoadingCompleteBroadcastIfNecessary() {
4112 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4113 String permission =
4114 getResources().getString(R.string.receive_first_load_broadcast_permission);
4115 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4116 sendBroadcast(intent, permission);
4117 SharedPreferences.Editor editor = mSharedPrefs.edit();
4118 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4119 editor.apply();
4120 }
4121 }
4122
Winson Chunga0b7e862013-09-05 16:03:15 -07004123 public boolean isAllAppsButtonRank(int rank) {
4124 if (mHotseat != null) {
4125 return mHotseat.isAllAppsButtonRank(rank);
4126 }
4127 return false;
4128 }
4129
Winson Chunga2413752012-04-03 14:22:34 -07004130 private boolean canRunNewAppsAnimation() {
4131 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4132 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4133 }
4134
Winson Chungc9168342013-06-26 14:54:55 -07004135 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4136 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4137 PropertyValuesHolder.ofFloat("alpha", 1f),
4138 PropertyValuesHolder.ofFloat("scaleX", 1f),
4139 PropertyValuesHolder.ofFloat("scaleY", 1f));
4140 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4141 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4142 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4143 return bounceAnim;
4144 }
4145
Adam Cohen27772732013-11-11 14:00:56 +00004146 public boolean useVerticalBarLayout() {
4147 return LauncherAppState.getInstance().getDynamicGrid().
4148 getDeviceProfile().isVerticalBarLayout();
4149 }
4150
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004151 protected Rect getSearchBarBounds() {
4152 return LauncherAppState.getInstance().getDynamicGrid().
4153 getDeviceProfile().getSearchBarBounds();
4154 }
4155
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004156 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004157 if (mSearchDropTargetBar == null) {
4158 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004159 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004160 if (mQsb != null) {
4161 mSearchDropTargetBar.removeView(mQsb);
4162 mQsb = null;
4163 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004164 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004165 }
4166
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004167 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004168 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4169 * multiple calls to bind the same list.)
4170 */
4171 @Thunk ArrayList<AppInfo> mTmpAppsList;
4172 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4173 public void run() {
4174 bindAllApplications(mTmpAppsList);
4175 mTmpAppsList = null;
4176 }
4177 };
4178
4179 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004180 * Add the icons for all apps.
4181 *
4182 * Implementation of the method from LauncherModel.Callbacks.
4183 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004184 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004185 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4186 mTmpAppsList = apps;
4187 return;
4188 }
4189
Winson Chungb745afb2015-03-02 11:51:23 -08004190 if (mAppsView != null) {
4191 mAppsView.setApps(apps);
4192 }
Adam Cohen9211d422014-10-07 18:14:53 -07004193 if (mLauncherCallbacks != null) {
4194 mLauncherCallbacks.bindAllApplications(apps);
4195 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004196 }
4197
4198 /**
4199 * A package was updated.
4200 *
4201 * Implementation of the method from LauncherModel.Callbacks.
4202 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004203 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004204 Runnable r = new Runnable() {
4205 public void run() {
4206 bindAppsUpdated(apps);
4207 }
4208 };
4209 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004210 return;
4211 }
4212
Winson Chungb745afb2015-03-02 11:51:23 -08004213 if (mAppsView != null) {
4214 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004215 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004216 }
4217
Sunny Goyal4390ace2014-10-13 11:33:11 -07004218 @Override
4219 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4220 Runnable r = new Runnable() {
4221 public void run() {
4222 bindWidgetsRestored(widgets);
4223 }
4224 };
4225 if (waitUntilResume(r)) {
4226 return;
4227 }
4228 mWorkspace.widgetsRestored(widgets);
4229 }
4230
Joe Onorato9c1289c2009-08-17 11:03:03 -04004231 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004232 * Some shortcuts were updated in the background.
4233 *
4234 * Implementation of the method from LauncherModel.Callbacks.
4235 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004236 @Override
4237 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4238 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004239 Runnable r = new Runnable() {
4240 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004241 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004242 }
4243 };
4244 if (waitUntilResume(r)) {
4245 return;
4246 }
4247
Sunny Goyal4390ace2014-10-13 11:33:11 -07004248 if (!updated.isEmpty()) {
4249 mWorkspace.updateShortcuts(updated);
4250 }
4251
4252 if (!removed.isEmpty()) {
4253 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4254 for (ShortcutInfo si : removed) {
4255 removedComponents.add(si.getTargetComponent());
4256 }
4257 mWorkspace.removeItemsByComponentName(removedComponents, user);
4258 // Notify the drag controller
4259 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004260 }
4261 }
4262
4263 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004264 * Update the state of a package, typically related to install state.
4265 *
4266 * Implementation of the method from LauncherModel.Callbacks.
4267 */
Sunny Goyale755d462014-07-22 13:48:29 -07004268 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004269 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4270 Runnable r = new Runnable() {
4271 public void run() {
4272 bindRestoreItemsChange(updates);
4273 }
4274 };
4275 if (waitUntilResume(r)) {
4276 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004277 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004278
Sunny Goyal756adbc2015-04-16 15:20:51 -07004279 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004280 }
4281
4282 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004283 * A package was uninstalled. We take both the super set of packageNames
4284 * in addition to specific applications to remove, the reason being that
4285 * this can be called when a package is updated as well. In that scenario,
4286 * we only remove specific components from the workspace, where as
4287 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004288 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004289 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004290 * Implementation of the method from LauncherModel.Callbacks.
4291 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004292 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004293 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004294 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004295 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004296 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004297 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004298 }
Winson Chungd64d1762013-08-20 14:37:16 -07004299 };
4300 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004301 return;
4302 }
4303
Sunny Goyal1a745e82014-10-02 15:58:31 -07004304 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004305 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4306 for (AppInfo info : appInfos) {
4307 removedComponents.add(info.componentName);
4308 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004309 if (!packageNames.isEmpty()) {
4310 mWorkspace.removeItemsByPackageName(packageNames, user);
4311 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004312 if (!removedComponents.isEmpty()) {
4313 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004314 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004315 // Notify the drag controller
4316 mDragController.onAppsRemoved(packageNames, removedComponents);
4317
Sunny Goyal1a745e82014-10-02 15:58:31 -07004318 } else {
4319 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004320 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004321
Winson Chungdf95eb12013-10-16 14:57:07 -07004322 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004323 if (mAppsView != null) {
4324 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004325 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004326 }
Joe Onoratobe386092009-11-17 17:32:16 -08004327
Adam Cohen091440a2015-03-18 14:16:05 -07004328 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004329 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004330 public void run() {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004331 bindAllPackages(mWidgetsAndShortcuts);
Winson Chung83892cc2013-05-01 16:53:33 -07004332 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004333 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004334
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004335 @Override
4336 public void bindAllPackages(final ArrayList<Object> widgetsAndShortcuts) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004337 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4338 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004339 return;
4340 }
4341
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004342 if (mWidgetsView != null && widgetsAndShortcuts != null) {
4343 mWidgetsView.addWidgets(widgetsAndShortcuts, getPackageManager());
4344 mWidgetsAndShortcuts = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004345 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004346 }
4347
Winson Chung400438b2011-01-16 17:53:48 -08004348 private int mapConfigurationOriActivityInfoOri(int configOri) {
4349 final Display d = getWindowManager().getDefaultDisplay();
4350 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4351 switch (d.getRotation()) {
4352 case Surface.ROTATION_0:
4353 case Surface.ROTATION_180:
4354 // We are currently in the same basic orientation as the natural orientation
4355 naturalOri = configOri;
4356 break;
4357 case Surface.ROTATION_90:
4358 case Surface.ROTATION_270:
4359 // We are currently in the other basic orientation to the natural orientation
4360 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4361 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4362 break;
4363 }
4364
4365 int[] oriMap = {
4366 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4367 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4368 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4369 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4370 };
4371 // Since the map starts at portrait, we need to offset if this device's natural orientation
4372 // is landscape.
4373 int indexOffset = 0;
4374 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4375 indexOffset = 1;
4376 }
4377 return oriMap[(d.getRotation() + indexOffset) % 4];
4378 }
Adam Cohen446e9402011-09-15 18:21:21 -07004379
Winson Chung4b919f82012-05-01 10:44:08 -07004380 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004381 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004382 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4383 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4384 .getConfiguration().orientation));
4385 } else {
4386 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4387 }
Winson Chung4b919f82012-05-01 10:44:08 -07004388 }
4389 }
4390 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004391 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004392 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004393 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004394 } else {
4395 mHandler.postDelayed(new Runnable() {
4396 public void run() {
4397 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4398 }
4399 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004400 }
Winson Chung4b919f82012-05-01 10:44:08 -07004401 }
Winson Chung400438b2011-01-16 17:53:48 -08004402 }
4403
Winson Chunge43a1e72014-01-15 10:33:02 -08004404 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004405 if (mLauncherCallbacks != null) {
4406 return mLauncherCallbacks.isLauncherPreinstalled();
4407 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004408 PackageManager pm = getPackageManager();
4409 try {
4410 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4411 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4412 return true;
4413 } else {
4414 return false;
4415 }
4416 } catch (NameNotFoundException e) {
4417 e.printStackTrace();
4418 return false;
4419 }
4420 }
4421
Adam Cohenea90f832014-05-21 15:03:34 -07004422 /**
4423 * This method indicates whether or not we should suggest default wallpaper dimensions
4424 * when our wallpaper cropper was not yet used to set a wallpaper.
4425 */
4426 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004427 if (mLauncherCallbacks != null) {
4428 return mLauncherCallbacks.overrideWallpaperDimensions();
4429 }
Adam Cohenea90f832014-05-21 15:03:34 -07004430 return true;
4431 }
4432
Adam Cohen432609a2014-03-13 17:03:22 -07004433 /**
4434 * To be overridden by subclasses to indicate that there is an activity to launch
4435 * before showing the standard launcher experience.
4436 */
4437 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004438 if (mLauncherCallbacks != null) {
4439 return mLauncherCallbacks.hasFirstRunActivity();
4440 }
Adam Cohen432609a2014-03-13 17:03:22 -07004441 return false;
4442 }
4443
4444 /**
4445 * To be overridden by subclasses to launch any first run activity
4446 */
4447 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004448 if (mLauncherCallbacks != null) {
4449 return mLauncherCallbacks.getFirstRunActivity();
4450 }
Adam Cohen432609a2014-03-13 17:03:22 -07004451 return null;
4452 }
4453
Winson Chung2826a402015-04-17 16:18:53 -07004454 /**
4455 * Returns whether the launcher callbacks overrides search in all apps.
4456 * @return
4457 */
4458 @Thunk boolean isAllAppsSearchOverridden() {
4459 if (mLauncherCallbacks != null) {
4460 return mLauncherCallbacks.overrideAllAppsSearch();
4461 }
4462 return false;
4463 }
4464
Winson Chunga6945242014-01-08 14:04:34 -08004465 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004466 return !ActivityManager.isRunningInTestHarness() &&
4467 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004468 }
4469
Adam Cohen4a9423d2014-03-28 14:22:31 -07004470 protected boolean hasRunFirstRunActivity() {
4471 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4472 }
4473
Adam Cohen432609a2014-03-13 17:03:22 -07004474 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004475 if (shouldRunFirstRunActivity() &&
4476 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004477 Intent firstRunIntent = getFirstRunActivity();
4478 if (firstRunIntent != null) {
4479 startActivity(firstRunIntent);
4480 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004481 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004482 }
4483 }
Adam Cohen432609a2014-03-13 17:03:22 -07004484 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004485 }
4486
4487 private void markFirstRunActivityShown() {
4488 SharedPreferences.Editor editor = mSharedPrefs.edit();
4489 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4490 editor.apply();
4491 }
4492
Adam Cohen432609a2014-03-13 17:03:22 -07004493 /**
4494 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4495 * screen that must be displayed and dismissed.
4496 */
4497 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004498 if (mLauncherCallbacks != null) {
4499 return mLauncherCallbacks.hasDismissableIntroScreen();
4500 }
Adam Cohen432609a2014-03-13 17:03:22 -07004501 return false;
4502 }
4503
4504 /**
4505 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4506 */
4507 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004508 if (mLauncherCallbacks != null) {
4509 return mLauncherCallbacks.getIntroScreen();
4510 }
Adam Cohen432609a2014-03-13 17:03:22 -07004511 return null;
4512 }
4513
4514 /**
4515 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4516 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4517 */
4518 private boolean shouldShowIntroScreen() {
4519 return hasDismissableIntroScreen() &&
4520 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4521 }
4522
4523 protected void showIntroScreen() {
4524 View introScreen = getIntroScreen();
4525 changeWallpaperVisiblity(false);
4526 if (introScreen != null) {
4527 mDragLayer.showOverlayView(introScreen);
4528 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004529 if (mLauncherOverlayContainer != null) {
4530 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4531 }
Adam Cohen432609a2014-03-13 17:03:22 -07004532 }
4533
4534 public void dismissIntroScreen() {
4535 markIntroScreenDismissed();
4536 if (showFirstRunActivity()) {
4537 // We delay hiding the intro view until the first run activity is showing. This
4538 // avoids a blip.
4539 mWorkspace.postDelayed(new Runnable() {
4540 @Override
4541 public void run() {
4542 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004543 if (mLauncherOverlayContainer != null) {
4544 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4545 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004546 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004547 }
4548 }, ACTIVITY_START_DELAY);
4549 } else {
4550 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004551 if (mLauncherOverlayContainer != null) {
4552 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4553 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004554 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004555 }
4556 changeWallpaperVisiblity(true);
4557 }
4558
4559 private void markIntroScreenDismissed() {
4560 SharedPreferences.Editor editor = mSharedPrefs.edit();
4561 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4562 editor.apply();
4563 }
4564
Adam Cohen091440a2015-03-18 14:16:05 -07004565 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004566 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4567 // on the device, then we always show the first run cling experience (or if there is no
4568 // launcher2). Otherwise, we prompt the user upon started for migration
4569 LauncherClings launcherClings = new LauncherClings(this);
4570 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4571 if (mModel.canMigrateFromOldLauncherDb(this)) {
4572 launcherClings.showMigrationCling();
4573 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004574 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004575 }
4576 }
4577 }
4578
Winson Chunga6945242014-01-08 14:04:34 -08004579 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004580 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4581 if (mHotseat != null) mHotseat.setAlpha(1f);
4582 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4583 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004584 }
4585
4586 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004587 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4588 if (mHotseat != null) mHotseat.setAlpha(0f);
4589 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4590 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004591 }
4592
Mathew Inwood72fbec12013-11-19 15:45:07 +00004593 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004594 // Called from search suggestion, not supported in other profiles.
4595 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4596 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4597 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4598 myUser);
4599 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004600 return null;
4601 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004602 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004603 }
4604
4605 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4606 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004607 // Called from search suggestion, not supported in other profiles.
4608 return createShortcutDragInfo(shortcutIntent, caption, icon,
4609 UserHandleCompat.myUserHandle());
4610 }
4611
4612 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4613 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004614 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004615 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004616 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004617 }
4618
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004619 protected void moveWorkspaceToDefaultScreen() {
4620 mWorkspace.moveToDefaultScreen(false);
4621 }
4622
Mathew Inwood72fbec12013-11-19 15:45:07 +00004623 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4624 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004625 mWorkspace.onExternalDragStartedWithItem(dragView);
4626 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004627 }
4628
Anjali Koppalf5d29132014-02-28 13:33:27 -08004629 @Override
4630 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004631 if (mLauncherCallbacks != null) {
4632 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4633 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004634 }
4635
Winson Chung80baf5a2010-08-09 16:03:15 -07004636 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004637 * Prints out out state for debugging.
4638 */
4639 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004640 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004641 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004642 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4643 Log.d(TAG, "mRestoring=" + mRestoring);
4644 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4645 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004646 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004647 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004648 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004649
Daniel Sandler325dc232013-06-05 22:57:57 -04004650 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004651 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004652
4653 @Override
4654 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4655 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004656 synchronized (sDumpLogs) {
4657 writer.println(" ");
4658 writer.println("Debug logs: ");
4659 for (int i = 0; i < sDumpLogs.size(); i++) {
4660 writer.println(" " + sDumpLogs.get(i));
4661 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004662 }
Adam Cohen9211d422014-10-07 18:14:53 -07004663 if (mLauncherCallbacks != null) {
4664 mLauncherCallbacks.dump(prefix, fd, writer, args);
4665 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004666 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004667
4668 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004669 if (DEBUG_DUMP_LOG) {
4670 synchronized (sDumpLogs) {
4671 Log.d(TAG, "");
4672 Log.d(TAG, "*********************");
4673 Log.d(TAG, "Launcher debug logs: ");
4674 for (int i = 0; i < sDumpLogs.size(); i++) {
4675 Log.d(TAG, " " + sDumpLogs.get(i));
4676 }
4677 Log.d(TAG, "*********************");
4678 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004679 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004680 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004681 }
4682
4683 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004684 addDumpLog(tag, log, null, debugLog);
4685 }
4686
4687 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004688 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004689 if (e != null) {
4690 Log.d(tag, log, e);
4691 } else {
4692 Log.d(tag, log);
4693 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004694 }
Winson Chungede41292013-09-19 16:27:36 -07004695 if (DEBUG_DUMP_LOG) {
4696 sDateStamp.setTime(System.currentTimeMillis());
4697 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004698 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4699 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004700 }
Winson Chungede41292013-09-19 16:27:36 -07004701 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004702 }
4703
Adam Cohen59400422014-03-05 18:07:04 -08004704 public static CustomAppWidget getCustomAppWidget(String name) {
4705 return sCustomAppWidgets.get(name);
4706 }
4707
4708 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4709 return sCustomAppWidgets;
4710 }
4711
Winson Chungede41292013-09-19 16:27:36 -07004712 public void dumpLogsToLocalData() {
4713 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004714 new AsyncTask<Void, Void, Void>() {
4715 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004716 boolean success = false;
4717 sDateStamp.setTime(sRunStart);
4718 String FILENAME = sDateStamp.getMonth() + "-"
4719 + sDateStamp.getDay() + "_"
4720 + sDateStamp.getHours() + "-"
4721 + sDateStamp.getMinutes() + "_"
4722 + sDateStamp.getSeconds() + ".txt";
4723
4724 FileOutputStream fos = null;
4725 File outFile = null;
4726 try {
4727 outFile = new File(getFilesDir(), FILENAME);
4728 outFile.createNewFile();
4729 fos = new FileOutputStream(outFile);
4730 } catch (Exception e) {
4731 e.printStackTrace();
4732 }
4733 if (fos != null) {
4734 PrintWriter writer = new PrintWriter(fos);
4735
4736 writer.println(" ");
4737 writer.println("Debug logs: ");
4738 synchronized (sDumpLogs) {
4739 for (int i = 0; i < sDumpLogs.size(); i++) {
4740 writer.println(" " + sDumpLogs.get(i));
4741 }
4742 }
4743 writer.close();
4744 }
4745 try {
4746 if (fos != null) {
4747 fos.close();
4748 success = true;
4749 }
4750 } catch (IOException e) {
4751 e.printStackTrace();
4752 }
Michael Jurka43467462013-11-14 12:03:58 +01004753 return null;
Winson Chungede41292013-09-19 16:27:36 -07004754 }
Michael Jurka43467462013-11-14 12:03:58 +01004755 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004756 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004758}
Michael Jurka2763be32011-02-24 11:19:57 -08004759
Michael Jurkaabded662011-03-04 12:06:57 -08004760interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004761 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004762 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004763 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004764 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004765 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004766}
Dan Sandlerd5024042014-01-09 15:01:33 -05004767
4768interface DebugIntents {
4769 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4770 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004771}