blob: 9ad87c3a640831bb73678997daea23e38dc7f49e [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 Sandler6053b802013-08-15 15:44:26 -0700207 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700208
Sunny Goyal594d76d2014-11-06 10:12:54 -0800209 private static final String QSB_WIDGET_ID = "qsb_widget_id";
210 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
211
Winson Chunga6945242014-01-08 14:04:34 -0800212 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
213
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700214 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800215 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk State mState = State.WORKSPACE;
217 @Thunk AnimatorSet mStateAnimation;
218 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700219
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700220 private boolean mIsSafeModeEnabled;
221
Adam Cohenc2d6e892014-10-16 09:49:24 -0700222 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
223 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700224 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700227 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
228 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700229 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000231 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
232 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
233
Winson Chunga2413752012-04-03 14:22:34 -0700234 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700235 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
236 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700237 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700238
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800239 private final BroadcastReceiver mCloseSystemDialogsReceiver
240 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800241 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private LayoutInflater mInflater;
244
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700246 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800247 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700248 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800249 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700250 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700251
Sunny Goyalffe83f12014-08-14 17:39:34 -0700252 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700253 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700254
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800256 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800257 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700258
Michael Jurka0280c3b2010-09-17 15:00:07 -0700259 private int[] mTmpAddItemCellCoordinates = new int[2];
260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private FolderInfo mFolderInfo;
262
Winson Chung3d503fb2011-07-13 17:25:49 -0700263 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800264 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700265
Michael Jurka838a4ca2011-02-07 13:33:06 -0800266 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700267
Winson Chungc51db6a2011-10-05 11:44:49 -0700268 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700269
270 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700271 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700272
273 // Main container view for the widget tray screen.
274 private WidgetsContainerView mWidgetsView;
275
Winson Chungf0ea4d32011-06-06 14:27:16 -0700276 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800277 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700280 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
281 // scroll issues (because the workspace may not have been measured yet) and extra work.
282 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
283 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
285 private SpannableStringBuilder mDefaultKeySsb = null;
286
Adam Cohen091440a2015-03-18 14:16:05 -0700287 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800289 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private boolean mRestoring;
291 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700292 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293
Michael Jurka1e2f4652013-07-08 18:03:46 -0700294 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700295 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
296
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private Bundle mSavedInstanceState;
298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800300 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700301 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800302 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700303 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305
Adam Cohen091440a2015-03-18 14:16:05 -0700306 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700307
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700308 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700309
Adam Cohen61f560d2013-09-30 15:58:20 -0700310 private View.OnTouchListener mHapticFeedbackTouchListener;
311
Adam Cohended9f8d2010-11-03 13:25:16 -0700312 // Related to the auto-advancing of widgets
313 private final int ADVANCE_MSG = 1;
314 private final int mAdvanceInterval = 20000;
315 private final int mAdvanceStagger = 250;
316 private long mAutoAdvanceSentTime;
317 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700318 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700319 new HashMap<View, AppWidgetProviderInfo>();
320
Winson Chung400438b2011-01-16 17:53:48 -0800321 // Determines how long to wait after a rotation before restoring the screen orientation to
322 // match the sensor state.
323 private final int mRestoreScreenOrientationDelay = 500;
324
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700326
Adam Cohen1462de32012-07-24 22:34:36 -0700327 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700328 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700329
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700330 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700331 static Date sDateStamp = new Date();
332 static DateFormat sDateFormat =
333 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
334 static long sRunStart = System.currentTimeMillis();
335 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700336
Winson Chung46353de2012-02-16 14:05:10 -0800337 // We only want to get the SharedPreferences once since it does an FS stat each time we get
338 // it from the context.
339 private SharedPreferences mSharedPrefs;
340
Winson Chungf0c6ae02012-03-21 16:10:31 -0700341 // Holds the page that we need to animate to, and the icon views that we need to animate up
342 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700343 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700344 private Bitmap mFolderIconBitmap;
345 private Canvas mFolderIconCanvas;
346 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700347
Michael Jurkaddd62e92011-02-16 17:49:14 -0800348 private BubbleTextView mWaitingForResume;
349
Adam Cohen59400422014-03-05 18:07:04 -0800350 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
351 new HashMap<String, CustomAppWidget>();
352
353 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
354 static {
355 if (ENABLE_CUSTOM_WIDGET_TEST) {
356 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
357 }
358 }
359
Chet Haasea8f996d2015-02-17 12:56:04 -0800360 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
361 private static Method sClipRevealMethod = null;
362 static {
363 Class<?> activityOptionsClass = ActivityOptions.class;
364 try {
365 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
366 View.class, int.class, int.class, int.class, int.class);
367 } catch (Exception e) {
368 // Earlier version
369 }
370 }
371
Adam Cohen091440a2015-03-18 14:16:05 -0700372 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700373 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700374 if (mWorkspace != null) {
375 mWorkspace.buildPageHardwareLayers();
376 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700377 }
378 };
379
Adam Cohendb364c32014-05-20 14:23:40 -0700380 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800381
Adam Cohen091440a2015-03-18 14:16:05 -0700382 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383 int requestCode;
384 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700385 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700386 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800387 int cellX;
388 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700389 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800390 }
391
Daniel Sandlerff02d492013-08-05 02:12:05 -0400392 private Stats mStats;
393
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700394 FocusIndicatorView mFocusHandler;
395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 @Override
397 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700398 if (DEBUG_STRICT_MODE) {
399 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
400 .detectDiskReads()
401 .detectDiskWrites()
402 .detectNetwork() // or .detectAll() for all detectable problems
403 .penaltyLog()
404 .build());
405 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
406 .detectLeakedSqlLiteObjects()
407 .detectLeakedClosableObjects()
408 .penaltyLog()
409 .penaltyDeath()
410 .build());
411 }
412
Adam Cohen9211d422014-10-07 18:14:53 -0700413 if (mLauncherCallbacks != null) {
414 mLauncherCallbacks.preOnCreate();
415 }
416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400418
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400419 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400420 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700421 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700422
Winson Chung5f8afe62013-08-12 16:19:28 -0700423 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700424 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700425
426 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400427 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700428 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700429 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800430 mModel = app.setLauncher(this);
431 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700432 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400433 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800435 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Daniel Sandlerff02d492013-08-05 02:12:05 -0400437 mStats = new Stats(this);
438
Sunny Goyalffe83f12014-08-14 17:39:34 -0700439 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700440
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700441 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
442 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700443
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700444 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
445 // this also ensures that any synchronous binding below doesn't re-trigger another
446 // LauncherModel load.
447 mPaused = false;
448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200450 android.os.Debug.startMethodTracing(
451 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 }
453
454 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100458 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800460 registerContentObservers();
461
Joe Onorato7c312c12009-08-13 21:36:53 -0700462 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 mSavedState = savedInstanceState;
465 restoreState(mSavedState);
466
467 if (PROFILE_STARTUP) {
468 android.os.Debug.stopMethodTracing();
469 }
470
471 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700472 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 // If the user leaves launcher, then we should just load items asynchronously when
474 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500475 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 } else {
477 // We only load the page synchronously if the user rotates (or triggers a
478 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800479 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
482
483 // For handling default keys
484 mDefaultKeySsb = new SpannableStringBuilder();
485 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800486
487 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
488 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800489
Adam Cohenf9426d52012-06-04 17:26:21 -0700490 // On large interfaces, we want the screen to auto-rotate based on the current orientation
491 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700492
Adam Cohen9211d422014-10-07 18:14:53 -0700493 if (mLauncherCallbacks != null) {
494 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700495 if (mLauncherCallbacks.hasLauncherOverlay()) {
496 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700497 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
498 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
499 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700500 mWorkspace.setLauncherOverlay(mLauncherOverlay);
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700503
504 if (shouldShowIntroScreen()) {
505 showIntroScreen();
506 } else {
507 showFirstRunActivity();
508 showFirstRunClings();
509 }
Adam Cohen9211d422014-10-07 18:14:53 -0700510 }
511
512 private LauncherCallbacks mLauncherCallbacks;
513
514 public void onPostCreate(Bundle savedInstanceState) {
515 super.onPostCreate(savedInstanceState);
516 if (mLauncherCallbacks != null) {
517 mLauncherCallbacks.onPostCreate(savedInstanceState);
518 }
519 }
520
521 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
522 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700523 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
524 @Override
525 public void onAllAppsBoundsChanged(Rect bounds) {
526 mAppsView.setFixedBounds(Launcher.this, bounds);
527 }
528
529 @Override
530 public void dismissAllApps() {
531 showWorkspace(true);
532 }
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()) {
788 mWorkspace.exitOverviewMode(false);
789 }
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 Chungb745afb2015-03-02 11:51:23 -08001006 if (mOnResumeState == State.WORKSPACE || mOnResumeState == State.NONE) {
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 Cohenbffe7452013-07-22 18:21:45 -07001223
Adam Cohen9211d422014-10-07 18:14:53 -07001224 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001225 CustomContentCallbacks callbacks, String description) {
1226 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001227 }
1228
Adam Cohenedb40762013-07-18 16:45:45 -07001229 // The custom content needs to offset its content to account for the QSB
1230 public int getTopOffsetForCustomContent() {
1231 return mWorkspace.getPaddingTop();
1232 }
1233
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001234 @Override
1235 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001236 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001237 if (mModel.isCurrentCallbacks(this)) {
1238 mModel.stopLoader();
1239 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001240 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1241
Romain Guy13c2e7b2010-03-10 19:45:00 -08001242 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001243 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001244
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001245 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001246 @Override
1247 public void onWindowFocusChanged(boolean hasFocus) {
1248 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001249 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001250
1251 if (mLauncherCallbacks != null) {
1252 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1253 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001254 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 private boolean acceptFilter() {
1257 final InputMethodManager inputManager = (InputMethodManager)
1258 getSystemService(Context.INPUT_METHOD_SERVICE);
1259 return !inputManager.isFullscreenMode();
1260 }
1261
1262 @Override
1263 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001264 final int uniChar = event.getUnicodeChar();
1265 final boolean handled = super.onKeyDown(keyCode, event);
1266 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1267 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1269 keyCode, event);
1270 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001271 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001272 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001273 // showSearchDialog()
1274 // If there are multiple keystrokes before the search dialog takes focus,
1275 // onSearchRequested() will be called for every keystroke,
1276 // but it is idempotent, so it's fine.
1277 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
1279 }
1280
Joe Onorato8a9625e2010-01-28 15:55:35 -08001281 // Eat the long press event so the keyboard doesn't come up.
1282 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1283 return true;
1284 }
1285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 return handled;
1287 }
1288
Karl Rosaen138a0412009-04-23 19:00:21 -07001289 private String getTypedText() {
1290 return mDefaultKeySsb.toString();
1291 }
1292
1293 private void clearTypedText() {
1294 mDefaultKeySsb.clear();
1295 mDefaultKeySsb.clearSpans();
1296 Selection.setSelection(mDefaultKeySsb, 0);
1297 }
1298
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001300 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1301 * State
1302 */
1303 private static State intToState(int stateOrdinal) {
1304 State state = State.WORKSPACE;
1305 final State[] stateValues = State.values();
1306 for (int i = 0; i < stateValues.length; i++) {
1307 if (stateValues[i].ordinal() == stateOrdinal) {
1308 state = stateValues[i];
1309 break;
1310 }
1311 }
1312 return state;
1313 }
1314
1315 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 * Restores the previous state, if it exists.
1317 *
1318 * @param savedState The previous state.
1319 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001320 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 private void restoreState(Bundle savedState) {
1322 if (savedState == null) {
1323 return;
1324 }
1325
Michael Jurka883f55b2010-10-21 15:47:14 -07001326 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001327 if (state == State.APPS || state == State.WIDGETS) {
1328 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001329 }
1330
Adam Cohen21cd0022013-10-09 18:57:02 -07001331 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1332 PagedView.INVALID_RESTORE_PAGE);
1333 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001334 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 }
1336
Winson Chung3d503fb2011-07-13 17:25:49 -07001337 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001338 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001339
Winson Chung3d503fb2011-07-13 17:25:49 -07001340 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1341 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001342 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001343 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1344 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001345 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1346 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001347 AppWidgetProviderInfo info = savedState.getParcelable(
1348 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1349 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001350 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001351 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 mRestoring = true;
1353 }
1354
1355 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1356 if (renameFolder) {
1357 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001358 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 mRestoring = true;
1360 }
Winson Chunga12a2502010-12-20 14:41:35 -08001361
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001362 mItemIdToViewId = (HashMap<Integer, Integer>)
1363 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 }
1365
1366 /**
1367 * Finds all the views we need and configure them properly.
1368 */
1369 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001370 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001371
Craig Mautner360310b2012-10-26 15:13:08 -07001372 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001373 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001374 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1375 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001376 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001377 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001378
John Spurlock77e1f472013-09-11 10:09:51 -04001379 mLauncherView.setSystemUiVisibility(
1380 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001381 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382
Winson Chung4c98d922011-05-31 16:50:48 -07001383 // Setup the drag layer
1384 mDragLayer.setup(this, dragController);
1385
Winson Chung3d503fb2011-07-13 17:25:49 -07001386 // Setup the hotseat
1387 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1388 if (mHotseat != null) {
1389 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001390 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001391 }
1392
Jorim Jaggid017f882014-01-14 17:08:48 -08001393 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001394 View widgetButton = findViewById(R.id.widget_button);
1395 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001396 @Override
1397 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001398 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001399 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001400 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001401 }
1402 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001403 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1404
1405 View wallpaperButton = findViewById(R.id.wallpaper_button);
1406 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001407 @Override
1408 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001409 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001410 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001411 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001412 }
1413 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001414 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1415
1416 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001417 if (hasSettings()) {
1418 settingsButton.setOnClickListener(new OnClickListener() {
1419 @Override
1420 public void onClick(View arg0) {
1421 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001422 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001423 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001424 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001425 });
1426 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427 } else {
1428 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001429 }
1430
Adam Cohendbdff6b2013-09-18 19:09:15 -07001431 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001432
Winson Chung4c98d922011-05-31 16:50:48 -07001433 // Setup the workspace
1434 mWorkspace.setHapticFeedbackEnabled(false);
1435 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001436 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001437 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001438
Winson Chungf0ea4d32011-06-06 14:27:16 -07001439 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001440 mSearchDropTargetBar = (SearchDropTargetBar)
1441 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001442
Winson Chungb745afb2015-03-02 11:51:23 -08001443 // Setup Apps
1444 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung0f785722015-04-08 10:27:49 -07001445 if (mLauncherCallbacks != null && mLauncherCallbacks.overrideAllAppsSearch()) {
1446 mAppsView.hideSearchBar();
1447 }
Winson Chungb745afb2015-03-02 11:51:23 -08001448
Winson Chungf0ea4d32011-06-06 14:27:16 -07001449 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001450 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001451
Winson Chung3d503fb2011-07-13 17:25:49 -07001452 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001453 dragController.setDragScoller(mWorkspace);
1454 dragController.setScrollView(mDragLayer);
1455 dragController.setMoveTarget(mWorkspace);
1456 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001457 if (mSearchDropTargetBar != null) {
1458 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001459 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001460 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001461
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001462 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001463 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001464 mWeightWatcher = new WeightWatcher(this);
1465 mWeightWatcher.setAlpha(0.5f);
1466 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001467 new FrameLayout.LayoutParams(
1468 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001469 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001470 Gravity.BOTTOM)
1471 );
Adam Cohen39a06042013-07-19 14:30:12 -07001472
1473 boolean show = shouldShowWeightWatcher();
1474 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477
1478 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001479 * Sets the all apps button. This method is called from {@link Hotseat}.
1480 */
1481 public void setAllAppsButton(View allAppsButton) {
1482 mAllAppsButton = allAppsButton;
1483 }
1484
1485 public View getAllAppsButton() {
1486 return mAllAppsButton;
1487 }
1488
1489 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 * Creates a view representing a shortcut.
1491 *
1492 * @param info The data structure describing the shortcut.
1493 *
1494 * @return A View inflated from R.layout.application.
1495 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001496 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001498 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
1500
1501 /**
1502 * Creates a view representing a shortcut inflated from the specified resource.
1503 *
1504 * @param layoutResId The id of the XML layout used to create the shortcut.
1505 * @param parent The group the shortcut belongs to.
1506 * @param info The data structure describing the shortcut.
1507 *
1508 * @return A View inflated from layoutResId.
1509 */
Adam Cohen59400422014-03-05 18:07:04 -08001510 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001511 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001512 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001514 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 return favorite;
1516 }
1517
1518 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 * Add a shortcut to the workspace.
1520 *
1521 * @param data The intent describing the shortcut.
1522 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001525 int cellY) {
1526 int[] cellXY = mTmpAddItemCellCoordinates;
1527 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001528 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001529
Michael Jurkad3ef3062010-11-23 16:23:58 -08001530 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001531
Sunny Goyal2350bc92014-10-14 16:42:54 -07001532 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001533 if (info == null) {
1534 return;
1535 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001536 final View view = createShortcut(info);
1537
Adam Cohenfbba09b2011-07-18 21:43:05 -07001538 // First we check if we already know the exact location where we want to add this item.
1539 if (cellX >= 0 && cellY >= 0) {
1540 cellXY[0] = cellX;
1541 cellXY[1] = cellY;
1542 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001543
1544 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001545 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001546 true, null,null)) {
1547 return;
1548 }
1549 DragObject dragObject = new DragObject();
1550 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001551 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1552 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001553 return;
1554 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001555 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001556 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001557 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001558 foundCellSpan = (result != null);
1559 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001560 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001561 }
1562
1563 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001564 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001565 return;
1566 }
1567
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001568 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569
1570 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001571 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001572 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574 }
1575
Adam Cohen2f093b62012-04-30 18:59:53 -07001576 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1577 int minHeight) {
1578 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001579 // We want to account for the extra amount of padding that we are adding to the widget
1580 // to ensure that it gets the full amount of space that it has requested
1581 int requiredWidth = minWidth + padding.left + padding.right;
1582 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001583 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001584 }
1585
Adam Cohen2f093b62012-04-30 18:59:53 -07001586 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1587 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001588 }
1589
Adam Cohen2f093b62012-04-30 18:59:53 -07001590 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1591 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001592 }
1593
Adam Cohen2f093b62012-04-30 18:59:53 -07001594 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1595 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001596 }
1597
Adam Cohen2f093b62012-04-30 18:59:53 -07001598 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1599 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001600 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001601 }
1602
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001604 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001606 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 */
Adam Cohen091440a2015-03-18 14:16:05 -07001608 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001609 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1610
1611 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001612 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001613 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1614 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001615 }
Romain Guycbb89e42009-06-08 15:52:54 -07001616
Adam Cohen59400422014-03-05 18:07:04 -08001617 if (appWidgetInfo.isCustomWidget) {
1618 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001619 }
1620
Adam Cohen59400422014-03-05 18:07:04 -08001621 LauncherAppWidgetInfo launcherInfo;
1622 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1623 launcherInfo.spanX = info.spanX;
1624 launcherInfo.spanY = info.spanY;
1625 launcherInfo.minSpanX = info.minSpanX;
1626 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001627 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001630 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631
1632 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001633 if (hostView == null) {
1634 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001635 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1636 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001637 } else {
1638 // The AppWidgetHostView has already been inflated and instantiated
1639 launcherInfo.hostView = hostView;
1640 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001642 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001643 launcherInfo.notifyWidgetSizeChanged(this);
1644
Adam Cohen59400422014-03-05 18:07:04 -08001645 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1646 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001647
1648 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001650 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 }
Romain Guycbb89e42009-06-08 15:52:54 -07001652
Adam Cohended9f8d2010-11-03 13:25:16 -07001653 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1654 @Override
1655 public void onReceive(Context context, Intent intent) {
1656 final String action = intent.getAction();
1657 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1658 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001659 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001660 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001661
Winson Chungc100e8e2011-08-09 16:02:43 -07001662 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001663 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001664 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001665 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001666 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001667 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001668 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1669 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001670 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001671 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1672 mModel.resetLoadedState(false, true);
1673 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1674 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1675 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1676 mModel.resetLoadedState(false, true);
1677 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1678 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1679 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001680 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1681 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1682 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001683 }
1684 }
1685 };
1686
1687 @Override
1688 public void onAttachedToWindow() {
1689 super.onAttachedToWindow();
1690
1691 // Listen for broadcasts related to user-presence
1692 final IntentFilter filter = new IntentFilter();
1693 filter.addAction(Intent.ACTION_SCREEN_OFF);
1694 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001695 // For handling managed profiles
1696 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1697 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001698 if (ENABLE_DEBUG_INTENTS) {
1699 filter.addAction(DebugIntents.DELETE_DATABASE);
1700 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1701 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001703 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001704 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001705 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 mVisible = true;
1707 }
1708
Adam Cohen0b395352014-06-09 22:54:36 +00001709 /**
1710 * Sets up transparent navigation and status bars in LMP.
1711 * This method is a no-op for other platform versions.
1712 */
Sunny Goyald0091012015-01-20 15:55:34 -08001713 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001714 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001715 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001716 Window window = getWindow();
1717 window.getAttributes().systemUiVisibility |=
1718 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1719 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1720 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1721 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1722 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1723 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1724 window.setStatusBarColor(Color.TRANSPARENT);
1725 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001726 }
1727 }
1728
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 @Override
1730 public void onDetachedFromWindow() {
1731 super.onDetachedFromWindow();
1732 mVisible = false;
1733
Adam Cohend113e0c2010-11-11 10:48:05 -08001734 if (mAttached) {
1735 unregisterReceiver(mReceiver);
1736 mAttached = false;
1737 }
Winson Chungb745afb2015-03-02 11:51:23 -08001738 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 }
1740
1741 public void onWindowVisibilityChanged(int visibility) {
1742 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001743 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001744 // The following code used to be in onResume, but it turns out onResume is called when
1745 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1746 // is a more appropriate event to handle
1747 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001748 if (!mWorkspaceLoading) {
1749 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001750 // We want to let Launcher draw itself at least once before we force it to build
1751 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001752 // apps is nice and speedy.
1753 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001754 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001755 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001756 if (mStarted) return;
1757 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001758 // We delay the layer building a bit in order to give
1759 // other message processing a time to run. In particular
1760 // this avoids a delay in hiding the IME if it was
1761 // currently shown, because doing that may involve
1762 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001763 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001764 final ViewTreeObserver.OnDrawListener listener = this;
1765 mWorkspace.post(new Runnable() {
1766 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001767 if (mWorkspace != null &&
1768 mWorkspace.getViewTreeObserver() != null) {
1769 mWorkspace.getViewTreeObserver().
1770 removeOnDrawListener(listener);
1771 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001772 }
1773 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001774 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001775 }
1776 });
1777 }
1778 clearTypedText();
1779 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 }
1781
Adam Cohen091440a2015-03-18 14:16:05 -07001782 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001783 mHandler.removeMessages(ADVANCE_MSG);
1784 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1785 mHandler.sendMessageDelayed(msg, delay);
1786 mAutoAdvanceSentTime = System.currentTimeMillis();
1787 }
1788
Adam Cohen091440a2015-03-18 14:16:05 -07001789 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001790 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1791 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1792 mAutoAdvanceRunning = autoAdvanceRunning;
1793 if (autoAdvanceRunning) {
1794 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1795 sendAdvanceMessage(delay);
1796 } else {
1797 if (!mWidgetsToAdvance.isEmpty()) {
1798 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1799 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1800 }
1801 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001802 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001803 }
1804 }
1805 }
1806
1807 private final Handler mHandler = new Handler() {
1808 @Override
1809 public void handleMessage(Message msg) {
1810 if (msg.what == ADVANCE_MSG) {
1811 int i = 0;
1812 for (View key: mWidgetsToAdvance.keySet()) {
1813 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1814 final int delay = mAdvanceStagger * i;
1815 if (v instanceof Advanceable) {
1816 postDelayed(new Runnable() {
1817 public void run() {
1818 ((Advanceable) v).advance();
1819 }
1820 }, delay);
1821 }
1822 i++;
1823 }
1824 sendAdvanceMessage(mAdvanceInterval);
1825 }
1826 }
1827 };
1828
1829 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001830 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001831 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1832 if (v instanceof Advanceable) {
1833 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001834 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001835 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001836 }
1837 }
1838
1839 void removeWidgetToAutoAdvance(View hostView) {
1840 if (mWidgetsToAdvance.containsKey(hostView)) {
1841 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001842 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001844 }
1845
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001847 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 launcherInfo.hostView = null;
1849 }
1850
Hyunyoung Song3f471442015-04-08 19:01:34 -07001851 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001852 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1853 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001854 }
1855
Winson Chunga6945242014-01-08 14:04:34 -08001856 public DragLayer getDragLayer() {
1857 return mDragLayer;
1858 }
1859
Winson Chungb745afb2015-03-02 11:51:23 -08001860 public AppsContainerView getAppsView() {
1861 return mAppsView;
1862 }
1863
Hyunyoung Song3f471442015-04-08 19:01:34 -07001864 public WidgetsContainerView getWidgetsView() {
1865 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001866 }
1867
Winson Chunga6945242014-01-08 14:04:34 -08001868 public Workspace getWorkspace() {
1869 return mWorkspace;
1870 }
1871
1872 public Hotseat getHotseat() {
1873 return mHotseat;
1874 }
1875
Jorim Jaggid017f882014-01-14 17:08:48 -08001876 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001877 return mOverviewPanel;
1878 }
1879
1880 public SearchDropTargetBar getSearchBar() {
1881 return mSearchDropTargetBar;
1882 }
1883
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001884 public LauncherAppWidgetHost getAppWidgetHost() {
1885 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 }
Romain Guycbb89e42009-06-08 15:52:54 -07001887
Winson Chunga9abd0e2010-10-27 17:18:37 -07001888 public LauncherModel getModel() {
1889 return mModel;
1890 }
1891
Winson Chunga6945242014-01-08 14:04:34 -08001892 protected SharedPreferences getSharedPrefs() {
1893 return mSharedPrefs;
1894 }
1895
Bjorn Bringertc459e522013-06-07 19:36:01 +01001896 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001897 getWindow().closeAllPanels();
1898
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001899 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001900 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001901 }
1902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 @Override
1904 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001905 long startTime = 0;
1906 if (DEBUG_RESUME_TIME) {
1907 startTime = System.currentTimeMillis();
1908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 super.onNewIntent(intent);
1910
1911 // Close the menu
1912 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001913 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001914 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001915
Adam Cohened307df2013-10-02 09:37:31 -07001916 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1917 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1918 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001919
Adam Cohen6fecd412013-10-02 17:41:50 -07001920 if (mWorkspace == null) {
1921 // Can be cases where mWorkspace is null, this prevents a NPE
1922 return;
1923 }
1924 Folder openFolder = mWorkspace.getOpenFolder();
1925 // In all these cases, only animate if we're already on home
1926 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001927
1928 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1929 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001930 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001931 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001933 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001934
Adam Cohen6fecd412013-10-02 17:41:50 -07001935 closeFolder();
1936 exitSpringLoadedDragMode();
1937
1938 // If we are already on home, then just animate back to the workspace,
1939 // otherwise, just wait until onResume to set the state back to Workspace
1940 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001941 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001942 } else {
1943 mOnResumeState = State.WORKSPACE;
1944 }
1945
1946 final View v = getWindow().peekDecorView();
1947 if (v != null && v.getWindowToken() != null) {
1948 InputMethodManager imm = (InputMethodManager)getSystemService(
1949 INPUT_METHOD_SERVICE);
1950 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1951 }
1952
Winson Chungb745afb2015-03-02 11:51:23 -08001953 // Reset the apps view
1954 if (!alreadyOnHome && mAppsView != null) {
1955 mAppsView.scrollToTop();
1956 }
1957
Hyunyoung Song3f471442015-04-08 19:01:34 -07001958 // Reset the widgets view
1959 if (!alreadyOnHome && mWidgetsView != null) {
1960 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001961 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001962
Adam Cohen9211d422014-10-07 18:14:53 -07001963 if (mLauncherCallbacks != null) {
1964 mLauncherCallbacks.onHomeIntent();
1965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
Adam Cohened307df2013-10-02 09:37:31 -07001967
Michael Jurka447bf842013-05-15 14:52:15 +02001968 if (DEBUG_RESUME_TIME) {
1969 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971
Adam Cohen9211d422014-10-07 18:14:53 -07001972 if (mLauncherCallbacks != null) {
1973 mLauncherCallbacks.onNewIntent(intent);
1974 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001975 }
1976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001978 public void onRestoreInstanceState(Bundle state) {
1979 super.onRestoreInstanceState(state);
1980 for (int page: mSynchronouslyBoundPages) {
1981 mWorkspace.restoreInstanceStateForChild(page);
1982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
1984
1985 @Override
1986 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001987 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001988 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1989 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001990 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001991 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
Michael Jurka883f55b2010-10-21 15:47:14 -07001993 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001994 // We close any open folder since it will not be re-opened, and we need to make sure
1995 // this state is reflected.
1996 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997
Adam Cohendcd297f2013-06-18 13:13:40 -07001998 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001999 mWaitingForResult) {
2000 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002001 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002002 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2003 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002004 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2005 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2006 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002007 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
2009
2010 if (mFolderInfo != null && mWaitingForResult) {
2011 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2012 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2013 }
Michael Jurka946ad472010-07-09 18:05:18 -07002014
Hyunyoung Song3f471442015-04-08 19:01:34 -07002015 // Save the current widgets tray?
2016 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002017 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002018
2019 if (mLauncherCallbacks != null) {
2020 mLauncherCallbacks.onSaveInstanceState(outState);
2021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023
2024 @Override
2025 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002027
Winson Chunge7a03942011-08-05 15:05:12 -07002028 // Remove all pending runnables
2029 mHandler.removeMessages(ADVANCE_MSG);
2030 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002031 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002032
Winson Chungcd2b0142011-06-08 16:02:26 -07002033 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002034 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002035
2036 // It's possible to receive onDestroy after a new Launcher activity has
2037 // been created. In this case, don't interfere with the new Launcher.
2038 if (mModel.isCurrentCallbacks(this)) {
2039 mModel.stopLoader();
2040 app.setLauncher(null);
2041 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002044 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002046 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002048 mAppWidgetHost = null;
2049
2050 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051
2052 TextKeyListener.getInstance().release();
2053
Winson Chung81b52252012-08-27 15:34:29 -07002054 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2055 // to prevent leaking Launcher activities on orientation change.
2056 if (mModel != null) {
2057 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2058 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002059
2060 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002061 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002062
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002063 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002064 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002065 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002066 mWorkspace = null;
2067 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002068
Michael Jurka2ecf9952012-06-18 12:52:28 -07002069 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002070
2071 if (mLauncherCallbacks != null) {
2072 mLauncherCallbacks.onDestroy();
2073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
Adam Cohena9cf38f2011-05-02 15:36:58 -07002076 public DragController getDragController() {
2077 return mDragController;
2078 }
2079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 @Override
2081 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002082 onStartForResult(requestCode);
2083 super.startActivityForResult(intent, requestCode);
2084 }
2085
2086 @Override
2087 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2088 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2089 onStartForResult(requestCode);
2090 try {
2091 super.startIntentSenderForResult(intent, requestCode,
2092 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2093 } catch (IntentSender.SendIntentException e) {
2094 throw new ActivityNotFoundException();
2095 }
2096 }
2097
2098 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002099 if (requestCode >= 0) {
2100 setWaitingForResult(true);
2101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 }
2103
Winson Chung70d72102011-08-12 11:24:35 -07002104 /**
2105 * Indicates that we want global search for this activity by setting the globalSearch
2106 * argument for {@link #startSearch} to true.
2107 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002109 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002111
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002112 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002113
Karl Rosaen138a0412009-04-23 19:00:21 -07002114 if (initialQuery == null) {
2115 // Use any text typed in the launcher as the initial query
2116 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 if (appSearchData == null) {
2119 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002120 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 }
Winson Chungadf0c182012-08-23 14:59:07 -07002122 Rect sourceBounds = new Rect();
2123 if (mSearchDropTargetBar != null) {
2124 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2125 }
Romain Guycbb89e42009-06-08 15:52:54 -07002126
Ian Parkinson047036e2014-09-01 15:40:53 +01002127 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002128 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002129 if (clearTextImmediately) {
2130 clearTypedText();
2131 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002132 }
2133
Ian Parkinson047036e2014-09-01 15:40:53 +01002134 /**
2135 * Start a text search.
2136 *
2137 * @return {@code true} if the search will start immediately, so any further keypresses
2138 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2139 * to buffer keypresses.
2140 */
2141 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002142 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002143 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2144 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2145 sourceBounds);
2146 }
2147
Michael Jurkaa33411c2012-06-14 16:18:21 -07002148 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002149 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002150 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002151 }
2152
2153 /**
2154 * Starts the global search activity. This code is a copied from SearchManager
2155 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002156 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002157 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002158 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002159 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2160 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2161 if (globalSearchActivity == null) {
2162 Log.w(TAG, "No global search activity found.");
2163 return;
2164 }
2165 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2166 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2167 intent.setComponent(globalSearchActivity);
2168 // Make sure that we have a Bundle to put source in
2169 if (appSearchData == null) {
2170 appSearchData = new Bundle();
2171 } else {
2172 appSearchData = new Bundle(appSearchData);
2173 }
Adam Cohen9211d422014-10-07 18:14:53 -07002174 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002175 if (!appSearchData.containsKey("source")) {
2176 appSearchData.putString("source", getPackageName());
2177 }
2178 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2179 if (!TextUtils.isEmpty(initialQuery)) {
2180 intent.putExtra(SearchManager.QUERY, initialQuery);
2181 }
2182 if (selectInitialQuery) {
2183 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2184 }
2185 intent.setSourceBounds(sourceBounds);
2186 try {
2187 startActivity(intent);
2188 } catch (ActivityNotFoundException ex) {
2189 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
2192
Yura4f93ec62014-02-04 14:15:21 +00002193 public boolean isOnCustomContent() {
2194 return mWorkspace.isOnOrMovingToCustomContent();
2195 }
2196
Winson Chung70d72102011-08-12 11:24:35 -07002197 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002198 public boolean onPrepareOptionsMenu(Menu menu) {
2199 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002200 if (!isOnCustomContent()) {
2201 // Close any open folders
2202 closeFolder();
2203 // Stop resizing any widgets
2204 mWorkspace.exitWidgetResizeMode();
2205 if (!mWorkspace.isInOverviewMode()) {
2206 // Show the overview mode
2207 showOverviewMode(true);
2208 } else {
2209 showWorkspace(true);
2210 }
Winson Chunge0298742014-01-17 12:03:00 -08002211 }
Adam Cohen9211d422014-10-07 18:14:53 -07002212 if (mLauncherCallbacks != null) {
2213 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2214 }
2215
Michael Jurkab94f3f82013-09-11 18:08:54 +02002216 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002219 @Override
2220 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002221 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002222 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002223 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002224 }
2225
Joe Onorato9c1289c2009-08-17 11:03:03 -04002226 public boolean isWorkspaceLocked() {
2227 return mWorkspaceLoading || mWaitingForResult;
2228 }
2229
Adam Cohen517a7f52014-03-01 12:12:59 -08002230 public boolean isWorkspaceLoading() {
2231 return mWorkspaceLoading;
2232 }
2233
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002234 private void setWorkspaceLoading(boolean value) {
2235 boolean isLocked = isWorkspaceLocked();
2236 mWorkspaceLoading = value;
2237 if (isLocked != isWorkspaceLocked()) {
2238 onWorkspaceLockedChanged();
2239 }
2240 }
2241
2242 private void setWaitingForResult(boolean value) {
2243 boolean isLocked = isWorkspaceLocked();
2244 mWaitingForResult = value;
2245 if (isLocked != isWorkspaceLocked()) {
2246 onWorkspaceLockedChanged();
2247 }
2248 }
2249
Adam Cohen9211d422014-10-07 18:14:53 -07002250 protected void onWorkspaceLockedChanged() {
2251 if (mLauncherCallbacks != null) {
2252 mLauncherCallbacks.onWorkspaceLockedChanged();
2253 }
2254 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002255
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002259 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2260 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002261 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002264
Sunny Goyale6b63a32015-01-16 16:14:29 -08002265 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002266 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002267 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2268 }
2269
Sunny Goyale6b63a32015-01-16 16:14:29 -08002270 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002271 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2272 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002273 if (appWidgetInfo.configure != null) {
2274 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002275 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002276
Bjorn Bringert7984c942009-12-09 15:38:25 +00002277 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002278 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2279 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002282 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002283 Runnable onComplete = new Runnable() {
2284 @Override
2285 public void run() {
2286 // Exit spring loaded mode if necessary after adding the widget
2287 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2288 null);
2289 }
2290 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002291 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002292 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002293 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002294 }
2295 }
Romain Guycbb89e42009-06-08 15:52:54 -07002296
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002297 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002298 // Close any folders that may be open.
2299 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002300 mWorkspace.moveToCustomContentScreen(animate);
2301 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002302
2303 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2304 int[] cell, int spanX, int spanY) {
2305 switch (info.itemType) {
2306 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2307 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2308 int span[] = new int[2];
2309 span[0] = spanX;
2310 span[1] = spanY;
2311 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2312 container, screenId, cell, span);
2313 break;
2314 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2315 processShortcutFromDrop(info.componentName, container, screenId, cell);
2316 break;
2317 default:
2318 throw new IllegalStateException("Unknown item type: " + info.itemType);
2319 }
2320 }
2321
Adam Cohenfbba09b2011-07-18 21:43:05 -07002322 /**
2323 * Process a shortcut drop.
2324 *
2325 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002326 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002329 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2330 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002331 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002332 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002334 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002335
2336 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 mPendingAddInfo.cellX = cell[0];
2338 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002339 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002340
2341 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2342 createShortcutIntent.setComponent(componentName);
2343 processShortcut(createShortcutIntent);
2344 }
2345
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 /**
2347 * Process a widget drop.
2348 *
2349 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002350 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002352 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002353 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2354 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002355 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002356 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002357 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002358 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002359 mPendingAddInfo.minSpanX = info.minSpanX;
2360 mPendingAddInfo.minSpanY = info.minSpanY;
2361
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 mPendingAddInfo.cellX = cell[0];
2364 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002365 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002366 if (span != null) {
2367 mPendingAddInfo.spanX = span[0];
2368 mPendingAddInfo.spanY = span[1];
2369 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002370
Adam Cohened66b2b2012-01-23 17:28:51 -08002371 AppWidgetHostView hostView = info.boundWidget;
2372 int appWidgetId;
2373 if (hostView != null) {
2374 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002375 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002376 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002377 // In this case, we either need to start an activity to get permission to bind
2378 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002379 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002380 Bundle options = info.bindOptions;
2381
Sunny Goyalffe83f12014-08-14 17:39:34 -07002382 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2383 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002384 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002385 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002386 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002387 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002388 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2389 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2390 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002391 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2392 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002393 // TODO: we need to make sure that this accounts for the options bundle.
2394 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002395 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2396 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002397 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002398 }
2399
Joe Onoratodeb98af2010-02-19 14:59:39 -08002400 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002401 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 }
2403
Winson Chung24ab2f12010-09-16 14:10:47 -07002404 void processWallpaper(Intent intent) {
2405 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2406 }
2407
Adam Cohendcd297f2013-06-18 13:13:40 -07002408 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002409 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002410 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 folderInfo.title = getText(R.string.folder_name);
2412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002414 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2415 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002416 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417
2418 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002419 FolderIcon newFolder =
2420 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002421 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002422 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002423 // Force measure the new folder icon
2424 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2425 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002426 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 }
Romain Guycbb89e42009-06-08 15:52:54 -07002428
Joe Onorato9c1289c2009-08-17 11:03:03 -04002429 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002430 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002431 }
2432
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002433 /**
2434 * Registers various content observers. The current implementation registers
2435 * only a favorites observer to keep track of the favorites applications.
2436 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002437 private void registerContentObservers() {
2438 ContentResolver resolver = getContentResolver();
2439 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2440 true, mWidgetObserver);
2441 }
2442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 @Override
2444 public boolean dispatchKeyEvent(KeyEvent event) {
2445 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2446 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002448 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002449 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002450 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002451 dumpState();
2452 return true;
2453 }
2454 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002455 }
2456 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2457 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002458 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 return true;
2460 }
2461 }
2462
2463 return super.dispatchKeyEvent(event);
2464 }
2465
Joe Onorato88ec0992009-11-19 13:16:06 -08002466 @Override
2467 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002468 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2469 return;
2470 }
2471
Winson Chung3d9490a2015-03-24 17:38:42 -07002472 LauncherAccessibilityDelegate delegate =
2473 LauncherAppState.getInstance().getAccessibilityDelegate();
2474 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002475 return;
2476 }
2477
Winson Chungb745afb2015-03-02 11:51:23 -08002478 if (isAppsViewVisible()) {
2479 showWorkspace(true);
2480 } else if (isWidgetsViewVisible()) {
2481 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002482 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002483 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002484 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002485 Folder openFolder = mWorkspace.getOpenFolder();
2486 if (openFolder.isEditingName()) {
2487 openFolder.dismissEditingName();
2488 } else {
2489 closeFolder();
2490 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002491 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002492 mWorkspace.exitWidgetResizeMode();
2493
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002494 // Back button is a no-op here, but give at least some feedback for the button press
2495 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002496 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002497 }
2498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002500 * Re-listen when widgets are reset.
2501 */
Adam Cohen091440a2015-03-18 14:16:05 -07002502 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002503 if (mAppWidgetHost != null) {
2504 mAppWidgetHost.startListening();
2505 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002506 }
2507
2508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 * Launches the intent referred by the clicked shortcut.
2510 *
2511 * @param v The view representing the clicked shortcut.
2512 */
2513 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002514 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2515 // view has detached (it's possible for this to happen if the view is removed mid touch).
2516 if (v.getWindowToken() == null) {
2517 return;
2518 }
2519
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002520 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002521 return;
2522 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002523
Adam Cohen1697b792013-09-17 19:08:21 -07002524 if (v instanceof Workspace) {
2525 if (mWorkspace.isInOverviewMode()) {
2526 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002527 }
2528 return;
2529 }
2530
2531 if (v instanceof CellLayout) {
2532 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002533 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002534 }
2535 }
2536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002538 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002539 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002541 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002542 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002543 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002544 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002546 } else if (tag instanceof AppInfo) {
2547 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002548 } else if (tag instanceof LauncherAppWidgetInfo) {
2549 if (v instanceof PendingAppWidgetHostView) {
2550 onClickPendingWidget((PendingAppWidgetHostView) v);
2551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 }
2553 }
2554
Sunny Goyal508da152014-08-14 10:53:27 -07002555 public void onClickPagedViewIcon(View v) {
2556 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002557 if (mLauncherCallbacks != null) {
2558 mLauncherCallbacks.onClickPagedViewIcon(v);
2559 }
Sunny Goyal508da152014-08-14 10:53:27 -07002560 }
2561
Michael Jurka0e260592010-06-30 17:07:39 -07002562 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002563 return false;
2564 }
2565
Michael Jurkaaf442092010-06-10 17:01:57 -07002566 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002567 * Event handler for the app widget view which has not fully restored.
2568 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002569 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002570 if (mIsSafeModeEnabled) {
2571 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2572 return;
2573 }
2574
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002576 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002577 int widgetId = info.appWidgetId;
2578 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2579 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002580 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2581 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002582 mPendingAddInfo.copyFrom(info);
2583 mPendingAddWidgetId = widgetId;
2584
Sunny Goyalffe83f12014-08-14 17:39:34 -07002585 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2586 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002587 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002588 } else if (info.installProgress < 0) {
2589 // The install has not been queued
2590 final String packageName = info.providerName.getPackageName();
2591 showBrokenAppInstallDialog(packageName,
2592 new DialogInterface.OnClickListener() {
2593 public void onClick(DialogInterface dialog, int id) {
2594 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2595 }
2596 });
2597 } else {
2598 // Download has started.
2599 final String packageName = info.providerName.getPackageName();
2600 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002601 }
2602 }
2603
2604 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002605 * Event handler for the "grid" button that appears on the home screen, which
2606 * enters all apps mode.
2607 *
2608 * @param v The view that was clicked.
2609 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002610 protected void onClickAllAppsButton(View v) {
2611 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002612 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002613 showWorkspace(true);
2614 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002615 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002616 }
2617 }
2618
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002619 private void showBrokenAppInstallDialog(final String packageName,
2620 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002621 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002622 .setTitle(R.string.abandoned_promises_title)
2623 .setMessage(R.string.abandoned_promise_explanation)
2624 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2625 .setNeutralButton(R.string.abandoned_clean_this,
2626 new DialogInterface.OnClickListener() {
2627 public void onClick(DialogInterface dialog, int id) {
2628 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2629 mWorkspace.removeAbandonedPromise(packageName, user);
2630 }
2631 })
2632 .create().show();
2633 return;
2634 }
2635
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002636 /**
2637 * Event handler for an app shortcut click.
2638 *
2639 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2640 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002641 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2643 Object tag = v.getTag();
2644 if (!(tag instanceof ShortcutInfo)) {
2645 throw new IllegalArgumentException("Input must be a Shortcut");
2646 }
2647
2648 // Open shortcut
2649 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002650
2651 if (shortcut.isDisabled != 0) {
2652 int error = R.string.activity_not_available;
2653 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2654 error = R.string.safemode_shortcut_error;
2655 }
2656 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2657 return;
2658 }
2659
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002660 final Intent intent = shortcut.intent;
2661
2662 // Check for special shortcuts
2663 if (intent.getComponent() != null) {
2664 final String shortcutClass = intent.getComponent().getClassName();
2665
2666 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2667 MemoryDumpActivity.startDump(this);
2668 return;
2669 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2670 toggleShowWeightWatcher();
2671 return;
2672 }
2673 }
2674
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002676 if ((v instanceof BubbleTextView)
2677 && shortcut.isPromise()
2678 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002679 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002680 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 new DialogInterface.OnClickListener() {
2682 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002683 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 return;
2687 }
2688
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002689 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002690 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002691
2692 if (mLauncherCallbacks != null) {
2693 mLauncherCallbacks.onClickAppShortcut(v);
2694 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002695 }
2696
Adam Cohen091440a2015-03-18 14:16:05 -07002697 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002698 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002699 final ShortcutInfo shortcut;
2700 final Intent intent;
2701 if (tag instanceof ShortcutInfo) {
2702 shortcut = (ShortcutInfo) tag;
2703 intent = shortcut.intent;
2704 int[] pos = new int[2];
2705 v.getLocationOnScreen(pos);
2706 intent.setSourceBounds(new Rect(pos[0], pos[1],
2707 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002708
Sunny Goyal508da152014-08-14 10:53:27 -07002709 } else if (tag instanceof AppInfo) {
2710 shortcut = null;
2711 intent = ((AppInfo) tag).intent;
2712 } else {
2713 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2714 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715
2716 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002717 mStats.recordLaunch(intent, shortcut);
2718
2719 if (success && v instanceof BubbleTextView) {
2720 mWaitingForResume = (BubbleTextView) v;
2721 mWaitingForResume.setStayPressed(true);
2722 }
2723 }
2724
2725 /**
2726 * Event handler for a folder icon click.
2727 *
2728 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2729 */
2730 protected void onClickFolderIcon(View v) {
2731 if (LOGD) Log.d(TAG, "onClickFolder");
2732 if (!(v instanceof FolderIcon)){
2733 throw new IllegalArgumentException("Input must be a FolderIcon");
2734 }
2735
2736 FolderIcon folderIcon = (FolderIcon) v;
2737 final FolderInfo info = folderIcon.getFolderInfo();
2738 Folder openFolder = mWorkspace.getFolderForTag(info);
2739
2740 // If the folder info reports that the associated folder is open, then verify that
2741 // it is actually opened. There have been a few instances where this gets out of sync.
2742 if (info.opened && openFolder == null) {
2743 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2744 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2745 info.opened = false;
2746 }
2747
2748 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2749 // Close any open folder
2750 closeFolder();
2751 // Open the requested folder
2752 openFolder(folderIcon);
2753 } else {
2754 // Find the open folder...
2755 int folderScreen;
2756 if (openFolder != null) {
2757 folderScreen = mWorkspace.getPageForView(openFolder);
2758 // .. and close it
2759 closeFolder(openFolder);
2760 if (folderScreen != mWorkspace.getCurrentPage()) {
2761 // Close any folder open on the current screen
2762 closeFolder();
2763 // Pull the folder onto this screen
2764 openFolder(folderIcon);
2765 }
2766 }
2767 }
Adam Cohen9211d422014-10-07 18:14:53 -07002768
2769 if (mLauncherCallbacks != null) {
2770 mLauncherCallbacks.onClickFolderIcon(v);
2771 }
Michael Jurka5130e402011-10-13 04:55:35 -07002772 }
2773
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002774 /**
2775 * Event handler for the (Add) Widgets button that appears after a long press
2776 * on the home screen.
2777 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002778 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002779 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002780 if (mIsSafeModeEnabled) {
2781 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2782 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002783 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002784 if (mLauncherCallbacks != null) {
2785 mLauncherCallbacks.onClickAddWidgetButton(view);
2786 }
Adam Cohen9211d422014-10-07 18:14:53 -07002787 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002788 }
2789
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002790 /**
2791 * Event handler for the wallpaper picker button that appears after a long press
2792 * on the home screen.
2793 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002794 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002795 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002796 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2797 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002798
2799 if (mLauncherCallbacks != null) {
2800 mLauncherCallbacks.onClickWallpaperPicker(v);
2801 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002802 }
2803
2804 /**
2805 * Event handler for a click on the settings button that appears after a long press
2806 * on the home screen.
2807 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002808 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002809 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002810 if (mLauncherCallbacks != null) {
2811 mLauncherCallbacks.onClickSettingsButton(v);
2812 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002813 }
2814
Michael Jurka5130e402011-10-13 04:55:35 -07002815 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002816 // Provide the same haptic feedback that the system offers for virtual keys.
2817 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002818 }
2819
Adam Cohen61f560d2013-09-30 15:58:20 -07002820 public void performHapticFeedbackOnTouchDown(View v) {
2821 // Provide the same haptic feedback that the system offers for virtual keys.
2822 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2823 }
2824
2825 public View.OnTouchListener getHapticFeedbackTouchListener() {
2826 if (mHapticFeedbackTouchListener == null) {
2827 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2828 @Override
2829 public boolean onTouch(View v, MotionEvent event) {
2830 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2831 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2832 }
2833 return false;
2834 }
2835 };
2836 }
2837 return mHapticFeedbackTouchListener;
2838 }
2839
Adam Cohen9211d422014-10-07 18:14:53 -07002840 public void onDragStarted(View view) {
2841 if (isOnCustomContent()) {
2842 // Custom content screen doesn't participate in drag and drop. If on custom
2843 // content screen, move to default.
2844 moveWorkspaceToDefaultScreen();
2845 }
2846
2847 if (mLauncherCallbacks != null) {
2848 mLauncherCallbacks.onDragStarted(view);
2849 }
2850 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002851
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002852 /**
2853 * Called when the user stops interacting with the launcher.
2854 * This implies that the user is now on the homescreen and is not doing housekeeping.
2855 */
Adam Cohen9211d422014-10-07 18:14:53 -07002856 protected void onInteractionEnd() {
2857 if (mLauncherCallbacks != null) {
2858 mLauncherCallbacks.onInteractionEnd();
2859 }
2860 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002861
2862 /**
2863 * Called when the user starts interacting with the launcher.
2864 * The possible interactions are:
2865 * - open all apps
2866 * - reorder an app shortcut, or a widget
2867 * - open the overview mode.
2868 * This is a good time to stop doing things that only make sense
2869 * when the user is on the homescreen and not doing housekeeping.
2870 */
Adam Cohen9211d422014-10-07 18:14:53 -07002871 protected void onInteractionBegin() {
2872 if (mLauncherCallbacks != null) {
2873 mLauncherCallbacks.onInteractionBegin();
2874 }
2875 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002876
Kenny Guyf07af7b2014-07-31 11:39:16 +01002877 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002878 try {
2879 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002880 launcherApps.showAppDetailsForProfile(componentName, user);
2881 } catch (SecurityException e) {
2882 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2883 Log.e(TAG, "Launcher does not have permission to launch settings");
2884 } catch (ActivityNotFoundException e) {
2885 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2886 Log.e(TAG, "Unable to launch settings");
2887 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002888 }
2889
Michael Jurka1e2f4652013-07-08 18:03:46 -07002890 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002891 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2892 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002893 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002894 // System applications cannot be installed. For now, show a toast explaining that.
2895 // We may give them the option of disabling apps this way.
2896 int messageId = R.string.uninstall_system_app_text;
2897 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002898 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002899 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002900 String packageName = componentName.getPackageName();
2901 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002902 Intent intent = new Intent(
2903 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002904 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2905 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002906 if (user != null) {
2907 user.addToIntent(intent, Intent.EXTRA_USER);
2908 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002909 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002910 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002911 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002912 }
2913
Michael Jurka86a720e2012-05-09 11:23:23 -07002914 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002915 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002917 // Only launch using the new animation if the shortcut has not opted out (this is a
2918 // private contract between launcher and may be ignored in the future).
2919 boolean useLaunchAnimation = (v != null) &&
2920 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002921 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2922 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002923
Kenny Guy1317e2d2014-05-08 18:52:50 +01002924 UserHandleCompat user = null;
2925 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2926 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2927 user = userManager.getUserForSerialNumber(serialNumber);
2928 }
2929
2930 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002931 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002932 ActivityOptions opts = null;
2933 if (sClipRevealMethod != null) {
2934 // TODO: call method directly when Launcher3 can depend on M APIs
2935 int left = 0, top = 0;
2936 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2937 if (v instanceof TextView) {
2938 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002939 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2940 if (icon != null) {
2941 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002942 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002943 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002944 width = bounds.width();
2945 height = bounds.height();
2946 }
2947 }
2948 try {
2949 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2950 left, top, width, height);
2951 } catch (IllegalAccessException e) {
2952 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2953 sClipRevealMethod = null;
2954 } catch (InvocationTargetException e) {
2955 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2956 sClipRevealMethod = null;
2957 }
2958 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002959 if (opts == null && !Utilities.isLmpOrAbove()) {
2960 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002961 v.getMeasuredWidth(), v.getMeasuredHeight());
2962 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002963 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002964 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002965
2966 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2967 // Could be launching some bookkeeping activity
2968 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002969 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002970 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002971 launcherApps.startActivityForProfile(intent.getComponent(), user,
2972 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002973 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002974 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 } catch (SecurityException e) {
2976 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002977 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002979 "or use the exported attribute for this activity. "
2980 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002982 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002984
Michael Jurka86a720e2012-05-09 11:23:23 -07002985 boolean startActivitySafely(View v, Intent intent, Object tag) {
2986 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002987 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2988 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2989 return false;
2990 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002991 try {
2992 success = startActivity(v, intent, tag);
2993 } catch (ActivityNotFoundException e) {
2994 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2995 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2996 }
2997 return success;
2998 }
2999
Adam Cohen268c4752012-06-06 17:47:33 -07003000 /**
3001 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3002 * in the DragLayer in the exact absolute location of the original FolderIcon.
3003 */
3004 private void copyFolderIconToImage(FolderIcon fi) {
3005 final int width = fi.getMeasuredWidth();
3006 final int height = fi.getMeasuredHeight();
3007
3008 // Lazy load ImageView, Bitmap and Canvas
3009 if (mFolderIconImageView == null) {
3010 mFolderIconImageView = new ImageView(this);
3011 }
3012 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3013 mFolderIconBitmap.getHeight() != height) {
3014 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3015 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3016 }
3017
3018 DragLayer.LayoutParams lp;
3019 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3020 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3021 } else {
3022 lp = new DragLayer.LayoutParams(width, height);
3023 }
3024
Adam Cohen307fe232012-08-16 17:55:58 -07003025 // The layout from which the folder is being opened may be scaled, adjust the starting
3026 // view size by this scale factor.
3027 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003028 lp.customPosition = true;
3029 lp.x = mRectForFolderAnimation.left;
3030 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003031 lp.width = (int) (scale * width);
3032 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003033
3034 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3035 fi.draw(mFolderIconCanvas);
3036 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003037 if (fi.getFolder() != null) {
3038 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3039 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003040 }
Adam Cohen268c4752012-06-06 17:47:33 -07003041 // Just in case this image view is still in the drag layer from a previous animation,
3042 // we remove it and re-add it.
3043 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3044 mDragLayer.removeView(mFolderIconImageView);
3045 }
3046 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003047 if (fi.getFolder() != null) {
3048 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003049 }
Adam Cohen268c4752012-06-06 17:47:33 -07003050 }
3051
Adam Cohen2801caf2011-05-13 20:57:39 -07003052 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003053 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003054 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3055 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3056 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3057
Adam Cohenc51934b2011-07-26 21:07:43 -07003058 FolderInfo info = (FolderInfo) fi.getTag();
3059 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3060 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003061 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3062 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003063 }
3064
Adam Cohen268c4752012-06-06 17:47:33 -07003065 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3066 copyFolderIconToImage(fi);
3067 fi.setVisibility(View.INVISIBLE);
3068
Michael Jurka2ecf9952012-06-18 12:52:28 -07003069 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003070 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003071 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003072 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3073 }
3074 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003075 oa.start();
3076 }
3077
Adam Cohen268c4752012-06-06 17:47:33 -07003078 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003079 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003080 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3081 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3082 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3083
Adam Cohen268c4752012-06-06 17:47:33 -07003084 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003085
Adam Cohen268c4752012-06-06 17:47:33 -07003086 // We remove and re-draw the FolderIcon in-case it has changed
3087 mDragLayer.removeView(mFolderIconImageView);
3088 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003089 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003090 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003091 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003092 oa.addListener(new AnimatorListenerAdapter() {
3093 @Override
3094 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003095 if (cl != null) {
3096 cl.clearFolderLeaveBehind();
3097 // Remove the ImageView copy of the FolderIcon and make the original visible.
3098 mDragLayer.removeView(mFolderIconImageView);
3099 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003100 }
3101 }
3102 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003103 oa.start();
3104 }
3105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003107 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108 * is animated relative to the specified View. If the View is null, no animation
3109 * is played.
3110 *
3111 * @param folderInfo The FolderInfo describing the folder to open.
3112 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003113 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003114 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003115 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3116 if (openFolder != null && openFolder != folder) {
3117 // Close any open folder before opening a folder.
3118 closeFolder();
3119 }
3120
Adam Cohena9cf38f2011-05-02 15:36:58 -07003121 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122
Adam Cohena9cf38f2011-05-02 15:36:58 -07003123 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124
Sunny Goyalf4066152015-04-15 09:42:19 -07003125 // While the folder is open, the position of the icon cannot change.
3126 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3127
Adam Cohen4554ee12011-08-03 16:13:21 -07003128 // Just verify that the folder hasn't already been added to the DragLayer.
3129 // There was a one-off crash where the folder had a parent already.
3130 if (folder.getParent() == null) {
3131 mDragLayer.addView(folder);
3132 mDragController.addDropTarget((DropTarget) folder);
3133 } else {
3134 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3135 folder.getParent() + ").");
3136 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003137 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003138 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003139
3140 // Notify the accessibility manager that this folder "window" has appeared and occluded
3141 // the workspace items
3142 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3143 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003144 }
3145
3146 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003147 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003148 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003149 if (folder.isEditingName()) {
3150 folder.dismissEditingName();
3151 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003152 closeFolder(folder);
3153 }
3154 }
3155
3156 void closeFolder(Folder folder) {
3157 folder.getInfo().opened = false;
3158
3159 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3160 if (parent != null) {
3161 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3162 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003163 if (fi != null) {
3164 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3165 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003166 }
3167 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003168
3169 // Notify the accessibility manager that this folder "window" has disappeard and no
3170 // longer occludeds the workspace items
3171 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 }
3173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003175 if (!isDraggingEnabled()) return false;
3176 if (isWorkspaceLocked()) return false;
3177 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178
Adam Cohen1697b792013-09-17 19:08:21 -07003179 if (v instanceof Workspace) {
3180 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003181 if (mWorkspace.enterOverviewMode()) {
3182 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3183 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3184 return true;
3185 } else {
3186 return false;
3187 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003188 } else {
3189 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003190 }
Adam Cohen1697b792013-09-17 19:08:21 -07003191 }
3192
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003193 CellLayout.CellInfo longClickCellInfo = null;
3194 View itemUnderLongClick = null;
3195 if (v.getTag() instanceof ItemInfo) {
3196 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003197 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003198 itemUnderLongClick = longClickCellInfo.cell;
3199 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003200 }
3201
Winson Chung3d503fb2011-07-13 17:25:49 -07003202 // The hotseat touch handling does not go through Workspace, and we always allow long press
3203 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003204 final boolean inHotseat = isHotseatLayout(v);
3205 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003206 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003207 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003208 // User long pressed on empty space
3209 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3210 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003211 if (mWorkspace.isInOverviewMode()) {
3212 mWorkspace.startReordering(v);
3213 } else {
3214 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003217 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3218 mHotseat.getOrderInHotseat(
3219 longClickCellInfo.cellX,
3220 longClickCellInfo.cellY));
3221 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003223 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003224 }
3225 }
3226 }
3227 return true;
3228 }
3229
Winson Chung3d503fb2011-07-13 17:25:49 -07003230 boolean isHotseatLayout(View layout) {
3231 return mHotseat != null && layout != null &&
3232 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3233 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003234
Winson Chung3d503fb2011-07-13 17:25:49 -07003235 /**
3236 * Returns the CellLayout of the specified container at the specified screen.
3237 */
Sunny Goyal92820592015-03-02 11:31:35 -08003238 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003239 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3240 if (mHotseat != null) {
3241 return mHotseat.getLayout();
3242 } else {
3243 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003244 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003245 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003246 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003247 }
3248 }
3249
Winson Chungb745afb2015-03-02 11:51:23 -08003250 /**
3251 * For overridden classes.
3252 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003253 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003254 return isAppsViewVisible();
3255 }
3256
3257 public boolean isAppsViewVisible() {
3258 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3259 }
3260
3261 public boolean isWidgetsViewVisible() {
3262 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003263 }
3264
Craig Mautner360310b2012-10-26 15:13:08 -07003265 private void setWorkspaceBackground(boolean workspace) {
3266 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003267 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003268 }
3269
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003270 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003271 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3272 int curflags = getWindow().getAttributes().flags
3273 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3274 if (wpflags != curflags) {
3275 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3276 }
Craig Mautner360310b2012-10-26 15:13:08 -07003277 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003278 }
3279
Michael Jurkae326f182011-11-21 14:05:46 -08003280 @Override
3281 public void onTrimMemory(int level) {
3282 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003283 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3284 // The widget preview db can result in holding onto over
3285 // 3MB of memory for caching which isn't necessary.
3286 SQLiteDatabase.releaseMemory();
3287
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003288 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003289 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003290 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003291 if (mLauncherCallbacks != null) {
3292 mLauncherCallbacks.onTrimMemory(level);
3293 }
Michael Jurkae326f182011-11-21 14:05:46 -08003294 }
3295
Winson Chungb745afb2015-03-02 11:51:23 -08003296 @Override
3297 public void onStateTransitionHideSearchBar() {
3298 // Hide the search bar
3299 if (mSearchDropTargetBar != null) {
3300 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3301 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003302 }
3303
Winson Chungb745afb2015-03-02 11:51:23 -08003304 protected void showWorkspace(boolean animated) {
3305 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003306 }
3307
Adam Cohened66b2b2012-01-23 17:28:51 -08003308 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003309 boolean changed = mState != State.WORKSPACE ||
3310 mWorkspace.getState() != Workspace.State.NORMAL;
3311 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003312 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003314 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
3315 animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003316
Winson Chungc7d2b602012-05-16 17:02:20 -07003317 // Show the search bar (only animate if we were showing the drop target bar in spring
3318 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003319 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003320 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003321 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003322
Michael Jurkab3e22d92011-10-31 15:58:33 -07003323 // Set focus to the AppsCustomize button
3324 if (mAllAppsButton != null) {
3325 mAllAppsButton.requestFocus();
3326 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003327 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003328
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003329 // Change the state *after* we've called all the transition code
3330 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003331
Winson Chung5fb63472011-02-02 17:03:37 -08003332 // Resume the auto-advance of widgets
3333 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003334 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003335
Winson Chung0f785722015-04-08 10:27:49 -07003336 if (changed) {
3337 // Send an accessibility event to announce the context change
3338 getWindow().getDecorView()
3339 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003340
Winson Chung0f785722015-04-08 10:27:49 -07003341 onWorkspaceShown(animated);
3342 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003343 }
3344
Adam Cohened307df2013-10-02 09:37:31 -07003345 void showOverviewMode(boolean animated) {
3346 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003347 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
3348 animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003349 mState = State.WORKSPACE;
3350 onWorkspaceShown(animated);
3351 }
3352
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003353 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003354 }
3355
Winson Chungb745afb2015-03-02 11:51:23 -08003356 /**
3357 * Shows the apps view.
3358 */
3359 void showAppsView(boolean animated, boolean resetListToTop) {
3360 if (resetListToTop) {
3361 mAppsView.scrollToTop();
3362 }
3363 showAppsOrWidgets(animated, State.APPS);
3364 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003365
Winson Chungb745afb2015-03-02 11:51:23 -08003366 /**
3367 * Shows the widgets view.
3368 */
3369 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003370 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003371 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003372 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003373 }
Winson Chungb745afb2015-03-02 11:51:23 -08003374 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003375
3376 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003377 @Override
3378 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003379 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003380 }
3381 });
Winson Chungb745afb2015-03-02 11:51:23 -08003382 }
3383
3384 /**
3385 * Sets up the transition to show the apps/widgets view.
3386 */
3387 private void showAppsOrWidgets(boolean animated, State toState) {
3388 if (mState != State.WORKSPACE) return;
3389 if (toState != State.APPS && toState != State.WIDGETS) return;
3390
3391 if (toState == State.APPS) {
3392 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung0f785722015-04-08 10:27:49 -07003393 if (mLauncherCallbacks != null) {
3394 mLauncherCallbacks.onAllAppsShown();
3395 }
Winson Chungb745afb2015-03-02 11:51:23 -08003396 } else {
3397 mStateTransitionAnimation.startAnimationToWidgets(animated);
3398 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003399
Michael Jurkab3e22d92011-10-31 15:58:33 -07003400 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003401 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003402
3403 // Pause the auto-advance of widgets until we are out of AllApps
3404 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003405 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003406 closeFolder();
3407
3408 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003409 getWindow().getDecorView()
3410 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003411 }
3412
Hyunyoung Song3f471442015-04-08 19:01:34 -07003413 public void enterSpringLoadedDragMode() {
3414 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3415 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003416 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3417 mState == State.WIDGETS_SPRING_LOADED) {
3418 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003419 }
Winson Chungb745afb2015-03-02 11:51:23 -08003420
3421 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
3422 true /* animated */, null /* onCompleteRunnable */);
3423 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003424 }
Adam Cohen7777d962011-08-18 18:58:38 -07003425
Hyunyoung Song3f471442015-04-08 19:01:34 -07003426 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003427 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003428 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003429
Winson Chunge7a03942011-08-05 15:05:12 -07003430 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003431 @Override
3432 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003433 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003434 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3435 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003436 // Before we show workspace, hide all apps again because
3437 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3438 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003439 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003440 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003441 } else {
3442 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003443 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003444 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003445 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003446 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003447
Michael Jurkad3ef3062010-11-23 16:23:58 -08003448 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003449 if (mState == State.APPS_SPRING_LOADED) {
3450 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3451 mState = State.APPS;
3452 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3453 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3454 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003455 }
3456 // Otherwise, we are not in spring loaded mode, so don't do anything.
3457 }
3458
Michael Jurkab3e22d92011-10-31 15:58:33 -07003459 void lockAllApps() {
3460 // TODO
3461 }
3462
3463 void unlockAllApps() {
3464 // TODO
3465 }
3466
Sunny Goyal594d76d2014-11-06 10:12:54 -08003467 protected void disableVoiceButtonProxy(boolean disable) {
3468 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003469 }
3470
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003471 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003472 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3473 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003474 }
3475
Adam Cohen24ce0b32014-01-14 16:18:14 -08003476 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003477 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3478 if (searchProvider == null) {
3479 return null;
3480 }
3481
3482 Bundle opts = new Bundle();
3483 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003484 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003485
3486 SharedPreferences sp = getSharedPreferences(
3487 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3488 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003489 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003490 if (!searchProvider.provider.flattenToString().equals(
3491 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003492 || (widgetInfo == null)
3493 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003494 // A valid widget is not already bound.
3495 if (widgetId > -1) {
3496 mAppWidgetHost.deleteAppWidgetId(widgetId);
3497 widgetId = -1;
3498 }
3499
3500 // Try to bind a new widget
3501 widgetId = mAppWidgetHost.allocateAppWidgetId();
3502
3503 if (!AppWidgetManagerCompat.getInstance(this)
3504 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3505 mAppWidgetHost.deleteAppWidgetId(widgetId);
3506 widgetId = -1;
3507 }
3508
3509 sp.edit()
3510 .putInt(QSB_WIDGET_ID, widgetId)
3511 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3512 .commit();
3513 }
3514
3515 if (widgetId != -1) {
3516 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3517 mQsb.updateAppWidgetOptions(opts);
3518 mQsb.setPadding(0, 0, 0, 0);
3519 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003520 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003521 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003522 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003523 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003524 }
3525
Sunny Goyal22235bc2015-04-03 09:23:43 -07003526 private void reinflateQSBIfNecessary() {
3527 if (mQsb instanceof LauncherAppWidgetHostView &&
3528 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3529 mSearchDropTargetBar.removeView(mQsb);
3530 mQsb = null;
3531 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3532 }
3533 }
3534
Michael Jurkad7c28052012-04-27 15:43:36 -07003535 @Override
3536 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003537 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003538 final List<CharSequence> text = event.getText();
3539 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003540 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003541 if (mState == State.APPS) {
3542 text.add("Apps");
3543 } else if (mState == State.WIDGETS) {
3544 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003545 } else {
3546 text.add(getString(R.string.all_apps_home_button_label));
3547 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003548 return result;
3549 }
3550
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003551 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003552 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003553 */
Adam Cohen091440a2015-03-18 14:16:05 -07003554 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003555 @Override
3556 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003557 closeSystemDialogs();
3558 }
3559 }
3560
3561 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003562 * Receives notifications whenever the appwidgets are reset.
3563 */
3564 private class AppWidgetResetObserver extends ContentObserver {
3565 public AppWidgetResetObserver() {
3566 super(new Handler());
3567 }
3568
3569 @Override
3570 public void onChange(boolean selfChange) {
3571 onAppWidgetReset();
3572 }
3573 }
3574
3575 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003576 * If the activity is currently paused, signal that we need to run the passed Runnable
3577 * in onResume.
3578 *
3579 * This needs to be called from incoming places where resources might have been loaded
3580 * while we are paused. That is becaues the Configuration might be wrong
3581 * when we're not running, and if it comes back to what it was when we
3582 * were paused, we are not restarted.
3583 *
3584 * Implementation of the method from LauncherModel.Callbacks.
3585 *
3586 * @return true if we are currently paused. The caller might be able to
3587 * skip some work in that case since we will come back again.
3588 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003589 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003590 if (mPaused) {
3591 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003592 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003593 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003594 }
3595 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003596 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003597 return true;
3598 } else {
3599 return false;
3600 }
3601 }
3602
Michael Jurkac402cd92013-05-20 15:49:32 +02003603 private boolean waitUntilResume(Runnable run) {
3604 return waitUntilResume(run, false);
3605 }
3606
Michael Jurka1e2f4652013-07-08 18:03:46 -07003607 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003608 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003609 }
3610
Michael Jurka7607c2f2013-04-03 14:33:19 -07003611 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003612 * If the activity is currently paused, signal that we need to re-run the loader
3613 * in onResume.
3614 *
3615 * This needs to be called from incoming places where resources might have been loaded
3616 * while we are paused. That is becaues the Configuration might be wrong
3617 * when we're not running, and if it comes back to what it was when we
3618 * were paused, we are not restarted.
3619 *
3620 * Implementation of the method from LauncherModel.Callbacks.
3621 *
3622 * @return true if we are currently paused. The caller might be able to
3623 * skip some work in that case since we will come back again.
3624 */
3625 public boolean setLoadOnResume() {
3626 if (mPaused) {
3627 Log.i(TAG, "setLoadOnResume");
3628 mOnResumeNeedsLoad = true;
3629 return true;
3630 } else {
3631 return false;
3632 }
3633 }
3634
3635 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003636 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003637 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003639 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003640 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003641 } else {
3642 return SCREEN_COUNT / 2;
3643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003644 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003645
Joe Onorato9c1289c2009-08-17 11:03:03 -04003646 /**
3647 * Refreshes the shortcuts shown on the workspace.
3648 *
3649 * Implementation of the method from LauncherModel.Callbacks.
3650 */
3651 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003652 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003653
Michael Jurka7607c2f2013-04-03 14:33:19 -07003654 // If we're starting binding all over again, clear any bind calls we'd postponed in
3655 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3656 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003657 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003658
Winson Chungd64d1762013-08-20 14:37:16 -07003659 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003660 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003661 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003662
Michael Jurka05bf6442011-11-30 20:28:53 -08003663 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003664 if (mHotseat != null) {
3665 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 }
3667 }
3668
Adam Cohendcd297f2013-06-18 13:13:40 -07003669 @Override
3670 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003671 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003672
Adam Cohen5084cba2013-09-03 12:01:16 -07003673 // If there are no screens, we need to have an empty screen
3674 if (orderedScreenIds.size() == 0) {
3675 mWorkspace.addExtraEmptyScreen();
3676 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003677
3678 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003679 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003680 if (hasCustomContentToLeft()) {
3681 mWorkspace.createCustomContentContainer();
3682 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003683 }
Winson Chung64359a52013-07-08 17:17:08 -07003684 }
3685
3686 @Override
3687 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003688 // Log to disk
3689 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3690 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3691 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003692 int count = orderedScreenIds.size();
3693 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003694 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003695 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003696 }
3697
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003698 @Override
3699 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3700 final long screenId, final int[] cell, final int spanX, final int spanY) {
3701 showWorkspace(true, new Runnable() {
3702
3703 @Override
3704 public void run() {
3705 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3706 addPendingItem(info, container, screenId, cell, spanX, spanY);
3707 }
3708 });
3709 }
3710
Adam Cohen39a06042013-07-19 14:30:12 -07003711 private boolean shouldShowWeightWatcher() {
3712 String spKey = LauncherAppState.getSharedPreferencesKey();
3713 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003714 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003715
3716 return show;
3717 }
3718
3719 private void toggleShowWeightWatcher() {
3720 String spKey = LauncherAppState.getSharedPreferencesKey();
3721 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3722 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3723
3724 show = !show;
3725
3726 SharedPreferences.Editor editor = sp.edit();
3727 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3728 editor.commit();
3729
3730 if (mWeightWatcher != null) {
3731 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3732 }
3733 }
3734
Winson Chungd64d1762013-08-20 14:37:16 -07003735 public void bindAppsAdded(final ArrayList<Long> newScreens,
3736 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003737 final ArrayList<ItemInfo> addAnimated,
3738 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003739 Runnable r = new Runnable() {
3740 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003741 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003742 }
3743 };
3744 if (waitUntilResume(r)) {
3745 return;
3746 }
3747
Winson Chungd64d1762013-08-20 14:37:16 -07003748 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003749 if (newScreens != null) {
3750 bindAddScreens(newScreens);
3751 }
Winson Chungd64d1762013-08-20 14:37:16 -07003752
3753 // We add the items without animation on non-visible pages, and with
3754 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003755 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003756 bindItems(addNotAnimated, 0,
3757 addNotAnimated.size(), false);
3758 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003759 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003760 bindItems(addAnimated, 0,
3761 addAnimated.size(), true);
3762 }
Winson Chungc58497e2013-09-03 17:48:37 -07003763
Winson Chung87412982013-10-03 18:34:14 -07003764 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003765 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003766
Winson Chungb745afb2015-03-02 11:51:23 -08003767 if (addedApps != null && mAppsView != null) {
3768 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003769 }
Winson Chungd64d1762013-08-20 14:37:16 -07003770 }
3771
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003772 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773 * Bind the items start-end from the list.
3774 *
3775 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003776 */
Winson Chung64359a52013-07-08 17:17:08 -07003777 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3778 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003779 Runnable r = new Runnable() {
3780 public void run() {
3781 bindItems(shortcuts, start, end, forceAnimateIcons);
3782 }
3783 };
3784 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003785 return;
3786 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003787
Winson Chungf0c6ae02012-03-21 16:10:31 -07003788 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003789 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3790 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003791 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003792 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003793 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003794 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003796
3797 // Short circuit if we are loading dock items for a configuration which has no dock
3798 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3799 mHotseat == null) {
3800 continue;
3801 }
3802
Joe Onorato9c1289c2009-08-17 11:03:03 -04003803 switch (item.itemType) {
3804 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3805 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003806 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003807 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003808
Winson Chung64359a52013-07-08 17:17:08 -07003809 /*
3810 * TODO: FIX collision case
3811 */
Winson Chungce376632013-07-11 16:12:41 -07003812 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3813 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3814 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003815 View v = cl.getChildAt(item.cellX, item.cellY);
3816 Object tag = v.getTag();
3817 String desc = "Collision while binding workspace item: " + item
3818 + ". Collides with " + tag;
3819 if (LauncherAppState.isDogfoodBuild()) {
3820 throw (new RuntimeException(desc));
3821 } else {
3822 Log.d(TAG, desc);
3823 }
Winson Chungce376632013-07-11 16:12:41 -07003824 }
Winson Chung64359a52013-07-08 17:17:08 -07003825 }
3826
Adam Cohendcd297f2013-06-18 13:13:40 -07003827 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3828 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003829 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003830 // Animate all the applications up now
3831 shortcut.setAlpha(0f);
3832 shortcut.setScaleX(0f);
3833 shortcut.setScaleY(0f);
3834 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003835 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003838 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003839 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003840 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003841 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003842 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3843 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003844 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003845 default:
3846 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003847 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003848 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003849
Winson Chung997a9232013-07-24 15:33:46 -07003850 if (animateIcons) {
3851 // Animate to the correct page
3852 if (newShortcutsScreenId > -1) {
3853 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003854 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003855 final Runnable startBounceAnimRunnable = new Runnable() {
3856 public void run() {
3857 anim.playTogether(bounceAnims);
3858 anim.start();
3859 }
3860 };
Winson Chung997a9232013-07-24 15:33:46 -07003861 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003862 // We post the animation slightly delayed to prevent slowdowns
3863 // when we are loading right after we return to launcher.
3864 mWorkspace.postDelayed(new Runnable() {
3865 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003866 if (mWorkspace != null) {
3867 mWorkspace.snapToPage(newScreenIndex);
3868 mWorkspace.postDelayed(startBounceAnimRunnable,
3869 NEW_APPS_ANIMATION_DELAY);
3870 }
Winson Chung94d67682013-09-25 16:29:40 -07003871 }
3872 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003873 } else {
3874 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003875 }
3876 }
Winson Chung64359a52013-07-08 17:17:08 -07003877 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003878 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003879 }
3880
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 /**
3882 * Implementation of the method from LauncherModel.Callbacks.
3883 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003884 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003885 Runnable r = new Runnable() {
3886 public void run() {
3887 bindFolders(folders);
3888 }
3889 };
3890 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003891 return;
3892 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003893 sFolders.clear();
3894 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896
3897 /**
3898 * Add the views for a widget to the workspace.
3899 *
3900 * Implementation of the method from LauncherModel.Callbacks.
3901 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003902 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003903 Runnable r = new Runnable() {
3904 public void run() {
3905 bindAppWidget(item);
3906 }
3907 };
3908 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003909 return;
3910 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003911
Daniel Sandler843e8602010-06-07 14:59:01 -04003912 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3913 if (DEBUG_WIDGETS) {
3914 Log.d(TAG, "bindAppWidget: " + item);
3915 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003916 final Workspace workspace = mWorkspace;
3917
Adam Cohen59400422014-03-05 18:07:04 -08003918 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003919 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003920
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003921 if (!mIsSafeModeEnabled
3922 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3923 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003924 if (appWidgetInfo == null) {
3925 if (DEBUG_WIDGETS) {
3926 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3927 + " belongs to component " + item.providerName
3928 + ", as the povider is null");
3929 }
3930 LauncherModel.deleteItemFromDatabase(this, item);
3931 return;
3932 }
3933 // Note: This assumes that the id remap broadcast is received before this step.
3934 // If that is not the case, the id remap will be ignored and user may see the
3935 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003936 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003937 pendingInfo.spanX = item.spanX;
3938 pendingInfo.spanY = item.spanY;
3939 pendingInfo.minSpanX = item.minSpanX;
3940 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003941 Bundle options = null;
3942 // AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003943
Sunny Goyalff572272014-07-23 13:58:07 -07003944 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003945 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3946 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003947
3948 // TODO consider showing a permission dialog when the widget is clicked.
3949 if (!success) {
3950 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3951 if (DEBUG_WIDGETS) {
3952 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3953 + " belongs to component " + item.providerName
3954 + ", as the launcher is unable to bing a new widget id");
3955 }
3956 LauncherModel.deleteItemFromDatabase(this, item);
3957 return;
3958 }
3959
3960 item.appWidgetId = newWidgetId;
3961
3962 // If the widget has a configure activity, it is still needs to set it up, otherwise
3963 // the widget is ready to go.
3964 item.restoreStatus = (appWidgetInfo.configure == null)
3965 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3966 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3967
3968 LauncherModel.updateItemInDatabase(this, item);
3969 }
3970
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003971 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003972 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003973 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003974 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3975 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003976 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003977
Sunny Goyal651077b2014-06-30 14:15:31 -07003978 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3979 } else {
3980 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003981 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3982 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003983 view.updateIcon(mIconCache);
3984 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003985 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003986 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003988
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003990 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003991
Adam Cohendcd297f2013-06-18 13:13:40 -07003992 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003994 if (!item.isCustomWidget()) {
3995 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3996 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003997
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 workspace.requestLayout();
3999
Daniel Sandler843e8602010-06-07 14:59:01 -04004000 if (DEBUG_WIDGETS) {
4001 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4002 + (SystemClock.uptimeMillis()-start) + "ms");
4003 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 }
4005
Sunny Goyalff572272014-07-23 13:58:07 -07004006 /**
4007 * Restores a pending widget.
4008 *
4009 * @param appWidgetId The app widget id
4010 * @param cellInfo The position on screen where to create the widget.
4011 */
4012 private void completeRestoreAppWidget(final int appWidgetId) {
4013 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4014 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4015 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4016 return;
4017 }
4018
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004019 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004020 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4021
4022 mWorkspace.reinflateWidgetsIfNecessary();
4023 LauncherModel.updateItemInDatabase(this, info);
4024 }
4025
Adam Cohen1462de32012-07-24 22:34:36 -07004026 public void onPageBoundSynchronously(int page) {
4027 mSynchronouslyBoundPages.add(page);
4028 }
4029
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030 /**
4031 * Callback saying that there aren't any more items to bind.
4032 *
4033 * Implementation of the method from LauncherModel.Callbacks.
4034 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004035 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004036 Runnable r = new Runnable() {
4037 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004038 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004039 }
4040 };
4041 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004042 return;
4043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004044 if (mSavedState != null) {
4045 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004046 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004047 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004048 mSavedState = null;
4049 }
4050
Adam Cohen1462de32012-07-24 22:34:36 -07004051 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004053 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004054 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004055
4056 // If we received the result of any pending adds while the loader was running (e.g. the
4057 // widget configuration forced an orientation change), process them now.
4058 if (sPendingAddItem != null) {
4059 final long screenId = completeAdd(sPendingAddItem);
4060
4061 // TODO: this moves the user to the page where the pending item was added. Ideally,
4062 // the screen would be guaranteed to exist after bind, and the page would be set through
4063 // the workspace restore process.
4064 mWorkspace.post(new Runnable() {
4065 @Override
4066 public void run() {
4067 mWorkspace.snapToScreenId(screenId);
4068 }
4069 });
4070 sPendingAddItem = null;
4071 }
4072
Sunny Goyale755d462014-07-22 13:48:29 -07004073 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004074
4075 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004076 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004077 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004078 }
4079
Adam Cohen3ed316a2014-07-23 18:21:20 -07004080 private void sendLoadingCompleteBroadcastIfNecessary() {
4081 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4082 String permission =
4083 getResources().getString(R.string.receive_first_load_broadcast_permission);
4084 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4085 sendBroadcast(intent, permission);
4086 SharedPreferences.Editor editor = mSharedPrefs.edit();
4087 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4088 editor.apply();
4089 }
4090 }
4091
Winson Chunga0b7e862013-09-05 16:03:15 -07004092 public boolean isAllAppsButtonRank(int rank) {
4093 if (mHotseat != null) {
4094 return mHotseat.isAllAppsButtonRank(rank);
4095 }
4096 return false;
4097 }
4098
Winson Chunga2413752012-04-03 14:22:34 -07004099 private boolean canRunNewAppsAnimation() {
4100 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4101 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4102 }
4103
Winson Chungc9168342013-06-26 14:54:55 -07004104 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4105 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4106 PropertyValuesHolder.ofFloat("alpha", 1f),
4107 PropertyValuesHolder.ofFloat("scaleX", 1f),
4108 PropertyValuesHolder.ofFloat("scaleY", 1f));
4109 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4110 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4111 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4112 return bounceAnim;
4113 }
4114
Adam Cohen27772732013-11-11 14:00:56 +00004115 public boolean useVerticalBarLayout() {
4116 return LauncherAppState.getInstance().getDynamicGrid().
4117 getDeviceProfile().isVerticalBarLayout();
4118 }
4119
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004120 protected Rect getSearchBarBounds() {
4121 return LauncherAppState.getInstance().getDynamicGrid().
4122 getDeviceProfile().getSearchBarBounds();
4123 }
4124
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004125 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004126 if (mSearchDropTargetBar == null) {
4127 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004128 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004129 if (mQsb != null) {
4130 mSearchDropTargetBar.removeView(mQsb);
4131 mQsb = null;
4132 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004133 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004134 }
4135
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004136 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004137 * Add the icons for all apps.
4138 *
4139 * Implementation of the method from LauncherModel.Callbacks.
4140 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004141 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004142 if (mAppsView != null) {
4143 mAppsView.setApps(apps);
4144 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07004145 if (mWidgetsView != null) {
4146 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4147 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004148 }
Adam Cohen9211d422014-10-07 18:14:53 -07004149 if (mLauncherCallbacks != null) {
4150 mLauncherCallbacks.bindAllApplications(apps);
4151 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004152 }
4153
4154 /**
4155 * A package was updated.
4156 *
4157 * Implementation of the method from LauncherModel.Callbacks.
4158 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004159 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004160 Runnable r = new Runnable() {
4161 public void run() {
4162 bindAppsUpdated(apps);
4163 }
4164 };
4165 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004166 return;
4167 }
4168
Winson Chungb745afb2015-03-02 11:51:23 -08004169 if (mAppsView != null) {
4170 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004171 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004172 }
4173
Sunny Goyal4390ace2014-10-13 11:33:11 -07004174 @Override
4175 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4176 Runnable r = new Runnable() {
4177 public void run() {
4178 bindWidgetsRestored(widgets);
4179 }
4180 };
4181 if (waitUntilResume(r)) {
4182 return;
4183 }
4184 mWorkspace.widgetsRestored(widgets);
4185 }
4186
Joe Onorato9c1289c2009-08-17 11:03:03 -04004187 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004188 * Some shortcuts were updated in the background.
4189 *
4190 * Implementation of the method from LauncherModel.Callbacks.
4191 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004192 @Override
4193 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4194 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004195 Runnable r = new Runnable() {
4196 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004197 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004198 }
4199 };
4200 if (waitUntilResume(r)) {
4201 return;
4202 }
4203
Sunny Goyal4390ace2014-10-13 11:33:11 -07004204 if (!updated.isEmpty()) {
4205 mWorkspace.updateShortcuts(updated);
4206 }
4207
4208 if (!removed.isEmpty()) {
4209 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4210 for (ShortcutInfo si : removed) {
4211 removedComponents.add(si.getTargetComponent());
4212 }
4213 mWorkspace.removeItemsByComponentName(removedComponents, user);
4214 // Notify the drag controller
4215 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004216 }
4217 }
4218
4219 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004220 * Update the state of a package, typically related to install state.
4221 *
4222 * Implementation of the method from LauncherModel.Callbacks.
4223 */
Sunny Goyale755d462014-07-22 13:48:29 -07004224 @Override
4225 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004226 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004227 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004228 }
4229 }
4230
4231 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004232 * Update the label and icon of all the icons in a package
4233 *
4234 * Implementation of the method from LauncherModel.Callbacks.
4235 */
4236 @Override
4237 public void updatePackageBadge(String packageName) {
4238 if (mWorkspace != null) {
4239 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4240 }
4241 }
4242
4243 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004244 * A package was uninstalled. We take both the super set of packageNames
4245 * in addition to specific applications to remove, the reason being that
4246 * this can be called when a package is updated as well. In that scenario,
4247 * we only remove specific components from the workspace, where as
4248 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004249 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004250 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004251 * Implementation of the method from LauncherModel.Callbacks.
4252 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004253 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004254 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004255 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004256 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004257 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004258 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004259 }
Winson Chungd64d1762013-08-20 14:37:16 -07004260 };
4261 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004262 return;
4263 }
4264
Sunny Goyal1a745e82014-10-02 15:58:31 -07004265 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004266 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4267 for (AppInfo info : appInfos) {
4268 removedComponents.add(info.componentName);
4269 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004270 if (!packageNames.isEmpty()) {
4271 mWorkspace.removeItemsByPackageName(packageNames, user);
4272 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004273 if (!removedComponents.isEmpty()) {
4274 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004275 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004276 // Notify the drag controller
4277 mDragController.onAppsRemoved(packageNames, removedComponents);
4278
Sunny Goyal1a745e82014-10-02 15:58:31 -07004279 } else {
4280 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004281 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004282
Winson Chungdf95eb12013-10-16 14:57:07 -07004283 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004284 if (mAppsView != null) {
4285 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004286 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004287 }
Joe Onoratobe386092009-11-17 17:32:16 -08004288
4289 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004290 * A number of packages were updated.
4291 */
Adam Cohen091440a2015-03-18 14:16:05 -07004292 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004293 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004294 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004295 bindPackagesUpdated(mWidgetsAndShortcuts);
4296 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004297 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004298 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004299
Michael Jurkac402cd92013-05-20 15:49:32 +02004300 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4301 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4302 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004303 return;
4304 }
4305
Hyunyoung Song3f471442015-04-08 19:01:34 -07004306 if (mWidgetsView != null) {
4307 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4308 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004309 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004310 }
4311
Winson Chung400438b2011-01-16 17:53:48 -08004312 private int mapConfigurationOriActivityInfoOri(int configOri) {
4313 final Display d = getWindowManager().getDefaultDisplay();
4314 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4315 switch (d.getRotation()) {
4316 case Surface.ROTATION_0:
4317 case Surface.ROTATION_180:
4318 // We are currently in the same basic orientation as the natural orientation
4319 naturalOri = configOri;
4320 break;
4321 case Surface.ROTATION_90:
4322 case Surface.ROTATION_270:
4323 // We are currently in the other basic orientation to the natural orientation
4324 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4325 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4326 break;
4327 }
4328
4329 int[] oriMap = {
4330 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4331 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4332 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4333 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4334 };
4335 // Since the map starts at portrait, we need to offset if this device's natural orientation
4336 // is landscape.
4337 int indexOffset = 0;
4338 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4339 indexOffset = 1;
4340 }
4341 return oriMap[(d.getRotation() + indexOffset) % 4];
4342 }
Adam Cohen446e9402011-09-15 18:21:21 -07004343
Winson Chung4b919f82012-05-01 10:44:08 -07004344 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004345 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004346 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4347 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4348 .getConfiguration().orientation));
4349 } else {
4350 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4351 }
Winson Chung4b919f82012-05-01 10:44:08 -07004352 }
4353 }
4354 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004355 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004356 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004357 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004358 } else {
4359 mHandler.postDelayed(new Runnable() {
4360 public void run() {
4361 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4362 }
4363 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004364 }
Winson Chung4b919f82012-05-01 10:44:08 -07004365 }
Winson Chung400438b2011-01-16 17:53:48 -08004366 }
4367
Winson Chunge43a1e72014-01-15 10:33:02 -08004368 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004369 if (mLauncherCallbacks != null) {
4370 return mLauncherCallbacks.isLauncherPreinstalled();
4371 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004372 PackageManager pm = getPackageManager();
4373 try {
4374 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4375 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4376 return true;
4377 } else {
4378 return false;
4379 }
4380 } catch (NameNotFoundException e) {
4381 e.printStackTrace();
4382 return false;
4383 }
4384 }
4385
Adam Cohenea90f832014-05-21 15:03:34 -07004386 /**
4387 * This method indicates whether or not we should suggest default wallpaper dimensions
4388 * when our wallpaper cropper was not yet used to set a wallpaper.
4389 */
4390 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004391 if (mLauncherCallbacks != null) {
4392 return mLauncherCallbacks.overrideWallpaperDimensions();
4393 }
Adam Cohenea90f832014-05-21 15:03:34 -07004394 return true;
4395 }
4396
Adam Cohen432609a2014-03-13 17:03:22 -07004397 /**
4398 * To be overridden by subclasses to indicate that there is an activity to launch
4399 * before showing the standard launcher experience.
4400 */
4401 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004402 if (mLauncherCallbacks != null) {
4403 return mLauncherCallbacks.hasFirstRunActivity();
4404 }
Adam Cohen432609a2014-03-13 17:03:22 -07004405 return false;
4406 }
4407
4408 /**
4409 * To be overridden by subclasses to launch any first run activity
4410 */
4411 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004412 if (mLauncherCallbacks != null) {
4413 return mLauncherCallbacks.getFirstRunActivity();
4414 }
Adam Cohen432609a2014-03-13 17:03:22 -07004415 return null;
4416 }
4417
Winson Chunga6945242014-01-08 14:04:34 -08004418 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004419 return !ActivityManager.isRunningInTestHarness() &&
4420 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004421 }
4422
Adam Cohen4a9423d2014-03-28 14:22:31 -07004423 protected boolean hasRunFirstRunActivity() {
4424 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4425 }
4426
Adam Cohen432609a2014-03-13 17:03:22 -07004427 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004428 if (shouldRunFirstRunActivity() &&
4429 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004430 Intent firstRunIntent = getFirstRunActivity();
4431 if (firstRunIntent != null) {
4432 startActivity(firstRunIntent);
4433 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004434 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004435 }
4436 }
Adam Cohen432609a2014-03-13 17:03:22 -07004437 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004438 }
4439
4440 private void markFirstRunActivityShown() {
4441 SharedPreferences.Editor editor = mSharedPrefs.edit();
4442 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4443 editor.apply();
4444 }
4445
Adam Cohen432609a2014-03-13 17:03:22 -07004446 /**
4447 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4448 * screen that must be displayed and dismissed.
4449 */
4450 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004451 if (mLauncherCallbacks != null) {
4452 return mLauncherCallbacks.hasDismissableIntroScreen();
4453 }
Adam Cohen432609a2014-03-13 17:03:22 -07004454 return false;
4455 }
4456
4457 /**
4458 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4459 */
4460 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004461 if (mLauncherCallbacks != null) {
4462 return mLauncherCallbacks.getIntroScreen();
4463 }
Adam Cohen432609a2014-03-13 17:03:22 -07004464 return null;
4465 }
4466
4467 /**
4468 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4469 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4470 */
4471 private boolean shouldShowIntroScreen() {
4472 return hasDismissableIntroScreen() &&
4473 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4474 }
4475
4476 protected void showIntroScreen() {
4477 View introScreen = getIntroScreen();
4478 changeWallpaperVisiblity(false);
4479 if (introScreen != null) {
4480 mDragLayer.showOverlayView(introScreen);
4481 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004482 if (mLauncherOverlayContainer != null) {
4483 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4484 }
Adam Cohen432609a2014-03-13 17:03:22 -07004485 }
4486
4487 public void dismissIntroScreen() {
4488 markIntroScreenDismissed();
4489 if (showFirstRunActivity()) {
4490 // We delay hiding the intro view until the first run activity is showing. This
4491 // avoids a blip.
4492 mWorkspace.postDelayed(new Runnable() {
4493 @Override
4494 public void run() {
4495 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004496 if (mLauncherOverlayContainer != null) {
4497 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4498 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004499 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004500 }
4501 }, ACTIVITY_START_DELAY);
4502 } else {
4503 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004504 if (mLauncherOverlayContainer != null) {
4505 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4506 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004507 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004508 }
4509 changeWallpaperVisiblity(true);
4510 }
4511
4512 private void markIntroScreenDismissed() {
4513 SharedPreferences.Editor editor = mSharedPrefs.edit();
4514 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4515 editor.apply();
4516 }
4517
Adam Cohen091440a2015-03-18 14:16:05 -07004518 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004519 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4520 // on the device, then we always show the first run cling experience (or if there is no
4521 // launcher2). Otherwise, we prompt the user upon started for migration
4522 LauncherClings launcherClings = new LauncherClings(this);
4523 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4524 if (mModel.canMigrateFromOldLauncherDb(this)) {
4525 launcherClings.showMigrationCling();
4526 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004527 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004528 }
4529 }
4530 }
4531
Winson Chunga6945242014-01-08 14:04:34 -08004532 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004533 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4534 if (mHotseat != null) mHotseat.setAlpha(1f);
4535 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4536 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004537 }
4538
4539 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004540 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4541 if (mHotseat != null) mHotseat.setAlpha(0f);
4542 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4543 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004544 }
4545
Mathew Inwood72fbec12013-11-19 15:45:07 +00004546 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004547 // Called from search suggestion, not supported in other profiles.
4548 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4549 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4550 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4551 myUser);
4552 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004553 return null;
4554 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004555 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004556 }
4557
4558 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4559 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004560 // Called from search suggestion, not supported in other profiles.
4561 return createShortcutDragInfo(shortcutIntent, caption, icon,
4562 UserHandleCompat.myUserHandle());
4563 }
4564
4565 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4566 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004567 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004568 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004569 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004570 }
4571
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004572 protected void moveWorkspaceToDefaultScreen() {
4573 mWorkspace.moveToDefaultScreen(false);
4574 }
4575
Mathew Inwood72fbec12013-11-19 15:45:07 +00004576 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4577 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004578 mWorkspace.onExternalDragStartedWithItem(dragView);
4579 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004580 }
4581
Anjali Koppalf5d29132014-02-28 13:33:27 -08004582 @Override
4583 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004584 if (mLauncherCallbacks != null) {
4585 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4586 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004587 }
4588
Winson Chung80baf5a2010-08-09 16:03:15 -07004589 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004590 * Prints out out state for debugging.
4591 */
4592 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004593 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004594 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004595 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4596 Log.d(TAG, "mRestoring=" + mRestoring);
4597 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4598 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004599 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004600 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004601 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004602
Daniel Sandler325dc232013-06-05 22:57:57 -04004603 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004604 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004605
4606 @Override
4607 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4608 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004609 synchronized (sDumpLogs) {
4610 writer.println(" ");
4611 writer.println("Debug logs: ");
4612 for (int i = 0; i < sDumpLogs.size(); i++) {
4613 writer.println(" " + sDumpLogs.get(i));
4614 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004615 }
Adam Cohen9211d422014-10-07 18:14:53 -07004616 if (mLauncherCallbacks != null) {
4617 mLauncherCallbacks.dump(prefix, fd, writer, args);
4618 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004619 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004620
4621 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004622 if (DEBUG_DUMP_LOG) {
4623 synchronized (sDumpLogs) {
4624 Log.d(TAG, "");
4625 Log.d(TAG, "*********************");
4626 Log.d(TAG, "Launcher debug logs: ");
4627 for (int i = 0; i < sDumpLogs.size(); i++) {
4628 Log.d(TAG, " " + sDumpLogs.get(i));
4629 }
4630 Log.d(TAG, "*********************");
4631 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004632 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004633 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004634 }
4635
4636 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004637 addDumpLog(tag, log, null, debugLog);
4638 }
4639
4640 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004641 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004642 if (e != null) {
4643 Log.d(tag, log, e);
4644 } else {
4645 Log.d(tag, log);
4646 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004647 }
Winson Chungede41292013-09-19 16:27:36 -07004648 if (DEBUG_DUMP_LOG) {
4649 sDateStamp.setTime(System.currentTimeMillis());
4650 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004651 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4652 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004653 }
Winson Chungede41292013-09-19 16:27:36 -07004654 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004655 }
4656
Adam Cohen59400422014-03-05 18:07:04 -08004657 public static CustomAppWidget getCustomAppWidget(String name) {
4658 return sCustomAppWidgets.get(name);
4659 }
4660
4661 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4662 return sCustomAppWidgets;
4663 }
4664
Winson Chungede41292013-09-19 16:27:36 -07004665 public void dumpLogsToLocalData() {
4666 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004667 new AsyncTask<Void, Void, Void>() {
4668 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004669 boolean success = false;
4670 sDateStamp.setTime(sRunStart);
4671 String FILENAME = sDateStamp.getMonth() + "-"
4672 + sDateStamp.getDay() + "_"
4673 + sDateStamp.getHours() + "-"
4674 + sDateStamp.getMinutes() + "_"
4675 + sDateStamp.getSeconds() + ".txt";
4676
4677 FileOutputStream fos = null;
4678 File outFile = null;
4679 try {
4680 outFile = new File(getFilesDir(), FILENAME);
4681 outFile.createNewFile();
4682 fos = new FileOutputStream(outFile);
4683 } catch (Exception e) {
4684 e.printStackTrace();
4685 }
4686 if (fos != null) {
4687 PrintWriter writer = new PrintWriter(fos);
4688
4689 writer.println(" ");
4690 writer.println("Debug logs: ");
4691 synchronized (sDumpLogs) {
4692 for (int i = 0; i < sDumpLogs.size(); i++) {
4693 writer.println(" " + sDumpLogs.get(i));
4694 }
4695 }
4696 writer.close();
4697 }
4698 try {
4699 if (fos != null) {
4700 fos.close();
4701 success = true;
4702 }
4703 } catch (IOException e) {
4704 e.printStackTrace();
4705 }
Michael Jurka43467462013-11-14 12:03:58 +01004706 return null;
Winson Chungede41292013-09-19 16:27:36 -07004707 }
Michael Jurka43467462013-11-14 12:03:58 +01004708 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004709 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004711}
Michael Jurka2763be32011-02-24 11:19:57 -08004712
Michael Jurkaabded662011-03-04 12:06:57 -08004713interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004714 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004715 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004716 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004717 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004718 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004719}
Dan Sandlerd5024042014-01-09 15:01:33 -05004720
4721interface DebugIntents {
4722 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4723 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004724}