blob: 7364a9f20fd58378b83c0368168d22a071b89a3f [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;
Sunny Goyale755d462014-07-22 13:48:29 -0700101import com.android.launcher3.compat.PackageInstallerCompat;
102import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen091440a2015-03-18 14:16:05 -0700105import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700106import com.android.launcher3.widget.PendingAddWidgetInfo;
107import 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 Sandler6053b8002013-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
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700307 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
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) {
525 mAppsView.setFixedBounds(Launcher.this, bounds);
526 }
527
528 @Override
529 public void dismissAllApps() {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700530 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true, null,
531 false /* notifyLauncherCallbacks */);
Winson Chung0f785722015-04-08 10:27:49 -0700532 }
533 });
Adam Cohen9211d422014-10-07 18:14:53 -0700534 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700535 }
536
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700537 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700538 public void onLauncherProviderChange() {
539 if (mLauncherCallbacks != null) {
540 mLauncherCallbacks.onLauncherProviderChange();
541 }
542 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700543
Adam Cohen9211d422014-10-07 18:14:53 -0700544 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700545 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700546 if (mLauncherCallbacks != null) {
547 return mLauncherCallbacks.hasCustomContentToLeft();
548 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700549 return false;
550 }
551
Dave Hawkeya8881582013-09-17 15:55:33 -0600552 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500553 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600554 * {@link #addToCustomContentPage}. This will only be invoked if
555 * {@link #hasCustomContentToLeft()} is {@code true}.
556 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500557 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700558 if (mLauncherCallbacks != null) {
559 mLauncherCallbacks.populateCustomContentContainer();
560 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600561 }
562
563 /**
564 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
565 * ensure the custom content page is added or removed if necessary.
566 */
567 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600568 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600569 // Not bound yet, wait for bindScreens to be called.
570 return;
571 }
572
573 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
574 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500575 mWorkspace.createCustomContentContainer();
576 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600577 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
578 mWorkspace.removeCustomContentPage();
579 }
580 }
581
Adam Cohen091440a2015-03-18 14:16:05 -0700582 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700583 if (sLocaleConfiguration == null) {
584 new AsyncTask<Void, Void, LocaleConfiguration>() {
585 @Override
586 protected LocaleConfiguration doInBackground(Void... unused) {
587 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
588 readConfiguration(Launcher.this, localeConfiguration);
589 return localeConfiguration;
590 }
591
592 @Override
593 protected void onPostExecute(LocaleConfiguration result) {
594 sLocaleConfiguration = result;
595 checkForLocaleChange(); // recursive, but now with a locale configuration
596 }
597 }.execute();
598 return;
599 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700600
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 final Configuration configuration = getResources().getConfiguration();
602
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700603 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 final String locale = configuration.locale.toString();
605
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700606 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 final int mcc = configuration.mcc;
608
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700609 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 final int mnc = configuration.mnc;
611
Romain Guy84f296c2009-11-04 15:00:44 -0800612 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613
Romain Guy84f296c2009-11-04 15:00:44 -0800614 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700615 sLocaleConfiguration.locale = locale;
616 sLocaleConfiguration.mcc = mcc;
617 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700618
Joe Onorato0589f0f2010-02-08 13:44:00 -0800619 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700620
621 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100622 new AsyncTask<Void, Void, Void>() {
623 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100625 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700626 }
Michael Jurka43467462013-11-14 12:03:58 +0100627 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700628 }
629 }
630
Adam Cohen091440a2015-03-18 14:16:05 -0700631 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700632 public String locale;
633 public int mcc = -1;
634 public int mnc = -1;
635 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700636
Adam Cohen091440a2015-03-18 14:16:05 -0700637 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700638 DataInputStream in = null;
639 try {
Helena Josol28db2802014-10-09 17:04:09 +0100640 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700641 configuration.locale = in.readUTF();
642 configuration.mcc = in.readInt();
643 configuration.mnc = in.readInt();
644 } catch (FileNotFoundException e) {
645 // Ignore
646 } catch (IOException e) {
647 // Ignore
648 } finally {
649 if (in != null) {
650 try {
651 in.close();
652 } catch (IOException e) {
653 // Ignore
654 }
655 }
656 }
657 }
658
Adam Cohen091440a2015-03-18 14:16:05 -0700659 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700660 DataOutputStream out = null;
661 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100662 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100663 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700664 out.writeUTF(configuration.locale);
665 out.writeInt(configuration.mcc);
666 out.writeInt(configuration.mnc);
667 out.flush();
668 } catch (FileNotFoundException e) {
669 // Ignore
670 } catch (IOException e) {
671 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100672 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700673 } finally {
674 if (out != null) {
675 try {
676 out.close();
677 } catch (IOException e) {
678 // Ignore
679 }
680 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
682 }
683
Anton Hanssona2f665f2013-09-26 12:18:32 +0100684 public Stats getStats() {
685 return mStats;
686 }
687
Bjorn Bringertc459e522013-06-07 19:36:01 +0100688 public LayoutInflater getInflater() {
689 return mInflater;
690 }
691
Hyunyoung Song3f471442015-04-08 19:01:34 -0700692 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700693 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
694 // that is subsequently removed from the workspace in startBinding().
695 return !mModel.isLoadingWorkspace();
696 }
697
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800698 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000699 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100700 if (Build.VERSION.SDK_INT >= 17) {
701 return View.generateViewId();
702 } else {
703 // View.generateViewId() is not available. The following fallback logic is a copy
704 // of its implementation.
705 for (;;) {
706 final int result = sNextGeneratedId.get();
707 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
708 int newValue = result + 1;
709 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
710 if (sNextGeneratedId.compareAndSet(result, newValue)) {
711 return result;
712 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000713 }
714 }
715 }
716
717 public int getViewIdForItem(ItemInfo info) {
718 // This cast is safe given the > 2B range for int.
719 int itemId = (int) info.id;
720 if (mItemIdToViewId.containsKey(itemId)) {
721 return mItemIdToViewId.get(itemId);
722 }
723 int viewId = generateViewId();
724 mItemIdToViewId.put(itemId, viewId);
725 return viewId;
726 }
727
728 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700729 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
730 * a configuration step, this allows the proper animations to run after other transitions.
731 */
Adam Cohendb364c32014-05-20 14:23:40 -0700732 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700733 long screenId = args.screenId;
734 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
735 // When the screen id represents an actual screen (as opposed to a rank) we make sure
736 // that the drop page actually exists.
737 screenId = ensurePendingDropLayoutExists(args.screenId);
738 }
Adam Cohendb364c32014-05-20 14:23:40 -0700739
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700742 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700743 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700744 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800745 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700746 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700747 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700748 case REQUEST_RECONFIGURE_APPWIDGET:
749 completeRestoreAppWidget(args.appWidgetId);
750 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800751 }
Michael Jurka27614d22012-04-02 06:40:22 -0700752 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
753 // if you turned the screen off and then back while in All Apps, Launcher would not
754 // return to the workspace. Clearing mAddInfo.container here fixes this issue
755 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700756 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800757 }
758
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800759 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700760 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700761 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700762 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700763 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800764 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700765
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 Runnable exitSpringLoaded = new Runnable() {
767 @Override
768 public void run() {
769 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
770 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
771 }
772 };
773
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700776 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
777 if (resultCode == RESULT_CANCELED) {
778 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700779 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800782 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700784 }
785 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200786 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
787 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700788 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200789 }
790 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200792
Adam Cohened66b2b2012-01-23 17:28:51 -0800793 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
794 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700795
Adam Cohendb364c32014-05-20 14:23:40 -0700796 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800797 // We have special handling for widgets
798 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800799 final int appWidgetId;
800 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
801 : -1;
802 if (widgetId < 0) {
803 appWidgetId = pendingAddWidgetId;
804 } else {
805 appWidgetId = widgetId;
806 }
807
Adam Cohenad4e15c2013-10-17 16:21:35 -0700808 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800809 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700810 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
811 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700812 result = RESULT_CANCELED;
813 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700814 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700815 @Override
816 public void run() {
817 exitSpringLoadedDragModeDelayed(false, 0, null);
818 }
819 };
Adam Cohendb364c32014-05-20 14:23:40 -0700820 if (workspaceLocked) {
821 // No need to remove the empty screen if we're mid-binding, as the
822 // the bind will not add the empty screen.
823 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
824 } else {
825 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
826 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
827 }
Winson Chung5aaab772012-04-27 15:24:02 -0700828 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700829 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700830 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
831 // When the screen id represents an actual screen (as opposed to a rank)
832 // we make sure that the drop page actually exists.
833 mPendingAddInfo.screenId =
834 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
835 }
Adam Cohendb364c32014-05-20 14:23:40 -0700836 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
837
838 dropLayout.setDropPending(true);
839 final Runnable onComplete = new Runnable() {
840 @Override
841 public void run() {
842 completeTwoStageWidgetDrop(resultCode, appWidgetId);
843 dropLayout.setDropPending(false);
844 }
845 };
846 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
847 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
848 } else {
849 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
850 mPendingAddInfo);
851 sPendingAddItem = args;
852 }
Winson Chung5aaab772012-04-27 15:24:02 -0700853 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800854 return;
855 }
856
Sunny Goyalff572272014-07-23 13:58:07 -0700857 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
858 if (resultCode == RESULT_OK) {
859 // Update the widget view.
860 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
861 pendingAddWidgetId, mPendingAddInfo);
862 if (workspaceLocked) {
863 sPendingAddItem = args;
864 } else {
865 completeAdd(args);
866 }
867 }
868 // Leave the widget in the pending state if the user canceled the configure.
869 return;
870 }
871
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 // The pattern used here is that a user PICKs a specific application,
873 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
876 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700877 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700878 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
879 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800880 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700881 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800882 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700883 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700884 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
885 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 }
Adam Cohen00074722013-10-11 17:46:09 -0700887 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700888 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700889 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800892
893 }
894
895 @Override
896 protected void onActivityResult(
897 final int requestCode, final int resultCode, final Intent data) {
898 handleActivityResult(requestCode, resultCode, data);
899 if (mLauncherCallbacks != null) {
900 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
901 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800902 }
903
Adam Cohendb364c32014-05-20 14:23:40 -0700904 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
905 appWidgetId, ItemInfo info) {
906 PendingAddArguments args = new PendingAddArguments();
907 args.requestCode = requestCode;
908 args.intent = data;
909 args.container = info.container;
910 args.screenId = info.screenId;
911 args.cellX = info.cellX;
912 args.cellY = info.cellY;
913 args.appWidgetId = appWidgetId;
914 return args;
915 }
916
917 /**
918 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
919 *
920 * @param screenId the screen id to check
921 * @return the new screen, or screenId if it exists
922 */
923 private long ensurePendingDropLayoutExists(long screenId) {
924 CellLayout dropLayout =
925 (CellLayout) mWorkspace.getScreenWithId(screenId);
926 if (dropLayout == null) {
927 // it's possible that the add screen was removed because it was
928 // empty and a re-bind occurred
929 mWorkspace.addExtraEmptyScreen();
930 return mWorkspace.commitExtraEmptyScreen();
931 } else {
932 return screenId;
933 }
934 }
935
Adam Cohen091440a2015-03-18 14:16:05 -0700936 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700937 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700938 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 Runnable onCompleteRunnable = null;
940 int animationType = 0;
941
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700942 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 if (resultCode == RESULT_OK) {
944 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
945 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700946 mPendingAddWidgetInfo);
947 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 onCompleteRunnable = new Runnable() {
949 @Override
950 public void run() {
951 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700952 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700953 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
954 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800955 }
956 };
957 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800958 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800959 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800960 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700961 if (mDragLayer.getAnimatedView() != null) {
962 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
963 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
964 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700965 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700966 // The animated view may be null in the case of a rotation during widget configuration
967 onCompleteRunnable.run();
968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 }
970
971 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700972 protected void onStop() {
973 super.onStop();
974 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700975
976 if (mLauncherCallbacks != null) {
977 mLauncherCallbacks.onStop();
978 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700979 }
980
981 @Override
982 protected void onStart() {
983 super.onStart();
984 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700985
986 if (mLauncherCallbacks != null) {
987 mLauncherCallbacks.onStart();
988 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700989 }
990
991 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200993 long startTime = 0;
994 if (DEBUG_RESUME_TIME) {
995 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400996 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200997 }
Adam Cohen9211d422014-10-07 18:14:53 -0700998
999 if (mLauncherCallbacks != null) {
1000 mLauncherCallbacks.preOnResume();
1001 }
1002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001004
Winson Chung4a2afa32012-07-19 14:53:05 -07001005 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001006 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001007 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001008 } else if (mOnResumeState == State.APPS) {
1009 showAppsView(false /* animated */, false /* resetListToTop */);
1010 } else if (mOnResumeState == State.WIDGETS) {
1011 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001012 }
1013 mOnResumeState = State.NONE;
1014
Craig Mautner360310b2012-10-26 15:13:08 -07001015 // Background was set to gradient in onPause(), restore to black if in all apps.
1016 setWorkspaceBackground(mState == State.WORKSPACE);
1017
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001018 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001019 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001020 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001021 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001023 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001025 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1027 // execute them here
1028 long startTimeCallbacks = 0;
1029 if (DEBUG_RESUME_TIME) {
1030 startTimeCallbacks = System.currentTimeMillis();
1031 }
1032
Michael Jurka1e2f4652013-07-08 18:03:46 -07001033 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1034 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001035 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001036 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 if (DEBUG_RESUME_TIME) {
1038 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1039 (System.currentTimeMillis() - startTimeCallbacks));
1040 }
Michael Jurka447bf842013-05-15 14:52:15 +02001041 }
Michael Jurka54554252013-08-01 12:52:23 +02001042 if (mOnResumeCallbacks.size() > 0) {
1043 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1044 mOnResumeCallbacks.get(i).run();
1045 }
1046 mOnResumeCallbacks.clear();
1047 }
Winson Chunge4e50662012-01-23 14:45:13 -08001048
1049 // Reset the pressed state of icons that were locked in the press state while activities
1050 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001051 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001052 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001053 mWaitingForResume.setStayPressed(false);
1054 }
Winson Chung82963d52013-10-09 11:20:57 -07001055
Adam Cohen06dff352012-06-01 17:17:08 -07001056 // It is possible that widgets can receive updates while launcher is not in the foreground.
1057 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1058 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1059 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001060 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001061 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001062
Winson Chung780fe592013-09-26 14:48:44 -07001063 // Process any items that were added while Launcher was away.
1064 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1065
Michael Jurka447bf842013-05-15 14:52:15 +02001066 if (DEBUG_RESUME_TIME) {
1067 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1068 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001069
1070 if (mWorkspace.getCustomContentCallbacks() != null) {
1071 // If we are resuming and the custom content is the current page, we call onShow().
1072 // It is also poassible that onShow will instead be called slightly after first layout
1073 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1074 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001075 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001076 }
1077 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001078 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001079 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001080
1081 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001082
1083 if (mLauncherCallbacks != null) {
1084 mLauncherCallbacks.onResume();
1085 }
Adam Cohen06dff352012-06-01 17:17:08 -07001086 }
1087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001089 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001090 // Ensure that items added to Launcher are queued until Launcher returns
1091 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001092 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001093
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001095 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001096 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001097 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001098
1099 // We call onHide() aggressively. The custom content callbacks should be able to
1100 // debounce excess onHide calls.
1101 if (mWorkspace.getCustomContentCallbacks() != null) {
1102 mWorkspace.getCustomContentCallbacks().onHide();
1103 }
Romain Guycbb89e42009-06-08 15:52:54 -07001104
Adam Cohen9211d422014-10-07 18:14:53 -07001105 if (mLauncherCallbacks != null) {
1106 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001107 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001108 }
1109
1110 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001111 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1112 // by a onResume or by scrolling otherwise.
1113 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001114
1115 // Custom content is completely hidden
1116 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001117
1118 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1119 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001120
1121 // Indicates whether the user is allowed to scroll away from the custom content.
1122 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001123 }
1124
Adam Cohenc2d6e892014-10-16 09:49:24 -07001125 public interface LauncherOverlay {
1126
1127 /**
1128 * Touch interaction leading to overscroll has begun
1129 */
1130 public void onScrollInteractionBegin();
1131
1132 /**
1133 * Touch interaction related to overscroll has ended
1134 */
1135 public void onScrollInteractionEnd();
1136
1137 /**
1138 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1139 * screen (or in the case of RTL, the rightmost screen).
1140 */
1141 public void onScrollChange(int progress, boolean rtl);
1142
1143 /**
1144 * Screen has stopped scrolling
1145 */
1146 public void onScrollSettled();
1147
1148 /**
1149 * This method can be called by the Launcher in order to force the LauncherOverlay
1150 * to exit fully immersive mode.
1151 */
1152 public void forceExitFullImmersion();
1153 }
1154
Winson Chung0f785722015-04-08 10:27:49 -07001155 public interface LauncherAppsCallbacks {
1156 /**
1157 * Updates launcher to the available space that AllApps can take so as not to overlap with
1158 * any other views.
1159 */
1160 public void onAllAppsBoundsChanged(Rect bounds);
1161
1162 /**
1163 * Called to dismiss all apps if it is showing.
1164 */
1165 public void dismissAllApps();
1166 }
1167
Adam Cohenc2d6e892014-10-16 09:49:24 -07001168 public interface LauncherOverlayCallbacks {
1169 /**
1170 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1171 * however it doesn't modify any state within the launcher.
1172 */
1173 public boolean canEnterFullImmersion();
1174
1175 /**
1176 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1177 * eg. by occupying the full screen and handling all touch events.
1178 *
1179 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1180 * case, Launcher will modify any necessary state and assumes the overlay is
1181 * handling all interaction. If false, the LauncherOverlay should cancel any
1182 *
1183 */
1184 public boolean enterFullImmersion();
1185
1186 /**
1187 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1188 * full control over UI and state.
1189 */
1190 public void exitFullImmersion();
1191 }
1192
1193 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1194
1195 @Override
1196 public boolean canEnterFullImmersion() {
1197 return mState == State.WORKSPACE;
1198 }
1199
1200 @Override
1201 public boolean enterFullImmersion() {
1202 if (mState == State.WORKSPACE) {
1203 // When fully immersed, disregard any touches which fall through.
1204 mDragLayer.setBlockTouch(true);
1205 return true;
1206 }
1207 return false;
1208 }
1209
1210 @Override
1211 public void exitFullImmersion() {
1212 mDragLayer.setBlockTouch(false);
1213 }
1214 }
1215
Jorim Jaggid017f882014-01-14 17:08:48 -08001216 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001217 if (mLauncherCallbacks != null) {
1218 return mLauncherCallbacks.hasSettings();
1219 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001220 return false;
1221 }
1222
Adam Cohen9211d422014-10-07 18:14:53 -07001223 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001224 CustomContentCallbacks callbacks, String description) {
1225 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001226 }
1227
Adam Cohenedb40762013-07-18 16:45:45 -07001228 // The custom content needs to offset its content to account for the QSB
1229 public int getTopOffsetForCustomContent() {
1230 return mWorkspace.getPaddingTop();
1231 }
1232
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001233 @Override
1234 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001235 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001236 if (mModel.isCurrentCallbacks(this)) {
1237 mModel.stopLoader();
1238 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001239 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1240
Romain Guy13c2e7b2010-03-10 19:45:00 -08001241 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001242 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001243
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001244 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001245 @Override
1246 public void onWindowFocusChanged(boolean hasFocus) {
1247 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001248 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001249
1250 if (mLauncherCallbacks != null) {
1251 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1252 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001253 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 private boolean acceptFilter() {
1256 final InputMethodManager inputManager = (InputMethodManager)
1257 getSystemService(Context.INPUT_METHOD_SERVICE);
1258 return !inputManager.isFullscreenMode();
1259 }
1260
1261 @Override
1262 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001263 final int uniChar = event.getUnicodeChar();
1264 final boolean handled = super.onKeyDown(keyCode, event);
1265 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1266 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1268 keyCode, event);
1269 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001270 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001271 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001272 // showSearchDialog()
1273 // If there are multiple keystrokes before the search dialog takes focus,
1274 // onSearchRequested() will be called for every keystroke,
1275 // but it is idempotent, so it's fine.
1276 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
1278 }
1279
Joe Onorato8a9625e2010-01-28 15:55:35 -08001280 // Eat the long press event so the keyboard doesn't come up.
1281 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1282 return true;
1283 }
1284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 return handled;
1286 }
1287
Karl Rosaen138a0412009-04-23 19:00:21 -07001288 private String getTypedText() {
1289 return mDefaultKeySsb.toString();
1290 }
1291
1292 private void clearTypedText() {
1293 mDefaultKeySsb.clear();
1294 mDefaultKeySsb.clearSpans();
1295 Selection.setSelection(mDefaultKeySsb, 0);
1296 }
1297
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001299 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1300 * State
1301 */
1302 private static State intToState(int stateOrdinal) {
1303 State state = State.WORKSPACE;
1304 final State[] stateValues = State.values();
1305 for (int i = 0; i < stateValues.length; i++) {
1306 if (stateValues[i].ordinal() == stateOrdinal) {
1307 state = stateValues[i];
1308 break;
1309 }
1310 }
1311 return state;
1312 }
1313
1314 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 * Restores the previous state, if it exists.
1316 *
1317 * @param savedState The previous state.
1318 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001319 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 private void restoreState(Bundle savedState) {
1321 if (savedState == null) {
1322 return;
1323 }
1324
Michael Jurka883f55b2010-10-21 15:47:14 -07001325 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001326 if (state == State.APPS || state == State.WIDGETS) {
1327 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001328 }
1329
Adam Cohen21cd0022013-10-09 18:57:02 -07001330 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1331 PagedView.INVALID_RESTORE_PAGE);
1332 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001333 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335
Winson Chung3d503fb2011-07-13 17:25:49 -07001336 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001337 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001338
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1340 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001341 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1343 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001344 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1345 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001346 AppWidgetProviderInfo info = savedState.getParcelable(
1347 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1348 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001349 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001350 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 mRestoring = true;
1352 }
1353
1354 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1355 if (renameFolder) {
1356 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001357 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 mRestoring = true;
1359 }
Winson Chunga12a2502010-12-20 14:41:35 -08001360
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001361 mItemIdToViewId = (HashMap<Integer, Integer>)
1362 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
1364
1365 /**
1366 * Finds all the views we need and configure them properly.
1367 */
1368 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001369 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001370
Craig Mautner360310b2012-10-26 15:13:08 -07001371 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001372 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001373 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1374 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001375 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001376 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001377
John Spurlock77e1f472013-09-11 10:09:51 -04001378 mLauncherView.setSystemUiVisibility(
1379 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001380 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381
Winson Chung4c98d922011-05-31 16:50:48 -07001382 // Setup the drag layer
1383 mDragLayer.setup(this, dragController);
1384
Winson Chung3d503fb2011-07-13 17:25:49 -07001385 // Setup the hotseat
1386 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1387 if (mHotseat != null) {
1388 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001389 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 }
1391
Jorim Jaggid017f882014-01-14 17:08:48 -08001392 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001393 View widgetButton = findViewById(R.id.widget_button);
1394 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001395 @Override
1396 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001397 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001398 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001399 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001400 }
1401 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001402 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1403
1404 View wallpaperButton = findViewById(R.id.wallpaper_button);
1405 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001406 @Override
1407 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001408 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001409 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001410 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001411 }
1412 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001413 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1414
1415 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001416 if (hasSettings()) {
1417 settingsButton.setOnClickListener(new OnClickListener() {
1418 @Override
1419 public void onClick(View arg0) {
1420 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001421 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001422 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001423 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001424 });
1425 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1426 } else {
1427 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001428 }
1429
Adam Cohendbdff6b2013-09-18 19:09:15 -07001430 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001431
Winson Chung4c98d922011-05-31 16:50:48 -07001432 // Setup the workspace
1433 mWorkspace.setHapticFeedbackEnabled(false);
1434 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001435 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001436 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001437
Winson Chungf0ea4d32011-06-06 14:27:16 -07001438 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001439 mSearchDropTargetBar = (SearchDropTargetBar)
1440 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001441
Winson Chungb745afb2015-03-02 11:51:23 -08001442 // Setup Apps
1443 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung0f785722015-04-08 10:27:49 -07001444 if (mLauncherCallbacks != null && mLauncherCallbacks.overrideAllAppsSearch()) {
1445 mAppsView.hideSearchBar();
1446 }
Winson Chungb745afb2015-03-02 11:51:23 -08001447
Winson Chungf0ea4d32011-06-06 14:27:16 -07001448 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001449 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001450
Winson Chung3d503fb2011-07-13 17:25:49 -07001451 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001452 dragController.setDragScoller(mWorkspace);
1453 dragController.setScrollView(mDragLayer);
1454 dragController.setMoveTarget(mWorkspace);
1455 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001456 if (mSearchDropTargetBar != null) {
1457 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001458 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001459 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001460
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001461 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001462 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001463 mWeightWatcher = new WeightWatcher(this);
1464 mWeightWatcher.setAlpha(0.5f);
1465 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001466 new FrameLayout.LayoutParams(
1467 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001468 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001469 Gravity.BOTTOM)
1470 );
Adam Cohen39a06042013-07-19 14:30:12 -07001471
1472 boolean show = shouldShowWeightWatcher();
1473 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001474 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
1476
1477 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001478 * Sets the all apps button. This method is called from {@link Hotseat}.
1479 */
1480 public void setAllAppsButton(View allAppsButton) {
1481 mAllAppsButton = allAppsButton;
1482 }
1483
1484 public View getAllAppsButton() {
1485 return mAllAppsButton;
1486 }
1487
1488 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 * Creates a view representing a shortcut.
1490 *
1491 * @param info The data structure describing the shortcut.
1492 *
1493 * @return A View inflated from R.layout.application.
1494 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001495 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001497 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
1499
1500 /**
1501 * Creates a view representing a shortcut inflated from the specified resource.
1502 *
1503 * @param layoutResId The id of the XML layout used to create the shortcut.
1504 * @param parent The group the shortcut belongs to.
1505 * @param info The data structure describing the shortcut.
1506 *
1507 * @return A View inflated from layoutResId.
1508 */
Adam Cohen59400422014-03-05 18:07:04 -08001509 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001510 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001511 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001513 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 return favorite;
1515 }
1516
1517 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 * Add a shortcut to the workspace.
1519 *
1520 * @param data The intent describing the shortcut.
1521 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001523 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001524 int cellY) {
1525 int[] cellXY = mTmpAddItemCellCoordinates;
1526 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001527 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001528
Michael Jurkad3ef3062010-11-23 16:23:58 -08001529 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001530
Sunny Goyal2350bc92014-10-14 16:42:54 -07001531 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001532 if (info == null) {
1533 return;
1534 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001535 final View view = createShortcut(info);
1536
Adam Cohenfbba09b2011-07-18 21:43:05 -07001537 // First we check if we already know the exact location where we want to add this item.
1538 if (cellX >= 0 && cellY >= 0) {
1539 cellXY[0] = cellX;
1540 cellXY[1] = cellY;
1541 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001542
1543 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001544 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001545 true, null,null)) {
1546 return;
1547 }
1548 DragObject dragObject = new DragObject();
1549 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001550 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1551 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001552 return;
1553 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001554 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001555 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001556 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001557 foundCellSpan = (result != null);
1558 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001559 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001560 }
1561
1562 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001563 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001564 return;
1565 }
1566
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001567 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568
1569 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001570 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001571 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
1573 }
1574
Adam Cohen2f093b62012-04-30 18:59:53 -07001575 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1576 int minHeight) {
1577 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001578 // We want to account for the extra amount of padding that we are adding to the widget
1579 // to ensure that it gets the full amount of space that it has requested
1580 int requiredWidth = minWidth + padding.left + padding.right;
1581 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001582 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001583 }
1584
Adam Cohen2f093b62012-04-30 18:59:53 -07001585 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1586 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001587 }
1588
Adam Cohen2f093b62012-04-30 18:59:53 -07001589 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1590 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001591 }
1592
Adam Cohen2f093b62012-04-30 18:59:53 -07001593 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1594 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001595 }
1596
Adam Cohen2f093b62012-04-30 18:59:53 -07001597 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1598 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001599 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001600 }
1601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001603 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001605 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 */
Adam Cohen091440a2015-03-18 14:16:05 -07001607 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001608 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1609
1610 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001611 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001612 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1613 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001614 }
Romain Guycbb89e42009-06-08 15:52:54 -07001615
Adam Cohen59400422014-03-05 18:07:04 -08001616 if (appWidgetInfo.isCustomWidget) {
1617 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001618 }
1619
Adam Cohen59400422014-03-05 18:07:04 -08001620 LauncherAppWidgetInfo launcherInfo;
1621 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1622 launcherInfo.spanX = info.spanX;
1623 launcherInfo.spanY = info.spanY;
1624 launcherInfo.minSpanX = info.minSpanX;
1625 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001626 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001627
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001629 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630
1631 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001632 if (hostView == null) {
1633 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001634 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1635 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001636 } else {
1637 // The AppWidgetHostView has already been inflated and instantiated
1638 launcherInfo.hostView = hostView;
1639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001641 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001642 launcherInfo.notifyWidgetSizeChanged(this);
1643
Adam Cohen59400422014-03-05 18:07:04 -08001644 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1645 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001646
1647 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001649 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 }
Romain Guycbb89e42009-06-08 15:52:54 -07001651
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1653 @Override
1654 public void onReceive(Context context, Intent intent) {
1655 final String action = intent.getAction();
1656 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1657 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001658 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001659 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001660
Winson Chungc100e8e2011-08-09 16:02:43 -07001661 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001662 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001663 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001664 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001665 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001666 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001667 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1668 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001669 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001670 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1671 mModel.resetLoadedState(false, true);
1672 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1673 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1674 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1675 mModel.resetLoadedState(false, true);
1676 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1677 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1678 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001679 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1680 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1681 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 }
1683 }
1684 };
1685
1686 @Override
1687 public void onAttachedToWindow() {
1688 super.onAttachedToWindow();
1689
1690 // Listen for broadcasts related to user-presence
1691 final IntentFilter filter = new IntentFilter();
1692 filter.addAction(Intent.ACTION_SCREEN_OFF);
1693 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001694 // For handling managed profiles
1695 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1696 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001697 if (ENABLE_DEBUG_INTENTS) {
1698 filter.addAction(DebugIntents.DELETE_DATABASE);
1699 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1700 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001702 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001703 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001704 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 mVisible = true;
1706 }
1707
Adam Cohen0b395352014-06-09 22:54:36 +00001708 /**
1709 * Sets up transparent navigation and status bars in LMP.
1710 * This method is a no-op for other platform versions.
1711 */
Sunny Goyald0091012015-01-20 15:55:34 -08001712 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001713 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001714 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001715 Window window = getWindow();
1716 window.getAttributes().systemUiVisibility |=
1717 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1718 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1719 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1720 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1721 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1722 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1723 window.setStatusBarColor(Color.TRANSPARENT);
1724 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001725 }
1726 }
1727
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 @Override
1729 public void onDetachedFromWindow() {
1730 super.onDetachedFromWindow();
1731 mVisible = false;
1732
Adam Cohend113e0c2010-11-11 10:48:05 -08001733 if (mAttached) {
1734 unregisterReceiver(mReceiver);
1735 mAttached = false;
1736 }
Winson Chungb745afb2015-03-02 11:51:23 -08001737 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001738 }
1739
1740 public void onWindowVisibilityChanged(int visibility) {
1741 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001742 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001743 // The following code used to be in onResume, but it turns out onResume is called when
1744 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1745 // is a more appropriate event to handle
1746 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001747 if (!mWorkspaceLoading) {
1748 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001749 // We want to let Launcher draw itself at least once before we force it to build
1750 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001751 // apps is nice and speedy.
1752 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001753 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001754 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001755 if (mStarted) return;
1756 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001757 // We delay the layer building a bit in order to give
1758 // other message processing a time to run. In particular
1759 // this avoids a delay in hiding the IME if it was
1760 // currently shown, because doing that may involve
1761 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001762 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001763 final ViewTreeObserver.OnDrawListener listener = this;
1764 mWorkspace.post(new Runnable() {
1765 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001766 if (mWorkspace != null &&
1767 mWorkspace.getViewTreeObserver() != null) {
1768 mWorkspace.getViewTreeObserver().
1769 removeOnDrawListener(listener);
1770 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001771 }
1772 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001773 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001774 }
1775 });
1776 }
1777 clearTypedText();
1778 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 }
1780
Adam Cohen091440a2015-03-18 14:16:05 -07001781 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 mHandler.removeMessages(ADVANCE_MSG);
1783 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1784 mHandler.sendMessageDelayed(msg, delay);
1785 mAutoAdvanceSentTime = System.currentTimeMillis();
1786 }
1787
Adam Cohen091440a2015-03-18 14:16:05 -07001788 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001789 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1790 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1791 mAutoAdvanceRunning = autoAdvanceRunning;
1792 if (autoAdvanceRunning) {
1793 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1794 sendAdvanceMessage(delay);
1795 } else {
1796 if (!mWidgetsToAdvance.isEmpty()) {
1797 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1798 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1799 }
1800 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001801 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001802 }
1803 }
1804 }
1805
1806 private final Handler mHandler = new Handler() {
1807 @Override
1808 public void handleMessage(Message msg) {
1809 if (msg.what == ADVANCE_MSG) {
1810 int i = 0;
1811 for (View key: mWidgetsToAdvance.keySet()) {
1812 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1813 final int delay = mAdvanceStagger * i;
1814 if (v instanceof Advanceable) {
1815 postDelayed(new Runnable() {
1816 public void run() {
1817 ((Advanceable) v).advance();
1818 }
1819 }, delay);
1820 }
1821 i++;
1822 }
1823 sendAdvanceMessage(mAdvanceInterval);
1824 }
1825 }
1826 };
1827
1828 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001829 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001830 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1831 if (v instanceof Advanceable) {
1832 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001833 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001834 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001835 }
1836 }
1837
1838 void removeWidgetToAutoAdvance(View hostView) {
1839 if (mWidgetsToAdvance.containsKey(hostView)) {
1840 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001841 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001842 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001843 }
1844
Joe Onorato9c1289c2009-08-17 11:03:03 -04001845 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001846 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847 launcherInfo.hostView = null;
1848 }
1849
Hyunyoung Song3f471442015-04-08 19:01:34 -07001850 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001851 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1852 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001853 }
1854
Winson Chunga6945242014-01-08 14:04:34 -08001855 public DragLayer getDragLayer() {
1856 return mDragLayer;
1857 }
1858
Winson Chungb745afb2015-03-02 11:51:23 -08001859 public AppsContainerView getAppsView() {
1860 return mAppsView;
1861 }
1862
Hyunyoung Song3f471442015-04-08 19:01:34 -07001863 public WidgetsContainerView getWidgetsView() {
1864 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001865 }
1866
Winson Chunga6945242014-01-08 14:04:34 -08001867 public Workspace getWorkspace() {
1868 return mWorkspace;
1869 }
1870
1871 public Hotseat getHotseat() {
1872 return mHotseat;
1873 }
1874
Jorim Jaggid017f882014-01-14 17:08:48 -08001875 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001876 return mOverviewPanel;
1877 }
1878
1879 public SearchDropTargetBar getSearchBar() {
1880 return mSearchDropTargetBar;
1881 }
1882
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001883 public LauncherAppWidgetHost getAppWidgetHost() {
1884 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
Romain Guycbb89e42009-06-08 15:52:54 -07001886
Winson Chunga9abd0e2010-10-27 17:18:37 -07001887 public LauncherModel getModel() {
1888 return mModel;
1889 }
1890
Winson Chunga6945242014-01-08 14:04:34 -08001891 protected SharedPreferences getSharedPrefs() {
1892 return mSharedPrefs;
1893 }
1894
Bjorn Bringertc459e522013-06-07 19:36:01 +01001895 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001896 getWindow().closeAllPanels();
1897
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001898 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001899 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001900 }
1901
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 @Override
1903 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001904 long startTime = 0;
1905 if (DEBUG_RESUME_TIME) {
1906 startTime = System.currentTimeMillis();
1907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 super.onNewIntent(intent);
1909
1910 // Close the menu
1911 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001912 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001913 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001914
Adam Cohened307df2013-10-02 09:37:31 -07001915 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1916 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1917 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001918
Adam Cohen6fecd412013-10-02 17:41:50 -07001919 if (mWorkspace == null) {
1920 // Can be cases where mWorkspace is null, this prevents a NPE
1921 return;
1922 }
1923 Folder openFolder = mWorkspace.getOpenFolder();
1924 // In all these cases, only animate if we're already on home
1925 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001926
1927 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1928 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001929 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001930 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001931 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001932 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001933
Adam Cohen6fecd412013-10-02 17:41:50 -07001934 closeFolder();
1935 exitSpringLoadedDragMode();
1936
1937 // If we are already on home, then just animate back to the workspace,
1938 // otherwise, just wait until onResume to set the state back to Workspace
1939 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001940 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001941 } else {
1942 mOnResumeState = State.WORKSPACE;
1943 }
1944
1945 final View v = getWindow().peekDecorView();
1946 if (v != null && v.getWindowToken() != null) {
1947 InputMethodManager imm = (InputMethodManager)getSystemService(
1948 INPUT_METHOD_SERVICE);
1949 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1950 }
1951
Winson Chungb745afb2015-03-02 11:51:23 -08001952 // Reset the apps view
1953 if (!alreadyOnHome && mAppsView != null) {
1954 mAppsView.scrollToTop();
1955 }
1956
Hyunyoung Song3f471442015-04-08 19:01:34 -07001957 // Reset the widgets view
1958 if (!alreadyOnHome && mWidgetsView != null) {
1959 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001960 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001961
Adam Cohen9211d422014-10-07 18:14:53 -07001962 if (mLauncherCallbacks != null) {
1963 mLauncherCallbacks.onHomeIntent();
1964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
Adam Cohened307df2013-10-02 09:37:31 -07001966
Michael Jurka447bf842013-05-15 14:52:15 +02001967 if (DEBUG_RESUME_TIME) {
1968 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970
Adam Cohen9211d422014-10-07 18:14:53 -07001971 if (mLauncherCallbacks != null) {
1972 mLauncherCallbacks.onNewIntent(intent);
1973 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001974 }
1975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001977 public void onRestoreInstanceState(Bundle state) {
1978 super.onRestoreInstanceState(state);
1979 for (int page: mSynchronouslyBoundPages) {
1980 mWorkspace.restoreInstanceStateForChild(page);
1981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
1983
1984 @Override
1985 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001986 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001987 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1988 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001989 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001990 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991
Michael Jurka883f55b2010-10-21 15:47:14 -07001992 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001993 // We close any open folder since it will not be re-opened, and we need to make sure
1994 // this state is reflected.
1995 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001998 mWaitingForResult) {
1999 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002000 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002001 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2002 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002003 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2004 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2005 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002006 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008
2009 if (mFolderInfo != null && mWaitingForResult) {
2010 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2011 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2012 }
Michael Jurka946ad472010-07-09 18:05:18 -07002013
Hyunyoung Song3f471442015-04-08 19:01:34 -07002014 // Save the current widgets tray?
2015 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002016 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002017
2018 if (mLauncherCallbacks != null) {
2019 mLauncherCallbacks.onSaveInstanceState(outState);
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022
2023 @Override
2024 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002026
Winson Chunge7a03942011-08-05 15:05:12 -07002027 // Remove all pending runnables
2028 mHandler.removeMessages(ADVANCE_MSG);
2029 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002030 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002031
Winson Chungcd2b0142011-06-08 16:02:26 -07002032 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002033 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002034
2035 // It's possible to receive onDestroy after a new Launcher activity has
2036 // been created. In this case, don't interfere with the new Launcher.
2037 if (mModel.isCurrentCallbacks(this)) {
2038 mModel.stopLoader();
2039 app.setLauncher(null);
2040 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002043 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002045 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002047 mAppWidgetHost = null;
2048
2049 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050
2051 TextKeyListener.getInstance().release();
2052
Winson Chung81b52252012-08-27 15:34:29 -07002053 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2054 // to prevent leaking Launcher activities on orientation change.
2055 if (mModel != null) {
2056 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2057 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002058
2059 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002060 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002061
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002062 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002063 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002064 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002065 mWorkspace = null;
2066 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002067
Michael Jurka2ecf9952012-06-18 12:52:28 -07002068 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002069
2070 if (mLauncherCallbacks != null) {
2071 mLauncherCallbacks.onDestroy();
2072 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
2074
Adam Cohena9cf38f2011-05-02 15:36:58 -07002075 public DragController getDragController() {
2076 return mDragController;
2077 }
2078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 @Override
2080 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002081 onStartForResult(requestCode);
2082 super.startActivityForResult(intent, requestCode);
2083 }
2084
2085 @Override
2086 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2087 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2088 onStartForResult(requestCode);
2089 try {
2090 super.startIntentSenderForResult(intent, requestCode,
2091 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2092 } catch (IntentSender.SendIntentException e) {
2093 throw new ActivityNotFoundException();
2094 }
2095 }
2096
2097 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002098 if (requestCode >= 0) {
2099 setWaitingForResult(true);
2100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 }
2102
Winson Chung70d72102011-08-12 11:24:35 -07002103 /**
2104 * Indicates that we want global search for this activity by setting the globalSearch
2105 * argument for {@link #startSearch} to true.
2106 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002108 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002110
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002111 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002112
Karl Rosaen138a0412009-04-23 19:00:21 -07002113 if (initialQuery == null) {
2114 // Use any text typed in the launcher as the initial query
2115 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 if (appSearchData == null) {
2118 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002119 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 }
Winson Chungadf0c182012-08-23 14:59:07 -07002121 Rect sourceBounds = new Rect();
2122 if (mSearchDropTargetBar != null) {
2123 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2124 }
Romain Guycbb89e42009-06-08 15:52:54 -07002125
Ian Parkinson047036e2014-09-01 15:40:53 +01002126 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002127 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002128 if (clearTextImmediately) {
2129 clearTypedText();
2130 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002131 }
2132
Ian Parkinson047036e2014-09-01 15:40:53 +01002133 /**
2134 * Start a text search.
2135 *
2136 * @return {@code true} if the search will start immediately, so any further keypresses
2137 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2138 * to buffer keypresses.
2139 */
2140 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002141 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002142 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2143 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2144 sourceBounds);
2145 }
2146
Michael Jurkaa33411c2012-06-14 16:18:21 -07002147 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002148 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002149 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002150 }
2151
2152 /**
2153 * Starts the global search activity. This code is a copied from SearchManager
2154 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002155 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002156 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002157 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002158 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2159 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2160 if (globalSearchActivity == null) {
2161 Log.w(TAG, "No global search activity found.");
2162 return;
2163 }
2164 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2165 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2166 intent.setComponent(globalSearchActivity);
2167 // Make sure that we have a Bundle to put source in
2168 if (appSearchData == null) {
2169 appSearchData = new Bundle();
2170 } else {
2171 appSearchData = new Bundle(appSearchData);
2172 }
Adam Cohen9211d422014-10-07 18:14:53 -07002173 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002174 if (!appSearchData.containsKey("source")) {
2175 appSearchData.putString("source", getPackageName());
2176 }
2177 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2178 if (!TextUtils.isEmpty(initialQuery)) {
2179 intent.putExtra(SearchManager.QUERY, initialQuery);
2180 }
2181 if (selectInitialQuery) {
2182 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2183 }
2184 intent.setSourceBounds(sourceBounds);
2185 try {
2186 startActivity(intent);
2187 } catch (ActivityNotFoundException ex) {
2188 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 }
2191
Yura4f93ec62014-02-04 14:15:21 +00002192 public boolean isOnCustomContent() {
2193 return mWorkspace.isOnOrMovingToCustomContent();
2194 }
2195
Winson Chung70d72102011-08-12 11:24:35 -07002196 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002197 public boolean onPrepareOptionsMenu(Menu menu) {
2198 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002199 if (!isOnCustomContent()) {
2200 // Close any open folders
2201 closeFolder();
2202 // Stop resizing any widgets
2203 mWorkspace.exitWidgetResizeMode();
2204 if (!mWorkspace.isInOverviewMode()) {
2205 // Show the overview mode
2206 showOverviewMode(true);
2207 } else {
2208 showWorkspace(true);
2209 }
Winson Chunge0298742014-01-17 12:03:00 -08002210 }
Adam Cohen9211d422014-10-07 18:14:53 -07002211 if (mLauncherCallbacks != null) {
2212 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2213 }
2214
Michael Jurkab94f3f82013-09-11 18:08:54 +02002215 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002218 @Override
2219 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002220 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002221 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002222 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002223 }
2224
Joe Onorato9c1289c2009-08-17 11:03:03 -04002225 public boolean isWorkspaceLocked() {
2226 return mWorkspaceLoading || mWaitingForResult;
2227 }
2228
Adam Cohen517a7f52014-03-01 12:12:59 -08002229 public boolean isWorkspaceLoading() {
2230 return mWorkspaceLoading;
2231 }
2232
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002233 private void setWorkspaceLoading(boolean value) {
2234 boolean isLocked = isWorkspaceLocked();
2235 mWorkspaceLoading = value;
2236 if (isLocked != isWorkspaceLocked()) {
2237 onWorkspaceLockedChanged();
2238 }
2239 }
2240
2241 private void setWaitingForResult(boolean value) {
2242 boolean isLocked = isWorkspaceLocked();
2243 mWaitingForResult = value;
2244 if (isLocked != isWorkspaceLocked()) {
2245 onWorkspaceLockedChanged();
2246 }
2247 }
2248
Adam Cohen9211d422014-10-07 18:14:53 -07002249 protected void onWorkspaceLockedChanged() {
2250 if (mLauncherCallbacks != null) {
2251 mLauncherCallbacks.onWorkspaceLockedChanged();
2252 }
2253 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002254
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002257 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2259 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002260 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002261 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002262 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002263
Sunny Goyale6b63a32015-01-16 16:14:29 -08002264 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002265 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002266 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2267 }
2268
Sunny Goyale6b63a32015-01-16 16:14:29 -08002269 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002270 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2271 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002272 if (appWidgetInfo.configure != null) {
2273 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002274 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002275
Bjorn Bringert7984c942009-12-09 15:38:25 +00002276 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002277 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2278 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002281 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002282 Runnable onComplete = new Runnable() {
2283 @Override
2284 public void run() {
2285 // Exit spring loaded mode if necessary after adding the widget
2286 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2287 null);
2288 }
2289 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002290 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002291 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002292 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 }
2294 }
Romain Guycbb89e42009-06-08 15:52:54 -07002295
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002296 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002297 // Close any folders that may be open.
2298 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002299 mWorkspace.moveToCustomContentScreen(animate);
2300 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002301
2302 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2303 int[] cell, int spanX, int spanY) {
2304 switch (info.itemType) {
2305 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2306 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2307 int span[] = new int[2];
2308 span[0] = spanX;
2309 span[1] = spanY;
2310 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2311 container, screenId, cell, span);
2312 break;
2313 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2314 processShortcutFromDrop(info.componentName, container, screenId, cell);
2315 break;
2316 default:
2317 throw new IllegalStateException("Unknown item type: " + info.itemType);
2318 }
2319 }
2320
Adam Cohenfbba09b2011-07-18 21:43:05 -07002321 /**
2322 * Process a shortcut drop.
2323 *
2324 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002325 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002326 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002328 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2329 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002330 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002333 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334
2335 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002336 mPendingAddInfo.cellX = cell[0];
2337 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002338 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002339
2340 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2341 createShortcutIntent.setComponent(componentName);
2342 processShortcut(createShortcutIntent);
2343 }
2344
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 /**
2346 * Process a widget drop.
2347 *
2348 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002349 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002350 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002352 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2353 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002354 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002355 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002356 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002357 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002358 mPendingAddInfo.minSpanX = info.minSpanX;
2359 mPendingAddInfo.minSpanY = info.minSpanY;
2360
Adam Cohenfbba09b2011-07-18 21:43:05 -07002361 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002362 mPendingAddInfo.cellX = cell[0];
2363 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002364 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002365 if (span != null) {
2366 mPendingAddInfo.spanX = span[0];
2367 mPendingAddInfo.spanY = span[1];
2368 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002369
Adam Cohened66b2b2012-01-23 17:28:51 -08002370 AppWidgetHostView hostView = info.boundWidget;
2371 int appWidgetId;
2372 if (hostView != null) {
2373 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002374 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002375 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002376 // In this case, we either need to start an activity to get permission to bind
2377 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002378 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002379 Bundle options = info.bindOptions;
2380
Sunny Goyalffe83f12014-08-14 17:39:34 -07002381 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2382 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002383 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002384 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002385 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002386 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002387 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2388 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2389 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002390 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2391 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002392 // TODO: we need to make sure that this accounts for the options bundle.
2393 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002394 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2395 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002396 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002397 }
2398
Joe Onoratodeb98af2010-02-19 14:59:39 -08002399 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002400 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 }
2402
Winson Chung24ab2f12010-09-16 14:10:47 -07002403 void processWallpaper(Intent intent) {
2404 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2405 }
2406
Adam Cohendcd297f2013-06-18 13:13:40 -07002407 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002408 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002409 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 folderInfo.title = getText(R.string.folder_name);
2411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002413 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2414 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002415 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416
2417 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002418 FolderIcon newFolder =
2419 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002420 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002421 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002422 // Force measure the new folder icon
2423 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2424 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002425 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 }
Romain Guycbb89e42009-06-08 15:52:54 -07002427
Joe Onorato9c1289c2009-08-17 11:03:03 -04002428 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002429 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002430 }
2431
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002432 /**
2433 * Registers various content observers. The current implementation registers
2434 * only a favorites observer to keep track of the favorites applications.
2435 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002436 private void registerContentObservers() {
2437 ContentResolver resolver = getContentResolver();
2438 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2439 true, mWidgetObserver);
2440 }
2441
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 @Override
2443 public boolean dispatchKeyEvent(KeyEvent event) {
2444 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2445 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002447 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002448 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002449 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002450 dumpState();
2451 return true;
2452 }
2453 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002454 }
2455 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2456 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002457 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 return true;
2459 }
2460 }
2461
2462 return super.dispatchKeyEvent(event);
2463 }
2464
Joe Onorato88ec0992009-11-19 13:16:06 -08002465 @Override
2466 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002467 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2468 return;
2469 }
2470
Winson Chung3d9490a2015-03-24 17:38:42 -07002471 LauncherAccessibilityDelegate delegate =
2472 LauncherAppState.getInstance().getAccessibilityDelegate();
2473 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002474 return;
2475 }
2476
Winson Chungb745afb2015-03-02 11:51:23 -08002477 if (isAppsViewVisible()) {
2478 showWorkspace(true);
2479 } else if (isWidgetsViewVisible()) {
2480 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002481 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002482 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002483 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002484 Folder openFolder = mWorkspace.getOpenFolder();
2485 if (openFolder.isEditingName()) {
2486 openFolder.dismissEditingName();
2487 } else {
2488 closeFolder();
2489 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002490 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002491 mWorkspace.exitWidgetResizeMode();
2492
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002493 // Back button is a no-op here, but give at least some feedback for the button press
2494 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002495 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002496 }
2497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002499 * Re-listen when widgets are reset.
2500 */
Adam Cohen091440a2015-03-18 14:16:05 -07002501 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002502 if (mAppWidgetHost != null) {
2503 mAppWidgetHost.startListening();
2504 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002505 }
2506
2507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 * Launches the intent referred by the clicked shortcut.
2509 *
2510 * @param v The view representing the clicked shortcut.
2511 */
2512 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002513 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2514 // view has detached (it's possible for this to happen if the view is removed mid touch).
2515 if (v.getWindowToken() == null) {
2516 return;
2517 }
2518
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002519 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002520 return;
2521 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002522
Adam Cohen1697b792013-09-17 19:08:21 -07002523 if (v instanceof Workspace) {
2524 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002525 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002526 }
2527 return;
2528 }
2529
2530 if (v instanceof CellLayout) {
2531 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002532 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 }
2534 }
2535
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002537 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002540 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002541 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002542 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002543 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002544 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002545 } else if (tag instanceof AppInfo) {
2546 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002547 } else if (tag instanceof LauncherAppWidgetInfo) {
2548 if (v instanceof PendingAppWidgetHostView) {
2549 onClickPendingWidget((PendingAppWidgetHostView) v);
2550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 }
2552 }
2553
Sunny Goyal508da152014-08-14 10:53:27 -07002554 public void onClickPagedViewIcon(View v) {
2555 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002556 if (mLauncherCallbacks != null) {
2557 mLauncherCallbacks.onClickPagedViewIcon(v);
2558 }
Sunny Goyal508da152014-08-14 10:53:27 -07002559 }
2560
Michael Jurka0e260592010-06-30 17:07:39 -07002561 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002562 return false;
2563 }
2564
Michael Jurkaaf442092010-06-10 17:01:57 -07002565 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002566 * Event handler for the app widget view which has not fully restored.
2567 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002568 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002569 if (mIsSafeModeEnabled) {
2570 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2571 return;
2572 }
2573
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002574 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002575 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002576 int widgetId = info.appWidgetId;
2577 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2578 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002579 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2580 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002581 mPendingAddInfo.copyFrom(info);
2582 mPendingAddWidgetId = widgetId;
2583
Sunny Goyalffe83f12014-08-14 17:39:34 -07002584 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2585 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002586 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002587 } else if (info.installProgress < 0) {
2588 // The install has not been queued
2589 final String packageName = info.providerName.getPackageName();
2590 showBrokenAppInstallDialog(packageName,
2591 new DialogInterface.OnClickListener() {
2592 public void onClick(DialogInterface dialog, int id) {
2593 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2594 }
2595 });
2596 } else {
2597 // Download has started.
2598 final String packageName = info.providerName.getPackageName();
2599 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002600 }
2601 }
2602
2603 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002604 * Event handler for the "grid" button that appears on the home screen, which
2605 * enters all apps mode.
2606 *
2607 * @param v The view that was clicked.
2608 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002609 protected void onClickAllAppsButton(View v) {
2610 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002611 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002612 showWorkspace(true);
2613 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002614 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002615 }
2616 }
2617
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002618 private void showBrokenAppInstallDialog(final String packageName,
2619 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002620 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002621 .setTitle(R.string.abandoned_promises_title)
2622 .setMessage(R.string.abandoned_promise_explanation)
2623 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2624 .setNeutralButton(R.string.abandoned_clean_this,
2625 new DialogInterface.OnClickListener() {
2626 public void onClick(DialogInterface dialog, int id) {
2627 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2628 mWorkspace.removeAbandonedPromise(packageName, user);
2629 }
2630 })
2631 .create().show();
2632 return;
2633 }
2634
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002635 /**
2636 * Event handler for an app shortcut click.
2637 *
2638 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2639 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002640 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002641 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2642 Object tag = v.getTag();
2643 if (!(tag instanceof ShortcutInfo)) {
2644 throw new IllegalArgumentException("Input must be a Shortcut");
2645 }
2646
2647 // Open shortcut
2648 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002649
2650 if (shortcut.isDisabled != 0) {
2651 int error = R.string.activity_not_available;
2652 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2653 error = R.string.safemode_shortcut_error;
2654 }
2655 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2656 return;
2657 }
2658
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002659 final Intent intent = shortcut.intent;
2660
2661 // Check for special shortcuts
2662 if (intent.getComponent() != null) {
2663 final String shortcutClass = intent.getComponent().getClassName();
2664
2665 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2666 MemoryDumpActivity.startDump(this);
2667 return;
2668 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2669 toggleShowWeightWatcher();
2670 return;
2671 }
2672 }
2673
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002675 if ((v instanceof BubbleTextView)
2676 && shortcut.isPromise()
2677 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002678 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002679 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002680 new DialogInterface.OnClickListener() {
2681 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002682 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002683 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 return;
2686 }
2687
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002688 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002689 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002690
2691 if (mLauncherCallbacks != null) {
2692 mLauncherCallbacks.onClickAppShortcut(v);
2693 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 }
2695
Adam Cohen091440a2015-03-18 14:16:05 -07002696 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002697 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002698 final ShortcutInfo shortcut;
2699 final Intent intent;
2700 if (tag instanceof ShortcutInfo) {
2701 shortcut = (ShortcutInfo) tag;
2702 intent = shortcut.intent;
2703 int[] pos = new int[2];
2704 v.getLocationOnScreen(pos);
2705 intent.setSourceBounds(new Rect(pos[0], pos[1],
2706 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002707
Sunny Goyal508da152014-08-14 10:53:27 -07002708 } else if (tag instanceof AppInfo) {
2709 shortcut = null;
2710 intent = ((AppInfo) tag).intent;
2711 } else {
2712 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2713 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002714
2715 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 mStats.recordLaunch(intent, shortcut);
2717
2718 if (success && v instanceof BubbleTextView) {
2719 mWaitingForResume = (BubbleTextView) v;
2720 mWaitingForResume.setStayPressed(true);
2721 }
2722 }
2723
2724 /**
2725 * Event handler for a folder icon click.
2726 *
2727 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2728 */
2729 protected void onClickFolderIcon(View v) {
2730 if (LOGD) Log.d(TAG, "onClickFolder");
2731 if (!(v instanceof FolderIcon)){
2732 throw new IllegalArgumentException("Input must be a FolderIcon");
2733 }
2734
2735 FolderIcon folderIcon = (FolderIcon) v;
2736 final FolderInfo info = folderIcon.getFolderInfo();
2737 Folder openFolder = mWorkspace.getFolderForTag(info);
2738
2739 // If the folder info reports that the associated folder is open, then verify that
2740 // it is actually opened. There have been a few instances where this gets out of sync.
2741 if (info.opened && openFolder == null) {
2742 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2743 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2744 info.opened = false;
2745 }
2746
2747 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2748 // Close any open folder
2749 closeFolder();
2750 // Open the requested folder
2751 openFolder(folderIcon);
2752 } else {
2753 // Find the open folder...
2754 int folderScreen;
2755 if (openFolder != null) {
2756 folderScreen = mWorkspace.getPageForView(openFolder);
2757 // .. and close it
2758 closeFolder(openFolder);
2759 if (folderScreen != mWorkspace.getCurrentPage()) {
2760 // Close any folder open on the current screen
2761 closeFolder();
2762 // Pull the folder onto this screen
2763 openFolder(folderIcon);
2764 }
2765 }
2766 }
Adam Cohen9211d422014-10-07 18:14:53 -07002767
2768 if (mLauncherCallbacks != null) {
2769 mLauncherCallbacks.onClickFolderIcon(v);
2770 }
Michael Jurka5130e402011-10-13 04:55:35 -07002771 }
2772
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002773 /**
2774 * Event handler for the (Add) Widgets button that appears after a long press
2775 * on the home screen.
2776 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002777 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002778 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002779 if (mIsSafeModeEnabled) {
2780 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2781 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002782 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002783 if (mLauncherCallbacks != null) {
2784 mLauncherCallbacks.onClickAddWidgetButton(view);
2785 }
Adam Cohen9211d422014-10-07 18:14:53 -07002786 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002787 }
2788
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002789 /**
2790 * Event handler for the wallpaper picker button that appears after a long press
2791 * on the home screen.
2792 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002793 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002794 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002795 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2796 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002797
2798 if (mLauncherCallbacks != null) {
2799 mLauncherCallbacks.onClickWallpaperPicker(v);
2800 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002801 }
2802
2803 /**
2804 * Event handler for a click on the settings button that appears after a long press
2805 * on the home screen.
2806 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002807 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002808 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002809 if (mLauncherCallbacks != null) {
2810 mLauncherCallbacks.onClickSettingsButton(v);
2811 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002812 }
2813
Michael Jurka5130e402011-10-13 04:55:35 -07002814 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002815 // Provide the same haptic feedback that the system offers for virtual keys.
2816 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002817 }
2818
Adam Cohen61f560d2013-09-30 15:58:20 -07002819 public void performHapticFeedbackOnTouchDown(View v) {
2820 // Provide the same haptic feedback that the system offers for virtual keys.
2821 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2822 }
2823
2824 public View.OnTouchListener getHapticFeedbackTouchListener() {
2825 if (mHapticFeedbackTouchListener == null) {
2826 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2827 @Override
2828 public boolean onTouch(View v, MotionEvent event) {
2829 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2830 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2831 }
2832 return false;
2833 }
2834 };
2835 }
2836 return mHapticFeedbackTouchListener;
2837 }
2838
Adam Cohen9211d422014-10-07 18:14:53 -07002839 public void onDragStarted(View view) {
2840 if (isOnCustomContent()) {
2841 // Custom content screen doesn't participate in drag and drop. If on custom
2842 // content screen, move to default.
2843 moveWorkspaceToDefaultScreen();
2844 }
2845
2846 if (mLauncherCallbacks != null) {
2847 mLauncherCallbacks.onDragStarted(view);
2848 }
2849 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002850
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002851 /**
2852 * Called when the user stops interacting with the launcher.
2853 * This implies that the user is now on the homescreen and is not doing housekeeping.
2854 */
Adam Cohen9211d422014-10-07 18:14:53 -07002855 protected void onInteractionEnd() {
2856 if (mLauncherCallbacks != null) {
2857 mLauncherCallbacks.onInteractionEnd();
2858 }
2859 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002860
2861 /**
2862 * Called when the user starts interacting with the launcher.
2863 * The possible interactions are:
2864 * - open all apps
2865 * - reorder an app shortcut, or a widget
2866 * - open the overview mode.
2867 * This is a good time to stop doing things that only make sense
2868 * when the user is on the homescreen and not doing housekeeping.
2869 */
Adam Cohen9211d422014-10-07 18:14:53 -07002870 protected void onInteractionBegin() {
2871 if (mLauncherCallbacks != null) {
2872 mLauncherCallbacks.onInteractionBegin();
2873 }
2874 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002875
Kenny Guyf07af7b2014-07-31 11:39:16 +01002876 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002877 try {
2878 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002879 launcherApps.showAppDetailsForProfile(componentName, user);
2880 } catch (SecurityException e) {
2881 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2882 Log.e(TAG, "Launcher does not have permission to launch settings");
2883 } catch (ActivityNotFoundException e) {
2884 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2885 Log.e(TAG, "Unable to launch settings");
2886 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002887 }
2888
Michael Jurka1e2f4652013-07-08 18:03:46 -07002889 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002890 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2891 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002892 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002893 // System applications cannot be installed. For now, show a toast explaining that.
2894 // We may give them the option of disabling apps this way.
2895 int messageId = R.string.uninstall_system_app_text;
2896 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002897 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002898 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002899 String packageName = componentName.getPackageName();
2900 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002901 Intent intent = new Intent(
2902 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002903 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2904 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002905 if (user != null) {
2906 user.addToIntent(intent, Intent.EXTRA_USER);
2907 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002908 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002909 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002910 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002911 }
2912
Michael Jurka86a720e2012-05-09 11:23:23 -07002913 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002914 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002915 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002916 // Only launch using the new animation if the shortcut has not opted out (this is a
2917 // private contract between launcher and may be ignored in the future).
2918 boolean useLaunchAnimation = (v != null) &&
2919 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002920 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2921 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002922
Kenny Guy1317e2d2014-05-08 18:52:50 +01002923 UserHandleCompat user = null;
2924 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2925 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2926 user = userManager.getUserForSerialNumber(serialNumber);
2927 }
2928
2929 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002930 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002931 ActivityOptions opts = null;
2932 if (sClipRevealMethod != null) {
2933 // TODO: call method directly when Launcher3 can depend on M APIs
2934 int left = 0, top = 0;
2935 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2936 if (v instanceof TextView) {
2937 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002938 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2939 if (icon != null) {
2940 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002941 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002942 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002943 width = bounds.width();
2944 height = bounds.height();
2945 }
2946 }
2947 try {
2948 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2949 left, top, width, height);
2950 } catch (IllegalAccessException e) {
2951 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2952 sClipRevealMethod = null;
2953 } catch (InvocationTargetException e) {
2954 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2955 sClipRevealMethod = null;
2956 }
2957 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002958 if (opts == null && !Utilities.isLmpOrAbove()) {
2959 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002960 v.getMeasuredWidth(), v.getMeasuredHeight());
2961 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002962 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002963 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002964
2965 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2966 // Could be launching some bookkeeping activity
2967 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002968 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002969 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002970 launcherApps.startActivityForProfile(intent.getComponent(), user,
2971 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002972 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002973 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 } catch (SecurityException e) {
2975 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002976 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002978 "or use the exported attribute for this activity. "
2979 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002981 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002982 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002983
Michael Jurka86a720e2012-05-09 11:23:23 -07002984 boolean startActivitySafely(View v, Intent intent, Object tag) {
2985 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002986 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2987 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2988 return false;
2989 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002990 try {
2991 success = startActivity(v, intent, tag);
2992 } catch (ActivityNotFoundException e) {
2993 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2994 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2995 }
2996 return success;
2997 }
2998
Adam Cohen268c4752012-06-06 17:47:33 -07002999 /**
3000 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3001 * in the DragLayer in the exact absolute location of the original FolderIcon.
3002 */
3003 private void copyFolderIconToImage(FolderIcon fi) {
3004 final int width = fi.getMeasuredWidth();
3005 final int height = fi.getMeasuredHeight();
3006
3007 // Lazy load ImageView, Bitmap and Canvas
3008 if (mFolderIconImageView == null) {
3009 mFolderIconImageView = new ImageView(this);
3010 }
3011 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3012 mFolderIconBitmap.getHeight() != height) {
3013 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3014 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3015 }
3016
3017 DragLayer.LayoutParams lp;
3018 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3019 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3020 } else {
3021 lp = new DragLayer.LayoutParams(width, height);
3022 }
3023
Adam Cohen307fe232012-08-16 17:55:58 -07003024 // The layout from which the folder is being opened may be scaled, adjust the starting
3025 // view size by this scale factor.
3026 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003027 lp.customPosition = true;
3028 lp.x = mRectForFolderAnimation.left;
3029 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003030 lp.width = (int) (scale * width);
3031 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003032
3033 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3034 fi.draw(mFolderIconCanvas);
3035 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003036 if (fi.getFolder() != null) {
3037 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3038 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003039 }
Adam Cohen268c4752012-06-06 17:47:33 -07003040 // Just in case this image view is still in the drag layer from a previous animation,
3041 // we remove it and re-add it.
3042 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3043 mDragLayer.removeView(mFolderIconImageView);
3044 }
3045 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003046 if (fi.getFolder() != null) {
3047 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003048 }
Adam Cohen268c4752012-06-06 17:47:33 -07003049 }
3050
Adam Cohen2801caf2011-05-13 20:57:39 -07003051 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003052 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003053 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3054 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3055 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3056
Adam Cohenc51934b2011-07-26 21:07:43 -07003057 FolderInfo info = (FolderInfo) fi.getTag();
3058 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3059 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003060 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3061 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003062 }
3063
Adam Cohen268c4752012-06-06 17:47:33 -07003064 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3065 copyFolderIconToImage(fi);
3066 fi.setVisibility(View.INVISIBLE);
3067
Michael Jurka2ecf9952012-06-18 12:52:28 -07003068 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003069 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003070 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003071 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3072 }
3073 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003074 oa.start();
3075 }
3076
Adam Cohen268c4752012-06-06 17:47:33 -07003077 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003078 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003079 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3080 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3081 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3082
Adam Cohen268c4752012-06-06 17:47:33 -07003083 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003084
Adam Cohen268c4752012-06-06 17:47:33 -07003085 // We remove and re-draw the FolderIcon in-case it has changed
3086 mDragLayer.removeView(mFolderIconImageView);
3087 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003088 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003089 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003090 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003091 oa.addListener(new AnimatorListenerAdapter() {
3092 @Override
3093 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003094 if (cl != null) {
3095 cl.clearFolderLeaveBehind();
3096 // Remove the ImageView copy of the FolderIcon and make the original visible.
3097 mDragLayer.removeView(mFolderIconImageView);
3098 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003099 }
3100 }
3101 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003102 oa.start();
3103 }
3104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003105 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003106 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003107 * is animated relative to the specified View. If the View is null, no animation
3108 * is played.
3109 *
3110 * @param folderInfo The FolderInfo describing the folder to open.
3111 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003112 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003113 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003114 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3115 if (openFolder != null && openFolder != folder) {
3116 // Close any open folder before opening a folder.
3117 closeFolder();
3118 }
3119
Adam Cohena9cf38f2011-05-02 15:36:58 -07003120 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121
Adam Cohena9cf38f2011-05-02 15:36:58 -07003122 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123
Sunny Goyalf4066152015-04-15 09:42:19 -07003124 // While the folder is open, the position of the icon cannot change.
3125 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3126
Adam Cohen4554ee12011-08-03 16:13:21 -07003127 // Just verify that the folder hasn't already been added to the DragLayer.
3128 // There was a one-off crash where the folder had a parent already.
3129 if (folder.getParent() == null) {
3130 mDragLayer.addView(folder);
3131 mDragController.addDropTarget((DropTarget) folder);
3132 } else {
3133 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3134 folder.getParent() + ").");
3135 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003136 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003137 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003138
3139 // Notify the accessibility manager that this folder "window" has appeared and occluded
3140 // the workspace items
3141 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3142 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003143 }
3144
3145 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003146 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003147 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003148 if (folder.isEditingName()) {
3149 folder.dismissEditingName();
3150 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003151 closeFolder(folder);
3152 }
3153 }
3154
3155 void closeFolder(Folder folder) {
3156 folder.getInfo().opened = false;
3157
3158 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3159 if (parent != null) {
3160 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3161 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003162 if (fi != null) {
3163 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3164 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003165 }
3166 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003167
3168 // Notify the accessibility manager that this folder "window" has disappeard and no
3169 // longer occludeds the workspace items
3170 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 }
3172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003174 if (!isDraggingEnabled()) return false;
3175 if (isWorkspaceLocked()) return false;
3176 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003177
Adam Cohen1697b792013-09-17 19:08:21 -07003178 if (v instanceof Workspace) {
3179 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003180
3181 if (!mWorkspace.isTouchActive()) {
3182 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003183 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3184 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3185 return true;
3186 } else {
3187 return false;
3188 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003189 } else {
3190 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003191 }
Adam Cohen1697b792013-09-17 19:08:21 -07003192 }
3193
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003194 CellLayout.CellInfo longClickCellInfo = null;
3195 View itemUnderLongClick = null;
3196 if (v.getTag() instanceof ItemInfo) {
3197 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003198 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003199 itemUnderLongClick = longClickCellInfo.cell;
3200 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003201 }
3202
Winson Chung3d503fb2011-07-13 17:25:49 -07003203 // The hotseat touch handling does not go through Workspace, and we always allow long press
3204 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003205 final boolean inHotseat = isHotseatLayout(v);
3206 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003207 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003208 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003209 // User long pressed on empty space
3210 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3211 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003212 if (mWorkspace.isInOverviewMode()) {
3213 mWorkspace.startReordering(v);
3214 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003215 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003217 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003218 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3219 mHotseat.getOrderInHotseat(
3220 longClickCellInfo.cellX,
3221 longClickCellInfo.cellY));
3222 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003224 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 }
3226 }
3227 }
3228 return true;
3229 }
3230
Winson Chung3d503fb2011-07-13 17:25:49 -07003231 boolean isHotseatLayout(View layout) {
3232 return mHotseat != null && layout != null &&
3233 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3234 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003235
Winson Chung3d503fb2011-07-13 17:25:49 -07003236 /**
3237 * Returns the CellLayout of the specified container at the specified screen.
3238 */
Sunny Goyal92820592015-03-02 11:31:35 -08003239 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003240 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3241 if (mHotseat != null) {
3242 return mHotseat.getLayout();
3243 } else {
3244 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003245 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003246 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003247 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003248 }
3249 }
3250
Winson Chungb745afb2015-03-02 11:51:23 -08003251 /**
3252 * For overridden classes.
3253 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003254 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003255 return isAppsViewVisible();
3256 }
3257
3258 public boolean isAppsViewVisible() {
3259 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3260 }
3261
3262 public boolean isWidgetsViewVisible() {
3263 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003264 }
3265
Craig Mautner360310b2012-10-26 15:13:08 -07003266 private void setWorkspaceBackground(boolean workspace) {
3267 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003268 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003269 }
3270
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003271 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003272 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3273 int curflags = getWindow().getAttributes().flags
3274 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3275 if (wpflags != curflags) {
3276 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3277 }
Craig Mautner360310b2012-10-26 15:13:08 -07003278 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003279 }
3280
Michael Jurkae326f182011-11-21 14:05:46 -08003281 @Override
3282 public void onTrimMemory(int level) {
3283 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003284 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3285 // The widget preview db can result in holding onto over
3286 // 3MB of memory for caching which isn't necessary.
3287 SQLiteDatabase.releaseMemory();
3288
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003289 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003290 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003291 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003292 if (mLauncherCallbacks != null) {
3293 mLauncherCallbacks.onTrimMemory(level);
3294 }
Michael Jurkae326f182011-11-21 14:05:46 -08003295 }
3296
Winson Chungb745afb2015-03-02 11:51:23 -08003297 @Override
3298 public void onStateTransitionHideSearchBar() {
3299 // Hide the search bar
3300 if (mSearchDropTargetBar != null) {
3301 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3302 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003303 }
3304
Winson Chungb745afb2015-03-02 11:51:23 -08003305 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003306 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3307 true);
Adam Cohened307df2013-10-02 09:37:31 -07003308 }
3309
Winson Chungdc61c4d2015-04-20 18:26:57 -07003310 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3311 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3312 onCompleteRunnable, true);
3313 }
3314
3315 protected void showWorkspace(int snapToPage, boolean animated) {
3316 showWorkspace(snapToPage, animated, null, true);
3317 }
3318
3319 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3320 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003321 boolean changed = mState != State.WORKSPACE ||
3322 mWorkspace.getState() != Workspace.State.NORMAL;
3323 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003324 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003325 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003326 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003327 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003328
Winson Chungc7d2b602012-05-16 17:02:20 -07003329 // Show the search bar (only animate if we were showing the drop target bar in spring
3330 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003331 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003332 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003333 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003334
Michael Jurkab3e22d92011-10-31 15:58:33 -07003335 // Set focus to the AppsCustomize button
3336 if (mAllAppsButton != null) {
3337 mAllAppsButton.requestFocus();
3338 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003339 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003340
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003341 // Change the state *after* we've called all the transition code
3342 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003343
Winson Chung5fb63472011-02-02 17:03:37 -08003344 // Resume the auto-advance of widgets
3345 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003346 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003347
Winson Chung0f785722015-04-08 10:27:49 -07003348 if (changed) {
3349 // Send an accessibility event to announce the context change
3350 getWindow().getDecorView()
3351 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003352
Winson Chung0f785722015-04-08 10:27:49 -07003353 onWorkspaceShown(animated);
3354 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003355 }
3356
Adam Cohened307df2013-10-02 09:37:31 -07003357 void showOverviewMode(boolean animated) {
3358 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003359 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003360 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3361 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003362 mState = State.WORKSPACE;
3363 onWorkspaceShown(animated);
3364 }
3365
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003366 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003367 }
3368
Winson Chungb745afb2015-03-02 11:51:23 -08003369 /**
3370 * Shows the apps view.
3371 */
3372 void showAppsView(boolean animated, boolean resetListToTop) {
3373 if (resetListToTop) {
3374 mAppsView.scrollToTop();
3375 }
3376 showAppsOrWidgets(animated, State.APPS);
3377 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003378
Winson Chungb745afb2015-03-02 11:51:23 -08003379 /**
3380 * Shows the widgets view.
3381 */
3382 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003383 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003384 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003385 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003386 }
Winson Chungb745afb2015-03-02 11:51:23 -08003387 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003388
3389 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003390 @Override
3391 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003392 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003393 }
3394 });
Winson Chungb745afb2015-03-02 11:51:23 -08003395 }
3396
3397 /**
3398 * Sets up the transition to show the apps/widgets view.
3399 */
3400 private void showAppsOrWidgets(boolean animated, State toState) {
3401 if (mState != State.WORKSPACE) return;
3402 if (toState != State.APPS && toState != State.WIDGETS) return;
3403
3404 if (toState == State.APPS) {
3405 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung0f785722015-04-08 10:27:49 -07003406 if (mLauncherCallbacks != null) {
3407 mLauncherCallbacks.onAllAppsShown();
3408 }
Winson Chungb745afb2015-03-02 11:51:23 -08003409 } else {
3410 mStateTransitionAnimation.startAnimationToWidgets(animated);
3411 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003412
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003414 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003415
3416 // Pause the auto-advance of widgets until we are out of AllApps
3417 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003418 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003419 closeFolder();
3420
3421 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003422 getWindow().getDecorView()
3423 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003424 }
3425
Hyunyoung Song3f471442015-04-08 19:01:34 -07003426 public void enterSpringLoadedDragMode() {
3427 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3428 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003429 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3430 mState == State.WIDGETS_SPRING_LOADED) {
3431 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003432 }
Winson Chungb745afb2015-03-02 11:51:23 -08003433
3434 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003435 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3436 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003437 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003438 }
Adam Cohen7777d962011-08-18 18:58:38 -07003439
Hyunyoung Song3f471442015-04-08 19:01:34 -07003440 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003441 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003442 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003443
Winson Chunge7a03942011-08-05 15:05:12 -07003444 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003445 @Override
3446 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003447 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003448 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3449 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003450 // Before we show workspace, hide all apps again because
3451 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3452 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003453 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003454 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003455 } else {
3456 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003457 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003458 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003459 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003460 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003461
Michael Jurkad3ef3062010-11-23 16:23:58 -08003462 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003463 if (mState == State.APPS_SPRING_LOADED) {
3464 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3465 mState = State.APPS;
3466 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3467 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3468 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003469 }
3470 // Otherwise, we are not in spring loaded mode, so don't do anything.
3471 }
3472
Michael Jurkab3e22d92011-10-31 15:58:33 -07003473 void lockAllApps() {
3474 // TODO
3475 }
3476
3477 void unlockAllApps() {
3478 // TODO
3479 }
3480
Sunny Goyal594d76d2014-11-06 10:12:54 -08003481 protected void disableVoiceButtonProxy(boolean disable) {
3482 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003483 }
3484
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003485 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003486 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3487 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003488 }
3489
Adam Cohen24ce0b32014-01-14 16:18:14 -08003490 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003491 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3492 if (searchProvider == null) {
3493 return null;
3494 }
3495
3496 Bundle opts = new Bundle();
3497 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003498 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003499
3500 SharedPreferences sp = getSharedPreferences(
3501 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3502 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003503 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003504 if (!searchProvider.provider.flattenToString().equals(
3505 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003506 || (widgetInfo == null)
3507 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003508 // A valid widget is not already bound.
3509 if (widgetId > -1) {
3510 mAppWidgetHost.deleteAppWidgetId(widgetId);
3511 widgetId = -1;
3512 }
3513
3514 // Try to bind a new widget
3515 widgetId = mAppWidgetHost.allocateAppWidgetId();
3516
3517 if (!AppWidgetManagerCompat.getInstance(this)
3518 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3519 mAppWidgetHost.deleteAppWidgetId(widgetId);
3520 widgetId = -1;
3521 }
3522
3523 sp.edit()
3524 .putInt(QSB_WIDGET_ID, widgetId)
3525 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3526 .commit();
3527 }
3528
3529 if (widgetId != -1) {
3530 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3531 mQsb.updateAppWidgetOptions(opts);
3532 mQsb.setPadding(0, 0, 0, 0);
3533 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003534 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003535 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003536 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003537 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003538 }
3539
Sunny Goyal22235bc2015-04-03 09:23:43 -07003540 private void reinflateQSBIfNecessary() {
3541 if (mQsb instanceof LauncherAppWidgetHostView &&
3542 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3543 mSearchDropTargetBar.removeView(mQsb);
3544 mQsb = null;
3545 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3546 }
3547 }
3548
Michael Jurkad7c28052012-04-27 15:43:36 -07003549 @Override
3550 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003551 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003552 final List<CharSequence> text = event.getText();
3553 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003554 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003555 if (mState == State.APPS) {
3556 text.add("Apps");
3557 } else if (mState == State.WIDGETS) {
3558 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003559 } else {
3560 text.add(getString(R.string.all_apps_home_button_label));
3561 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003562 return result;
3563 }
3564
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003565 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003566 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003567 */
Adam Cohen091440a2015-03-18 14:16:05 -07003568 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003569 @Override
3570 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003571 closeSystemDialogs();
3572 }
3573 }
3574
3575 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003576 * Receives notifications whenever the appwidgets are reset.
3577 */
3578 private class AppWidgetResetObserver extends ContentObserver {
3579 public AppWidgetResetObserver() {
3580 super(new Handler());
3581 }
3582
3583 @Override
3584 public void onChange(boolean selfChange) {
3585 onAppWidgetReset();
3586 }
3587 }
3588
3589 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003590 * If the activity is currently paused, signal that we need to run the passed Runnable
3591 * in onResume.
3592 *
3593 * This needs to be called from incoming places where resources might have been loaded
3594 * while we are paused. That is becaues the Configuration might be wrong
3595 * when we're not running, and if it comes back to what it was when we
3596 * were paused, we are not restarted.
3597 *
3598 * Implementation of the method from LauncherModel.Callbacks.
3599 *
3600 * @return true if we are currently paused. The caller might be able to
3601 * skip some work in that case since we will come back again.
3602 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003603 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003604 if (mPaused) {
3605 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003606 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003607 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003608 }
3609 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003610 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003611 return true;
3612 } else {
3613 return false;
3614 }
3615 }
3616
Michael Jurkac402cd92013-05-20 15:49:32 +02003617 private boolean waitUntilResume(Runnable run) {
3618 return waitUntilResume(run, false);
3619 }
3620
Michael Jurka1e2f4652013-07-08 18:03:46 -07003621 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003622 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003623 }
3624
Michael Jurka7607c2f2013-04-03 14:33:19 -07003625 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003626 * If the activity is currently paused, signal that we need to re-run the loader
3627 * in onResume.
3628 *
3629 * This needs to be called from incoming places where resources might have been loaded
3630 * while we are paused. That is becaues the Configuration might be wrong
3631 * when we're not running, and if it comes back to what it was when we
3632 * were paused, we are not restarted.
3633 *
3634 * Implementation of the method from LauncherModel.Callbacks.
3635 *
3636 * @return true if we are currently paused. The caller might be able to
3637 * skip some work in that case since we will come back again.
3638 */
3639 public boolean setLoadOnResume() {
3640 if (mPaused) {
3641 Log.i(TAG, "setLoadOnResume");
3642 mOnResumeNeedsLoad = true;
3643 return true;
3644 } else {
3645 return false;
3646 }
3647 }
3648
3649 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003651 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003653 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003654 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003655 } else {
3656 return SCREEN_COUNT / 2;
3657 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003658 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003659
Joe Onorato9c1289c2009-08-17 11:03:03 -04003660 /**
3661 * Refreshes the shortcuts shown on the workspace.
3662 *
3663 * Implementation of the method from LauncherModel.Callbacks.
3664 */
3665 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003666 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003667
Michael Jurka7607c2f2013-04-03 14:33:19 -07003668 // If we're starting binding all over again, clear any bind calls we'd postponed in
3669 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3670 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003671 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003672
Winson Chungd64d1762013-08-20 14:37:16 -07003673 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003674 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003675 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003676
Michael Jurka05bf644e2011-11-30 20:28:53 -08003677 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003678 if (mHotseat != null) {
3679 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003680 }
3681 }
3682
Adam Cohendcd297f2013-06-18 13:13:40 -07003683 @Override
3684 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003685 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003686
Adam Cohen5084cba2013-09-03 12:01:16 -07003687 // If there are no screens, we need to have an empty screen
3688 if (orderedScreenIds.size() == 0) {
3689 mWorkspace.addExtraEmptyScreen();
3690 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003691
3692 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003693 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003694 if (hasCustomContentToLeft()) {
3695 mWorkspace.createCustomContentContainer();
3696 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003697 }
Winson Chung64359a52013-07-08 17:17:08 -07003698 }
3699
3700 @Override
3701 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003702 // Log to disk
3703 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3704 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3705 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003706 int count = orderedScreenIds.size();
3707 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003708 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003709 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003710 }
3711
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003712 @Override
3713 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3714 final long screenId, final int[] cell, final int spanX, final int spanY) {
3715 showWorkspace(true, new Runnable() {
3716
3717 @Override
3718 public void run() {
3719 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3720 addPendingItem(info, container, screenId, cell, spanX, spanY);
3721 }
3722 });
3723 }
3724
Adam Cohen39a06042013-07-19 14:30:12 -07003725 private boolean shouldShowWeightWatcher() {
3726 String spKey = LauncherAppState.getSharedPreferencesKey();
3727 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003728 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003729
3730 return show;
3731 }
3732
3733 private void toggleShowWeightWatcher() {
3734 String spKey = LauncherAppState.getSharedPreferencesKey();
3735 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3736 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3737
3738 show = !show;
3739
3740 SharedPreferences.Editor editor = sp.edit();
3741 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3742 editor.commit();
3743
3744 if (mWeightWatcher != null) {
3745 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3746 }
3747 }
3748
Winson Chungd64d1762013-08-20 14:37:16 -07003749 public void bindAppsAdded(final ArrayList<Long> newScreens,
3750 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003751 final ArrayList<ItemInfo> addAnimated,
3752 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003753 Runnable r = new Runnable() {
3754 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003755 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003756 }
3757 };
3758 if (waitUntilResume(r)) {
3759 return;
3760 }
3761
Winson Chungd64d1762013-08-20 14:37:16 -07003762 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003763 if (newScreens != null) {
3764 bindAddScreens(newScreens);
3765 }
Winson Chungd64d1762013-08-20 14:37:16 -07003766
3767 // We add the items without animation on non-visible pages, and with
3768 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003769 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003770 bindItems(addNotAnimated, 0,
3771 addNotAnimated.size(), false);
3772 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003773 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003774 bindItems(addAnimated, 0,
3775 addAnimated.size(), true);
3776 }
Winson Chungc58497e2013-09-03 17:48:37 -07003777
Winson Chung87412982013-10-03 18:34:14 -07003778 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003779 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003780
Winson Chungb745afb2015-03-02 11:51:23 -08003781 if (addedApps != null && mAppsView != null) {
3782 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003783 }
Winson Chungd64d1762013-08-20 14:37:16 -07003784 }
3785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003786 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787 * Bind the items start-end from the list.
3788 *
3789 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003790 */
Winson Chung64359a52013-07-08 17:17:08 -07003791 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3792 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003793 Runnable r = new Runnable() {
3794 public void run() {
3795 bindItems(shortcuts, start, end, forceAnimateIcons);
3796 }
3797 };
3798 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003799 return;
3800 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003801
Winson Chungf0c6ae02012-03-21 16:10:31 -07003802 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003803 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3804 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003805 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003806 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003807 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003808 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003809 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003810
3811 // Short circuit if we are loading dock items for a configuration which has no dock
3812 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3813 mHotseat == null) {
3814 continue;
3815 }
3816
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 switch (item.itemType) {
3818 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3819 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003820 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003821 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003822
Winson Chung64359a52013-07-08 17:17:08 -07003823 /*
3824 * TODO: FIX collision case
3825 */
Winson Chungce376632013-07-11 16:12:41 -07003826 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3827 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3828 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003829 View v = cl.getChildAt(item.cellX, item.cellY);
3830 Object tag = v.getTag();
3831 String desc = "Collision while binding workspace item: " + item
3832 + ". Collides with " + tag;
3833 if (LauncherAppState.isDogfoodBuild()) {
3834 throw (new RuntimeException(desc));
3835 } else {
3836 Log.d(TAG, desc);
3837 }
Winson Chungce376632013-07-11 16:12:41 -07003838 }
Winson Chung64359a52013-07-08 17:17:08 -07003839 }
3840
Adam Cohendcd297f2013-06-18 13:13:40 -07003841 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3842 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003843 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003844 // Animate all the applications up now
3845 shortcut.setAlpha(0f);
3846 shortcut.setScaleX(0f);
3847 shortcut.setScaleY(0f);
3848 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003849 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003850 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003852 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003853 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003854 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003855 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003856 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3857 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003858 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003859 default:
3860 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003861 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003862 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003863
Winson Chung997a9232013-07-24 15:33:46 -07003864 if (animateIcons) {
3865 // Animate to the correct page
3866 if (newShortcutsScreenId > -1) {
3867 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003868 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003869 final Runnable startBounceAnimRunnable = new Runnable() {
3870 public void run() {
3871 anim.playTogether(bounceAnims);
3872 anim.start();
3873 }
3874 };
Winson Chung997a9232013-07-24 15:33:46 -07003875 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003876 // We post the animation slightly delayed to prevent slowdowns
3877 // when we are loading right after we return to launcher.
3878 mWorkspace.postDelayed(new Runnable() {
3879 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003880 if (mWorkspace != null) {
3881 mWorkspace.snapToPage(newScreenIndex);
3882 mWorkspace.postDelayed(startBounceAnimRunnable,
3883 NEW_APPS_ANIMATION_DELAY);
3884 }
Winson Chung94d67682013-09-25 16:29:40 -07003885 }
3886 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003887 } else {
3888 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003889 }
3890 }
Winson Chung64359a52013-07-08 17:17:08 -07003891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003892 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003893 }
3894
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895 /**
3896 * Implementation of the method from LauncherModel.Callbacks.
3897 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003898 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003899 Runnable r = new Runnable() {
3900 public void run() {
3901 bindFolders(folders);
3902 }
3903 };
3904 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003905 return;
3906 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003907 sFolders.clear();
3908 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003910
3911 /**
3912 * Add the views for a widget to the workspace.
3913 *
3914 * Implementation of the method from LauncherModel.Callbacks.
3915 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003916 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003917 Runnable r = new Runnable() {
3918 public void run() {
3919 bindAppWidget(item);
3920 }
3921 };
3922 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003923 return;
3924 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003925
Daniel Sandler843e8602010-06-07 14:59:01 -04003926 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3927 if (DEBUG_WIDGETS) {
3928 Log.d(TAG, "bindAppWidget: " + item);
3929 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003930 final Workspace workspace = mWorkspace;
3931
Adam Cohen59400422014-03-05 18:07:04 -08003932 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003933 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003934
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003935 if (!mIsSafeModeEnabled
3936 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3937 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003938 if (appWidgetInfo == null) {
3939 if (DEBUG_WIDGETS) {
3940 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3941 + " belongs to component " + item.providerName
3942 + ", as the povider is null");
3943 }
3944 LauncherModel.deleteItemFromDatabase(this, item);
3945 return;
3946 }
3947 // Note: This assumes that the id remap broadcast is received before this step.
3948 // If that is not the case, the id remap will be ignored and user may see the
3949 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003950 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003951 pendingInfo.spanX = item.spanX;
3952 pendingInfo.spanY = item.spanY;
3953 pendingInfo.minSpanX = item.minSpanX;
3954 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003955 Bundle options = null;
3956 // AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003957
Sunny Goyalff572272014-07-23 13:58:07 -07003958 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003959 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3960 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003961
3962 // TODO consider showing a permission dialog when the widget is clicked.
3963 if (!success) {
3964 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3965 if (DEBUG_WIDGETS) {
3966 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3967 + " belongs to component " + item.providerName
3968 + ", as the launcher is unable to bing a new widget id");
3969 }
3970 LauncherModel.deleteItemFromDatabase(this, item);
3971 return;
3972 }
3973
3974 item.appWidgetId = newWidgetId;
3975
3976 // If the widget has a configure activity, it is still needs to set it up, otherwise
3977 // the widget is ready to go.
3978 item.restoreStatus = (appWidgetInfo.configure == null)
3979 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3980 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3981
3982 LauncherModel.updateItemInDatabase(this, item);
3983 }
3984
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003985 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003986 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003987 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003988 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3989 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003990 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003991
Sunny Goyal651077b2014-06-30 14:15:31 -07003992 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3993 } else {
3994 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003995 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3996 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003997 view.updateIcon(mIconCache);
3998 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003999 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004000 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004001 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004004 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004005
Adam Cohendcd297f2013-06-18 13:13:40 -07004006 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004007 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004008 if (!item.isCustomWidget()) {
4009 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4010 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004011
Joe Onorato9c1289c2009-08-17 11:03:03 -04004012 workspace.requestLayout();
4013
Daniel Sandler843e8602010-06-07 14:59:01 -04004014 if (DEBUG_WIDGETS) {
4015 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4016 + (SystemClock.uptimeMillis()-start) + "ms");
4017 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004018 }
4019
Sunny Goyalff572272014-07-23 13:58:07 -07004020 /**
4021 * Restores a pending widget.
4022 *
4023 * @param appWidgetId The app widget id
4024 * @param cellInfo The position on screen where to create the widget.
4025 */
4026 private void completeRestoreAppWidget(final int appWidgetId) {
4027 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4028 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4029 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4030 return;
4031 }
4032
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004033 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004034 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4035
4036 mWorkspace.reinflateWidgetsIfNecessary();
4037 LauncherModel.updateItemInDatabase(this, info);
4038 }
4039
Adam Cohen1462de32012-07-24 22:34:36 -07004040 public void onPageBoundSynchronously(int page) {
4041 mSynchronouslyBoundPages.add(page);
4042 }
4043
Joe Onorato9c1289c2009-08-17 11:03:03 -04004044 /**
4045 * Callback saying that there aren't any more items to bind.
4046 *
4047 * Implementation of the method from LauncherModel.Callbacks.
4048 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004049 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004050 Runnable r = new Runnable() {
4051 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004052 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004053 }
4054 };
4055 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004056 return;
4057 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058 if (mSavedState != null) {
4059 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004060 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062 mSavedState = null;
4063 }
4064
Adam Cohen1462de32012-07-24 22:34:36 -07004065 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004067 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004068 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004069
4070 // If we received the result of any pending adds while the loader was running (e.g. the
4071 // widget configuration forced an orientation change), process them now.
4072 if (sPendingAddItem != null) {
4073 final long screenId = completeAdd(sPendingAddItem);
4074
4075 // TODO: this moves the user to the page where the pending item was added. Ideally,
4076 // the screen would be guaranteed to exist after bind, and the page would be set through
4077 // the workspace restore process.
4078 mWorkspace.post(new Runnable() {
4079 @Override
4080 public void run() {
4081 mWorkspace.snapToScreenId(screenId);
4082 }
4083 });
4084 sPendingAddItem = null;
4085 }
4086
Sunny Goyale755d462014-07-22 13:48:29 -07004087 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004088
4089 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004090 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004091 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004092 }
4093
Adam Cohen3ed316a2014-07-23 18:21:20 -07004094 private void sendLoadingCompleteBroadcastIfNecessary() {
4095 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4096 String permission =
4097 getResources().getString(R.string.receive_first_load_broadcast_permission);
4098 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4099 sendBroadcast(intent, permission);
4100 SharedPreferences.Editor editor = mSharedPrefs.edit();
4101 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4102 editor.apply();
4103 }
4104 }
4105
Winson Chunga0b7e862013-09-05 16:03:15 -07004106 public boolean isAllAppsButtonRank(int rank) {
4107 if (mHotseat != null) {
4108 return mHotseat.isAllAppsButtonRank(rank);
4109 }
4110 return false;
4111 }
4112
Winson Chunga2413752012-04-03 14:22:34 -07004113 private boolean canRunNewAppsAnimation() {
4114 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4115 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4116 }
4117
Winson Chungc9168342013-06-26 14:54:55 -07004118 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4119 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4120 PropertyValuesHolder.ofFloat("alpha", 1f),
4121 PropertyValuesHolder.ofFloat("scaleX", 1f),
4122 PropertyValuesHolder.ofFloat("scaleY", 1f));
4123 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4124 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4125 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4126 return bounceAnim;
4127 }
4128
Adam Cohen27772732013-11-11 14:00:56 +00004129 public boolean useVerticalBarLayout() {
4130 return LauncherAppState.getInstance().getDynamicGrid().
4131 getDeviceProfile().isVerticalBarLayout();
4132 }
4133
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004134 protected Rect getSearchBarBounds() {
4135 return LauncherAppState.getInstance().getDynamicGrid().
4136 getDeviceProfile().getSearchBarBounds();
4137 }
4138
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004139 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004140 if (mSearchDropTargetBar == null) {
4141 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004142 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004143 if (mQsb != null) {
4144 mSearchDropTargetBar.removeView(mQsb);
4145 mQsb = null;
4146 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004147 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004148 }
4149
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004150 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004151 * Add the icons for all apps.
4152 *
4153 * Implementation of the method from LauncherModel.Callbacks.
4154 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004155 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004156 if (mAppsView != null) {
4157 mAppsView.setApps(apps);
4158 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07004159 if (mWidgetsView != null) {
4160 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4161 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004162 }
Adam Cohen9211d422014-10-07 18:14:53 -07004163 if (mLauncherCallbacks != null) {
4164 mLauncherCallbacks.bindAllApplications(apps);
4165 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004166 }
4167
4168 /**
4169 * A package was updated.
4170 *
4171 * Implementation of the method from LauncherModel.Callbacks.
4172 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004173 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004174 Runnable r = new Runnable() {
4175 public void run() {
4176 bindAppsUpdated(apps);
4177 }
4178 };
4179 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004180 return;
4181 }
4182
Winson Chungb745afb2015-03-02 11:51:23 -08004183 if (mAppsView != null) {
4184 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004186 }
4187
Sunny Goyal4390ace2014-10-13 11:33:11 -07004188 @Override
4189 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4190 Runnable r = new Runnable() {
4191 public void run() {
4192 bindWidgetsRestored(widgets);
4193 }
4194 };
4195 if (waitUntilResume(r)) {
4196 return;
4197 }
4198 mWorkspace.widgetsRestored(widgets);
4199 }
4200
Joe Onorato9c1289c2009-08-17 11:03:03 -04004201 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004202 * Some shortcuts were updated in the background.
4203 *
4204 * Implementation of the method from LauncherModel.Callbacks.
4205 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004206 @Override
4207 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4208 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004209 Runnable r = new Runnable() {
4210 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004211 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004212 }
4213 };
4214 if (waitUntilResume(r)) {
4215 return;
4216 }
4217
Sunny Goyal4390ace2014-10-13 11:33:11 -07004218 if (!updated.isEmpty()) {
4219 mWorkspace.updateShortcuts(updated);
4220 }
4221
4222 if (!removed.isEmpty()) {
4223 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4224 for (ShortcutInfo si : removed) {
4225 removedComponents.add(si.getTargetComponent());
4226 }
4227 mWorkspace.removeItemsByComponentName(removedComponents, user);
4228 // Notify the drag controller
4229 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004230 }
4231 }
4232
4233 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004234 * Update the state of a package, typically related to install state.
4235 *
4236 * Implementation of the method from LauncherModel.Callbacks.
4237 */
Sunny Goyale755d462014-07-22 13:48:29 -07004238 @Override
4239 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004240 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004241 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004242 }
4243 }
4244
4245 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004246 * Update the label and icon of all the icons in a package
4247 *
4248 * Implementation of the method from LauncherModel.Callbacks.
4249 */
4250 @Override
4251 public void updatePackageBadge(String packageName) {
4252 if (mWorkspace != null) {
4253 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4254 }
4255 }
4256
4257 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004258 * A package was uninstalled. We take both the super set of packageNames
4259 * in addition to specific applications to remove, the reason being that
4260 * this can be called when a package is updated as well. In that scenario,
4261 * we only remove specific components from the workspace, where as
4262 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004263 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004264 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004265 * Implementation of the method from LauncherModel.Callbacks.
4266 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004267 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004268 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004269 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004270 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004271 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004272 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004273 }
Winson Chungd64d1762013-08-20 14:37:16 -07004274 };
4275 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004276 return;
4277 }
4278
Sunny Goyal1a745e82014-10-02 15:58:31 -07004279 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004280 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4281 for (AppInfo info : appInfos) {
4282 removedComponents.add(info.componentName);
4283 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004284 if (!packageNames.isEmpty()) {
4285 mWorkspace.removeItemsByPackageName(packageNames, user);
4286 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004287 if (!removedComponents.isEmpty()) {
4288 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004289 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004290 // Notify the drag controller
4291 mDragController.onAppsRemoved(packageNames, removedComponents);
4292
Sunny Goyal1a745e82014-10-02 15:58:31 -07004293 } else {
4294 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004295 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004296
Winson Chungdf95eb12013-10-16 14:57:07 -07004297 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004298 if (mAppsView != null) {
4299 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004301 }
Joe Onoratobe386092009-11-17 17:32:16 -08004302
4303 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004304 * A number of packages were updated.
4305 */
Adam Cohen091440a2015-03-18 14:16:05 -07004306 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004307 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004308 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004309 bindPackagesUpdated(mWidgetsAndShortcuts);
4310 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004311 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004312 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004313
Michael Jurkac402cd92013-05-20 15:49:32 +02004314 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4315 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4316 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004317 return;
4318 }
4319
Hyunyoung Song3f471442015-04-08 19:01:34 -07004320 if (mWidgetsView != null) {
4321 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4322 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004323 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004324 }
4325
Winson Chung400438b2011-01-16 17:53:48 -08004326 private int mapConfigurationOriActivityInfoOri(int configOri) {
4327 final Display d = getWindowManager().getDefaultDisplay();
4328 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4329 switch (d.getRotation()) {
4330 case Surface.ROTATION_0:
4331 case Surface.ROTATION_180:
4332 // We are currently in the same basic orientation as the natural orientation
4333 naturalOri = configOri;
4334 break;
4335 case Surface.ROTATION_90:
4336 case Surface.ROTATION_270:
4337 // We are currently in the other basic orientation to the natural orientation
4338 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4339 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4340 break;
4341 }
4342
4343 int[] oriMap = {
4344 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4345 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4346 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4347 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4348 };
4349 // Since the map starts at portrait, we need to offset if this device's natural orientation
4350 // is landscape.
4351 int indexOffset = 0;
4352 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4353 indexOffset = 1;
4354 }
4355 return oriMap[(d.getRotation() + indexOffset) % 4];
4356 }
Adam Cohen446e9402011-09-15 18:21:21 -07004357
Winson Chung4b919f82012-05-01 10:44:08 -07004358 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004359 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004360 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4361 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4362 .getConfiguration().orientation));
4363 } else {
4364 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4365 }
Winson Chung4b919f82012-05-01 10:44:08 -07004366 }
4367 }
4368 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004369 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004370 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004371 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004372 } else {
4373 mHandler.postDelayed(new Runnable() {
4374 public void run() {
4375 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4376 }
4377 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004378 }
Winson Chung4b919f82012-05-01 10:44:08 -07004379 }
Winson Chung400438b2011-01-16 17:53:48 -08004380 }
4381
Winson Chunge43a1e72014-01-15 10:33:02 -08004382 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004383 if (mLauncherCallbacks != null) {
4384 return mLauncherCallbacks.isLauncherPreinstalled();
4385 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004386 PackageManager pm = getPackageManager();
4387 try {
4388 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4389 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4390 return true;
4391 } else {
4392 return false;
4393 }
4394 } catch (NameNotFoundException e) {
4395 e.printStackTrace();
4396 return false;
4397 }
4398 }
4399
Adam Cohenea90f832014-05-21 15:03:34 -07004400 /**
4401 * This method indicates whether or not we should suggest default wallpaper dimensions
4402 * when our wallpaper cropper was not yet used to set a wallpaper.
4403 */
4404 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004405 if (mLauncherCallbacks != null) {
4406 return mLauncherCallbacks.overrideWallpaperDimensions();
4407 }
Adam Cohenea90f832014-05-21 15:03:34 -07004408 return true;
4409 }
4410
Adam Cohen432609a2014-03-13 17:03:22 -07004411 /**
4412 * To be overridden by subclasses to indicate that there is an activity to launch
4413 * before showing the standard launcher experience.
4414 */
4415 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004416 if (mLauncherCallbacks != null) {
4417 return mLauncherCallbacks.hasFirstRunActivity();
4418 }
Adam Cohen432609a2014-03-13 17:03:22 -07004419 return false;
4420 }
4421
4422 /**
4423 * To be overridden by subclasses to launch any first run activity
4424 */
4425 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004426 if (mLauncherCallbacks != null) {
4427 return mLauncherCallbacks.getFirstRunActivity();
4428 }
Adam Cohen432609a2014-03-13 17:03:22 -07004429 return null;
4430 }
4431
Winson Chung2826a402015-04-17 16:18:53 -07004432 /**
4433 * Returns whether the launcher callbacks overrides search in all apps.
4434 * @return
4435 */
4436 @Thunk boolean isAllAppsSearchOverridden() {
4437 if (mLauncherCallbacks != null) {
4438 return mLauncherCallbacks.overrideAllAppsSearch();
4439 }
4440 return false;
4441 }
4442
Winson Chunga6945242014-01-08 14:04:34 -08004443 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004444 return !ActivityManager.isRunningInTestHarness() &&
4445 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004446 }
4447
Adam Cohen4a9423d2014-03-28 14:22:31 -07004448 protected boolean hasRunFirstRunActivity() {
4449 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4450 }
4451
Adam Cohen432609a2014-03-13 17:03:22 -07004452 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004453 if (shouldRunFirstRunActivity() &&
4454 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004455 Intent firstRunIntent = getFirstRunActivity();
4456 if (firstRunIntent != null) {
4457 startActivity(firstRunIntent);
4458 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004459 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004460 }
4461 }
Adam Cohen432609a2014-03-13 17:03:22 -07004462 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004463 }
4464
4465 private void markFirstRunActivityShown() {
4466 SharedPreferences.Editor editor = mSharedPrefs.edit();
4467 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4468 editor.apply();
4469 }
4470
Adam Cohen432609a2014-03-13 17:03:22 -07004471 /**
4472 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4473 * screen that must be displayed and dismissed.
4474 */
4475 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004476 if (mLauncherCallbacks != null) {
4477 return mLauncherCallbacks.hasDismissableIntroScreen();
4478 }
Adam Cohen432609a2014-03-13 17:03:22 -07004479 return false;
4480 }
4481
4482 /**
4483 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4484 */
4485 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004486 if (mLauncherCallbacks != null) {
4487 return mLauncherCallbacks.getIntroScreen();
4488 }
Adam Cohen432609a2014-03-13 17:03:22 -07004489 return null;
4490 }
4491
4492 /**
4493 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4494 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4495 */
4496 private boolean shouldShowIntroScreen() {
4497 return hasDismissableIntroScreen() &&
4498 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4499 }
4500
4501 protected void showIntroScreen() {
4502 View introScreen = getIntroScreen();
4503 changeWallpaperVisiblity(false);
4504 if (introScreen != null) {
4505 mDragLayer.showOverlayView(introScreen);
4506 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004507 if (mLauncherOverlayContainer != null) {
4508 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4509 }
Adam Cohen432609a2014-03-13 17:03:22 -07004510 }
4511
4512 public void dismissIntroScreen() {
4513 markIntroScreenDismissed();
4514 if (showFirstRunActivity()) {
4515 // We delay hiding the intro view until the first run activity is showing. This
4516 // avoids a blip.
4517 mWorkspace.postDelayed(new Runnable() {
4518 @Override
4519 public void run() {
4520 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004521 if (mLauncherOverlayContainer != null) {
4522 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4523 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004524 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004525 }
4526 }, ACTIVITY_START_DELAY);
4527 } else {
4528 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004529 if (mLauncherOverlayContainer != null) {
4530 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4531 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004532 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004533 }
4534 changeWallpaperVisiblity(true);
4535 }
4536
4537 private void markIntroScreenDismissed() {
4538 SharedPreferences.Editor editor = mSharedPrefs.edit();
4539 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4540 editor.apply();
4541 }
4542
Adam Cohen091440a2015-03-18 14:16:05 -07004543 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004544 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4545 // on the device, then we always show the first run cling experience (or if there is no
4546 // launcher2). Otherwise, we prompt the user upon started for migration
4547 LauncherClings launcherClings = new LauncherClings(this);
4548 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4549 if (mModel.canMigrateFromOldLauncherDb(this)) {
4550 launcherClings.showMigrationCling();
4551 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004552 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004553 }
4554 }
4555 }
4556
Winson Chunga6945242014-01-08 14:04:34 -08004557 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004558 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4559 if (mHotseat != null) mHotseat.setAlpha(1f);
4560 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4561 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004562 }
4563
4564 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004565 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4566 if (mHotseat != null) mHotseat.setAlpha(0f);
4567 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4568 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004569 }
4570
Mathew Inwood72fbec12013-11-19 15:45:07 +00004571 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004572 // Called from search suggestion, not supported in other profiles.
4573 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4574 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4575 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4576 myUser);
4577 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004578 return null;
4579 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004580 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004581 }
4582
4583 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4584 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004585 // Called from search suggestion, not supported in other profiles.
4586 return createShortcutDragInfo(shortcutIntent, caption, icon,
4587 UserHandleCompat.myUserHandle());
4588 }
4589
4590 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4591 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004592 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004593 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004594 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004595 }
4596
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004597 protected void moveWorkspaceToDefaultScreen() {
4598 mWorkspace.moveToDefaultScreen(false);
4599 }
4600
Mathew Inwood72fbec12013-11-19 15:45:07 +00004601 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4602 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004603 mWorkspace.onExternalDragStartedWithItem(dragView);
4604 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004605 }
4606
Anjali Koppalf5d29132014-02-28 13:33:27 -08004607 @Override
4608 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004609 if (mLauncherCallbacks != null) {
4610 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4611 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004612 }
4613
Winson Chung80baf5a2010-08-09 16:03:15 -07004614 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004615 * Prints out out state for debugging.
4616 */
4617 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004618 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004619 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004620 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4621 Log.d(TAG, "mRestoring=" + mRestoring);
4622 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4623 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004624 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004625 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004626 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004627
Daniel Sandler325dc232013-06-05 22:57:57 -04004628 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004629 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004630
4631 @Override
4632 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4633 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004634 synchronized (sDumpLogs) {
4635 writer.println(" ");
4636 writer.println("Debug logs: ");
4637 for (int i = 0; i < sDumpLogs.size(); i++) {
4638 writer.println(" " + sDumpLogs.get(i));
4639 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004640 }
Adam Cohen9211d422014-10-07 18:14:53 -07004641 if (mLauncherCallbacks != null) {
4642 mLauncherCallbacks.dump(prefix, fd, writer, args);
4643 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004644 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004645
4646 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004647 if (DEBUG_DUMP_LOG) {
4648 synchronized (sDumpLogs) {
4649 Log.d(TAG, "");
4650 Log.d(TAG, "*********************");
4651 Log.d(TAG, "Launcher debug logs: ");
4652 for (int i = 0; i < sDumpLogs.size(); i++) {
4653 Log.d(TAG, " " + sDumpLogs.get(i));
4654 }
4655 Log.d(TAG, "*********************");
4656 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004657 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004658 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004659 }
4660
4661 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004662 addDumpLog(tag, log, null, debugLog);
4663 }
4664
4665 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004666 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004667 if (e != null) {
4668 Log.d(tag, log, e);
4669 } else {
4670 Log.d(tag, log);
4671 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004672 }
Winson Chungede41292013-09-19 16:27:36 -07004673 if (DEBUG_DUMP_LOG) {
4674 sDateStamp.setTime(System.currentTimeMillis());
4675 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004676 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4677 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004678 }
Winson Chungede41292013-09-19 16:27:36 -07004679 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004680 }
4681
Adam Cohen59400422014-03-05 18:07:04 -08004682 public static CustomAppWidget getCustomAppWidget(String name) {
4683 return sCustomAppWidgets.get(name);
4684 }
4685
4686 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4687 return sCustomAppWidgets;
4688 }
4689
Winson Chungede41292013-09-19 16:27:36 -07004690 public void dumpLogsToLocalData() {
4691 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004692 new AsyncTask<Void, Void, Void>() {
4693 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004694 boolean success = false;
4695 sDateStamp.setTime(sRunStart);
4696 String FILENAME = sDateStamp.getMonth() + "-"
4697 + sDateStamp.getDay() + "_"
4698 + sDateStamp.getHours() + "-"
4699 + sDateStamp.getMinutes() + "_"
4700 + sDateStamp.getSeconds() + ".txt";
4701
4702 FileOutputStream fos = null;
4703 File outFile = null;
4704 try {
4705 outFile = new File(getFilesDir(), FILENAME);
4706 outFile.createNewFile();
4707 fos = new FileOutputStream(outFile);
4708 } catch (Exception e) {
4709 e.printStackTrace();
4710 }
4711 if (fos != null) {
4712 PrintWriter writer = new PrintWriter(fos);
4713
4714 writer.println(" ");
4715 writer.println("Debug logs: ");
4716 synchronized (sDumpLogs) {
4717 for (int i = 0; i < sDumpLogs.size(); i++) {
4718 writer.println(" " + sDumpLogs.get(i));
4719 }
4720 }
4721 writer.close();
4722 }
4723 try {
4724 if (fos != null) {
4725 fos.close();
4726 success = true;
4727 }
4728 } catch (IOException e) {
4729 e.printStackTrace();
4730 }
Michael Jurka43467462013-11-14 12:03:58 +01004731 return null;
Winson Chungede41292013-09-19 16:27:36 -07004732 }
Michael Jurka43467462013-11-14 12:03:58 +01004733 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004734 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004736}
Michael Jurka2763be32011-02-24 11:19:57 -08004737
Michael Jurkaabded662011-03-04 12:06:57 -08004738interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004739 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004740 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004741 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004742 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004743 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004744}
Dan Sandlerd5024042014-01-09 15:01:33 -05004745
4746interface DebugIntents {
4747 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4748 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004749}