blob: 068934e1bacc1f75b5bf44cfc49b17d013c861d8 [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 Song3f471442015-04-08 19:01:34 -0700135 static final String TAG = "Launcher - MERONG";
136 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;
523 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700524 }
525
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700526 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700527 public void onLauncherProviderChange() {
528 if (mLauncherCallbacks != null) {
529 mLauncherCallbacks.onLauncherProviderChange();
530 }
531 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700532
Adam Cohen9211d422014-10-07 18:14:53 -0700533 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700534 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700535 if (mLauncherCallbacks != null) {
536 return mLauncherCallbacks.hasCustomContentToLeft();
537 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700538 return false;
539 }
540
Dave Hawkeya8881582013-09-17 15:55:33 -0600541 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500542 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600543 * {@link #addToCustomContentPage}. This will only be invoked if
544 * {@link #hasCustomContentToLeft()} is {@code true}.
545 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500546 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700547 if (mLauncherCallbacks != null) {
548 mLauncherCallbacks.populateCustomContentContainer();
549 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600550 }
551
552 /**
553 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
554 * ensure the custom content page is added or removed if necessary.
555 */
556 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600557 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600558 // Not bound yet, wait for bindScreens to be called.
559 return;
560 }
561
562 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
563 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500564 mWorkspace.createCustomContentContainer();
565 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600566 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
567 mWorkspace.removeCustomContentPage();
568 }
569 }
570
Adam Cohen091440a2015-03-18 14:16:05 -0700571 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700572 if (sLocaleConfiguration == null) {
573 new AsyncTask<Void, Void, LocaleConfiguration>() {
574 @Override
575 protected LocaleConfiguration doInBackground(Void... unused) {
576 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
577 readConfiguration(Launcher.this, localeConfiguration);
578 return localeConfiguration;
579 }
580
581 @Override
582 protected void onPostExecute(LocaleConfiguration result) {
583 sLocaleConfiguration = result;
584 checkForLocaleChange(); // recursive, but now with a locale configuration
585 }
586 }.execute();
587 return;
588 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700589
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 final Configuration configuration = getResources().getConfiguration();
591
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700592 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 final String locale = configuration.locale.toString();
594
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700595 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 final int mcc = configuration.mcc;
597
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700598 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 final int mnc = configuration.mnc;
600
Romain Guy84f296c2009-11-04 15:00:44 -0800601 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602
Romain Guy84f296c2009-11-04 15:00:44 -0800603 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 sLocaleConfiguration.locale = locale;
605 sLocaleConfiguration.mcc = mcc;
606 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700607
Joe Onorato0589f0f2010-02-08 13:44:00 -0800608 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700609
610 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100611 new AsyncTask<Void, Void, Void>() {
612 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700613 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100614 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700615 }
Michael Jurka43467462013-11-14 12:03:58 +0100616 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700617 }
618 }
619
Adam Cohen091440a2015-03-18 14:16:05 -0700620 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700621 public String locale;
622 public int mcc = -1;
623 public int mnc = -1;
624 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700625
Adam Cohen091440a2015-03-18 14:16:05 -0700626 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700627 DataInputStream in = null;
628 try {
Helena Josol28db2802014-10-09 17:04:09 +0100629 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700630 configuration.locale = in.readUTF();
631 configuration.mcc = in.readInt();
632 configuration.mnc = in.readInt();
633 } catch (FileNotFoundException e) {
634 // Ignore
635 } catch (IOException e) {
636 // Ignore
637 } finally {
638 if (in != null) {
639 try {
640 in.close();
641 } catch (IOException e) {
642 // Ignore
643 }
644 }
645 }
646 }
647
Adam Cohen091440a2015-03-18 14:16:05 -0700648 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700649 DataOutputStream out = null;
650 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100651 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100652 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700653 out.writeUTF(configuration.locale);
654 out.writeInt(configuration.mcc);
655 out.writeInt(configuration.mnc);
656 out.flush();
657 } catch (FileNotFoundException e) {
658 // Ignore
659 } catch (IOException e) {
660 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100661 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700662 } finally {
663 if (out != null) {
664 try {
665 out.close();
666 } catch (IOException e) {
667 // Ignore
668 }
669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671 }
672
Anton Hanssona2f665f2013-09-26 12:18:32 +0100673 public Stats getStats() {
674 return mStats;
675 }
676
Bjorn Bringertc459e522013-06-07 19:36:01 +0100677 public LayoutInflater getInflater() {
678 return mInflater;
679 }
680
Hyunyoung Song3f471442015-04-08 19:01:34 -0700681 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700682 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
683 // that is subsequently removed from the workspace in startBinding().
684 return !mModel.isLoadingWorkspace();
685 }
686
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800687 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000688 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100689 if (Build.VERSION.SDK_INT >= 17) {
690 return View.generateViewId();
691 } else {
692 // View.generateViewId() is not available. The following fallback logic is a copy
693 // of its implementation.
694 for (;;) {
695 final int result = sNextGeneratedId.get();
696 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
697 int newValue = result + 1;
698 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
699 if (sNextGeneratedId.compareAndSet(result, newValue)) {
700 return result;
701 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000702 }
703 }
704 }
705
706 public int getViewIdForItem(ItemInfo info) {
707 // This cast is safe given the > 2B range for int.
708 int itemId = (int) info.id;
709 if (mItemIdToViewId.containsKey(itemId)) {
710 return mItemIdToViewId.get(itemId);
711 }
712 int viewId = generateViewId();
713 mItemIdToViewId.put(itemId, viewId);
714 return viewId;
715 }
716
717 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700718 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
719 * a configuration step, this allows the proper animations to run after other transitions.
720 */
Adam Cohendb364c32014-05-20 14:23:40 -0700721 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700722 long screenId = args.screenId;
723 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
724 // When the screen id represents an actual screen (as opposed to a rank) we make sure
725 // that the drop page actually exists.
726 screenId = ensurePendingDropLayoutExists(args.screenId);
727 }
Adam Cohendb364c32014-05-20 14:23:40 -0700728
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800729 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800730 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700731 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700733 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800734 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700735 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700736 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700737 case REQUEST_RECONFIGURE_APPWIDGET:
738 completeRestoreAppWidget(args.appWidgetId);
739 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 }
Michael Jurka27614d22012-04-02 06:40:22 -0700741 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
742 // if you turned the screen off and then back while in All Apps, Launcher would not
743 // return to the workspace. Clearing mAddInfo.container here fixes this issue
744 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700745 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800746 }
747
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800748 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700749 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700750 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700751 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700752 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800753 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700754
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 Runnable exitSpringLoaded = new Runnable() {
756 @Override
757 public void run() {
758 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
759 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
760 }
761 };
762
Michael Jurka8b805b12012-04-18 14:23:14 -0700763 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
766 if (resultCode == RESULT_CANCELED) {
767 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700768 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700770 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800771 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700772 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 }
774 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200775 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
776 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
777 mWorkspace.exitOverviewMode(false);
778 }
779 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700780 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200781
Adam Cohened66b2b2012-01-23 17:28:51 -0800782 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
783 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700784
Adam Cohendb364c32014-05-20 14:23:40 -0700785 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800786 // We have special handling for widgets
787 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800788 final int appWidgetId;
789 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
790 : -1;
791 if (widgetId < 0) {
792 appWidgetId = pendingAddWidgetId;
793 } else {
794 appWidgetId = widgetId;
795 }
796
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800798 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700799 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
800 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700801 result = RESULT_CANCELED;
802 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700803 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700804 @Override
805 public void run() {
806 exitSpringLoadedDragModeDelayed(false, 0, null);
807 }
808 };
Adam Cohendb364c32014-05-20 14:23:40 -0700809 if (workspaceLocked) {
810 // No need to remove the empty screen if we're mid-binding, as the
811 // the bind will not add the empty screen.
812 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
813 } else {
814 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
815 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
816 }
Winson Chung5aaab772012-04-27 15:24:02 -0700817 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700818 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700819 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
820 // When the screen id represents an actual screen (as opposed to a rank)
821 // we make sure that the drop page actually exists.
822 mPendingAddInfo.screenId =
823 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
824 }
Adam Cohendb364c32014-05-20 14:23:40 -0700825 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
826
827 dropLayout.setDropPending(true);
828 final Runnable onComplete = new Runnable() {
829 @Override
830 public void run() {
831 completeTwoStageWidgetDrop(resultCode, appWidgetId);
832 dropLayout.setDropPending(false);
833 }
834 };
835 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
836 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
837 } else {
838 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
839 mPendingAddInfo);
840 sPendingAddItem = args;
841 }
Winson Chung5aaab772012-04-27 15:24:02 -0700842 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800843 return;
844 }
845
Sunny Goyalff572272014-07-23 13:58:07 -0700846 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
847 if (resultCode == RESULT_OK) {
848 // Update the widget view.
849 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
850 pendingAddWidgetId, mPendingAddInfo);
851 if (workspaceLocked) {
852 sPendingAddItem = args;
853 } else {
854 completeAdd(args);
855 }
856 }
857 // Leave the widget in the pending state if the user canceled the configure.
858 return;
859 }
860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 // The pattern used here is that a user PICKs a specific application,
862 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
865 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700866 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700867 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
868 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800869 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700870 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800871 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700872 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700873 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
874 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 }
Adam Cohen00074722013-10-11 17:46:09 -0700876 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700877 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700878 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800881
882 }
883
884 @Override
885 protected void onActivityResult(
886 final int requestCode, final int resultCode, final Intent data) {
887 handleActivityResult(requestCode, resultCode, data);
888 if (mLauncherCallbacks != null) {
889 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
890 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 }
892
Adam Cohendb364c32014-05-20 14:23:40 -0700893 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
894 appWidgetId, ItemInfo info) {
895 PendingAddArguments args = new PendingAddArguments();
896 args.requestCode = requestCode;
897 args.intent = data;
898 args.container = info.container;
899 args.screenId = info.screenId;
900 args.cellX = info.cellX;
901 args.cellY = info.cellY;
902 args.appWidgetId = appWidgetId;
903 return args;
904 }
905
906 /**
907 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
908 *
909 * @param screenId the screen id to check
910 * @return the new screen, or screenId if it exists
911 */
912 private long ensurePendingDropLayoutExists(long screenId) {
913 CellLayout dropLayout =
914 (CellLayout) mWorkspace.getScreenWithId(screenId);
915 if (dropLayout == null) {
916 // it's possible that the add screen was removed because it was
917 // empty and a re-bind occurred
918 mWorkspace.addExtraEmptyScreen();
919 return mWorkspace.commitExtraEmptyScreen();
920 } else {
921 return screenId;
922 }
923 }
924
Adam Cohen091440a2015-03-18 14:16:05 -0700925 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700926 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700927 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 Runnable onCompleteRunnable = null;
929 int animationType = 0;
930
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700931 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 if (resultCode == RESULT_OK) {
933 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
934 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 mPendingAddWidgetInfo);
936 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 onCompleteRunnable = new Runnable() {
938 @Override
939 public void run() {
940 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700941 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700942 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
943 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 }
945 };
946 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800947 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 if (mDragLayer.getAnimatedView() != null) {
951 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
952 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
953 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700954 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700955 // The animated view may be null in the case of a rotation during widget configuration
956 onCompleteRunnable.run();
957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
960 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700961 protected void onStop() {
962 super.onStop();
963 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700964
965 if (mLauncherCallbacks != null) {
966 mLauncherCallbacks.onStop();
967 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700968 }
969
970 @Override
971 protected void onStart() {
972 super.onStart();
973 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700974
975 if (mLauncherCallbacks != null) {
976 mLauncherCallbacks.onStart();
977 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700978 }
979
980 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200982 long startTime = 0;
983 if (DEBUG_RESUME_TIME) {
984 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400985 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200986 }
Adam Cohen9211d422014-10-07 18:14:53 -0700987
988 if (mLauncherCallbacks != null) {
989 mLauncherCallbacks.preOnResume();
990 }
991
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700993
Winson Chung4a2afa32012-07-19 14:53:05 -0700994 // Restore the previous launcher state
Winson Chungb745afb2015-03-02 11:51:23 -0800995 if (mOnResumeState == State.WORKSPACE || mOnResumeState == State.NONE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700996 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800997 } else if (mOnResumeState == State.APPS) {
998 showAppsView(false /* animated */, false /* resetListToTop */);
999 } else if (mOnResumeState == State.WIDGETS) {
1000 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001001 }
1002 mOnResumeState = State.NONE;
1003
Craig Mautner360310b2012-10-26 15:13:08 -07001004 // Background was set to gradient in onPause(), restore to black if in all apps.
1005 setWorkspaceBackground(mState == State.WORKSPACE);
1006
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001007 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001008 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001009 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001010 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001012 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1016 // execute them here
1017 long startTimeCallbacks = 0;
1018 if (DEBUG_RESUME_TIME) {
1019 startTimeCallbacks = System.currentTimeMillis();
1020 }
1021
Michael Jurka1e2f4652013-07-08 18:03:46 -07001022 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1023 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001024 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001025 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 if (DEBUG_RESUME_TIME) {
1027 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1028 (System.currentTimeMillis() - startTimeCallbacks));
1029 }
Michael Jurka447bf842013-05-15 14:52:15 +02001030 }
Michael Jurka54554252013-08-01 12:52:23 +02001031 if (mOnResumeCallbacks.size() > 0) {
1032 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1033 mOnResumeCallbacks.get(i).run();
1034 }
1035 mOnResumeCallbacks.clear();
1036 }
Winson Chunge4e50662012-01-23 14:45:13 -08001037
1038 // Reset the pressed state of icons that were locked in the press state while activities
1039 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001040 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001041 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001042 mWaitingForResume.setStayPressed(false);
1043 }
Winson Chung82963d52013-10-09 11:20:57 -07001044
Adam Cohen06dff352012-06-01 17:17:08 -07001045 // It is possible that widgets can receive updates while launcher is not in the foreground.
1046 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1047 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1048 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001049 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001050 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001051
Winson Chung780fe592013-09-26 14:48:44 -07001052 // Process any items that were added while Launcher was away.
1053 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1054
Michael Jurka447bf842013-05-15 14:52:15 +02001055 if (DEBUG_RESUME_TIME) {
1056 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1057 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001058
1059 if (mWorkspace.getCustomContentCallbacks() != null) {
1060 // If we are resuming and the custom content is the current page, we call onShow().
1061 // It is also poassible that onShow will instead be called slightly after first layout
1062 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1063 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001064 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001065 }
1066 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001067 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001068 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001069
1070 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001071
1072 if (mLauncherCallbacks != null) {
1073 mLauncherCallbacks.onResume();
1074 }
Adam Cohen06dff352012-06-01 17:17:08 -07001075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001079 // Ensure that items added to Launcher are queued until Launcher returns
1080 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001081 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001082
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001083 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001084 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001085 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001086 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001087
1088 // We call onHide() aggressively. The custom content callbacks should be able to
1089 // debounce excess onHide calls.
1090 if (mWorkspace.getCustomContentCallbacks() != null) {
1091 mWorkspace.getCustomContentCallbacks().onHide();
1092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Adam Cohen9211d422014-10-07 18:14:53 -07001094 if (mLauncherCallbacks != null) {
1095 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001097 }
1098
1099 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001100 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1101 // by a onResume or by scrolling otherwise.
1102 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001103
1104 // Custom content is completely hidden
1105 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001106
1107 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1108 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001109
1110 // Indicates whether the user is allowed to scroll away from the custom content.
1111 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
1113
Adam Cohenc2d6e892014-10-16 09:49:24 -07001114 public interface LauncherOverlay {
1115
1116 /**
1117 * Touch interaction leading to overscroll has begun
1118 */
1119 public void onScrollInteractionBegin();
1120
1121 /**
1122 * Touch interaction related to overscroll has ended
1123 */
1124 public void onScrollInteractionEnd();
1125
1126 /**
1127 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1128 * screen (or in the case of RTL, the rightmost screen).
1129 */
1130 public void onScrollChange(int progress, boolean rtl);
1131
1132 /**
1133 * Screen has stopped scrolling
1134 */
1135 public void onScrollSettled();
1136
1137 /**
1138 * This method can be called by the Launcher in order to force the LauncherOverlay
1139 * to exit fully immersive mode.
1140 */
1141 public void forceExitFullImmersion();
1142 }
1143
1144 public interface LauncherOverlayCallbacks {
1145 /**
1146 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1147 * however it doesn't modify any state within the launcher.
1148 */
1149 public boolean canEnterFullImmersion();
1150
1151 /**
1152 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1153 * eg. by occupying the full screen and handling all touch events.
1154 *
1155 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1156 * case, Launcher will modify any necessary state and assumes the overlay is
1157 * handling all interaction. If false, the LauncherOverlay should cancel any
1158 *
1159 */
1160 public boolean enterFullImmersion();
1161
1162 /**
1163 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1164 * full control over UI and state.
1165 */
1166 public void exitFullImmersion();
1167 }
1168
1169 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1170
1171 @Override
1172 public boolean canEnterFullImmersion() {
1173 return mState == State.WORKSPACE;
1174 }
1175
1176 @Override
1177 public boolean enterFullImmersion() {
1178 if (mState == State.WORKSPACE) {
1179 // When fully immersed, disregard any touches which fall through.
1180 mDragLayer.setBlockTouch(true);
1181 return true;
1182 }
1183 return false;
1184 }
1185
1186 @Override
1187 public void exitFullImmersion() {
1188 mDragLayer.setBlockTouch(false);
1189 }
1190 }
1191
Jorim Jaggid017f882014-01-14 17:08:48 -08001192 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001193 if (mLauncherCallbacks != null) {
1194 return mLauncherCallbacks.hasSettings();
1195 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001196 return false;
1197 }
1198
Adam Cohenbffe7452013-07-22 18:21:45 -07001199
Adam Cohen9211d422014-10-07 18:14:53 -07001200 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001201 CustomContentCallbacks callbacks, String description) {
1202 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001203 }
1204
Adam Cohenedb40762013-07-18 16:45:45 -07001205 // The custom content needs to offset its content to account for the QSB
1206 public int getTopOffsetForCustomContent() {
1207 return mWorkspace.getPaddingTop();
1208 }
1209
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001210 @Override
1211 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001213 if (mModel.isCurrentCallbacks(this)) {
1214 mModel.stopLoader();
1215 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001216 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1217
Romain Guy13c2e7b2010-03-10 19:45:00 -08001218 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001219 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001220
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001222 @Override
1223 public void onWindowFocusChanged(boolean hasFocus) {
1224 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001225 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001226
1227 if (mLauncherCallbacks != null) {
1228 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1229 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001230 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 private boolean acceptFilter() {
1233 final InputMethodManager inputManager = (InputMethodManager)
1234 getSystemService(Context.INPUT_METHOD_SERVICE);
1235 return !inputManager.isFullscreenMode();
1236 }
1237
1238 @Override
1239 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001240 final int uniChar = event.getUnicodeChar();
1241 final boolean handled = super.onKeyDown(keyCode, event);
1242 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1243 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1245 keyCode, event);
1246 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001247 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001248 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001249 // showSearchDialog()
1250 // If there are multiple keystrokes before the search dialog takes focus,
1251 // onSearchRequested() will be called for every keystroke,
1252 // but it is idempotent, so it's fine.
1253 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255 }
1256
Joe Onorato8a9625e2010-01-28 15:55:35 -08001257 // Eat the long press event so the keyboard doesn't come up.
1258 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1259 return true;
1260 }
1261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 return handled;
1263 }
1264
Karl Rosaen138a0412009-04-23 19:00:21 -07001265 private String getTypedText() {
1266 return mDefaultKeySsb.toString();
1267 }
1268
1269 private void clearTypedText() {
1270 mDefaultKeySsb.clear();
1271 mDefaultKeySsb.clearSpans();
1272 Selection.setSelection(mDefaultKeySsb, 0);
1273 }
1274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001276 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1277 * State
1278 */
1279 private static State intToState(int stateOrdinal) {
1280 State state = State.WORKSPACE;
1281 final State[] stateValues = State.values();
1282 for (int i = 0; i < stateValues.length; i++) {
1283 if (stateValues[i].ordinal() == stateOrdinal) {
1284 state = stateValues[i];
1285 break;
1286 }
1287 }
1288 return state;
1289 }
1290
1291 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 * Restores the previous state, if it exists.
1293 *
1294 * @param savedState The previous state.
1295 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001296 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 private void restoreState(Bundle savedState) {
1298 if (savedState == null) {
1299 return;
1300 }
1301
Michael Jurka883f55b2010-10-21 15:47:14 -07001302 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001303 if (state == State.APPS || state == State.WIDGETS) {
1304 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001305 }
1306
Adam Cohen21cd0022013-10-09 18:57:02 -07001307 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1308 PagedView.INVALID_RESTORE_PAGE);
1309 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001310 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312
Winson Chung3d503fb2011-07-13 17:25:49 -07001313 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001314 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001315
Winson Chung3d503fb2011-07-13 17:25:49 -07001316 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1317 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001318 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001319 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1320 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001321 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1322 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001323 AppWidgetProviderInfo info = savedState.getParcelable(
1324 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1325 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001326 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001327 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 mRestoring = true;
1329 }
1330
1331 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1332 if (renameFolder) {
1333 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001334 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 mRestoring = true;
1336 }
Winson Chunga12a2502010-12-20 14:41:35 -08001337
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001338 mItemIdToViewId = (HashMap<Integer, Integer>)
1339 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 }
1341
1342 /**
1343 * Finds all the views we need and configure them properly.
1344 */
1345 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001346 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001347
Craig Mautner360310b2012-10-26 15:13:08 -07001348 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001349 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001350 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1351 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001352 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001353 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001354
John Spurlock77e1f472013-09-11 10:09:51 -04001355 mLauncherView.setSystemUiVisibility(
1356 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001357 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358
Winson Chung4c98d922011-05-31 16:50:48 -07001359 // Setup the drag layer
1360 mDragLayer.setup(this, dragController);
1361
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 // Setup the hotseat
1363 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1364 if (mHotseat != null) {
1365 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001366 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001367 }
1368
Jorim Jaggid017f882014-01-14 17:08:48 -08001369 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001370 View widgetButton = findViewById(R.id.widget_button);
1371 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001372 @Override
1373 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001374 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001375 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001376 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001377 }
1378 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001379 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1380
1381 View wallpaperButton = findViewById(R.id.wallpaper_button);
1382 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001383 @Override
1384 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001385 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001386 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001387 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001388 }
1389 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001390 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1391
1392 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001393 if (hasSettings()) {
1394 settingsButton.setOnClickListener(new OnClickListener() {
1395 @Override
1396 public void onClick(View arg0) {
1397 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001398 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001399 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001400 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001401 });
1402 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1403 } else {
1404 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001405 }
1406
Adam Cohendbdff6b2013-09-18 19:09:15 -07001407 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001408
Winson Chung4c98d922011-05-31 16:50:48 -07001409 // Setup the workspace
1410 mWorkspace.setHapticFeedbackEnabled(false);
1411 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001412 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001413 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001414
Winson Chungf0ea4d32011-06-06 14:27:16 -07001415 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001416 mSearchDropTargetBar = (SearchDropTargetBar)
1417 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001418
Winson Chungb745afb2015-03-02 11:51:23 -08001419 // Setup Apps
1420 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
1421
Winson Chungf0ea4d32011-06-06 14:27:16 -07001422 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001423 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001424
Winson Chung3d503fb2011-07-13 17:25:49 -07001425 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001426 dragController.setDragScoller(mWorkspace);
1427 dragController.setScrollView(mDragLayer);
1428 dragController.setMoveTarget(mWorkspace);
1429 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001430 if (mSearchDropTargetBar != null) {
1431 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001432 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001433 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001434
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001435 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001436 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001437 mWeightWatcher = new WeightWatcher(this);
1438 mWeightWatcher.setAlpha(0.5f);
1439 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001440 new FrameLayout.LayoutParams(
1441 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001442 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001443 Gravity.BOTTOM)
1444 );
Adam Cohen39a06042013-07-19 14:30:12 -07001445
1446 boolean show = shouldShowWeightWatcher();
1447 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001448 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 }
1450
1451 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001452 * Sets the all apps button. This method is called from {@link Hotseat}.
1453 */
1454 public void setAllAppsButton(View allAppsButton) {
1455 mAllAppsButton = allAppsButton;
1456 }
1457
1458 public View getAllAppsButton() {
1459 return mAllAppsButton;
1460 }
1461
1462 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 * Creates a view representing a shortcut.
1464 *
1465 * @param info The data structure describing the shortcut.
1466 *
1467 * @return A View inflated from R.layout.application.
1468 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001469 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001471 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
1474 /**
1475 * Creates a view representing a shortcut inflated from the specified resource.
1476 *
1477 * @param layoutResId The id of the XML layout used to create the shortcut.
1478 * @param parent The group the shortcut belongs to.
1479 * @param info The data structure describing the shortcut.
1480 *
1481 * @return A View inflated from layoutResId.
1482 */
Adam Cohen59400422014-03-05 18:07:04 -08001483 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001484 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001485 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001487 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 return favorite;
1489 }
1490
1491 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 * Add a shortcut to the workspace.
1493 *
1494 * @param data The intent describing the shortcut.
1495 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001497 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 int cellY) {
1499 int[] cellXY = mTmpAddItemCellCoordinates;
1500 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001501 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001502
Michael Jurkad3ef3062010-11-23 16:23:58 -08001503 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001504
Sunny Goyal2350bc92014-10-14 16:42:54 -07001505 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001506 if (info == null) {
1507 return;
1508 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001509 final View view = createShortcut(info);
1510
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511 // First we check if we already know the exact location where we want to add this item.
1512 if (cellX >= 0 && cellY >= 0) {
1513 cellXY[0] = cellX;
1514 cellXY[1] = cellY;
1515 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001516
1517 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001518 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001519 true, null,null)) {
1520 return;
1521 }
1522 DragObject dragObject = new DragObject();
1523 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001524 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1525 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 return;
1527 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001528 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001529 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001530 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001531 foundCellSpan = (result != null);
1532 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001533 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001534 }
1535
1536 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001537 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001538 return;
1539 }
1540
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001541 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
1543 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
1547 }
1548
Adam Cohen2f093b62012-04-30 18:59:53 -07001549 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1550 int minHeight) {
1551 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001552 // We want to account for the extra amount of padding that we are adding to the widget
1553 // to ensure that it gets the full amount of space that it has requested
1554 int requiredWidth = minWidth + padding.left + padding.right;
1555 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001556 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001557 }
1558
Adam Cohen2f093b62012-04-30 18:59:53 -07001559 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1560 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001561 }
1562
Adam Cohen2f093b62012-04-30 18:59:53 -07001563 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1564 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001565 }
1566
Adam Cohen2f093b62012-04-30 18:59:53 -07001567 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1568 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001569 }
1570
Adam Cohen2f093b62012-04-30 18:59:53 -07001571 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1572 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001573 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001574 }
1575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001577 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001579 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 */
Adam Cohen091440a2015-03-18 14:16:05 -07001581 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001582 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1583
1584 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001585 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001586 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1587 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001588 }
Romain Guycbb89e42009-06-08 15:52:54 -07001589
Adam Cohen59400422014-03-05 18:07:04 -08001590 if (appWidgetInfo.isCustomWidget) {
1591 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001592 }
1593
Adam Cohen59400422014-03-05 18:07:04 -08001594 LauncherAppWidgetInfo launcherInfo;
1595 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1596 launcherInfo.spanX = info.spanX;
1597 launcherInfo.spanY = info.spanY;
1598 launcherInfo.minSpanX = info.minSpanX;
1599 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001600 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001603 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604
1605 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001606 if (hostView == null) {
1607 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001608 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1609 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001610 } else {
1611 // The AppWidgetHostView has already been inflated and instantiated
1612 launcherInfo.hostView = hostView;
1613 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001615 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001616 launcherInfo.notifyWidgetSizeChanged(this);
1617
Adam Cohen59400422014-03-05 18:07:04 -08001618 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1619 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001620
1621 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001623 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 }
Romain Guycbb89e42009-06-08 15:52:54 -07001625
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1627 @Override
1628 public void onReceive(Context context, Intent intent) {
1629 final String action = intent.getAction();
1630 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1631 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001632 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001633 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001634
Winson Chungc100e8e2011-08-09 16:02:43 -07001635 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001636 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001637 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001638 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001639 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001640 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001641 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1642 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001643 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001644 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1645 mModel.resetLoadedState(false, true);
1646 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1647 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1648 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1649 mModel.resetLoadedState(false, true);
1650 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1651 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1652 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001653 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1654 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1655 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 }
1657 }
1658 };
1659
1660 @Override
1661 public void onAttachedToWindow() {
1662 super.onAttachedToWindow();
1663
1664 // Listen for broadcasts related to user-presence
1665 final IntentFilter filter = new IntentFilter();
1666 filter.addAction(Intent.ACTION_SCREEN_OFF);
1667 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001668 // For handling managed profiles
1669 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1670 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001671 if (ENABLE_DEBUG_INTENTS) {
1672 filter.addAction(DebugIntents.DELETE_DATABASE);
1673 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1674 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001675 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001676 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001677 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001678 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 mVisible = true;
1680 }
1681
Adam Cohen0b395352014-06-09 22:54:36 +00001682 /**
1683 * Sets up transparent navigation and status bars in LMP.
1684 * This method is a no-op for other platform versions.
1685 */
Sunny Goyald0091012015-01-20 15:55:34 -08001686 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001687 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001688 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001689 Window window = getWindow();
1690 window.getAttributes().systemUiVisibility |=
1691 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1692 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1693 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1694 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1695 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1696 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1697 window.setStatusBarColor(Color.TRANSPARENT);
1698 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001699 }
1700 }
1701
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 @Override
1703 public void onDetachedFromWindow() {
1704 super.onDetachedFromWindow();
1705 mVisible = false;
1706
Adam Cohend113e0c2010-11-11 10:48:05 -08001707 if (mAttached) {
1708 unregisterReceiver(mReceiver);
1709 mAttached = false;
1710 }
Winson Chungb745afb2015-03-02 11:51:23 -08001711 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 }
1713
1714 public void onWindowVisibilityChanged(int visibility) {
1715 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001716 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001717 // The following code used to be in onResume, but it turns out onResume is called when
1718 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1719 // is a more appropriate event to handle
1720 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001721 if (!mWorkspaceLoading) {
1722 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001723 // We want to let Launcher draw itself at least once before we force it to build
1724 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001725 // apps is nice and speedy.
1726 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001727 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001728 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001729 if (mStarted) return;
1730 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001731 // We delay the layer building a bit in order to give
1732 // other message processing a time to run. In particular
1733 // this avoids a delay in hiding the IME if it was
1734 // currently shown, because doing that may involve
1735 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001736 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001737 final ViewTreeObserver.OnDrawListener listener = this;
1738 mWorkspace.post(new Runnable() {
1739 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001740 if (mWorkspace != null &&
1741 mWorkspace.getViewTreeObserver() != null) {
1742 mWorkspace.getViewTreeObserver().
1743 removeOnDrawListener(listener);
1744 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001745 }
1746 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001747 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001748 }
1749 });
1750 }
1751 clearTypedText();
1752 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 }
1754
Adam Cohen091440a2015-03-18 14:16:05 -07001755 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 mHandler.removeMessages(ADVANCE_MSG);
1757 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1758 mHandler.sendMessageDelayed(msg, delay);
1759 mAutoAdvanceSentTime = System.currentTimeMillis();
1760 }
1761
Adam Cohen091440a2015-03-18 14:16:05 -07001762 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1764 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1765 mAutoAdvanceRunning = autoAdvanceRunning;
1766 if (autoAdvanceRunning) {
1767 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1768 sendAdvanceMessage(delay);
1769 } else {
1770 if (!mWidgetsToAdvance.isEmpty()) {
1771 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1772 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1773 }
1774 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001775 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 }
1777 }
1778 }
1779
1780 private final Handler mHandler = new Handler() {
1781 @Override
1782 public void handleMessage(Message msg) {
1783 if (msg.what == ADVANCE_MSG) {
1784 int i = 0;
1785 for (View key: mWidgetsToAdvance.keySet()) {
1786 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1787 final int delay = mAdvanceStagger * i;
1788 if (v instanceof Advanceable) {
1789 postDelayed(new Runnable() {
1790 public void run() {
1791 ((Advanceable) v).advance();
1792 }
1793 }, delay);
1794 }
1795 i++;
1796 }
1797 sendAdvanceMessage(mAdvanceInterval);
1798 }
1799 }
1800 };
1801
1802 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001803 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001804 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1805 if (v instanceof Advanceable) {
1806 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001807 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001808 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001809 }
1810 }
1811
1812 void removeWidgetToAutoAdvance(View hostView) {
1813 if (mWidgetsToAdvance.containsKey(hostView)) {
1814 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001815 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001816 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 }
1818
Joe Onorato9c1289c2009-08-17 11:03:03 -04001819 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001820 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001821 launcherInfo.hostView = null;
1822 }
1823
Hyunyoung Song3f471442015-04-08 19:01:34 -07001824 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001825 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1826 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001827 }
1828
Winson Chunga6945242014-01-08 14:04:34 -08001829 public DragLayer getDragLayer() {
1830 return mDragLayer;
1831 }
1832
Winson Chungb745afb2015-03-02 11:51:23 -08001833 public AppsContainerView getAppsView() {
1834 return mAppsView;
1835 }
1836
Hyunyoung Song3f471442015-04-08 19:01:34 -07001837 public WidgetsContainerView getWidgetsView() {
1838 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001839 }
1840
Winson Chunga6945242014-01-08 14:04:34 -08001841 public Workspace getWorkspace() {
1842 return mWorkspace;
1843 }
1844
1845 public Hotseat getHotseat() {
1846 return mHotseat;
1847 }
1848
Jorim Jaggid017f882014-01-14 17:08:48 -08001849 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001850 return mOverviewPanel;
1851 }
1852
1853 public SearchDropTargetBar getSearchBar() {
1854 return mSearchDropTargetBar;
1855 }
1856
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001857 public LauncherAppWidgetHost getAppWidgetHost() {
1858 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
Romain Guycbb89e42009-06-08 15:52:54 -07001860
Winson Chunga9abd0e2010-10-27 17:18:37 -07001861 public LauncherModel getModel() {
1862 return mModel;
1863 }
1864
Winson Chunga6945242014-01-08 14:04:34 -08001865 protected SharedPreferences getSharedPrefs() {
1866 return mSharedPrefs;
1867 }
1868
Bjorn Bringertc459e522013-06-07 19:36:01 +01001869 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001870 getWindow().closeAllPanels();
1871
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001872 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001873 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001874 }
1875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 @Override
1877 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001878 long startTime = 0;
1879 if (DEBUG_RESUME_TIME) {
1880 startTime = System.currentTimeMillis();
1881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 super.onNewIntent(intent);
1883
1884 // Close the menu
1885 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001886 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001887 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001888
Adam Cohened307df2013-10-02 09:37:31 -07001889 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1890 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1891 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001892
Adam Cohen6fecd412013-10-02 17:41:50 -07001893 if (mWorkspace == null) {
1894 // Can be cases where mWorkspace is null, this prevents a NPE
1895 return;
1896 }
1897 Folder openFolder = mWorkspace.getOpenFolder();
1898 // In all these cases, only animate if we're already on home
1899 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001900
1901 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1902 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001903 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001904 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001905 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001906 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001907
Adam Cohen6fecd412013-10-02 17:41:50 -07001908 closeFolder();
1909 exitSpringLoadedDragMode();
1910
1911 // If we are already on home, then just animate back to the workspace,
1912 // otherwise, just wait until onResume to set the state back to Workspace
1913 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001914 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001915 } else {
1916 mOnResumeState = State.WORKSPACE;
1917 }
1918
1919 final View v = getWindow().peekDecorView();
1920 if (v != null && v.getWindowToken() != null) {
1921 InputMethodManager imm = (InputMethodManager)getSystemService(
1922 INPUT_METHOD_SERVICE);
1923 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1924 }
1925
Winson Chungb745afb2015-03-02 11:51:23 -08001926 // Reset the apps view
1927 if (!alreadyOnHome && mAppsView != null) {
1928 mAppsView.scrollToTop();
1929 }
1930
Hyunyoung Song3f471442015-04-08 19:01:34 -07001931 // Reset the widgets view
1932 if (!alreadyOnHome && mWidgetsView != null) {
1933 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001934 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001935
Adam Cohen9211d422014-10-07 18:14:53 -07001936 if (mLauncherCallbacks != null) {
1937 mLauncherCallbacks.onHomeIntent();
1938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 }
Adam Cohened307df2013-10-02 09:37:31 -07001940
Michael Jurka447bf842013-05-15 14:52:15 +02001941 if (DEBUG_RESUME_TIME) {
1942 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944
Adam Cohen9211d422014-10-07 18:14:53 -07001945 if (mLauncherCallbacks != null) {
1946 mLauncherCallbacks.onNewIntent(intent);
1947 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001948 }
1949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001951 public void onRestoreInstanceState(Bundle state) {
1952 super.onRestoreInstanceState(state);
1953 for (int page: mSynchronouslyBoundPages) {
1954 mWorkspace.restoreInstanceStateForChild(page);
1955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
1957
1958 @Override
1959 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001960 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001961 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1962 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001963 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001964 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965
Michael Jurka883f55b2010-10-21 15:47:14 -07001966 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001967 // We close any open folder since it will not be re-opened, and we need to make sure
1968 // this state is reflected.
1969 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970
Adam Cohendcd297f2013-06-18 13:13:40 -07001971 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001972 mWaitingForResult) {
1973 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001974 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001975 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1976 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001977 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1978 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1979 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001980 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
1982
1983 if (mFolderInfo != null && mWaitingForResult) {
1984 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1985 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1986 }
Michael Jurka946ad472010-07-09 18:05:18 -07001987
Hyunyoung Song3f471442015-04-08 19:01:34 -07001988 // Save the current widgets tray?
1989 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001990 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001991
1992 if (mLauncherCallbacks != null) {
1993 mLauncherCallbacks.onSaveInstanceState(outState);
1994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
1996
1997 @Override
1998 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002000
Winson Chunge7a03942011-08-05 15:05:12 -07002001 // Remove all pending runnables
2002 mHandler.removeMessages(ADVANCE_MSG);
2003 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002004 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002005
Winson Chungcd2b0142011-06-08 16:02:26 -07002006 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002007 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002008
2009 // It's possible to receive onDestroy after a new Launcher activity has
2010 // been created. In this case, don't interfere with the new Launcher.
2011 if (mModel.isCurrentCallbacks(this)) {
2012 mModel.stopLoader();
2013 app.setLauncher(null);
2014 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002017 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002019 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002021 mAppWidgetHost = null;
2022
2023 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024
2025 TextKeyListener.getInstance().release();
2026
Winson Chung81b52252012-08-27 15:34:29 -07002027 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2028 // to prevent leaking Launcher activities on orientation change.
2029 if (mModel != null) {
2030 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2031 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002032
2033 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002034 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002035
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002036 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002037 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002038 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002039 mWorkspace = null;
2040 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002041
Michael Jurka2ecf9952012-06-18 12:52:28 -07002042 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002043
2044 if (mLauncherCallbacks != null) {
2045 mLauncherCallbacks.onDestroy();
2046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
2048
Adam Cohena9cf38f2011-05-02 15:36:58 -07002049 public DragController getDragController() {
2050 return mDragController;
2051 }
2052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 @Override
2054 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002055 onStartForResult(requestCode);
2056 super.startActivityForResult(intent, requestCode);
2057 }
2058
2059 @Override
2060 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2061 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2062 onStartForResult(requestCode);
2063 try {
2064 super.startIntentSenderForResult(intent, requestCode,
2065 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2066 } catch (IntentSender.SendIntentException e) {
2067 throw new ActivityNotFoundException();
2068 }
2069 }
2070
2071 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002072 if (requestCode >= 0) {
2073 setWaitingForResult(true);
2074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076
Winson Chung70d72102011-08-12 11:24:35 -07002077 /**
2078 * Indicates that we want global search for this activity by setting the globalSearch
2079 * argument for {@link #startSearch} to true.
2080 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002082 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002084
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002085 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002086
Karl Rosaen138a0412009-04-23 19:00:21 -07002087 if (initialQuery == null) {
2088 // Use any text typed in the launcher as the initial query
2089 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002090 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 if (appSearchData == null) {
2092 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002093 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 }
Winson Chungadf0c182012-08-23 14:59:07 -07002095 Rect sourceBounds = new Rect();
2096 if (mSearchDropTargetBar != null) {
2097 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2098 }
Romain Guycbb89e42009-06-08 15:52:54 -07002099
Ian Parkinson047036e2014-09-01 15:40:53 +01002100 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002101 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002102 if (clearTextImmediately) {
2103 clearTypedText();
2104 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002105 }
2106
Ian Parkinson047036e2014-09-01 15:40:53 +01002107 /**
2108 * Start a text search.
2109 *
2110 * @return {@code true} if the search will start immediately, so any further keypresses
2111 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2112 * to buffer keypresses.
2113 */
2114 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002115 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002116 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2117 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2118 sourceBounds);
2119 }
2120
Michael Jurkaa33411c2012-06-14 16:18:21 -07002121 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002122 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002123 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002124 }
2125
2126 /**
2127 * Starts the global search activity. This code is a copied from SearchManager
2128 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002129 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002130 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002131 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002132 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2133 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2134 if (globalSearchActivity == null) {
2135 Log.w(TAG, "No global search activity found.");
2136 return;
2137 }
2138 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2139 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2140 intent.setComponent(globalSearchActivity);
2141 // Make sure that we have a Bundle to put source in
2142 if (appSearchData == null) {
2143 appSearchData = new Bundle();
2144 } else {
2145 appSearchData = new Bundle(appSearchData);
2146 }
Adam Cohen9211d422014-10-07 18:14:53 -07002147 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002148 if (!appSearchData.containsKey("source")) {
2149 appSearchData.putString("source", getPackageName());
2150 }
2151 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2152 if (!TextUtils.isEmpty(initialQuery)) {
2153 intent.putExtra(SearchManager.QUERY, initialQuery);
2154 }
2155 if (selectInitialQuery) {
2156 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2157 }
2158 intent.setSourceBounds(sourceBounds);
2159 try {
2160 startActivity(intent);
2161 } catch (ActivityNotFoundException ex) {
2162 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2163 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 }
2165
Yura4f93ec62014-02-04 14:15:21 +00002166 public boolean isOnCustomContent() {
2167 return mWorkspace.isOnOrMovingToCustomContent();
2168 }
2169
Winson Chung70d72102011-08-12 11:24:35 -07002170 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002171 public boolean onPrepareOptionsMenu(Menu menu) {
2172 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002173 if (!isOnCustomContent()) {
2174 // Close any open folders
2175 closeFolder();
2176 // Stop resizing any widgets
2177 mWorkspace.exitWidgetResizeMode();
2178 if (!mWorkspace.isInOverviewMode()) {
2179 // Show the overview mode
2180 showOverviewMode(true);
2181 } else {
2182 showWorkspace(true);
2183 }
Winson Chunge0298742014-01-17 12:03:00 -08002184 }
Adam Cohen9211d422014-10-07 18:14:53 -07002185 if (mLauncherCallbacks != null) {
2186 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2187 }
2188
Michael Jurkab94f3f82013-09-11 18:08:54 +02002189 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002192 @Override
2193 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002194 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002195 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002196 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002197 }
2198
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 public boolean isWorkspaceLocked() {
2200 return mWorkspaceLoading || mWaitingForResult;
2201 }
2202
Adam Cohen517a7f52014-03-01 12:12:59 -08002203 public boolean isWorkspaceLoading() {
2204 return mWorkspaceLoading;
2205 }
2206
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002207 private void setWorkspaceLoading(boolean value) {
2208 boolean isLocked = isWorkspaceLocked();
2209 mWorkspaceLoading = value;
2210 if (isLocked != isWorkspaceLocked()) {
2211 onWorkspaceLockedChanged();
2212 }
2213 }
2214
2215 private void setWaitingForResult(boolean value) {
2216 boolean isLocked = isWorkspaceLocked();
2217 mWaitingForResult = value;
2218 if (isLocked != isWorkspaceLocked()) {
2219 onWorkspaceLockedChanged();
2220 }
2221 }
2222
Adam Cohen9211d422014-10-07 18:14:53 -07002223 protected void onWorkspaceLockedChanged() {
2224 if (mLauncherCallbacks != null) {
2225 mLauncherCallbacks.onWorkspaceLockedChanged();
2226 }
2227 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002228
Michael Jurka0280c3b2010-09-17 15:00:07 -07002229 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002230 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002231 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002232 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2233 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002234 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002235 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002236 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002237
Sunny Goyale6b63a32015-01-16 16:14:29 -08002238 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002239 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002240 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2241 }
2242
Sunny Goyale6b63a32015-01-16 16:14:29 -08002243 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002244 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2245 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002246 if (appWidgetInfo.configure != null) {
2247 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002248 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002249
Bjorn Bringert7984c942009-12-09 15:38:25 +00002250 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002251 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2252 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2253
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002255 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002256 Runnable onComplete = new Runnable() {
2257 @Override
2258 public void run() {
2259 // Exit spring loaded mode if necessary after adding the widget
2260 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2261 null);
2262 }
2263 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002264 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002265 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002266 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 }
2268 }
Romain Guycbb89e42009-06-08 15:52:54 -07002269
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002270 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002271 // Close any folders that may be open.
2272 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002273 mWorkspace.moveToCustomContentScreen(animate);
2274 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002275
2276 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2277 int[] cell, int spanX, int spanY) {
2278 switch (info.itemType) {
2279 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2280 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2281 int span[] = new int[2];
2282 span[0] = spanX;
2283 span[1] = spanY;
2284 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2285 container, screenId, cell, span);
2286 break;
2287 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2288 processShortcutFromDrop(info.componentName, container, screenId, cell);
2289 break;
2290 default:
2291 throw new IllegalStateException("Unknown item type: " + info.itemType);
2292 }
2293 }
2294
Adam Cohenfbba09b2011-07-18 21:43:05 -07002295 /**
2296 * Process a shortcut drop.
2297 *
2298 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002302 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2303 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002304 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002305 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002306 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002307 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002308
2309 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 mPendingAddInfo.cellX = cell[0];
2311 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002312 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002313
2314 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2315 createShortcutIntent.setComponent(componentName);
2316 processShortcut(createShortcutIntent);
2317 }
2318
Adam Cohenfbba09b2011-07-18 21:43:05 -07002319 /**
2320 * Process a widget drop.
2321 *
2322 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002326 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2327 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002329 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002331 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002332 mPendingAddInfo.minSpanX = info.minSpanX;
2333 mPendingAddInfo.minSpanY = info.minSpanY;
2334
Adam Cohenfbba09b2011-07-18 21:43:05 -07002335 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002336 mPendingAddInfo.cellX = cell[0];
2337 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002338 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002339 if (span != null) {
2340 mPendingAddInfo.spanX = span[0];
2341 mPendingAddInfo.spanY = span[1];
2342 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002343
Adam Cohened66b2b2012-01-23 17:28:51 -08002344 AppWidgetHostView hostView = info.boundWidget;
2345 int appWidgetId;
2346 if (hostView != null) {
2347 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002348 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002349 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002350 // In this case, we either need to start an activity to get permission to bind
2351 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002352 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002353 Bundle options = info.bindOptions;
2354
Sunny Goyalffe83f12014-08-14 17:39:34 -07002355 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2356 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002357 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002358 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002359 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002360 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002361 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2362 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2363 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002364 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2365 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002366 // TODO: we need to make sure that this accounts for the options bundle.
2367 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002368 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2369 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002370 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002371 }
2372
Joe Onoratodeb98af2010-02-19 14:59:39 -08002373 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002374 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002375 }
2376
Winson Chung24ab2f12010-09-16 14:10:47 -07002377 void processWallpaper(Intent intent) {
2378 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2379 }
2380
Adam Cohendcd297f2013-06-18 13:13:40 -07002381 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002382 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002383 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 folderInfo.title = getText(R.string.folder_name);
2385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002387 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2388 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002389 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390
2391 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002392 FolderIcon newFolder =
2393 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002394 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002395 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002396 // Force measure the new folder icon
2397 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2398 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002399 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 }
Romain Guycbb89e42009-06-08 15:52:54 -07002401
Joe Onorato9c1289c2009-08-17 11:03:03 -04002402 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002403 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002404 }
2405
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002406 /**
2407 * Registers various content observers. The current implementation registers
2408 * only a favorites observer to keep track of the favorites applications.
2409 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002410 private void registerContentObservers() {
2411 ContentResolver resolver = getContentResolver();
2412 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2413 true, mWidgetObserver);
2414 }
2415
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 @Override
2417 public boolean dispatchKeyEvent(KeyEvent event) {
2418 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2419 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002421 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002422 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002423 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002424 dumpState();
2425 return true;
2426 }
2427 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002428 }
2429 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2430 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002431 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 return true;
2433 }
2434 }
2435
2436 return super.dispatchKeyEvent(event);
2437 }
2438
Joe Onorato88ec0992009-11-19 13:16:06 -08002439 @Override
2440 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002441 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2442 return;
2443 }
2444
Winson Chung3d9490a2015-03-24 17:38:42 -07002445 LauncherAccessibilityDelegate delegate =
2446 LauncherAppState.getInstance().getAccessibilityDelegate();
2447 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002448 return;
2449 }
2450
Winson Chungb745afb2015-03-02 11:51:23 -08002451 if (isAppsViewVisible()) {
2452 showWorkspace(true);
2453 } else if (isWidgetsViewVisible()) {
2454 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002455 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002456 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002457 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002458 Folder openFolder = mWorkspace.getOpenFolder();
2459 if (openFolder.isEditingName()) {
2460 openFolder.dismissEditingName();
2461 } else {
2462 closeFolder();
2463 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002464 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002465 mWorkspace.exitWidgetResizeMode();
2466
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002467 // Back button is a no-op here, but give at least some feedback for the button press
2468 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002469 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002470 }
2471
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002472 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002473 * Re-listen when widgets are reset.
2474 */
Adam Cohen091440a2015-03-18 14:16:05 -07002475 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002476 if (mAppWidgetHost != null) {
2477 mAppWidgetHost.startListening();
2478 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002479 }
2480
2481 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 * Launches the intent referred by the clicked shortcut.
2483 *
2484 * @param v The view representing the clicked shortcut.
2485 */
2486 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002487 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2488 // view has detached (it's possible for this to happen if the view is removed mid touch).
2489 if (v.getWindowToken() == null) {
2490 return;
2491 }
2492
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002493 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002494 return;
2495 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002496
Adam Cohen1697b792013-09-17 19:08:21 -07002497 if (v instanceof Workspace) {
2498 if (mWorkspace.isInOverviewMode()) {
2499 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002500 }
2501 return;
2502 }
2503
2504 if (v instanceof CellLayout) {
2505 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002506 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002507 }
2508 }
2509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002511 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002512 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002514 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002515 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002516 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002517 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002518 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002519 } else if (tag instanceof AppInfo) {
2520 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002521 } else if (tag instanceof LauncherAppWidgetInfo) {
2522 if (v instanceof PendingAppWidgetHostView) {
2523 onClickPendingWidget((PendingAppWidgetHostView) v);
2524 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 }
2526 }
2527
Sunny Goyal508da152014-08-14 10:53:27 -07002528 public void onClickPagedViewIcon(View v) {
2529 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002530 if (mLauncherCallbacks != null) {
2531 mLauncherCallbacks.onClickPagedViewIcon(v);
2532 }
Sunny Goyal508da152014-08-14 10:53:27 -07002533 }
2534
Michael Jurka0e260592010-06-30 17:07:39 -07002535 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002536 return false;
2537 }
2538
Michael Jurkaaf442092010-06-10 17:01:57 -07002539 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002540 * Event handler for the app widget view which has not fully restored.
2541 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002542 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002543 if (mIsSafeModeEnabled) {
2544 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2545 return;
2546 }
2547
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002548 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002549 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002550 int widgetId = info.appWidgetId;
2551 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2552 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002553 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2554 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002555 mPendingAddInfo.copyFrom(info);
2556 mPendingAddWidgetId = widgetId;
2557
Sunny Goyalffe83f12014-08-14 17:39:34 -07002558 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2559 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002560 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002561 } else if (info.installProgress < 0) {
2562 // The install has not been queued
2563 final String packageName = info.providerName.getPackageName();
2564 showBrokenAppInstallDialog(packageName,
2565 new DialogInterface.OnClickListener() {
2566 public void onClick(DialogInterface dialog, int id) {
2567 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2568 }
2569 });
2570 } else {
2571 // Download has started.
2572 final String packageName = info.providerName.getPackageName();
2573 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002574 }
2575 }
2576
2577 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002578 * Event handler for the "grid" button that appears on the home screen, which
2579 * enters all apps mode.
2580 *
2581 * @param v The view that was clicked.
2582 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002583 protected void onClickAllAppsButton(View v) {
2584 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002585 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002586 showWorkspace(true);
2587 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002588 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002589 }
Adam Cohen9211d422014-10-07 18:14:53 -07002590 if (mLauncherCallbacks != null) {
2591 mLauncherCallbacks.onClickAllAppsButton(v);
2592 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002593 }
2594
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002595 private void showBrokenAppInstallDialog(final String packageName,
2596 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002597 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002598 .setTitle(R.string.abandoned_promises_title)
2599 .setMessage(R.string.abandoned_promise_explanation)
2600 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2601 .setNeutralButton(R.string.abandoned_clean_this,
2602 new DialogInterface.OnClickListener() {
2603 public void onClick(DialogInterface dialog, int id) {
2604 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2605 mWorkspace.removeAbandonedPromise(packageName, user);
2606 }
2607 })
2608 .create().show();
2609 return;
2610 }
2611
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002612 /**
2613 * Event handler for an app shortcut click.
2614 *
2615 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2616 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002617 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2619 Object tag = v.getTag();
2620 if (!(tag instanceof ShortcutInfo)) {
2621 throw new IllegalArgumentException("Input must be a Shortcut");
2622 }
2623
2624 // Open shortcut
2625 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002626
2627 if (shortcut.isDisabled != 0) {
2628 int error = R.string.activity_not_available;
2629 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2630 error = R.string.safemode_shortcut_error;
2631 }
2632 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2633 return;
2634 }
2635
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002636 final Intent intent = shortcut.intent;
2637
2638 // Check for special shortcuts
2639 if (intent.getComponent() != null) {
2640 final String shortcutClass = intent.getComponent().getClassName();
2641
2642 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2643 MemoryDumpActivity.startDump(this);
2644 return;
2645 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2646 toggleShowWeightWatcher();
2647 return;
2648 }
2649 }
2650
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002652 if ((v instanceof BubbleTextView)
2653 && shortcut.isPromise()
2654 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002655 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002656 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 new DialogInterface.OnClickListener() {
2658 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002659 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002660 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002661 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002662 return;
2663 }
2664
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002665 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002666 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002667
2668 if (mLauncherCallbacks != null) {
2669 mLauncherCallbacks.onClickAppShortcut(v);
2670 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002671 }
2672
Adam Cohen091440a2015-03-18 14:16:05 -07002673 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002675 final ShortcutInfo shortcut;
2676 final Intent intent;
2677 if (tag instanceof ShortcutInfo) {
2678 shortcut = (ShortcutInfo) tag;
2679 intent = shortcut.intent;
2680 int[] pos = new int[2];
2681 v.getLocationOnScreen(pos);
2682 intent.setSourceBounds(new Rect(pos[0], pos[1],
2683 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002684
Sunny Goyal508da152014-08-14 10:53:27 -07002685 } else if (tag instanceof AppInfo) {
2686 shortcut = null;
2687 intent = ((AppInfo) tag).intent;
2688 } else {
2689 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2690 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691
2692 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002693 mStats.recordLaunch(intent, shortcut);
2694
2695 if (success && v instanceof BubbleTextView) {
2696 mWaitingForResume = (BubbleTextView) v;
2697 mWaitingForResume.setStayPressed(true);
2698 }
2699 }
2700
2701 /**
2702 * Event handler for a folder icon click.
2703 *
2704 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2705 */
2706 protected void onClickFolderIcon(View v) {
2707 if (LOGD) Log.d(TAG, "onClickFolder");
2708 if (!(v instanceof FolderIcon)){
2709 throw new IllegalArgumentException("Input must be a FolderIcon");
2710 }
2711
2712 FolderIcon folderIcon = (FolderIcon) v;
2713 final FolderInfo info = folderIcon.getFolderInfo();
2714 Folder openFolder = mWorkspace.getFolderForTag(info);
2715
2716 // If the folder info reports that the associated folder is open, then verify that
2717 // it is actually opened. There have been a few instances where this gets out of sync.
2718 if (info.opened && openFolder == null) {
2719 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2720 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2721 info.opened = false;
2722 }
2723
2724 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2725 // Close any open folder
2726 closeFolder();
2727 // Open the requested folder
2728 openFolder(folderIcon);
2729 } else {
2730 // Find the open folder...
2731 int folderScreen;
2732 if (openFolder != null) {
2733 folderScreen = mWorkspace.getPageForView(openFolder);
2734 // .. and close it
2735 closeFolder(openFolder);
2736 if (folderScreen != mWorkspace.getCurrentPage()) {
2737 // Close any folder open on the current screen
2738 closeFolder();
2739 // Pull the folder onto this screen
2740 openFolder(folderIcon);
2741 }
2742 }
2743 }
Adam Cohen9211d422014-10-07 18:14:53 -07002744
2745 if (mLauncherCallbacks != null) {
2746 mLauncherCallbacks.onClickFolderIcon(v);
2747 }
Michael Jurka5130e402011-10-13 04:55:35 -07002748 }
2749
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002750 /**
2751 * Event handler for the (Add) Widgets button that appears after a long press
2752 * on the home screen.
2753 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002754 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002755 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002756 if (mIsSafeModeEnabled) {
2757 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2758 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002759 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002760 if (mLauncherCallbacks != null) {
2761 mLauncherCallbacks.onClickAddWidgetButton(view);
2762 }
Adam Cohen9211d422014-10-07 18:14:53 -07002763 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002764 }
2765
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002766 /**
2767 * Event handler for the wallpaper picker button that appears after a long press
2768 * on the home screen.
2769 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002770 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002771 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002772 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2773 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002774
2775 if (mLauncherCallbacks != null) {
2776 mLauncherCallbacks.onClickWallpaperPicker(v);
2777 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002778 }
2779
2780 /**
2781 * Event handler for a click on the settings button that appears after a long press
2782 * on the home screen.
2783 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002784 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002785 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002786 if (mLauncherCallbacks != null) {
2787 mLauncherCallbacks.onClickSettingsButton(v);
2788 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002789 }
2790
Michael Jurka5130e402011-10-13 04:55:35 -07002791 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002792 // Provide the same haptic feedback that the system offers for virtual keys.
2793 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002794 }
2795
Adam Cohen61f560d2013-09-30 15:58:20 -07002796 public void performHapticFeedbackOnTouchDown(View v) {
2797 // Provide the same haptic feedback that the system offers for virtual keys.
2798 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2799 }
2800
2801 public View.OnTouchListener getHapticFeedbackTouchListener() {
2802 if (mHapticFeedbackTouchListener == null) {
2803 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2804 @Override
2805 public boolean onTouch(View v, MotionEvent event) {
2806 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2807 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2808 }
2809 return false;
2810 }
2811 };
2812 }
2813 return mHapticFeedbackTouchListener;
2814 }
2815
Adam Cohen9211d422014-10-07 18:14:53 -07002816 public void onDragStarted(View view) {
2817 if (isOnCustomContent()) {
2818 // Custom content screen doesn't participate in drag and drop. If on custom
2819 // content screen, move to default.
2820 moveWorkspaceToDefaultScreen();
2821 }
2822
2823 if (mLauncherCallbacks != null) {
2824 mLauncherCallbacks.onDragStarted(view);
2825 }
2826 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002827
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002828 /**
2829 * Called when the user stops interacting with the launcher.
2830 * This implies that the user is now on the homescreen and is not doing housekeeping.
2831 */
Adam Cohen9211d422014-10-07 18:14:53 -07002832 protected void onInteractionEnd() {
2833 if (mLauncherCallbacks != null) {
2834 mLauncherCallbacks.onInteractionEnd();
2835 }
2836 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002837
2838 /**
2839 * Called when the user starts interacting with the launcher.
2840 * The possible interactions are:
2841 * - open all apps
2842 * - reorder an app shortcut, or a widget
2843 * - open the overview mode.
2844 * This is a good time to stop doing things that only make sense
2845 * when the user is on the homescreen and not doing housekeeping.
2846 */
Adam Cohen9211d422014-10-07 18:14:53 -07002847 protected void onInteractionBegin() {
2848 if (mLauncherCallbacks != null) {
2849 mLauncherCallbacks.onInteractionBegin();
2850 }
2851 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002852
Kenny Guyf07af7b2014-07-31 11:39:16 +01002853 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002854 try {
2855 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002856 launcherApps.showAppDetailsForProfile(componentName, user);
2857 } catch (SecurityException e) {
2858 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2859 Log.e(TAG, "Launcher does not have permission to launch settings");
2860 } catch (ActivityNotFoundException e) {
2861 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2862 Log.e(TAG, "Unable to launch settings");
2863 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002864 }
2865
Michael Jurka1e2f4652013-07-08 18:03:46 -07002866 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002867 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2868 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002869 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002870 // System applications cannot be installed. For now, show a toast explaining that.
2871 // We may give them the option of disabling apps this way.
2872 int messageId = R.string.uninstall_system_app_text;
2873 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002874 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002875 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002876 String packageName = componentName.getPackageName();
2877 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002878 Intent intent = new Intent(
2879 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002880 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2881 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002882 if (user != null) {
2883 user.addToIntent(intent, Intent.EXTRA_USER);
2884 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002885 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002886 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002887 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002888 }
2889
Michael Jurka86a720e2012-05-09 11:23:23 -07002890 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002891 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002892 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002893 // Only launch using the new animation if the shortcut has not opted out (this is a
2894 // private contract between launcher and may be ignored in the future).
2895 boolean useLaunchAnimation = (v != null) &&
2896 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002897 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2898 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002899
Kenny Guy1317e2d2014-05-08 18:52:50 +01002900 UserHandleCompat user = null;
2901 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2902 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2903 user = userManager.getUserForSerialNumber(serialNumber);
2904 }
2905
2906 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002907 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002908 ActivityOptions opts = null;
2909 if (sClipRevealMethod != null) {
2910 // TODO: call method directly when Launcher3 can depend on M APIs
2911 int left = 0, top = 0;
2912 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2913 if (v instanceof TextView) {
2914 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002915 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2916 if (icon != null) {
2917 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002918 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002919 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002920 width = bounds.width();
2921 height = bounds.height();
2922 }
2923 }
2924 try {
2925 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2926 left, top, width, height);
2927 } catch (IllegalAccessException e) {
2928 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2929 sClipRevealMethod = null;
2930 } catch (InvocationTargetException e) {
2931 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2932 sClipRevealMethod = null;
2933 }
2934 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002935 if (opts == null && !Utilities.isLmpOrAbove()) {
2936 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002937 v.getMeasuredWidth(), v.getMeasuredHeight());
2938 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002939 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002940 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002941
2942 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2943 // Could be launching some bookkeeping activity
2944 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002945 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002946 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002947 launcherApps.startActivityForProfile(intent.getComponent(), user,
2948 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002949 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002950 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002951 } catch (SecurityException e) {
2952 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002953 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002955 "or use the exported attribute for this activity. "
2956 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002957 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002958 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002960
Michael Jurka86a720e2012-05-09 11:23:23 -07002961 boolean startActivitySafely(View v, Intent intent, Object tag) {
2962 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002963 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2964 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2965 return false;
2966 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002967 try {
2968 success = startActivity(v, intent, tag);
2969 } catch (ActivityNotFoundException e) {
2970 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2971 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2972 }
2973 return success;
2974 }
2975
Adam Cohen268c4752012-06-06 17:47:33 -07002976 /**
2977 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2978 * in the DragLayer in the exact absolute location of the original FolderIcon.
2979 */
2980 private void copyFolderIconToImage(FolderIcon fi) {
2981 final int width = fi.getMeasuredWidth();
2982 final int height = fi.getMeasuredHeight();
2983
2984 // Lazy load ImageView, Bitmap and Canvas
2985 if (mFolderIconImageView == null) {
2986 mFolderIconImageView = new ImageView(this);
2987 }
2988 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2989 mFolderIconBitmap.getHeight() != height) {
2990 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2991 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2992 }
2993
2994 DragLayer.LayoutParams lp;
2995 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2996 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2997 } else {
2998 lp = new DragLayer.LayoutParams(width, height);
2999 }
3000
Adam Cohen307fe232012-08-16 17:55:58 -07003001 // The layout from which the folder is being opened may be scaled, adjust the starting
3002 // view size by this scale factor.
3003 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003004 lp.customPosition = true;
3005 lp.x = mRectForFolderAnimation.left;
3006 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003007 lp.width = (int) (scale * width);
3008 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003009
3010 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3011 fi.draw(mFolderIconCanvas);
3012 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003013 if (fi.getFolder() != null) {
3014 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3015 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003016 }
Adam Cohen268c4752012-06-06 17:47:33 -07003017 // Just in case this image view is still in the drag layer from a previous animation,
3018 // we remove it and re-add it.
3019 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3020 mDragLayer.removeView(mFolderIconImageView);
3021 }
3022 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003023 if (fi.getFolder() != null) {
3024 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003025 }
Adam Cohen268c4752012-06-06 17:47:33 -07003026 }
3027
Adam Cohen2801caf2011-05-13 20:57:39 -07003028 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003029 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003030 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3031 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3032 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3033
Adam Cohenc51934b2011-07-26 21:07:43 -07003034 FolderInfo info = (FolderInfo) fi.getTag();
3035 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3036 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003037 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3038 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003039 }
3040
Adam Cohen268c4752012-06-06 17:47:33 -07003041 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3042 copyFolderIconToImage(fi);
3043 fi.setVisibility(View.INVISIBLE);
3044
Michael Jurka2ecf9952012-06-18 12:52:28 -07003045 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003046 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003047 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003048 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3049 }
3050 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003051 oa.start();
3052 }
3053
Adam Cohen268c4752012-06-06 17:47:33 -07003054 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003055 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003056 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3057 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3058 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3059
Adam Cohen268c4752012-06-06 17:47:33 -07003060 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003061
Adam Cohen268c4752012-06-06 17:47:33 -07003062 // We remove and re-draw the FolderIcon in-case it has changed
3063 mDragLayer.removeView(mFolderIconImageView);
3064 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003065 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003066 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003067 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003068 oa.addListener(new AnimatorListenerAdapter() {
3069 @Override
3070 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003071 if (cl != null) {
3072 cl.clearFolderLeaveBehind();
3073 // Remove the ImageView copy of the FolderIcon and make the original visible.
3074 mDragLayer.removeView(mFolderIconImageView);
3075 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003076 }
3077 }
3078 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003079 oa.start();
3080 }
3081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003083 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084 * is animated relative to the specified View. If the View is null, no animation
3085 * is played.
3086 *
3087 * @param folderInfo The FolderInfo describing the folder to open.
3088 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003089 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003090 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003091 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092
Adam Cohena9cf38f2011-05-02 15:36:58 -07003093 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094
Adam Cohen4554ee12011-08-03 16:13:21 -07003095 // Just verify that the folder hasn't already been added to the DragLayer.
3096 // There was a one-off crash where the folder had a parent already.
3097 if (folder.getParent() == null) {
3098 mDragLayer.addView(folder);
3099 mDragController.addDropTarget((DropTarget) folder);
3100 } else {
3101 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3102 folder.getParent() + ").");
3103 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003104 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003105 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003106
3107 // Notify the accessibility manager that this folder "window" has appeared and occluded
3108 // the workspace items
3109 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3110 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003111 }
3112
3113 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003114 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003115 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003116 if (folder.isEditingName()) {
3117 folder.dismissEditingName();
3118 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003119 closeFolder(folder);
3120 }
3121 }
3122
3123 void closeFolder(Folder folder) {
3124 folder.getInfo().opened = false;
3125
3126 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3127 if (parent != null) {
3128 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3129 shrinkAndFadeInFolderIcon(fi);
3130 }
3131 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003132
3133 // Notify the accessibility manager that this folder "window" has disappeard and no
3134 // longer occludeds the workspace items
3135 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 }
3137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003139 if (!isDraggingEnabled()) return false;
3140 if (isWorkspaceLocked()) return false;
3141 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142
Adam Cohen1697b792013-09-17 19:08:21 -07003143 if (v instanceof Workspace) {
3144 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003145 if (mWorkspace.enterOverviewMode()) {
3146 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3147 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3148 return true;
3149 } else {
3150 return false;
3151 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003152 } else {
3153 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003154 }
Adam Cohen1697b792013-09-17 19:08:21 -07003155 }
3156
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003157 CellLayout.CellInfo longClickCellInfo = null;
3158 View itemUnderLongClick = null;
3159 if (v.getTag() instanceof ItemInfo) {
3160 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003161 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003162 itemUnderLongClick = longClickCellInfo.cell;
3163 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 }
3165
Winson Chung3d503fb2011-07-13 17:25:49 -07003166 // The hotseat touch handling does not go through Workspace, and we always allow long press
3167 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003168 final boolean inHotseat = isHotseatLayout(v);
3169 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003170 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003171 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003172 // User long pressed on empty space
3173 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3174 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003175 if (mWorkspace.isInOverviewMode()) {
3176 mWorkspace.startReordering(v);
3177 } else {
3178 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003180 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003181 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3182 mHotseat.getOrderInHotseat(
3183 longClickCellInfo.cellX,
3184 longClickCellInfo.cellY));
3185 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003187 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 }
3189 }
3190 }
3191 return true;
3192 }
3193
Winson Chung3d503fb2011-07-13 17:25:49 -07003194 boolean isHotseatLayout(View layout) {
3195 return mHotseat != null && layout != null &&
3196 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3197 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003198
Winson Chung3d503fb2011-07-13 17:25:49 -07003199 /**
3200 * Returns the CellLayout of the specified container at the specified screen.
3201 */
Sunny Goyal92820592015-03-02 11:31:35 -08003202 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003203 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3204 if (mHotseat != null) {
3205 return mHotseat.getLayout();
3206 } else {
3207 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003208 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003209 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003210 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003211 }
3212 }
3213
Winson Chungb745afb2015-03-02 11:51:23 -08003214 /**
3215 * For overridden classes.
3216 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003217 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003218 return isAppsViewVisible();
3219 }
3220
3221 public boolean isAppsViewVisible() {
3222 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3223 }
3224
3225 public boolean isWidgetsViewVisible() {
3226 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003227 }
3228
Craig Mautner360310b2012-10-26 15:13:08 -07003229 private void setWorkspaceBackground(boolean workspace) {
3230 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003231 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003232 }
3233
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003234 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003235 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3236 int curflags = getWindow().getAttributes().flags
3237 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3238 if (wpflags != curflags) {
3239 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3240 }
Craig Mautner360310b2012-10-26 15:13:08 -07003241 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003242 }
3243
Michael Jurkae326f182011-11-21 14:05:46 -08003244 @Override
3245 public void onTrimMemory(int level) {
3246 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003247 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3248 // The widget preview db can result in holding onto over
3249 // 3MB of memory for caching which isn't necessary.
3250 SQLiteDatabase.releaseMemory();
3251
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003252 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003253 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003254 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003255 if (mLauncherCallbacks != null) {
3256 mLauncherCallbacks.onTrimMemory(level);
3257 }
Michael Jurkae326f182011-11-21 14:05:46 -08003258 }
3259
Winson Chungb745afb2015-03-02 11:51:23 -08003260 @Override
3261 public void onStateTransitionHideSearchBar() {
3262 // Hide the search bar
3263 if (mSearchDropTargetBar != null) {
3264 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3265 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003266 }
3267
Winson Chungb745afb2015-03-02 11:51:23 -08003268 protected void showWorkspace(boolean animated) {
3269 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003270 }
3271
Adam Cohened66b2b2012-01-23 17:28:51 -08003272 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003273 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003274 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003275 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003276 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
3277 animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003278
Winson Chungc7d2b602012-05-16 17:02:20 -07003279 // Show the search bar (only animate if we were showing the drop target bar in spring
3280 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003281 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003282 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003283 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003284
Michael Jurkab3e22d92011-10-31 15:58:33 -07003285 // Set focus to the AppsCustomize button
3286 if (mAllAppsButton != null) {
3287 mAllAppsButton.requestFocus();
3288 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003289 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003290
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003291 // Change the state *after* we've called all the transition code
3292 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003293
Winson Chung5fb63472011-02-02 17:03:37 -08003294 // Resume the auto-advance of widgets
3295 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003296 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003297
alanv1d4fde62012-10-17 13:15:47 -07003298 // Send an accessibility event to announce the context change
3299 getWindow().getDecorView()
3300 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003301
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003302 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003303 }
3304
Adam Cohened307df2013-10-02 09:37:31 -07003305 void showOverviewMode(boolean animated) {
3306 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003307 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
3308 animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003309 mState = State.WORKSPACE;
3310 onWorkspaceShown(animated);
3311 }
3312
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003313 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003314 }
3315
Winson Chungb745afb2015-03-02 11:51:23 -08003316 /**
3317 * Shows the apps view.
3318 */
3319 void showAppsView(boolean animated, boolean resetListToTop) {
3320 if (resetListToTop) {
3321 mAppsView.scrollToTop();
3322 }
3323 showAppsOrWidgets(animated, State.APPS);
3324 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003325
Winson Chungb745afb2015-03-02 11:51:23 -08003326 /**
3327 * Shows the widgets view.
3328 */
3329 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003330 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003331 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003332 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003333 }
Winson Chungb745afb2015-03-02 11:51:23 -08003334 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003335
3336 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003337 @Override
3338 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003339 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003340 }
3341 });
Winson Chungb745afb2015-03-02 11:51:23 -08003342 }
3343
3344 /**
3345 * Sets up the transition to show the apps/widgets view.
3346 */
3347 private void showAppsOrWidgets(boolean animated, State toState) {
3348 if (mState != State.WORKSPACE) return;
3349 if (toState != State.APPS && toState != State.WIDGETS) return;
3350
3351 if (toState == State.APPS) {
3352 mStateTransitionAnimation.startAnimationToAllApps(animated);
3353 } else {
3354 mStateTransitionAnimation.startAnimationToWidgets(animated);
3355 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003356
Michael Jurkab3e22d92011-10-31 15:58:33 -07003357 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003358 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003359
3360 // Pause the auto-advance of widgets until we are out of AllApps
3361 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003362 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003363 closeFolder();
3364
3365 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003366 getWindow().getDecorView()
3367 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003368 }
3369
Hyunyoung Song3f471442015-04-08 19:01:34 -07003370 public void enterSpringLoadedDragMode() {
3371 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3372 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003373 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3374 mState == State.WIDGETS_SPRING_LOADED) {
3375 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003376 }
Winson Chungb745afb2015-03-02 11:51:23 -08003377
3378 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
3379 true /* animated */, null /* onCompleteRunnable */);
3380 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003381 }
Adam Cohen7777d962011-08-18 18:58:38 -07003382
Hyunyoung Song3f471442015-04-08 19:01:34 -07003383 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003384 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003385 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003386
Winson Chunge7a03942011-08-05 15:05:12 -07003387 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003388 @Override
3389 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003390 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003391 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3392 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003393 // Before we show workspace, hide all apps again because
3394 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3395 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003396 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003397 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003398 } else {
3399 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003400 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003401 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003402 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003403 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003404
Michael Jurkad3ef3062010-11-23 16:23:58 -08003405 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003406 if (mState == State.APPS_SPRING_LOADED) {
3407 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3408 mState = State.APPS;
3409 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3410 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3411 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003412 }
3413 // Otherwise, we are not in spring loaded mode, so don't do anything.
3414 }
3415
Michael Jurkab3e22d92011-10-31 15:58:33 -07003416 void lockAllApps() {
3417 // TODO
3418 }
3419
3420 void unlockAllApps() {
3421 // TODO
3422 }
3423
Sunny Goyal594d76d2014-11-06 10:12:54 -08003424 protected void disableVoiceButtonProxy(boolean disable) {
3425 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003426 }
3427
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003428 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003429 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3430 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003431 }
3432
Adam Cohen24ce0b32014-01-14 16:18:14 -08003433 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003434 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3435 if (searchProvider == null) {
3436 return null;
3437 }
3438
3439 Bundle opts = new Bundle();
3440 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003441 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003442
3443 SharedPreferences sp = getSharedPreferences(
3444 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3445 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003446 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003447 if (!searchProvider.provider.flattenToString().equals(
3448 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003449 || (widgetInfo == null)
3450 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003451 // A valid widget is not already bound.
3452 if (widgetId > -1) {
3453 mAppWidgetHost.deleteAppWidgetId(widgetId);
3454 widgetId = -1;
3455 }
3456
3457 // Try to bind a new widget
3458 widgetId = mAppWidgetHost.allocateAppWidgetId();
3459
3460 if (!AppWidgetManagerCompat.getInstance(this)
3461 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3462 mAppWidgetHost.deleteAppWidgetId(widgetId);
3463 widgetId = -1;
3464 }
3465
3466 sp.edit()
3467 .putInt(QSB_WIDGET_ID, widgetId)
3468 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3469 .commit();
3470 }
3471
3472 if (widgetId != -1) {
3473 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3474 mQsb.updateAppWidgetOptions(opts);
3475 mQsb.setPadding(0, 0, 0, 0);
3476 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003477 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003478 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003479 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003480 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003481 }
3482
Sunny Goyal22235bc2015-04-03 09:23:43 -07003483 private void reinflateQSBIfNecessary() {
3484 if (mQsb instanceof LauncherAppWidgetHostView &&
3485 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3486 mSearchDropTargetBar.removeView(mQsb);
3487 mQsb = null;
3488 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3489 }
3490 }
3491
Michael Jurkad7c28052012-04-27 15:43:36 -07003492 @Override
3493 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003494 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003495 final List<CharSequence> text = event.getText();
3496 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003497 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003498 if (mState == State.APPS) {
3499 text.add("Apps");
3500 } else if (mState == State.WIDGETS) {
3501 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003502 } else {
3503 text.add(getString(R.string.all_apps_home_button_label));
3504 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003505 return result;
3506 }
3507
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003508 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003509 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003510 */
Adam Cohen091440a2015-03-18 14:16:05 -07003511 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003512 @Override
3513 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003514 closeSystemDialogs();
3515 }
3516 }
3517
3518 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003519 * Receives notifications whenever the appwidgets are reset.
3520 */
3521 private class AppWidgetResetObserver extends ContentObserver {
3522 public AppWidgetResetObserver() {
3523 super(new Handler());
3524 }
3525
3526 @Override
3527 public void onChange(boolean selfChange) {
3528 onAppWidgetReset();
3529 }
3530 }
3531
3532 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003533 * If the activity is currently paused, signal that we need to run the passed Runnable
3534 * in onResume.
3535 *
3536 * This needs to be called from incoming places where resources might have been loaded
3537 * while we are paused. That is becaues the Configuration might be wrong
3538 * when we're not running, and if it comes back to what it was when we
3539 * were paused, we are not restarted.
3540 *
3541 * Implementation of the method from LauncherModel.Callbacks.
3542 *
3543 * @return true if we are currently paused. The caller might be able to
3544 * skip some work in that case since we will come back again.
3545 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003546 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003547 if (mPaused) {
3548 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003549 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003550 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003551 }
3552 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003553 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003554 return true;
3555 } else {
3556 return false;
3557 }
3558 }
3559
Michael Jurkac402cd92013-05-20 15:49:32 +02003560 private boolean waitUntilResume(Runnable run) {
3561 return waitUntilResume(run, false);
3562 }
3563
Michael Jurka1e2f4652013-07-08 18:03:46 -07003564 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003565 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003566 }
3567
Michael Jurka7607c2f2013-04-03 14:33:19 -07003568 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003569 * If the activity is currently paused, signal that we need to re-run the loader
3570 * in onResume.
3571 *
3572 * This needs to be called from incoming places where resources might have been loaded
3573 * while we are paused. That is becaues the Configuration might be wrong
3574 * when we're not running, and if it comes back to what it was when we
3575 * were paused, we are not restarted.
3576 *
3577 * Implementation of the method from LauncherModel.Callbacks.
3578 *
3579 * @return true if we are currently paused. The caller might be able to
3580 * skip some work in that case since we will come back again.
3581 */
3582 public boolean setLoadOnResume() {
3583 if (mPaused) {
3584 Log.i(TAG, "setLoadOnResume");
3585 mOnResumeNeedsLoad = true;
3586 return true;
3587 } else {
3588 return false;
3589 }
3590 }
3591
3592 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003593 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003594 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003596 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003597 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003598 } else {
3599 return SCREEN_COUNT / 2;
3600 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003602
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 /**
3604 * Refreshes the shortcuts shown on the workspace.
3605 *
3606 * Implementation of the method from LauncherModel.Callbacks.
3607 */
3608 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003609 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003610
Michael Jurka7607c2f2013-04-03 14:33:19 -07003611 // If we're starting binding all over again, clear any bind calls we'd postponed in
3612 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3613 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003614 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003615
Winson Chungd64d1762013-08-20 14:37:16 -07003616 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003617 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003618 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003619
Michael Jurka05bf6442011-11-30 20:28:53 -08003620 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003621 if (mHotseat != null) {
3622 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003623 }
3624 }
3625
Adam Cohendcd297f2013-06-18 13:13:40 -07003626 @Override
3627 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003628 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003629
Adam Cohen5084cba2013-09-03 12:01:16 -07003630 // If there are no screens, we need to have an empty screen
3631 if (orderedScreenIds.size() == 0) {
3632 mWorkspace.addExtraEmptyScreen();
3633 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003634
3635 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003636 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003637 if (hasCustomContentToLeft()) {
3638 mWorkspace.createCustomContentContainer();
3639 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003640 }
Winson Chung64359a52013-07-08 17:17:08 -07003641 }
3642
3643 @Override
3644 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003645 // Log to disk
3646 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3647 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3648 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003649 int count = orderedScreenIds.size();
3650 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003651 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003652 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003653 }
3654
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003655 @Override
3656 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3657 final long screenId, final int[] cell, final int spanX, final int spanY) {
3658 showWorkspace(true, new Runnable() {
3659
3660 @Override
3661 public void run() {
3662 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3663 addPendingItem(info, container, screenId, cell, spanX, spanY);
3664 }
3665 });
3666 }
3667
Adam Cohen39a06042013-07-19 14:30:12 -07003668 private boolean shouldShowWeightWatcher() {
3669 String spKey = LauncherAppState.getSharedPreferencesKey();
3670 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003671 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003672
3673 return show;
3674 }
3675
3676 private void toggleShowWeightWatcher() {
3677 String spKey = LauncherAppState.getSharedPreferencesKey();
3678 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3679 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3680
3681 show = !show;
3682
3683 SharedPreferences.Editor editor = sp.edit();
3684 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3685 editor.commit();
3686
3687 if (mWeightWatcher != null) {
3688 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3689 }
3690 }
3691
Winson Chungd64d1762013-08-20 14:37:16 -07003692 public void bindAppsAdded(final ArrayList<Long> newScreens,
3693 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003694 final ArrayList<ItemInfo> addAnimated,
3695 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003696 Runnable r = new Runnable() {
3697 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003698 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003699 }
3700 };
3701 if (waitUntilResume(r)) {
3702 return;
3703 }
3704
Winson Chungd64d1762013-08-20 14:37:16 -07003705 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003706 if (newScreens != null) {
3707 bindAddScreens(newScreens);
3708 }
Winson Chungd64d1762013-08-20 14:37:16 -07003709
3710 // We add the items without animation on non-visible pages, and with
3711 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003712 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003713 bindItems(addNotAnimated, 0,
3714 addNotAnimated.size(), false);
3715 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003716 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003717 bindItems(addAnimated, 0,
3718 addAnimated.size(), true);
3719 }
Winson Chungc58497e2013-09-03 17:48:37 -07003720
Winson Chung87412982013-10-03 18:34:14 -07003721 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003722 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003723
Winson Chungb745afb2015-03-02 11:51:23 -08003724 if (addedApps != null && mAppsView != null) {
3725 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003726 }
Winson Chungd64d1762013-08-20 14:37:16 -07003727 }
3728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003729 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 * Bind the items start-end from the list.
3731 *
3732 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003733 */
Winson Chung64359a52013-07-08 17:17:08 -07003734 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3735 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003736 Runnable r = new Runnable() {
3737 public void run() {
3738 bindItems(shortcuts, start, end, forceAnimateIcons);
3739 }
3740 };
3741 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003742 return;
3743 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003744
Winson Chungf0c6ae02012-03-21 16:10:31 -07003745 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003746 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3747 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003748 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003749 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003750 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003751 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003752 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003753
3754 // Short circuit if we are loading dock items for a configuration which has no dock
3755 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3756 mHotseat == null) {
3757 continue;
3758 }
3759
Joe Onorato9c1289c2009-08-17 11:03:03 -04003760 switch (item.itemType) {
3761 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3762 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003763 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003764 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003765
Winson Chung64359a52013-07-08 17:17:08 -07003766 /*
3767 * TODO: FIX collision case
3768 */
Winson Chungce376632013-07-11 16:12:41 -07003769 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3770 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3771 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003772 View v = cl.getChildAt(item.cellX, item.cellY);
3773 Object tag = v.getTag();
3774 String desc = "Collision while binding workspace item: " + item
3775 + ". Collides with " + tag;
3776 if (LauncherAppState.isDogfoodBuild()) {
3777 throw (new RuntimeException(desc));
3778 } else {
3779 Log.d(TAG, desc);
3780 }
Winson Chungce376632013-07-11 16:12:41 -07003781 }
Winson Chung64359a52013-07-08 17:17:08 -07003782 }
3783
Adam Cohendcd297f2013-06-18 13:13:40 -07003784 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3785 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003786 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003787 // Animate all the applications up now
3788 shortcut.setAlpha(0f);
3789 shortcut.setScaleX(0f);
3790 shortcut.setScaleY(0f);
3791 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003792 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003794 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003795 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003796 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003797 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003798 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003799 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3800 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003801 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003802 default:
3803 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003805 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003806
Winson Chung997a9232013-07-24 15:33:46 -07003807 if (animateIcons) {
3808 // Animate to the correct page
3809 if (newShortcutsScreenId > -1) {
3810 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003811 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003812 final Runnable startBounceAnimRunnable = new Runnable() {
3813 public void run() {
3814 anim.playTogether(bounceAnims);
3815 anim.start();
3816 }
3817 };
Winson Chung997a9232013-07-24 15:33:46 -07003818 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003819 // We post the animation slightly delayed to prevent slowdowns
3820 // when we are loading right after we return to launcher.
3821 mWorkspace.postDelayed(new Runnable() {
3822 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003823 if (mWorkspace != null) {
3824 mWorkspace.snapToPage(newScreenIndex);
3825 mWorkspace.postDelayed(startBounceAnimRunnable,
3826 NEW_APPS_ANIMATION_DELAY);
3827 }
Winson Chung94d67682013-09-25 16:29:40 -07003828 }
3829 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003830 } else {
3831 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003832 }
3833 }
Winson Chung64359a52013-07-08 17:17:08 -07003834 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003836 }
3837
Joe Onorato9c1289c2009-08-17 11:03:03 -04003838 /**
3839 * Implementation of the method from LauncherModel.Callbacks.
3840 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003841 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003842 Runnable r = new Runnable() {
3843 public void run() {
3844 bindFolders(folders);
3845 }
3846 };
3847 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003848 return;
3849 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003850 sFolders.clear();
3851 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003853
3854 /**
3855 * Add the views for a widget to the workspace.
3856 *
3857 * Implementation of the method from LauncherModel.Callbacks.
3858 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003859 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003860 Runnable r = new Runnable() {
3861 public void run() {
3862 bindAppWidget(item);
3863 }
3864 };
3865 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003866 return;
3867 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003868
Daniel Sandler843e8602010-06-07 14:59:01 -04003869 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3870 if (DEBUG_WIDGETS) {
3871 Log.d(TAG, "bindAppWidget: " + item);
3872 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 final Workspace workspace = mWorkspace;
3874
Adam Cohen59400422014-03-05 18:07:04 -08003875 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003876 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003877
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003878 if (!mIsSafeModeEnabled
3879 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3880 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003881 if (appWidgetInfo == null) {
3882 if (DEBUG_WIDGETS) {
3883 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3884 + " belongs to component " + item.providerName
3885 + ", as the povider is null");
3886 }
3887 LauncherModel.deleteItemFromDatabase(this, item);
3888 return;
3889 }
3890 // Note: This assumes that the id remap broadcast is received before this step.
3891 // If that is not the case, the id remap will be ignored and user may see the
3892 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003893 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003894 pendingInfo.spanX = item.spanX;
3895 pendingInfo.spanY = item.spanY;
3896 pendingInfo.minSpanX = item.minSpanX;
3897 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003898 Bundle options = null;
3899 // AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003900
Sunny Goyalff572272014-07-23 13:58:07 -07003901 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003902 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3903 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003904
3905 // TODO consider showing a permission dialog when the widget is clicked.
3906 if (!success) {
3907 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3908 if (DEBUG_WIDGETS) {
3909 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3910 + " belongs to component " + item.providerName
3911 + ", as the launcher is unable to bing a new widget id");
3912 }
3913 LauncherModel.deleteItemFromDatabase(this, item);
3914 return;
3915 }
3916
3917 item.appWidgetId = newWidgetId;
3918
3919 // If the widget has a configure activity, it is still needs to set it up, otherwise
3920 // the widget is ready to go.
3921 item.restoreStatus = (appWidgetInfo.configure == null)
3922 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3923 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3924
3925 LauncherModel.updateItemInDatabase(this, item);
3926 }
3927
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003928 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003929 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003930 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003931 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3932 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003933 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003934
Sunny Goyal651077b2014-06-30 14:15:31 -07003935 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3936 } else {
3937 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003938 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3939 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003940 view.updateIcon(mIconCache);
3941 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003942 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003943 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003944 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945
Joe Onorato9c1289c2009-08-17 11:03:03 -04003946 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003947 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003948
Adam Cohendcd297f2013-06-18 13:13:40 -07003949 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003950 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003951 if (!item.isCustomWidget()) {
3952 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3953 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003954
Joe Onorato9c1289c2009-08-17 11:03:03 -04003955 workspace.requestLayout();
3956
Daniel Sandler843e8602010-06-07 14:59:01 -04003957 if (DEBUG_WIDGETS) {
3958 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3959 + (SystemClock.uptimeMillis()-start) + "ms");
3960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961 }
3962
Sunny Goyalff572272014-07-23 13:58:07 -07003963 /**
3964 * Restores a pending widget.
3965 *
3966 * @param appWidgetId The app widget id
3967 * @param cellInfo The position on screen where to create the widget.
3968 */
3969 private void completeRestoreAppWidget(final int appWidgetId) {
3970 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3971 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3972 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3973 return;
3974 }
3975
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003976 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003977 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3978
3979 mWorkspace.reinflateWidgetsIfNecessary();
3980 LauncherModel.updateItemInDatabase(this, info);
3981 }
3982
Adam Cohen1462de32012-07-24 22:34:36 -07003983 public void onPageBoundSynchronously(int page) {
3984 mSynchronouslyBoundPages.add(page);
3985 }
3986
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 /**
3988 * Callback saying that there aren't any more items to bind.
3989 *
3990 * Implementation of the method from LauncherModel.Callbacks.
3991 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003992 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003993 Runnable r = new Runnable() {
3994 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003995 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003996 }
3997 };
3998 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003999 return;
4000 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004001 if (mSavedState != null) {
4002 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004003 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004005 mSavedState = null;
4006 }
4007
Adam Cohen1462de32012-07-24 22:34:36 -07004008 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004009
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004010 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004011 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004012
4013 // If we received the result of any pending adds while the loader was running (e.g. the
4014 // widget configuration forced an orientation change), process them now.
4015 if (sPendingAddItem != null) {
4016 final long screenId = completeAdd(sPendingAddItem);
4017
4018 // TODO: this moves the user to the page where the pending item was added. Ideally,
4019 // the screen would be guaranteed to exist after bind, and the page would be set through
4020 // the workspace restore process.
4021 mWorkspace.post(new Runnable() {
4022 @Override
4023 public void run() {
4024 mWorkspace.snapToScreenId(screenId);
4025 }
4026 });
4027 sPendingAddItem = null;
4028 }
4029
Sunny Goyale755d462014-07-22 13:48:29 -07004030 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004031
4032 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004033 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004034 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004035 }
4036
Adam Cohen3ed316a2014-07-23 18:21:20 -07004037 private void sendLoadingCompleteBroadcastIfNecessary() {
4038 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4039 String permission =
4040 getResources().getString(R.string.receive_first_load_broadcast_permission);
4041 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4042 sendBroadcast(intent, permission);
4043 SharedPreferences.Editor editor = mSharedPrefs.edit();
4044 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4045 editor.apply();
4046 }
4047 }
4048
Winson Chunga0b7e862013-09-05 16:03:15 -07004049 public boolean isAllAppsButtonRank(int rank) {
4050 if (mHotseat != null) {
4051 return mHotseat.isAllAppsButtonRank(rank);
4052 }
4053 return false;
4054 }
4055
Winson Chunga2413752012-04-03 14:22:34 -07004056 private boolean canRunNewAppsAnimation() {
4057 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4058 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4059 }
4060
Winson Chungc9168342013-06-26 14:54:55 -07004061 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4062 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4063 PropertyValuesHolder.ofFloat("alpha", 1f),
4064 PropertyValuesHolder.ofFloat("scaleX", 1f),
4065 PropertyValuesHolder.ofFloat("scaleY", 1f));
4066 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4067 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4068 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4069 return bounceAnim;
4070 }
4071
Adam Cohen27772732013-11-11 14:00:56 +00004072 public boolean useVerticalBarLayout() {
4073 return LauncherAppState.getInstance().getDynamicGrid().
4074 getDeviceProfile().isVerticalBarLayout();
4075 }
4076
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004077 protected Rect getSearchBarBounds() {
4078 return LauncherAppState.getInstance().getDynamicGrid().
4079 getDeviceProfile().getSearchBarBounds();
4080 }
4081
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004082 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004083 if (mSearchDropTargetBar == null) {
4084 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004085 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004086 if (mQsb != null) {
4087 mSearchDropTargetBar.removeView(mQsb);
4088 mQsb = null;
4089 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004090 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004091 }
4092
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004093 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004094 * Add the icons for all apps.
4095 *
4096 * Implementation of the method from LauncherModel.Callbacks.
4097 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004098 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004099 if (mAppsView != null) {
4100 mAppsView.setApps(apps);
4101 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07004102 if (mWidgetsView != null) {
4103 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4104 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004105 }
Adam Cohen9211d422014-10-07 18:14:53 -07004106 if (mLauncherCallbacks != null) {
4107 mLauncherCallbacks.bindAllApplications(apps);
4108 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004109 }
4110
4111 /**
4112 * A package was updated.
4113 *
4114 * Implementation of the method from LauncherModel.Callbacks.
4115 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004116 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004117 Runnable r = new Runnable() {
4118 public void run() {
4119 bindAppsUpdated(apps);
4120 }
4121 };
4122 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004123 return;
4124 }
4125
Winson Chungb745afb2015-03-02 11:51:23 -08004126 if (mAppsView != null) {
4127 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004128 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004129 }
4130
Sunny Goyal4390ace2014-10-13 11:33:11 -07004131 @Override
4132 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4133 Runnable r = new Runnable() {
4134 public void run() {
4135 bindWidgetsRestored(widgets);
4136 }
4137 };
4138 if (waitUntilResume(r)) {
4139 return;
4140 }
4141 mWorkspace.widgetsRestored(widgets);
4142 }
4143
Joe Onorato9c1289c2009-08-17 11:03:03 -04004144 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004145 * Some shortcuts were updated in the background.
4146 *
4147 * Implementation of the method from LauncherModel.Callbacks.
4148 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004149 @Override
4150 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4151 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004152 Runnable r = new Runnable() {
4153 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004154 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004155 }
4156 };
4157 if (waitUntilResume(r)) {
4158 return;
4159 }
4160
Sunny Goyal4390ace2014-10-13 11:33:11 -07004161 if (!updated.isEmpty()) {
4162 mWorkspace.updateShortcuts(updated);
4163 }
4164
4165 if (!removed.isEmpty()) {
4166 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4167 for (ShortcutInfo si : removed) {
4168 removedComponents.add(si.getTargetComponent());
4169 }
4170 mWorkspace.removeItemsByComponentName(removedComponents, user);
4171 // Notify the drag controller
4172 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004173 }
4174 }
4175
4176 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004177 * Update the state of a package, typically related to install state.
4178 *
4179 * Implementation of the method from LauncherModel.Callbacks.
4180 */
Sunny Goyale755d462014-07-22 13:48:29 -07004181 @Override
4182 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004183 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004184 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004185 }
4186 }
4187
4188 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004189 * Update the label and icon of all the icons in a package
4190 *
4191 * Implementation of the method from LauncherModel.Callbacks.
4192 */
4193 @Override
4194 public void updatePackageBadge(String packageName) {
4195 if (mWorkspace != null) {
4196 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4197 }
4198 }
4199
4200 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004201 * A package was uninstalled. We take both the super set of packageNames
4202 * in addition to specific applications to remove, the reason being that
4203 * this can be called when a package is updated as well. In that scenario,
4204 * we only remove specific components from the workspace, where as
4205 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004206 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004207 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004208 * Implementation of the method from LauncherModel.Callbacks.
4209 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004210 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004211 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004212 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004213 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004214 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004215 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004216 }
Winson Chungd64d1762013-08-20 14:37:16 -07004217 };
4218 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004219 return;
4220 }
4221
Sunny Goyal1a745e82014-10-02 15:58:31 -07004222 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004223 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4224 for (AppInfo info : appInfos) {
4225 removedComponents.add(info.componentName);
4226 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004227 if (!packageNames.isEmpty()) {
4228 mWorkspace.removeItemsByPackageName(packageNames, user);
4229 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004230 if (!removedComponents.isEmpty()) {
4231 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004232 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004233 // Notify the drag controller
4234 mDragController.onAppsRemoved(packageNames, removedComponents);
4235
Sunny Goyal1a745e82014-10-02 15:58:31 -07004236 } else {
4237 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004238 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004239
Winson Chungdf95eb12013-10-16 14:57:07 -07004240 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004241 if (mAppsView != null) {
4242 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004243 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004244 }
Joe Onoratobe386092009-11-17 17:32:16 -08004245
4246 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004247 * A number of packages were updated.
4248 */
Adam Cohen091440a2015-03-18 14:16:05 -07004249 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004250 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004251 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004252 bindPackagesUpdated(mWidgetsAndShortcuts);
4253 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004254 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004255 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004256
Michael Jurkac402cd92013-05-20 15:49:32 +02004257 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4258 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4259 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004260 return;
4261 }
4262
Hyunyoung Song3f471442015-04-08 19:01:34 -07004263 if (mWidgetsView != null) {
4264 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4265 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004266 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004267 }
4268
Winson Chung400438b2011-01-16 17:53:48 -08004269 private int mapConfigurationOriActivityInfoOri(int configOri) {
4270 final Display d = getWindowManager().getDefaultDisplay();
4271 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4272 switch (d.getRotation()) {
4273 case Surface.ROTATION_0:
4274 case Surface.ROTATION_180:
4275 // We are currently in the same basic orientation as the natural orientation
4276 naturalOri = configOri;
4277 break;
4278 case Surface.ROTATION_90:
4279 case Surface.ROTATION_270:
4280 // We are currently in the other basic orientation to the natural orientation
4281 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4282 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4283 break;
4284 }
4285
4286 int[] oriMap = {
4287 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4288 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4289 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4290 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4291 };
4292 // Since the map starts at portrait, we need to offset if this device's natural orientation
4293 // is landscape.
4294 int indexOffset = 0;
4295 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4296 indexOffset = 1;
4297 }
4298 return oriMap[(d.getRotation() + indexOffset) % 4];
4299 }
Adam Cohen446e9402011-09-15 18:21:21 -07004300
Winson Chung4b919f82012-05-01 10:44:08 -07004301 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004302 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004303 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4304 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4305 .getConfiguration().orientation));
4306 } else {
4307 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4308 }
Winson Chung4b919f82012-05-01 10:44:08 -07004309 }
4310 }
4311 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004312 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004313 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004314 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004315 } else {
4316 mHandler.postDelayed(new Runnable() {
4317 public void run() {
4318 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4319 }
4320 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004321 }
Winson Chung4b919f82012-05-01 10:44:08 -07004322 }
Winson Chung400438b2011-01-16 17:53:48 -08004323 }
4324
Winson Chunge43a1e72014-01-15 10:33:02 -08004325 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004326 if (mLauncherCallbacks != null) {
4327 return mLauncherCallbacks.isLauncherPreinstalled();
4328 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004329 PackageManager pm = getPackageManager();
4330 try {
4331 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4332 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4333 return true;
4334 } else {
4335 return false;
4336 }
4337 } catch (NameNotFoundException e) {
4338 e.printStackTrace();
4339 return false;
4340 }
4341 }
4342
Adam Cohenea90f832014-05-21 15:03:34 -07004343 /**
4344 * This method indicates whether or not we should suggest default wallpaper dimensions
4345 * when our wallpaper cropper was not yet used to set a wallpaper.
4346 */
4347 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004348 if (mLauncherCallbacks != null) {
4349 return mLauncherCallbacks.overrideWallpaperDimensions();
4350 }
Adam Cohenea90f832014-05-21 15:03:34 -07004351 return true;
4352 }
4353
Adam Cohen432609a2014-03-13 17:03:22 -07004354 /**
4355 * To be overridden by subclasses to indicate that there is an activity to launch
4356 * before showing the standard launcher experience.
4357 */
4358 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004359 if (mLauncherCallbacks != null) {
4360 return mLauncherCallbacks.hasFirstRunActivity();
4361 }
Adam Cohen432609a2014-03-13 17:03:22 -07004362 return false;
4363 }
4364
4365 /**
4366 * To be overridden by subclasses to launch any first run activity
4367 */
4368 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004369 if (mLauncherCallbacks != null) {
4370 return mLauncherCallbacks.getFirstRunActivity();
4371 }
Adam Cohen432609a2014-03-13 17:03:22 -07004372 return null;
4373 }
4374
Winson Chunga6945242014-01-08 14:04:34 -08004375 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004376 return !ActivityManager.isRunningInTestHarness() &&
4377 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004378 }
4379
Adam Cohen4a9423d2014-03-28 14:22:31 -07004380 protected boolean hasRunFirstRunActivity() {
4381 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4382 }
4383
Adam Cohen432609a2014-03-13 17:03:22 -07004384 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004385 if (shouldRunFirstRunActivity() &&
4386 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004387 Intent firstRunIntent = getFirstRunActivity();
4388 if (firstRunIntent != null) {
4389 startActivity(firstRunIntent);
4390 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004391 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004392 }
4393 }
Adam Cohen432609a2014-03-13 17:03:22 -07004394 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004395 }
4396
4397 private void markFirstRunActivityShown() {
4398 SharedPreferences.Editor editor = mSharedPrefs.edit();
4399 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4400 editor.apply();
4401 }
4402
Adam Cohen432609a2014-03-13 17:03:22 -07004403 /**
4404 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4405 * screen that must be displayed and dismissed.
4406 */
4407 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004408 if (mLauncherCallbacks != null) {
4409 return mLauncherCallbacks.hasDismissableIntroScreen();
4410 }
Adam Cohen432609a2014-03-13 17:03:22 -07004411 return false;
4412 }
4413
4414 /**
4415 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4416 */
4417 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004418 if (mLauncherCallbacks != null) {
4419 return mLauncherCallbacks.getIntroScreen();
4420 }
Adam Cohen432609a2014-03-13 17:03:22 -07004421 return null;
4422 }
4423
4424 /**
4425 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4426 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4427 */
4428 private boolean shouldShowIntroScreen() {
4429 return hasDismissableIntroScreen() &&
4430 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4431 }
4432
4433 protected void showIntroScreen() {
4434 View introScreen = getIntroScreen();
4435 changeWallpaperVisiblity(false);
4436 if (introScreen != null) {
4437 mDragLayer.showOverlayView(introScreen);
4438 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004439 if (mLauncherOverlayContainer != null) {
4440 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4441 }
Adam Cohen432609a2014-03-13 17:03:22 -07004442 }
4443
4444 public void dismissIntroScreen() {
4445 markIntroScreenDismissed();
4446 if (showFirstRunActivity()) {
4447 // We delay hiding the intro view until the first run activity is showing. This
4448 // avoids a blip.
4449 mWorkspace.postDelayed(new Runnable() {
4450 @Override
4451 public void run() {
4452 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004453 if (mLauncherOverlayContainer != null) {
4454 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4455 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004456 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004457 }
4458 }, ACTIVITY_START_DELAY);
4459 } else {
4460 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004461 if (mLauncherOverlayContainer != null) {
4462 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4463 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004464 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004465 }
4466 changeWallpaperVisiblity(true);
4467 }
4468
4469 private void markIntroScreenDismissed() {
4470 SharedPreferences.Editor editor = mSharedPrefs.edit();
4471 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4472 editor.apply();
4473 }
4474
Adam Cohen091440a2015-03-18 14:16:05 -07004475 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004476 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4477 // on the device, then we always show the first run cling experience (or if there is no
4478 // launcher2). Otherwise, we prompt the user upon started for migration
4479 LauncherClings launcherClings = new LauncherClings(this);
4480 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4481 if (mModel.canMigrateFromOldLauncherDb(this)) {
4482 launcherClings.showMigrationCling();
4483 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004484 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004485 }
4486 }
4487 }
4488
Winson Chunga6945242014-01-08 14:04:34 -08004489 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004490 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4491 if (mHotseat != null) mHotseat.setAlpha(1f);
4492 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4493 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004494 }
4495
4496 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004497 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4498 if (mHotseat != null) mHotseat.setAlpha(0f);
4499 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4500 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004501 }
4502
Mathew Inwood72fbec12013-11-19 15:45:07 +00004503 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004504 // Called from search suggestion, not supported in other profiles.
4505 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4506 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4507 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4508 myUser);
4509 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004510 return null;
4511 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004512 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004513 }
4514
4515 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4516 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004517 // Called from search suggestion, not supported in other profiles.
4518 return createShortcutDragInfo(shortcutIntent, caption, icon,
4519 UserHandleCompat.myUserHandle());
4520 }
4521
4522 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4523 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004524 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004525 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004526 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004527 }
4528
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004529 protected void moveWorkspaceToDefaultScreen() {
4530 mWorkspace.moveToDefaultScreen(false);
4531 }
4532
Mathew Inwood72fbec12013-11-19 15:45:07 +00004533 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4534 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004535 mWorkspace.onExternalDragStartedWithItem(dragView);
4536 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004537 }
4538
Anjali Koppalf5d29132014-02-28 13:33:27 -08004539 @Override
4540 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004541 if (mLauncherCallbacks != null) {
4542 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4543 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004544 }
4545
Winson Chung80baf5a2010-08-09 16:03:15 -07004546 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004547 * Prints out out state for debugging.
4548 */
4549 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004550 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004551 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004552 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4553 Log.d(TAG, "mRestoring=" + mRestoring);
4554 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4555 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004556 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004557 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004558 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004559
Daniel Sandler325dc232013-06-05 22:57:57 -04004560 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004561 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004562
4563 @Override
4564 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4565 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004566 synchronized (sDumpLogs) {
4567 writer.println(" ");
4568 writer.println("Debug logs: ");
4569 for (int i = 0; i < sDumpLogs.size(); i++) {
4570 writer.println(" " + sDumpLogs.get(i));
4571 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004572 }
Adam Cohen9211d422014-10-07 18:14:53 -07004573 if (mLauncherCallbacks != null) {
4574 mLauncherCallbacks.dump(prefix, fd, writer, args);
4575 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004576 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004577
4578 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004579 if (DEBUG_DUMP_LOG) {
4580 synchronized (sDumpLogs) {
4581 Log.d(TAG, "");
4582 Log.d(TAG, "*********************");
4583 Log.d(TAG, "Launcher debug logs: ");
4584 for (int i = 0; i < sDumpLogs.size(); i++) {
4585 Log.d(TAG, " " + sDumpLogs.get(i));
4586 }
4587 Log.d(TAG, "*********************");
4588 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004589 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004590 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004591 }
4592
4593 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004594 addDumpLog(tag, log, null, debugLog);
4595 }
4596
4597 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004598 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004599 if (e != null) {
4600 Log.d(tag, log, e);
4601 } else {
4602 Log.d(tag, log);
4603 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004604 }
Winson Chungede41292013-09-19 16:27:36 -07004605 if (DEBUG_DUMP_LOG) {
4606 sDateStamp.setTime(System.currentTimeMillis());
4607 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004608 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4609 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004610 }
Winson Chungede41292013-09-19 16:27:36 -07004611 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004612 }
4613
Adam Cohen59400422014-03-05 18:07:04 -08004614 public static CustomAppWidget getCustomAppWidget(String name) {
4615 return sCustomAppWidgets.get(name);
4616 }
4617
4618 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4619 return sCustomAppWidgets;
4620 }
4621
Winson Chungede41292013-09-19 16:27:36 -07004622 public void dumpLogsToLocalData() {
4623 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004624 new AsyncTask<Void, Void, Void>() {
4625 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004626 boolean success = false;
4627 sDateStamp.setTime(sRunStart);
4628 String FILENAME = sDateStamp.getMonth() + "-"
4629 + sDateStamp.getDay() + "_"
4630 + sDateStamp.getHours() + "-"
4631 + sDateStamp.getMinutes() + "_"
4632 + sDateStamp.getSeconds() + ".txt";
4633
4634 FileOutputStream fos = null;
4635 File outFile = null;
4636 try {
4637 outFile = new File(getFilesDir(), FILENAME);
4638 outFile.createNewFile();
4639 fos = new FileOutputStream(outFile);
4640 } catch (Exception e) {
4641 e.printStackTrace();
4642 }
4643 if (fos != null) {
4644 PrintWriter writer = new PrintWriter(fos);
4645
4646 writer.println(" ");
4647 writer.println("Debug logs: ");
4648 synchronized (sDumpLogs) {
4649 for (int i = 0; i < sDumpLogs.size(); i++) {
4650 writer.println(" " + sDumpLogs.get(i));
4651 }
4652 }
4653 writer.close();
4654 }
4655 try {
4656 if (fos != null) {
4657 fos.close();
4658 success = true;
4659 }
4660 } catch (IOException e) {
4661 e.printStackTrace();
4662 }
Michael Jurka43467462013-11-14 12:03:58 +01004663 return null;
Winson Chungede41292013-09-19 16:27:36 -07004664 }
Michael Jurka43467462013-11-14 12:03:58 +01004665 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004666 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004668}
Michael Jurka2763be32011-02-24 11:19:57 -08004669
Michael Jurkaabded662011-03-04 12:06:57 -08004670interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004671 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004672 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004673 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004674 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004675 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004676}
Dan Sandlerd5024042014-01-09 15:01:33 -05004677
4678interface DebugIntents {
4679 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4680 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004681}