blob: 3d8890642380b881e06bec44f751f47d68129325 [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;
Winson Chung4ac30062015-05-08 17:34:17 -070067import android.preference.PreferenceManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070085import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070087import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080094import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070096
Sunny Goyal651077b2014-06-30 14:15:31 -070097import com.android.launcher3.DropTarget.DragObject;
98import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale2df0622015-04-24 11:27:00 -0700104import com.android.launcher3.util.LongArrayMap;
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;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700107import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700108import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700109
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.DataInputStream;
111import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800118import java.lang.reflect.InvocationTargetException;
119import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700122import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700125import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700126import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000127import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129/**
130 * Default launcher application.
131 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100132public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700133 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800134 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
135 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700136 static final String TAG = "Launcher";
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137 static final boolean LOGD = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Winson Chung83f59ab2015-05-05 17:21:58 -0700139 // Temporary flag
140 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
141
Daniel Sandlerf061f822013-06-27 13:59:36 -0400142 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700143 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700144 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700146 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700147
Dan Sandlerd5024042014-01-09 15:01:33 -0500148 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700156 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700157
Mathew Inwood876a8462013-06-14 14:12:41 +0100158 /**
159 * IntentStarter uses request codes starting with this. This must be greater than all activity
160 * request codes used internally.
161 */
162 protected static final int REQUEST_LAST = 100;
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
165
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800166 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
169 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung2672ff92012-05-04 16:22:30 -0700172 // The Intent extra that defines whether to ignore the launch animation
173 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400174 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
177 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700178 // Type: int
179 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
182 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
184 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700185 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: boolean
189 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
190 // Type: long
191 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700192 // Type: int
193 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
194 // Type: int
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
196 // Type: parcelable
197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000200 // Type: int[]
201 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Adam Cohen432609a2014-03-13 17:03:22 -0700203 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800204 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
205
Adam Cohen3ed316a2014-07-23 18:21:20 -0700206 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
207 static final String ACTION_FIRST_LOAD_COMPLETE =
208 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
209
Adam Cohen39a06042013-07-19 14:30:12 -0700210 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700211 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700212
Sunny Goyal594d76d2014-11-06 10:12:54 -0800213 private static final String QSB_WIDGET_ID = "qsb_widget_id";
214 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
215
Winson Chunga6945242014-01-08 14:04:34 -0800216 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
217
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800219 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700222
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700223 private boolean mIsSafeModeEnabled;
224
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
226 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700227 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700230 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
231 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700232 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000234 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
235 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
236
Winson Chunga2413752012-04-03 14:22:34 -0700237 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700238 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
239 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700240 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700241
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800242 private final BroadcastReceiver mCloseSystemDialogsReceiver
243 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800244 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private LayoutInflater mInflater;
247
Adam Cohen091440a2015-03-18 14:16:05 -0700248 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700249 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800250 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700251 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800252 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700253 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700254
Sunny Goyalffe83f12014-08-14 17:39:34 -0700255 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700256 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700257
Adam Cohen091440a2015-03-18 14:16:05 -0700258 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800259 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800260 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700261
Michael Jurka0280c3b2010-09-17 15:00:07 -0700262 private int[] mTmpAddItemCellCoordinates = new int[2];
263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private FolderInfo mFolderInfo;
265
Winson Chung3d503fb2011-07-13 17:25:49 -0700266 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800267 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700268
Michael Jurka838a4ca2011-02-07 13:33:06 -0800269 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700270
Winson Chungc51db6a2011-10-05 11:44:49 -0700271 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700272
273 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700274 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700275
276 // Main container view for the widget tray screen.
277 private WidgetsContainerView mWidgetsView;
278
Winson Chungf0ea4d32011-06-06 14:27:16 -0700279 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800280 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700283 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
284 // scroll issues (because the workspace may not have been measured yet) and extra work.
285 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
286 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287
288 private SpannableStringBuilder mDefaultKeySsb = null;
289
Adam Cohen091440a2015-03-18 14:16:05 -0700290 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800292 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 private boolean mRestoring;
294 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700295 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296
Michael Jurka1e2f4652013-07-08 18:03:46 -0700297 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700298 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 private Bundle mSavedInstanceState;
301
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800303 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700304 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800305 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700306 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800307 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400308
Adam Cohen091440a2015-03-18 14:16:05 -0700309 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700310
Sunny Goyale2df0622015-04-24 11:27:00 -0700311 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700312
Adam Cohen61f560d2013-09-30 15:58:20 -0700313 private View.OnTouchListener mHapticFeedbackTouchListener;
314
Adam Cohended9f8d2010-11-03 13:25:16 -0700315 // Related to the auto-advancing of widgets
316 private final int ADVANCE_MSG = 1;
317 private final int mAdvanceInterval = 20000;
318 private final int mAdvanceStagger = 250;
319 private long mAutoAdvanceSentTime;
320 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700321 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700322 new HashMap<View, AppWidgetProviderInfo>();
323
Winson Chung400438b2011-01-16 17:53:48 -0800324 // Determines how long to wait after a rotation before restoring the screen orientation to
325 // match the sensor state.
326 private final int mRestoreScreenOrientationDelay = 500;
327
Adam Cohen091440a2015-03-18 14:16:05 -0700328 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700329
Adam Cohen1462de32012-07-24 22:34:36 -0700330 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700331 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700332
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700334 static Date sDateStamp = new Date();
335 static DateFormat sDateFormat =
336 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
337 static long sRunStart = System.currentTimeMillis();
338 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700339
Winson Chung46353de2012-02-16 14:05:10 -0800340 // We only want to get the SharedPreferences once since it does an FS stat each time we get
341 // it from the context.
342 private SharedPreferences mSharedPrefs;
343
Winson Chungf0c6ae02012-03-21 16:10:31 -0700344 // Holds the page that we need to animate to, and the icon views that we need to animate up
345 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700346 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700347 private Bitmap mFolderIconBitmap;
348 private Canvas mFolderIconCanvas;
349 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700350
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700351 private DeviceProfile mDeviceProfile;
352
Michael Jurkaddd62e92011-02-16 17:49:14 -0800353 private BubbleTextView mWaitingForResume;
354
Adam Cohen59400422014-03-05 18:07:04 -0800355 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
356 new HashMap<String, CustomAppWidget>();
357
358 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
359 static {
360 if (ENABLE_CUSTOM_WIDGET_TEST) {
361 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
362 }
363 }
364
Chet Haasea8f996d2015-02-17 12:56:04 -0800365 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
366 private static Method sClipRevealMethod = null;
367 static {
368 Class<?> activityOptionsClass = ActivityOptions.class;
369 try {
370 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
371 View.class, int.class, int.class, int.class, int.class);
372 } catch (Exception e) {
373 // Earlier version
374 }
375 }
376
Adam Cohen091440a2015-03-18 14:16:05 -0700377 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700378 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700379 if (mWorkspace != null) {
380 mWorkspace.buildPageHardwareLayers();
381 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700382 }
383 };
384
Adam Cohendb364c32014-05-20 14:23:40 -0700385 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800386
Adam Cohen091440a2015-03-18 14:16:05 -0700387 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800388 int requestCode;
389 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700390 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700391 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392 int cellX;
393 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700394 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800395 }
396
Daniel Sandlerff02d492013-08-05 02:12:05 -0400397 private Stats mStats;
398
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700399 FocusIndicatorView mFocusHandler;
400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 @Override
402 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700403 if (DEBUG_STRICT_MODE) {
404 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
405 .detectDiskReads()
406 .detectDiskWrites()
407 .detectNetwork() // or .detectAll() for all detectable problems
408 .penaltyLog()
409 .build());
410 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
411 .detectLeakedSqlLiteObjects()
412 .detectLeakedClosableObjects()
413 .penaltyLog()
414 .penaltyDeath()
415 .build());
416 }
417
Adam Cohen9211d422014-10-07 18:14:53 -0700418 if (mLauncherCallbacks != null) {
419 mLauncherCallbacks.preOnCreate();
420 }
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400423
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400424 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400425 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700426 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700427
Winson Chung5f8afe62013-08-12 16:19:28 -0700428 // Lazy-initialize the dynamic grid
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700429 mDeviceProfile = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700430
431 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400432 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700433 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700434 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800435 mModel = app.setLauncher(this);
436 mIconCache = app.getIconCache();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700437 mIconCache.flushInvalidIcons(mDeviceProfile);
Joe Onorato41a12d22009-10-31 18:30:00 -0400438 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800440 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Daniel Sandlerff02d492013-08-05 02:12:05 -0400442 mStats = new Stats(this);
443
Sunny Goyalffe83f12014-08-14 17:39:34 -0700444 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700445
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
447 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700448
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700449 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
450 // this also ensures that any synchronous binding below doesn't re-trigger another
451 // LauncherModel load.
452 mPaused = false;
453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200455 android.os.Debug.startMethodTracing(
456 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458
459 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700463 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800465 registerContentObservers();
466
Joe Onorato7c312c12009-08-13 21:36:53 -0700467 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700468
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 mSavedState = savedInstanceState;
470 restoreState(mSavedState);
471
472 if (PROFILE_STARTUP) {
473 android.os.Debug.stopMethodTracing();
474 }
475
476 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700477 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 // If the user leaves launcher, then we should just load items asynchronously when
479 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500480 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700481 } else {
482 // We only load the page synchronously if the user rotates (or triggers a
483 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800484 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 }
487
488 // For handling default keys
489 mDefaultKeySsb = new SpannableStringBuilder();
490 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800491
492 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
493 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800494
Adam Cohenf9426d52012-06-04 17:26:21 -0700495 // On large interfaces, we want the screen to auto-rotate based on the current orientation
496 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700497
Adam Cohen9211d422014-10-07 18:14:53 -0700498 if (mLauncherCallbacks != null) {
499 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700500 if (mLauncherCallbacks.hasLauncherOverlay()) {
501 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700502 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
503 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
504 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700505 mWorkspace.setLauncherOverlay(mLauncherOverlay);
506 }
Adam Cohen9211d422014-10-07 18:14:53 -0700507 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700508
509 if (shouldShowIntroScreen()) {
510 showIntroScreen();
511 } else {
512 showFirstRunActivity();
513 showFirstRunClings();
514 }
Adam Cohen9211d422014-10-07 18:14:53 -0700515 }
516
517 private LauncherCallbacks mLauncherCallbacks;
518
519 public void onPostCreate(Bundle savedInstanceState) {
520 super.onPostCreate(savedInstanceState);
521 if (mLauncherCallbacks != null) {
522 mLauncherCallbacks.onPostCreate(savedInstanceState);
523 }
524 }
525
526 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
527 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700528 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
529 @Override
530 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700531 if (LOGD) {
532 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
533 }
Winson Chung94804152015-05-08 13:06:44 -0700534 mAppsView.setFixedBounds(bounds);
535 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700536 }
537
538 @Override
539 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700540 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
541 // Dismiss All Apps if we aren't already paused/invisible
542 if (!mPaused) {
543 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
544 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
545 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700546 }
Winson Chung0f785722015-04-08 10:27:49 -0700547 }
548 });
Jun Mukai8af0cd82015-05-12 12:32:12 -0700549 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
550 private boolean mImportanceStored = false;
551 private int mWorkspaceImportanceForAccessibility =
552 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
553 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
554
555 @Override
556 public void onSearchOverlayOpened() {
557 if (mImportanceStored) {
558 return;
559 }
560 // The underlying workspace and hotseat are temporarily suppressed by the search
561 // overlay. So they sholudn't be accessible.
562 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
563 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
564 mWorkspace.setImportantForAccessibility(
565 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
566 mHotseat.setImportantForAccessibility(
567 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
568 mImportanceStored = true;
569 }
570
571 @Override
572 public void onSearchOverlayClosed() {
573 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
574 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
575 mImportanceStored = false;
576 }
577 });
Adam Cohen9211d422014-10-07 18:14:53 -0700578 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700579 }
580
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700582 public void onLauncherProviderChange() {
583 if (mLauncherCallbacks != null) {
584 mLauncherCallbacks.onLauncherProviderChange();
585 }
586 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587
Adam Cohen9211d422014-10-07 18:14:53 -0700588 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700589 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700590 if (mLauncherCallbacks != null) {
591 return mLauncherCallbacks.hasCustomContentToLeft();
592 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700593 return false;
594 }
595
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500597 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 * {@link #addToCustomContentPage}. This will only be invoked if
599 * {@link #hasCustomContentToLeft()} is {@code true}.
600 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 mLauncherCallbacks.populateCustomContentContainer();
604 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 }
606
607 /**
608 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
609 * ensure the custom content page is added or removed if necessary.
610 */
611 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600612 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 // Not bound yet, wait for bindScreens to be called.
614 return;
615 }
616
617 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
618 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500619 mWorkspace.createCustomContentContainer();
620 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
622 mWorkspace.removeCustomContentPage();
623 }
624 }
625
Adam Cohen091440a2015-03-18 14:16:05 -0700626 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700627 if (sLocaleConfiguration == null) {
628 new AsyncTask<Void, Void, LocaleConfiguration>() {
629 @Override
630 protected LocaleConfiguration doInBackground(Void... unused) {
631 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
632 readConfiguration(Launcher.this, localeConfiguration);
633 return localeConfiguration;
634 }
635
636 @Override
637 protected void onPostExecute(LocaleConfiguration result) {
638 sLocaleConfiguration = result;
639 checkForLocaleChange(); // recursive, but now with a locale configuration
640 }
641 }.execute();
642 return;
643 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 final Configuration configuration = getResources().getConfiguration();
646
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700647 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 final String locale = configuration.locale.toString();
649
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700650 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 final int mcc = configuration.mcc;
652
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700653 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 final int mnc = configuration.mnc;
655
Romain Guy84f296c2009-11-04 15:00:44 -0800656 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657
Romain Guy84f296c2009-11-04 15:00:44 -0800658 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700659 sLocaleConfiguration.locale = locale;
660 sLocaleConfiguration.mcc = mcc;
661 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700662
Joe Onorato0589f0f2010-02-08 13:44:00 -0800663 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700664
665 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100666 new AsyncTask<Void, Void, Void>() {
667 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700668 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100669 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700670 }
Michael Jurka43467462013-11-14 12:03:58 +0100671 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700672 }
673 }
674
Adam Cohen091440a2015-03-18 14:16:05 -0700675 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700676 public String locale;
677 public int mcc = -1;
678 public int mnc = -1;
679 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700680
Adam Cohen091440a2015-03-18 14:16:05 -0700681 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700682 DataInputStream in = null;
683 try {
Helena Josol28db2802014-10-09 17:04:09 +0100684 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700685 configuration.locale = in.readUTF();
686 configuration.mcc = in.readInt();
687 configuration.mnc = in.readInt();
688 } catch (FileNotFoundException e) {
689 // Ignore
690 } catch (IOException e) {
691 // Ignore
692 } finally {
693 if (in != null) {
694 try {
695 in.close();
696 } catch (IOException e) {
697 // Ignore
698 }
699 }
700 }
701 }
702
Adam Cohen091440a2015-03-18 14:16:05 -0700703 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700704 DataOutputStream out = null;
705 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100706 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100707 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700708 out.writeUTF(configuration.locale);
709 out.writeInt(configuration.mcc);
710 out.writeInt(configuration.mnc);
711 out.flush();
712 } catch (FileNotFoundException e) {
713 // Ignore
714 } catch (IOException e) {
715 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100716 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700717 } finally {
718 if (out != null) {
719 try {
720 out.close();
721 } catch (IOException e) {
722 // Ignore
723 }
724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
726 }
727
Anton Hanssona2f665f2013-09-26 12:18:32 +0100728 public Stats getStats() {
729 return mStats;
730 }
731
Bjorn Bringertc459e522013-06-07 19:36:01 +0100732 public LayoutInflater getInflater() {
733 return mInflater;
734 }
735
Hyunyoung Song3f471442015-04-08 19:01:34 -0700736 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700737 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
738 // that is subsequently removed from the workspace in startBinding().
739 return !mModel.isLoadingWorkspace();
740 }
741
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800742 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000743 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100744 if (Build.VERSION.SDK_INT >= 17) {
745 return View.generateViewId();
746 } else {
747 // View.generateViewId() is not available. The following fallback logic is a copy
748 // of its implementation.
749 for (;;) {
750 final int result = sNextGeneratedId.get();
751 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
752 int newValue = result + 1;
753 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
754 if (sNextGeneratedId.compareAndSet(result, newValue)) {
755 return result;
756 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000757 }
758 }
759 }
760
761 public int getViewIdForItem(ItemInfo info) {
762 // This cast is safe given the > 2B range for int.
763 int itemId = (int) info.id;
764 if (mItemIdToViewId.containsKey(itemId)) {
765 return mItemIdToViewId.get(itemId);
766 }
767 int viewId = generateViewId();
768 mItemIdToViewId.put(itemId, viewId);
769 return viewId;
770 }
771
772 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700773 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
774 * a configuration step, this allows the proper animations to run after other transitions.
775 */
Adam Cohendb364c32014-05-20 14:23:40 -0700776 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700777 long screenId = args.screenId;
778 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
779 // When the screen id represents an actual screen (as opposed to a rank) we make sure
780 // that the drop page actually exists.
781 screenId = ensurePendingDropLayoutExists(args.screenId);
782 }
Adam Cohendb364c32014-05-20 14:23:40 -0700783
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800784 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800785 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700786 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700788 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800789 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700790 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700791 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700792 case REQUEST_RECONFIGURE_APPWIDGET:
793 completeRestoreAppWidget(args.appWidgetId);
794 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800795 }
Michael Jurka27614d22012-04-02 06:40:22 -0700796 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
797 // if you turned the screen off and then back while in All Apps, Launcher would not
798 // return to the workspace. Clearing mAddInfo.container here fixes this issue
799 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700800 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800801 }
802
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800803 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700804 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700805 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700806 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700807 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800808 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700809
Adam Cohenad4e15c2013-10-17 16:21:35 -0700810 Runnable exitSpringLoaded = new Runnable() {
811 @Override
812 public void run() {
813 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
814 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
815 }
816 };
817
Michael Jurka8b805b12012-04-18 14:23:14 -0700818 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700819 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700820 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
821 if (resultCode == RESULT_CANCELED) {
822 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700823 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700824 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700825 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800826 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700827 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700828 }
829 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200830 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
831 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700832 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200833 }
834 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700835 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200836
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
838 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700839
Adam Cohendb364c32014-05-20 14:23:40 -0700840 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800841 // We have special handling for widgets
842 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800843 final int appWidgetId;
844 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
845 : -1;
846 if (widgetId < 0) {
847 appWidgetId = pendingAddWidgetId;
848 } else {
849 appWidgetId = widgetId;
850 }
851
Adam Cohenad4e15c2013-10-17 16:21:35 -0700852 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800853 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700854 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
855 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700856 result = RESULT_CANCELED;
857 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700858 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700859 @Override
860 public void run() {
861 exitSpringLoadedDragModeDelayed(false, 0, null);
862 }
863 };
Adam Cohendb364c32014-05-20 14:23:40 -0700864 if (workspaceLocked) {
865 // No need to remove the empty screen if we're mid-binding, as the
866 // the bind will not add the empty screen.
867 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
868 } else {
869 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
870 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
871 }
Winson Chung5aaab772012-04-27 15:24:02 -0700872 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700873 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700874 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
875 // When the screen id represents an actual screen (as opposed to a rank)
876 // we make sure that the drop page actually exists.
877 mPendingAddInfo.screenId =
878 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
879 }
Adam Cohendb364c32014-05-20 14:23:40 -0700880 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
881
882 dropLayout.setDropPending(true);
883 final Runnable onComplete = new Runnable() {
884 @Override
885 public void run() {
886 completeTwoStageWidgetDrop(resultCode, appWidgetId);
887 dropLayout.setDropPending(false);
888 }
889 };
890 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
891 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
892 } else {
893 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
894 mPendingAddInfo);
895 sPendingAddItem = args;
896 }
Winson Chung5aaab772012-04-27 15:24:02 -0700897 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800898 return;
899 }
900
Sunny Goyalff572272014-07-23 13:58:07 -0700901 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
902 if (resultCode == RESULT_OK) {
903 // Update the widget view.
904 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
905 pendingAddWidgetId, mPendingAddInfo);
906 if (workspaceLocked) {
907 sPendingAddItem = args;
908 } else {
909 completeAdd(args);
910 }
911 }
912 // Leave the widget in the pending state if the user canceled the configure.
913 return;
914 }
915
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 // The pattern used here is that a user PICKs a specific application,
917 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700918
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
920 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700921 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700922 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
923 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800924 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700925 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800926 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700927 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700928 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
929 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 }
Adam Cohen00074722013-10-11 17:46:09 -0700931 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700932 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700933 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800935 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800936
937 }
938
939 @Override
940 protected void onActivityResult(
941 final int requestCode, final int resultCode, final Intent data) {
942 handleActivityResult(requestCode, resultCode, data);
943 if (mLauncherCallbacks != null) {
944 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
945 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800946 }
947
Adam Cohendb364c32014-05-20 14:23:40 -0700948 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
949 appWidgetId, ItemInfo info) {
950 PendingAddArguments args = new PendingAddArguments();
951 args.requestCode = requestCode;
952 args.intent = data;
953 args.container = info.container;
954 args.screenId = info.screenId;
955 args.cellX = info.cellX;
956 args.cellY = info.cellY;
957 args.appWidgetId = appWidgetId;
958 return args;
959 }
960
961 /**
962 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
963 *
964 * @param screenId the screen id to check
965 * @return the new screen, or screenId if it exists
966 */
967 private long ensurePendingDropLayoutExists(long screenId) {
968 CellLayout dropLayout =
969 (CellLayout) mWorkspace.getScreenWithId(screenId);
970 if (dropLayout == null) {
971 // it's possible that the add screen was removed because it was
972 // empty and a re-bind occurred
973 mWorkspace.addExtraEmptyScreen();
974 return mWorkspace.commitExtraEmptyScreen();
975 } else {
976 return screenId;
977 }
978 }
979
Adam Cohen091440a2015-03-18 14:16:05 -0700980 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700981 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700982 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800983 Runnable onCompleteRunnable = null;
984 int animationType = 0;
985
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700986 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800987 if (resultCode == RESULT_OK) {
988 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
989 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700990 mPendingAddWidgetInfo);
991 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800992 onCompleteRunnable = new Runnable() {
993 @Override
994 public void run() {
995 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700996 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700997 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
998 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800999 }
1000 };
1001 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -08001002 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001003 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -08001004 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001005 if (mDragLayer.getAnimatedView() != null) {
1006 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
1007 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
1008 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001009 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001010 // The animated view may be null in the case of a rotation during widget configuration
1011 onCompleteRunnable.run();
1012 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
1014
1015 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -07001016 protected void onStop() {
1017 super.onStop();
1018 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -07001019
1020 if (mLauncherCallbacks != null) {
1021 mLauncherCallbacks.onStop();
1022 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001023 }
1024
1025 @Override
1026 protected void onStart() {
1027 super.onStart();
1028 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -07001029
1030 if (mLauncherCallbacks != null) {
1031 mLauncherCallbacks.onStart();
1032 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001033 }
1034
1035 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001037 long startTime = 0;
1038 if (DEBUG_RESUME_TIME) {
1039 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001040 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001041 }
Adam Cohen9211d422014-10-07 18:14:53 -07001042
1043 if (mLauncherCallbacks != null) {
1044 mLauncherCallbacks.preOnResume();
1045 }
1046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001048
Winson Chung4a2afa32012-07-19 14:53:05 -07001049 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001050 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001051 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001052 } else if (mOnResumeState == State.APPS) {
Winson Chung4ac30062015-05-08 17:34:17 -07001053 // Don't update the predicted apps if the user is returning to launcher in the apps
1054 // view as they may be depending on the UI to be static to switch to another app
1055 showAppsView(false /* animated */, false /* resetListToTop */,
1056 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001057 } else if (mOnResumeState == State.WIDGETS) {
1058 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001059 }
1060 mOnResumeState = State.NONE;
1061
Winson Chungcd99cd32015-04-29 11:03:24 -07001062 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001063 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1064 // Otherwise, notify the callbacks if we are in all apps mode
1065 if (mState == State.APPS) {
1066 if (mLauncherCallbacks != null) {
1067 mLauncherCallbacks.onAllAppsShown();
1068 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001069 }
1070 }
1071
Craig Mautner360310b2012-10-26 15:13:08 -07001072 // Background was set to gradient in onPause(), restore to black if in all apps.
1073 setWorkspaceBackground(mState == State.WORKSPACE);
1074
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001075 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001076 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001077 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001078 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001079 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001080 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001082 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001083 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1084 // execute them here
1085 long startTimeCallbacks = 0;
1086 if (DEBUG_RESUME_TIME) {
1087 startTimeCallbacks = System.currentTimeMillis();
1088 }
1089
Michael Jurka1e2f4652013-07-08 18:03:46 -07001090 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1091 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001092 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001093 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001094 if (DEBUG_RESUME_TIME) {
1095 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1096 (System.currentTimeMillis() - startTimeCallbacks));
1097 }
Michael Jurka447bf842013-05-15 14:52:15 +02001098 }
Michael Jurka54554252013-08-01 12:52:23 +02001099 if (mOnResumeCallbacks.size() > 0) {
1100 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1101 mOnResumeCallbacks.get(i).run();
1102 }
1103 mOnResumeCallbacks.clear();
1104 }
Winson Chunge4e50662012-01-23 14:45:13 -08001105
1106 // Reset the pressed state of icons that were locked in the press state while activities
1107 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001108 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001109 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001110 mWaitingForResume.setStayPressed(false);
1111 }
Winson Chung82963d52013-10-09 11:20:57 -07001112
Adam Cohen06dff352012-06-01 17:17:08 -07001113 // It is possible that widgets can receive updates while launcher is not in the foreground.
1114 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1115 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1116 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001117 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001118 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001119
Michael Jurka447bf842013-05-15 14:52:15 +02001120 if (DEBUG_RESUME_TIME) {
1121 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1122 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001123
1124 if (mWorkspace.getCustomContentCallbacks() != null) {
1125 // If we are resuming and the custom content is the current page, we call onShow().
1126 // It is also poassible that onShow will instead be called slightly after first layout
1127 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1128 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001129 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001130 }
1131 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001132 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001133 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001134
Sunny Goyal756adbc2015-04-16 15:20:51 -07001135 if (!isWorkspaceLoading()) {
1136 // Process any items that were added while Launcher was away.
1137 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1138 }
Adam Cohen9211d422014-10-07 18:14:53 -07001139
1140 if (mLauncherCallbacks != null) {
1141 mLauncherCallbacks.onResume();
1142 }
Adam Cohen06dff352012-06-01 17:17:08 -07001143 }
1144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001146 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001147 // Ensure that items added to Launcher are queued until Launcher returns
1148 InstallShortcutReceiver.enableInstallQueue();
1149
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001150 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001151 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001152 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001153 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001154
1155 // We call onHide() aggressively. The custom content callbacks should be able to
1156 // debounce excess onHide calls.
1157 if (mWorkspace.getCustomContentCallbacks() != null) {
1158 mWorkspace.getCustomContentCallbacks().onHide();
1159 }
Romain Guycbb89e42009-06-08 15:52:54 -07001160
Adam Cohen9211d422014-10-07 18:14:53 -07001161 if (mLauncherCallbacks != null) {
1162 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001163 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001164 }
1165
1166 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001167 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1168 // by a onResume or by scrolling otherwise.
1169 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001170
1171 // Custom content is completely hidden
1172 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001173
1174 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1175 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001176
1177 // Indicates whether the user is allowed to scroll away from the custom content.
1178 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001179 }
1180
Adam Cohenc2d6e892014-10-16 09:49:24 -07001181 public interface LauncherOverlay {
1182
1183 /**
1184 * Touch interaction leading to overscroll has begun
1185 */
1186 public void onScrollInteractionBegin();
1187
1188 /**
1189 * Touch interaction related to overscroll has ended
1190 */
1191 public void onScrollInteractionEnd();
1192
1193 /**
1194 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1195 * screen (or in the case of RTL, the rightmost screen).
1196 */
1197 public void onScrollChange(int progress, boolean rtl);
1198
1199 /**
1200 * Screen has stopped scrolling
1201 */
1202 public void onScrollSettled();
1203
1204 /**
1205 * This method can be called by the Launcher in order to force the LauncherOverlay
1206 * to exit fully immersive mode.
1207 */
1208 public void forceExitFullImmersion();
1209 }
1210
Winson Chung0f785722015-04-08 10:27:49 -07001211 public interface LauncherAppsCallbacks {
1212 /**
1213 * Updates launcher to the available space that AllApps can take so as not to overlap with
1214 * any other views.
1215 */
1216 public void onAllAppsBoundsChanged(Rect bounds);
1217
1218 /**
1219 * Called to dismiss all apps if it is showing.
1220 */
1221 public void dismissAllApps();
1222 }
1223
Jun Mukai8af0cd82015-05-12 12:32:12 -07001224 public interface LauncherSearchCallbacks {
1225 /**
1226 * Called when the search overlay is shown.
1227 */
1228 public void onSearchOverlayOpened();
1229
1230 /**
1231 * Called when the search overlay is dismissed.
1232 */
1233 public void onSearchOverlayClosed();
1234 }
1235
Adam Cohenc2d6e892014-10-16 09:49:24 -07001236 public interface LauncherOverlayCallbacks {
1237 /**
1238 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1239 * however it doesn't modify any state within the launcher.
1240 */
1241 public boolean canEnterFullImmersion();
1242
1243 /**
1244 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1245 * eg. by occupying the full screen and handling all touch events.
1246 *
1247 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1248 * case, Launcher will modify any necessary state and assumes the overlay is
1249 * handling all interaction. If false, the LauncherOverlay should cancel any
1250 *
1251 */
1252 public boolean enterFullImmersion();
1253
1254 /**
1255 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1256 * full control over UI and state.
1257 */
1258 public void exitFullImmersion();
1259 }
1260
1261 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1262
1263 @Override
1264 public boolean canEnterFullImmersion() {
1265 return mState == State.WORKSPACE;
1266 }
1267
1268 @Override
1269 public boolean enterFullImmersion() {
1270 if (mState == State.WORKSPACE) {
1271 // When fully immersed, disregard any touches which fall through.
1272 mDragLayer.setBlockTouch(true);
1273 return true;
1274 }
1275 return false;
1276 }
1277
1278 @Override
1279 public void exitFullImmersion() {
1280 mDragLayer.setBlockTouch(false);
1281 }
1282 }
1283
Jorim Jaggid017f882014-01-14 17:08:48 -08001284 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001285 if (mLauncherCallbacks != null) {
1286 return mLauncherCallbacks.hasSettings();
1287 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001288 return false;
1289 }
1290
Adam Cohen9211d422014-10-07 18:14:53 -07001291 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001292 CustomContentCallbacks callbacks, String description) {
1293 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001294 }
1295
Adam Cohenedb40762013-07-18 16:45:45 -07001296 // The custom content needs to offset its content to account for the QSB
1297 public int getTopOffsetForCustomContent() {
1298 return mWorkspace.getPaddingTop();
1299 }
1300
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001301 @Override
1302 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001303 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001304 if (mModel.isCurrentCallbacks(this)) {
1305 mModel.stopLoader();
1306 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001307 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1308
Romain Guy13c2e7b2010-03-10 19:45:00 -08001309 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001310 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001311
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001312 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001313 @Override
1314 public void onWindowFocusChanged(boolean hasFocus) {
1315 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001316 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001317
1318 if (mLauncherCallbacks != null) {
1319 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1320 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001321 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001322
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 private boolean acceptFilter() {
1324 final InputMethodManager inputManager = (InputMethodManager)
1325 getSystemService(Context.INPUT_METHOD_SERVICE);
1326 return !inputManager.isFullscreenMode();
1327 }
1328
1329 @Override
1330 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001331 final int uniChar = event.getUnicodeChar();
1332 final boolean handled = super.onKeyDown(keyCode, event);
1333 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1334 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1336 keyCode, event);
1337 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001338 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001339 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001340 // showSearchDialog()
1341 // If there are multiple keystrokes before the search dialog takes focus,
1342 // onSearchRequested() will be called for every keystroke,
1343 // but it is idempotent, so it's fine.
1344 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346 }
1347
Joe Onorato8a9625e2010-01-28 15:55:35 -08001348 // Eat the long press event so the keyboard doesn't come up.
1349 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1350 return true;
1351 }
1352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 return handled;
1354 }
1355
Karl Rosaen138a0412009-04-23 19:00:21 -07001356 private String getTypedText() {
1357 return mDefaultKeySsb.toString();
1358 }
1359
1360 private void clearTypedText() {
1361 mDefaultKeySsb.clear();
1362 mDefaultKeySsb.clearSpans();
1363 Selection.setSelection(mDefaultKeySsb, 0);
1364 }
1365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001367 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1368 * State
1369 */
1370 private static State intToState(int stateOrdinal) {
1371 State state = State.WORKSPACE;
1372 final State[] stateValues = State.values();
1373 for (int i = 0; i < stateValues.length; i++) {
1374 if (stateValues[i].ordinal() == stateOrdinal) {
1375 state = stateValues[i];
1376 break;
1377 }
1378 }
1379 return state;
1380 }
1381
1382 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 * Restores the previous state, if it exists.
1384 *
1385 * @param savedState The previous state.
1386 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001387 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 private void restoreState(Bundle savedState) {
1389 if (savedState == null) {
1390 return;
1391 }
1392
Michael Jurka883f55b2010-10-21 15:47:14 -07001393 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001394 if (state == State.APPS || state == State.WIDGETS) {
1395 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001396 }
1397
Adam Cohen21cd0022013-10-09 18:57:02 -07001398 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1399 PagedView.INVALID_RESTORE_PAGE);
1400 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001401 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
1403
Winson Chung3d503fb2011-07-13 17:25:49 -07001404 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001405 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001406
Winson Chung3d503fb2011-07-13 17:25:49 -07001407 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1408 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001409 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001410 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1411 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001412 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1413 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001414 AppWidgetProviderInfo info = savedState.getParcelable(
1415 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1416 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001417 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001418 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 mRestoring = true;
1420 }
1421
1422 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1423 if (renameFolder) {
1424 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001425 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 mRestoring = true;
1427 }
Winson Chunga12a2502010-12-20 14:41:35 -08001428
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001429 mItemIdToViewId = (HashMap<Integer, Integer>)
1430 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 }
1432
1433 /**
1434 * Finds all the views we need and configure them properly.
1435 */
1436 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001437 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001438
Craig Mautner360310b2012-10-26 15:13:08 -07001439 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001440 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001441 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1442 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001443 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001444 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001445
John Spurlock77e1f472013-09-11 10:09:51 -04001446 mLauncherView.setSystemUiVisibility(
1447 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001448 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449
Winson Chung4c98d922011-05-31 16:50:48 -07001450 // Setup the drag layer
1451 mDragLayer.setup(this, dragController);
1452
Winson Chung3d503fb2011-07-13 17:25:49 -07001453 // Setup the hotseat
1454 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1455 if (mHotseat != null) {
1456 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001457 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001458 }
1459
Jorim Jaggid017f882014-01-14 17:08:48 -08001460 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001461 View widgetButton = findViewById(R.id.widget_button);
1462 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001463 @Override
1464 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001465 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001466 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001467 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001468 }
1469 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001470 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1471
1472 View wallpaperButton = findViewById(R.id.wallpaper_button);
1473 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001474 @Override
1475 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001476 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001477 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001478 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001479 }
1480 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001481 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1482
1483 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001484 if (hasSettings()) {
1485 settingsButton.setOnClickListener(new OnClickListener() {
1486 @Override
1487 public void onClick(View arg0) {
1488 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001489 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001490 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001491 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001492 });
1493 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1494 } else {
1495 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001496 }
1497
Adam Cohendbdff6b2013-09-18 19:09:15 -07001498 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001499
Winson Chung4c98d922011-05-31 16:50:48 -07001500 // Setup the workspace
1501 mWorkspace.setHapticFeedbackEnabled(false);
1502 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001503 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001504 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001505
Winson Chungf0ea4d32011-06-06 14:27:16 -07001506 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001507 mSearchDropTargetBar = (SearchDropTargetBar)
1508 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001509
Winson Chungb745afb2015-03-02 11:51:23 -08001510 // Setup Apps
1511 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001512 if (isAllAppsSearchOverridden()) {
1513 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001514 }
Winson Chungb745afb2015-03-02 11:51:23 -08001515
Winson Chungf0ea4d32011-06-06 14:27:16 -07001516 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001517 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001518
Winson Chung3d503fb2011-07-13 17:25:49 -07001519 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001520 dragController.setDragScoller(mWorkspace);
1521 dragController.setScrollView(mDragLayer);
1522 dragController.setMoveTarget(mWorkspace);
1523 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001524 if (mSearchDropTargetBar != null) {
1525 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001526 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001527 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001528
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001529 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001530 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001531 mWeightWatcher = new WeightWatcher(this);
1532 mWeightWatcher.setAlpha(0.5f);
1533 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001534 new FrameLayout.LayoutParams(
1535 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001536 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001537 Gravity.BOTTOM)
1538 );
Adam Cohen39a06042013-07-19 14:30:12 -07001539
1540 boolean show = shouldShowWeightWatcher();
1541 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
1544
1545 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001546 * Sets the all apps button. This method is called from {@link Hotseat}.
1547 */
1548 public void setAllAppsButton(View allAppsButton) {
1549 mAllAppsButton = allAppsButton;
1550 }
1551
1552 public View getAllAppsButton() {
1553 return mAllAppsButton;
1554 }
1555
1556 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 * Creates a view representing a shortcut.
1558 *
1559 * @param info The data structure describing the shortcut.
1560 *
1561 * @return A View inflated from R.layout.application.
1562 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001563 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001564 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
1566
1567 /**
1568 * Creates a view representing a shortcut inflated from the specified resource.
1569 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 * @param parent The group the shortcut belongs to.
1571 * @param info The data structure describing the shortcut.
1572 *
1573 * @return A View inflated from layoutResId.
1574 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001575 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1576 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.application,
1577 parent, false);
1578 favorite.applyFromShortcutInfo(info, mIconCache);
1579 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001581 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 return favorite;
1583 }
1584
1585 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 * Add a shortcut to the workspace.
1587 *
1588 * @param data The intent describing the shortcut.
1589 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001591 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001592 int cellY) {
1593 int[] cellXY = mTmpAddItemCellCoordinates;
1594 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001595 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Michael Jurkad3ef3062010-11-23 16:23:58 -08001597 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001598
Sunny Goyal2350bc92014-10-14 16:42:54 -07001599 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001600 if (info == null) {
1601 return;
1602 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001603 final View view = createShortcut(info);
1604
Adam Cohenfbba09b2011-07-18 21:43:05 -07001605 // First we check if we already know the exact location where we want to add this item.
1606 if (cellX >= 0 && cellY >= 0) {
1607 cellXY[0] = cellX;
1608 cellXY[1] = cellY;
1609 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001610
1611 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001612 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001613 true, null,null)) {
1614 return;
1615 }
1616 DragObject dragObject = new DragObject();
1617 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001618 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1619 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001620 return;
1621 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001622 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001623 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001624 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001625 foundCellSpan = (result != null);
1626 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001627 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001628 }
1629
1630 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001631 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001632 return;
1633 }
1634
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001635 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636
1637 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001638 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001639 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
1641 }
1642
Adam Cohen2f093b62012-04-30 18:59:53 -07001643 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1644 int minHeight) {
1645 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001646 // We want to account for the extra amount of padding that we are adding to the widget
1647 // to ensure that it gets the full amount of space that it has requested
1648 int requiredWidth = minWidth + padding.left + padding.right;
1649 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001650 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001651 }
1652
Adam Cohen2f093b62012-04-30 18:59:53 -07001653 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1654 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001655 }
1656
Adam Cohen2f093b62012-04-30 18:59:53 -07001657 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1658 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001659 }
1660
Adam Cohen2f093b62012-04-30 18:59:53 -07001661 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1662 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001663 }
1664
Adam Cohen2f093b62012-04-30 18:59:53 -07001665 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1666 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001667 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001668 }
1669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001671 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001673 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 */
Adam Cohen091440a2015-03-18 14:16:05 -07001675 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001676 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1677
1678 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001679 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001680 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1681 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001682 }
Romain Guycbb89e42009-06-08 15:52:54 -07001683
Adam Cohen59400422014-03-05 18:07:04 -08001684 if (appWidgetInfo.isCustomWidget) {
1685 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001686 }
1687
Adam Cohen59400422014-03-05 18:07:04 -08001688 LauncherAppWidgetInfo launcherInfo;
1689 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1690 launcherInfo.spanX = info.spanX;
1691 launcherInfo.spanY = info.spanY;
1692 launcherInfo.minSpanX = info.minSpanX;
1693 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001694 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001697 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698
1699 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001700 if (hostView == null) {
1701 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001702 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1703 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001704 } else {
1705 // The AppWidgetHostView has already been inflated and instantiated
1706 launcherInfo.hostView = hostView;
1707 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001709 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001710 launcherInfo.notifyWidgetSizeChanged(this);
1711
Adam Cohen59400422014-03-05 18:07:04 -08001712 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1713 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001714
1715 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001717 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 }
Romain Guycbb89e42009-06-08 15:52:54 -07001719
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1721 @Override
1722 public void onReceive(Context context, Intent intent) {
1723 final String action = intent.getAction();
1724 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1725 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001726 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001727 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001728
Winson Chungc100e8e2011-08-09 16:02:43 -07001729 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001730 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001731 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001732 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001733 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001734 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1736 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001737 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001738 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1739 mModel.resetLoadedState(false, true);
1740 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1741 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1742 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1743 mModel.resetLoadedState(false, true);
1744 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1745 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1746 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001747 }
1748 }
1749 };
1750
1751 @Override
1752 public void onAttachedToWindow() {
1753 super.onAttachedToWindow();
1754
1755 // Listen for broadcasts related to user-presence
1756 final IntentFilter filter = new IntentFilter();
1757 filter.addAction(Intent.ACTION_SCREEN_OFF);
1758 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001759 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001760 if (ENABLE_DEBUG_INTENTS) {
1761 filter.addAction(DebugIntents.DELETE_DATABASE);
1762 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1763 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001764 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001765 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001766 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001767 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 mVisible = true;
1769 }
1770
Adam Cohen0b395352014-06-09 22:54:36 +00001771 /**
1772 * Sets up transparent navigation and status bars in LMP.
1773 * This method is a no-op for other platform versions.
1774 */
Sunny Goyald0091012015-01-20 15:55:34 -08001775 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001776 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001777 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001778 Window window = getWindow();
1779 window.getAttributes().systemUiVisibility |=
1780 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1781 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1782 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1783 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1784 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1785 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1786 window.setStatusBarColor(Color.TRANSPARENT);
1787 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001788 }
1789 }
1790
Adam Cohended9f8d2010-11-03 13:25:16 -07001791 @Override
1792 public void onDetachedFromWindow() {
1793 super.onDetachedFromWindow();
1794 mVisible = false;
1795
Adam Cohend113e0c2010-11-11 10:48:05 -08001796 if (mAttached) {
1797 unregisterReceiver(mReceiver);
1798 mAttached = false;
1799 }
Winson Chungb745afb2015-03-02 11:51:23 -08001800 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001801 }
1802
1803 public void onWindowVisibilityChanged(int visibility) {
1804 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001805 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001806 // The following code used to be in onResume, but it turns out onResume is called when
1807 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1808 // is a more appropriate event to handle
1809 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001810 if (!mWorkspaceLoading) {
1811 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001812 // We want to let Launcher draw itself at least once before we force it to build
1813 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001814 // apps is nice and speedy.
1815 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001816 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001817 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001818 if (mStarted) return;
1819 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001820 // We delay the layer building a bit in order to give
1821 // other message processing a time to run. In particular
1822 // this avoids a delay in hiding the IME if it was
1823 // currently shown, because doing that may involve
1824 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001825 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001826 final ViewTreeObserver.OnDrawListener listener = this;
1827 mWorkspace.post(new Runnable() {
1828 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001829 if (mWorkspace != null &&
1830 mWorkspace.getViewTreeObserver() != null) {
1831 mWorkspace.getViewTreeObserver().
1832 removeOnDrawListener(listener);
1833 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001834 }
1835 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001836 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001837 }
1838 });
1839 }
1840 clearTypedText();
1841 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001842 }
1843
Adam Cohen091440a2015-03-18 14:16:05 -07001844 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001845 mHandler.removeMessages(ADVANCE_MSG);
1846 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1847 mHandler.sendMessageDelayed(msg, delay);
1848 mAutoAdvanceSentTime = System.currentTimeMillis();
1849 }
1850
Adam Cohen091440a2015-03-18 14:16:05 -07001851 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001852 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1853 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1854 mAutoAdvanceRunning = autoAdvanceRunning;
1855 if (autoAdvanceRunning) {
1856 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1857 sendAdvanceMessage(delay);
1858 } else {
1859 if (!mWidgetsToAdvance.isEmpty()) {
1860 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1861 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1862 }
1863 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001864 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001865 }
1866 }
1867 }
1868
1869 private final Handler mHandler = new Handler() {
1870 @Override
1871 public void handleMessage(Message msg) {
1872 if (msg.what == ADVANCE_MSG) {
1873 int i = 0;
1874 for (View key: mWidgetsToAdvance.keySet()) {
1875 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1876 final int delay = mAdvanceStagger * i;
1877 if (v instanceof Advanceable) {
1878 postDelayed(new Runnable() {
1879 public void run() {
1880 ((Advanceable) v).advance();
1881 }
1882 }, delay);
1883 }
1884 i++;
1885 }
1886 sendAdvanceMessage(mAdvanceInterval);
1887 }
1888 }
1889 };
1890
1891 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001892 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001893 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1894 if (v instanceof Advanceable) {
1895 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001896 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001897 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001898 }
1899 }
1900
1901 void removeWidgetToAutoAdvance(View hostView) {
1902 if (mWidgetsToAdvance.containsKey(hostView)) {
1903 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001904 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001905 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001906 }
1907
Joe Onorato9c1289c2009-08-17 11:03:03 -04001908 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001909 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001910 launcherInfo.hostView = null;
1911 }
1912
Hyunyoung Song3f471442015-04-08 19:01:34 -07001913 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001914 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1915 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001916 }
1917
Winson Chunga6945242014-01-08 14:04:34 -08001918 public DragLayer getDragLayer() {
1919 return mDragLayer;
1920 }
1921
Winson Chungb745afb2015-03-02 11:51:23 -08001922 public AppsContainerView getAppsView() {
1923 return mAppsView;
1924 }
1925
Hyunyoung Song3f471442015-04-08 19:01:34 -07001926 public WidgetsContainerView getWidgetsView() {
1927 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001928 }
1929
Winson Chunga6945242014-01-08 14:04:34 -08001930 public Workspace getWorkspace() {
1931 return mWorkspace;
1932 }
1933
1934 public Hotseat getHotseat() {
1935 return mHotseat;
1936 }
1937
Jorim Jaggid017f882014-01-14 17:08:48 -08001938 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001939 return mOverviewPanel;
1940 }
1941
1942 public SearchDropTargetBar getSearchBar() {
1943 return mSearchDropTargetBar;
1944 }
1945
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001946 public LauncherAppWidgetHost getAppWidgetHost() {
1947 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
Romain Guycbb89e42009-06-08 15:52:54 -07001949
Winson Chunga9abd0e2010-10-27 17:18:37 -07001950 public LauncherModel getModel() {
1951 return mModel;
1952 }
1953
Winson Chunga6945242014-01-08 14:04:34 -08001954 protected SharedPreferences getSharedPrefs() {
1955 return mSharedPrefs;
1956 }
1957
Bjorn Bringertc459e522013-06-07 19:36:01 +01001958 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001959 getWindow().closeAllPanels();
1960
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001961 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001962 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001963 }
1964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 @Override
1966 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001967 long startTime = 0;
1968 if (DEBUG_RESUME_TIME) {
1969 startTime = System.currentTimeMillis();
1970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 super.onNewIntent(intent);
1972
1973 // Close the menu
1974 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001975 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001976 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001977
Adam Cohened307df2013-10-02 09:37:31 -07001978 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1979 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1980 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001981
Adam Cohen6fecd412013-10-02 17:41:50 -07001982 if (mWorkspace == null) {
1983 // Can be cases where mWorkspace is null, this prevents a NPE
1984 return;
1985 }
1986 Folder openFolder = mWorkspace.getOpenFolder();
1987 // In all these cases, only animate if we're already on home
1988 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001989
1990 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1991 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001992 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001993 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001994 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001995 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001996
Adam Cohen6fecd412013-10-02 17:41:50 -07001997 closeFolder();
1998 exitSpringLoadedDragMode();
1999
2000 // If we are already on home, then just animate back to the workspace,
2001 // otherwise, just wait until onResume to set the state back to Workspace
2002 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07002003 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07002004 } else {
2005 mOnResumeState = State.WORKSPACE;
2006 }
2007
2008 final View v = getWindow().peekDecorView();
2009 if (v != null && v.getWindowToken() != null) {
2010 InputMethodManager imm = (InputMethodManager)getSystemService(
2011 INPUT_METHOD_SERVICE);
2012 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
2013 }
2014
Winson Chungb745afb2015-03-02 11:51:23 -08002015 // Reset the apps view
2016 if (!alreadyOnHome && mAppsView != null) {
2017 mAppsView.scrollToTop();
2018 }
2019
Hyunyoung Song3f471442015-04-08 19:01:34 -07002020 // Reset the widgets view
2021 if (!alreadyOnHome && mWidgetsView != null) {
2022 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07002023 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002024
Adam Cohen9211d422014-10-07 18:14:53 -07002025 if (mLauncherCallbacks != null) {
2026 mLauncherCallbacks.onHomeIntent();
2027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 }
Adam Cohened307df2013-10-02 09:37:31 -07002029
Michael Jurka447bf842013-05-15 14:52:15 +02002030 if (DEBUG_RESUME_TIME) {
2031 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
2032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033
Adam Cohen9211d422014-10-07 18:14:53 -07002034 if (mLauncherCallbacks != null) {
2035 mLauncherCallbacks.onNewIntent(intent);
2036 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002037 }
2038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002040 public void onRestoreInstanceState(Bundle state) {
2041 super.onRestoreInstanceState(state);
2042 for (int page: mSynchronouslyBoundPages) {
2043 mWorkspace.restoreInstanceStateForChild(page);
2044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046
2047 @Override
2048 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002049 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002050 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2051 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002052 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002053 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054
Michael Jurka883f55b2010-10-21 15:47:14 -07002055 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002056 // We close any open folder since it will not be re-opened, and we need to make sure
2057 // this state is reflected.
2058 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059
Adam Cohendcd297f2013-06-18 13:13:40 -07002060 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002061 mWaitingForResult) {
2062 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002063 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002064 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2065 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002066 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2067 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2068 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002069 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
2071
2072 if (mFolderInfo != null && mWaitingForResult) {
2073 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2074 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2075 }
Michael Jurka946ad472010-07-09 18:05:18 -07002076
Hyunyoung Song3f471442015-04-08 19:01:34 -07002077 // Save the current widgets tray?
2078 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002079 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002080
2081 if (mLauncherCallbacks != null) {
2082 mLauncherCallbacks.onSaveInstanceState(outState);
2083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 }
2085
2086 @Override
2087 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002089
Winson Chunge7a03942011-08-05 15:05:12 -07002090 // Remove all pending runnables
2091 mHandler.removeMessages(ADVANCE_MSG);
2092 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002093 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002094
Winson Chungcd2b0142011-06-08 16:02:26 -07002095 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002096 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002097
2098 // It's possible to receive onDestroy after a new Launcher activity has
2099 // been created. In this case, don't interfere with the new Launcher.
2100 if (mModel.isCurrentCallbacks(this)) {
2101 mModel.stopLoader();
2102 app.setLauncher(null);
2103 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002106 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002108 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002110 mAppWidgetHost = null;
2111
2112 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113
2114 TextKeyListener.getInstance().release();
2115
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002116 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002117 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002118
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002119 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002120 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002121 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002122 mWorkspace = null;
2123 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002124
Michael Jurka2ecf9952012-06-18 12:52:28 -07002125 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002126
2127 if (mLauncherCallbacks != null) {
2128 mLauncherCallbacks.onDestroy();
2129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 }
2131
Adam Cohena9cf38f2011-05-02 15:36:58 -07002132 public DragController getDragController() {
2133 return mDragController;
2134 }
2135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 @Override
2137 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002138 onStartForResult(requestCode);
2139 super.startActivityForResult(intent, requestCode);
2140 }
2141
2142 @Override
2143 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2144 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2145 onStartForResult(requestCode);
2146 try {
2147 super.startIntentSenderForResult(intent, requestCode,
2148 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2149 } catch (IntentSender.SendIntentException e) {
2150 throw new ActivityNotFoundException();
2151 }
2152 }
2153
2154 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002155 if (requestCode >= 0) {
2156 setWaitingForResult(true);
2157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 }
2159
Winson Chung70d72102011-08-12 11:24:35 -07002160 /**
2161 * Indicates that we want global search for this activity by setting the globalSearch
2162 * argument for {@link #startSearch} to true.
2163 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002165 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002167
Karl Rosaen138a0412009-04-23 19:00:21 -07002168 if (initialQuery == null) {
2169 // Use any text typed in the launcher as the initial query
2170 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 if (appSearchData == null) {
2173 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002174 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 }
Winson Chungadf0c182012-08-23 14:59:07 -07002176 Rect sourceBounds = new Rect();
2177 if (mSearchDropTargetBar != null) {
2178 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2179 }
Romain Guycbb89e42009-06-08 15:52:54 -07002180
Ian Parkinson047036e2014-09-01 15:40:53 +01002181 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002182 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002183 if (clearTextImmediately) {
2184 clearTypedText();
2185 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002186
2187 // We need to show the workspace after starting the search
2188 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002189 }
2190
Ian Parkinson047036e2014-09-01 15:40:53 +01002191 /**
2192 * Start a text search.
2193 *
2194 * @return {@code true} if the search will start immediately, so any further keypresses
2195 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2196 * to buffer keypresses.
2197 */
2198 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002199 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002200 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2201 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2202 sourceBounds);
2203 }
2204
Michael Jurkaa33411c2012-06-14 16:18:21 -07002205 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002206 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002207 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002208 }
2209
2210 /**
2211 * Starts the global search activity. This code is a copied from SearchManager
2212 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002213 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002214 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002215 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002216 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2217 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2218 if (globalSearchActivity == null) {
2219 Log.w(TAG, "No global search activity found.");
2220 return;
2221 }
2222 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2223 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2224 intent.setComponent(globalSearchActivity);
2225 // Make sure that we have a Bundle to put source in
2226 if (appSearchData == null) {
2227 appSearchData = new Bundle();
2228 } else {
2229 appSearchData = new Bundle(appSearchData);
2230 }
Adam Cohen9211d422014-10-07 18:14:53 -07002231 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002232 if (!appSearchData.containsKey("source")) {
2233 appSearchData.putString("source", getPackageName());
2234 }
2235 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2236 if (!TextUtils.isEmpty(initialQuery)) {
2237 intent.putExtra(SearchManager.QUERY, initialQuery);
2238 }
2239 if (selectInitialQuery) {
2240 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2241 }
2242 intent.setSourceBounds(sourceBounds);
2243 try {
2244 startActivity(intent);
2245 } catch (ActivityNotFoundException ex) {
2246 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2247 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249
Yura4f93ec62014-02-04 14:15:21 +00002250 public boolean isOnCustomContent() {
2251 return mWorkspace.isOnOrMovingToCustomContent();
2252 }
2253
Winson Chung70d72102011-08-12 11:24:35 -07002254 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002255 public boolean onPrepareOptionsMenu(Menu menu) {
2256 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002257 if (!isOnCustomContent()) {
2258 // Close any open folders
2259 closeFolder();
2260 // Stop resizing any widgets
2261 mWorkspace.exitWidgetResizeMode();
2262 if (!mWorkspace.isInOverviewMode()) {
2263 // Show the overview mode
2264 showOverviewMode(true);
2265 } else {
2266 showWorkspace(true);
2267 }
Winson Chunge0298742014-01-17 12:03:00 -08002268 }
Adam Cohen9211d422014-10-07 18:14:53 -07002269 if (mLauncherCallbacks != null) {
2270 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2271 }
2272
Michael Jurkab94f3f82013-09-11 18:08:54 +02002273 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002276 @Override
2277 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002278 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002279 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002280 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002281 }
2282
Joe Onorato9c1289c2009-08-17 11:03:03 -04002283 public boolean isWorkspaceLocked() {
2284 return mWorkspaceLoading || mWaitingForResult;
2285 }
2286
Adam Cohen517a7f52014-03-01 12:12:59 -08002287 public boolean isWorkspaceLoading() {
2288 return mWorkspaceLoading;
2289 }
2290
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002291 private void setWorkspaceLoading(boolean value) {
2292 boolean isLocked = isWorkspaceLocked();
2293 mWorkspaceLoading = value;
2294 if (isLocked != isWorkspaceLocked()) {
2295 onWorkspaceLockedChanged();
2296 }
2297 }
2298
2299 private void setWaitingForResult(boolean value) {
2300 boolean isLocked = isWorkspaceLocked();
2301 mWaitingForResult = value;
2302 if (isLocked != isWorkspaceLocked()) {
2303 onWorkspaceLockedChanged();
2304 }
2305 }
2306
Adam Cohen9211d422014-10-07 18:14:53 -07002307 protected void onWorkspaceLockedChanged() {
2308 if (mLauncherCallbacks != null) {
2309 mLauncherCallbacks.onWorkspaceLockedChanged();
2310 }
2311 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002312
Michael Jurka0280c3b2010-09-17 15:00:07 -07002313 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002314 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002315 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2317 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002318 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002320 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002321
Sunny Goyale6b63a32015-01-16 16:14:29 -08002322 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002323 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002324 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2325 }
2326
Sunny Goyale6b63a32015-01-16 16:14:29 -08002327 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002328 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2329 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002330 if (appWidgetInfo.configure != null) {
2331 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002332 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002333
Bjorn Bringert7984c942009-12-09 15:38:25 +00002334 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002335 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2336 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002339 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002340 Runnable onComplete = new Runnable() {
2341 @Override
2342 public void run() {
2343 // Exit spring loaded mode if necessary after adding the widget
2344 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2345 null);
2346 }
2347 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002348 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002349 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002350 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 }
2352 }
Romain Guycbb89e42009-06-08 15:52:54 -07002353
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002354 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002355 // Close any folders that may be open.
2356 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002357 mWorkspace.moveToCustomContentScreen(animate);
2358 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002359
2360 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2361 int[] cell, int spanX, int spanY) {
2362 switch (info.itemType) {
2363 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2364 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2365 int span[] = new int[2];
2366 span[0] = spanX;
2367 span[1] = spanY;
2368 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2369 container, screenId, cell, span);
2370 break;
2371 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2372 processShortcutFromDrop(info.componentName, container, screenId, cell);
2373 break;
2374 default:
2375 throw new IllegalStateException("Unknown item type: " + info.itemType);
2376 }
2377 }
2378
Adam Cohenfbba09b2011-07-18 21:43:05 -07002379 /**
2380 * Process a shortcut drop.
2381 *
2382 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002383 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002384 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002385 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002386 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2387 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002388 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002389 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002390 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002391 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002392
2393 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002394 mPendingAddInfo.cellX = cell[0];
2395 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002396 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002397
2398 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2399 createShortcutIntent.setComponent(componentName);
2400 processShortcut(createShortcutIntent);
2401 }
2402
Adam Cohenfbba09b2011-07-18 21:43:05 -07002403 /**
2404 * Process a widget drop.
2405 *
2406 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002407 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002408 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002409 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002410 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2411 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002412 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002413 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002414 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002415 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002416 mPendingAddInfo.minSpanX = info.minSpanX;
2417 mPendingAddInfo.minSpanY = info.minSpanY;
2418
Adam Cohenfbba09b2011-07-18 21:43:05 -07002419 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002420 mPendingAddInfo.cellX = cell[0];
2421 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002422 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002423 if (span != null) {
2424 mPendingAddInfo.spanX = span[0];
2425 mPendingAddInfo.spanY = span[1];
2426 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002427
Adam Cohened66b2b2012-01-23 17:28:51 -08002428 AppWidgetHostView hostView = info.boundWidget;
2429 int appWidgetId;
2430 if (hostView != null) {
2431 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002432 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002433 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002434 // In this case, we either need to start an activity to get permission to bind
2435 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002436 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002437 Bundle options = info.bindOptions;
2438
Sunny Goyalffe83f12014-08-14 17:39:34 -07002439 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2440 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002441 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002442 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002443 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002444 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002445 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2446 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2447 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002448 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2449 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002450 // TODO: we need to make sure that this accounts for the options bundle.
2451 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002452 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2453 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002454 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002455 }
2456
Joe Onoratodeb98af2010-02-19 14:59:39 -08002457 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002458 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 }
2460
Winson Chung24ab2f12010-09-16 14:10:47 -07002461 void processWallpaper(Intent intent) {
2462 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2463 }
2464
Adam Cohendcd297f2013-06-18 13:13:40 -07002465 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002466 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002467 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 folderInfo.title = getText(R.string.folder_name);
2469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002471 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2472 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002473 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474
2475 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002476 FolderIcon newFolder =
2477 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002478 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002479 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002480 // Force measure the new folder icon
2481 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2482 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002483 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 }
Romain Guycbb89e42009-06-08 15:52:54 -07002485
Joe Onorato9c1289c2009-08-17 11:03:03 -04002486 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002487 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002488 }
2489
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002490 /**
2491 * Registers various content observers. The current implementation registers
2492 * only a favorites observer to keep track of the favorites applications.
2493 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002494 private void registerContentObservers() {
2495 ContentResolver resolver = getContentResolver();
2496 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2497 true, mWidgetObserver);
2498 }
2499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 @Override
2501 public boolean dispatchKeyEvent(KeyEvent event) {
2502 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2503 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002505 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002506 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002507 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002508 dumpState();
2509 return true;
2510 }
2511 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002512 }
2513 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2514 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002515 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 return true;
2517 }
2518 }
2519
2520 return super.dispatchKeyEvent(event);
2521 }
2522
Joe Onorato88ec0992009-11-19 13:16:06 -08002523 @Override
2524 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002525 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2526 return;
2527 }
2528
Winson Chung3d9490a2015-03-24 17:38:42 -07002529 LauncherAccessibilityDelegate delegate =
2530 LauncherAppState.getInstance().getAccessibilityDelegate();
2531 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002532 return;
2533 }
2534
Winson Chungb745afb2015-03-02 11:51:23 -08002535 if (isAppsViewVisible()) {
2536 showWorkspace(true);
2537 } else if (isWidgetsViewVisible()) {
2538 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002539 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002540 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002541 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002542 Folder openFolder = mWorkspace.getOpenFolder();
2543 if (openFolder.isEditingName()) {
2544 openFolder.dismissEditingName();
2545 } else {
2546 closeFolder();
2547 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002548 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002549 mWorkspace.exitWidgetResizeMode();
2550
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002551 // Back button is a no-op here, but give at least some feedback for the button press
2552 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002553 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002554 }
2555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002557 * Re-listen when widgets are reset.
2558 */
Adam Cohen091440a2015-03-18 14:16:05 -07002559 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002560 if (mAppWidgetHost != null) {
2561 mAppWidgetHost.startListening();
2562 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002563 }
2564
2565 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 * Launches the intent referred by the clicked shortcut.
2567 *
2568 * @param v The view representing the clicked shortcut.
2569 */
2570 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002571 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2572 // view has detached (it's possible for this to happen if the view is removed mid touch).
2573 if (v.getWindowToken() == null) {
2574 return;
2575 }
2576
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002577 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002578 return;
2579 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002580
Adam Cohen1697b792013-09-17 19:08:21 -07002581 if (v instanceof Workspace) {
2582 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002583 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002584 }
2585 return;
2586 }
2587
2588 if (v instanceof CellLayout) {
2589 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002590 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002591 }
2592 }
2593
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002594 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002595 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002596 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002598 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002600 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002601 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002602 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002603 } else if (tag instanceof AppInfo) {
2604 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002605 } else if (tag instanceof LauncherAppWidgetInfo) {
2606 if (v instanceof PendingAppWidgetHostView) {
2607 onClickPendingWidget((PendingAppWidgetHostView) v);
2608 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002609 }
2610 }
2611
Sunny Goyal508da152014-08-14 10:53:27 -07002612 public void onClickPagedViewIcon(View v) {
2613 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002614 if (mLauncherCallbacks != null) {
2615 mLauncherCallbacks.onClickPagedViewIcon(v);
2616 }
Sunny Goyal508da152014-08-14 10:53:27 -07002617 }
2618
Michael Jurka0e260592010-06-30 17:07:39 -07002619 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002620 return false;
2621 }
2622
Michael Jurkaaf442092010-06-10 17:01:57 -07002623 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002624 * Event handler for the app widget view which has not fully restored.
2625 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002626 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002627 if (mIsSafeModeEnabled) {
2628 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2629 return;
2630 }
2631
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002632 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002633 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002634 int widgetId = info.appWidgetId;
2635 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2636 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002637 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2638 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002639 mPendingAddInfo.copyFrom(info);
2640 mPendingAddWidgetId = widgetId;
2641
Sunny Goyalffe83f12014-08-14 17:39:34 -07002642 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2643 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002644 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002645 } else if (info.installProgress < 0) {
2646 // The install has not been queued
2647 final String packageName = info.providerName.getPackageName();
2648 showBrokenAppInstallDialog(packageName,
2649 new DialogInterface.OnClickListener() {
2650 public void onClick(DialogInterface dialog, int id) {
2651 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2652 }
2653 });
2654 } else {
2655 // Download has started.
2656 final String packageName = info.providerName.getPackageName();
2657 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002658 }
2659 }
2660
2661 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002662 * Event handler for the "grid" button that appears on the home screen, which
2663 * enters all apps mode.
2664 *
2665 * @param v The view that was clicked.
2666 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002667 protected void onClickAllAppsButton(View v) {
2668 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002669 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002670 showWorkspace(true);
2671 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002672 // Try and refresh the set of predicted apps before we enter launcher
2673 showAppsView(true /* animated */, false /* resetListToTop */,
2674 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002675 }
2676 }
2677
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002678 private void showBrokenAppInstallDialog(final String packageName,
2679 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002680 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002681 .setTitle(R.string.abandoned_promises_title)
2682 .setMessage(R.string.abandoned_promise_explanation)
2683 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2684 .setNeutralButton(R.string.abandoned_clean_this,
2685 new DialogInterface.OnClickListener() {
2686 public void onClick(DialogInterface dialog, int id) {
2687 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2688 mWorkspace.removeAbandonedPromise(packageName, user);
2689 }
2690 })
2691 .create().show();
2692 return;
2693 }
2694
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002695 /**
2696 * Event handler for an app shortcut click.
2697 *
2698 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2699 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002700 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002701 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2702 Object tag = v.getTag();
2703 if (!(tag instanceof ShortcutInfo)) {
2704 throw new IllegalArgumentException("Input must be a Shortcut");
2705 }
2706
2707 // Open shortcut
2708 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002709
2710 if (shortcut.isDisabled != 0) {
2711 int error = R.string.activity_not_available;
2712 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2713 error = R.string.safemode_shortcut_error;
2714 }
2715 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2716 return;
2717 }
2718
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002719 final Intent intent = shortcut.intent;
2720
2721 // Check for special shortcuts
2722 if (intent.getComponent() != null) {
2723 final String shortcutClass = intent.getComponent().getClassName();
2724
2725 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2726 MemoryDumpActivity.startDump(this);
2727 return;
2728 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2729 toggleShowWeightWatcher();
2730 return;
2731 }
2732 }
2733
Chris Wren40c5ed32014-06-24 18:24:23 -04002734 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002735 if ((v instanceof BubbleTextView)
2736 && shortcut.isPromise()
2737 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002738 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002739 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002740 new DialogInterface.OnClickListener() {
2741 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002742 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002743 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002744 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002745 return;
2746 }
2747
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002748 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002749 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002750
2751 if (mLauncherCallbacks != null) {
2752 mLauncherCallbacks.onClickAppShortcut(v);
2753 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002754 }
2755
Adam Cohen091440a2015-03-18 14:16:05 -07002756 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002757 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002758 final ShortcutInfo shortcut;
2759 final Intent intent;
2760 if (tag instanceof ShortcutInfo) {
2761 shortcut = (ShortcutInfo) tag;
2762 intent = shortcut.intent;
2763 int[] pos = new int[2];
2764 v.getLocationOnScreen(pos);
2765 intent.setSourceBounds(new Rect(pos[0], pos[1],
2766 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002767
Sunny Goyal508da152014-08-14 10:53:27 -07002768 } else if (tag instanceof AppInfo) {
2769 shortcut = null;
2770 intent = ((AppInfo) tag).intent;
2771 } else {
2772 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2773 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002774
2775 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002776 mStats.recordLaunch(intent, shortcut);
2777
2778 if (success && v instanceof BubbleTextView) {
2779 mWaitingForResume = (BubbleTextView) v;
2780 mWaitingForResume.setStayPressed(true);
2781 }
2782 }
2783
2784 /**
2785 * Event handler for a folder icon click.
2786 *
2787 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2788 */
2789 protected void onClickFolderIcon(View v) {
2790 if (LOGD) Log.d(TAG, "onClickFolder");
2791 if (!(v instanceof FolderIcon)){
2792 throw new IllegalArgumentException("Input must be a FolderIcon");
2793 }
2794
2795 FolderIcon folderIcon = (FolderIcon) v;
2796 final FolderInfo info = folderIcon.getFolderInfo();
2797 Folder openFolder = mWorkspace.getFolderForTag(info);
2798
2799 // If the folder info reports that the associated folder is open, then verify that
2800 // it is actually opened. There have been a few instances where this gets out of sync.
2801 if (info.opened && openFolder == null) {
2802 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2803 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2804 info.opened = false;
2805 }
2806
2807 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2808 // Close any open folder
2809 closeFolder();
2810 // Open the requested folder
2811 openFolder(folderIcon);
2812 } else {
2813 // Find the open folder...
2814 int folderScreen;
2815 if (openFolder != null) {
2816 folderScreen = mWorkspace.getPageForView(openFolder);
2817 // .. and close it
2818 closeFolder(openFolder);
2819 if (folderScreen != mWorkspace.getCurrentPage()) {
2820 // Close any folder open on the current screen
2821 closeFolder();
2822 // Pull the folder onto this screen
2823 openFolder(folderIcon);
2824 }
2825 }
2826 }
Adam Cohen9211d422014-10-07 18:14:53 -07002827
2828 if (mLauncherCallbacks != null) {
2829 mLauncherCallbacks.onClickFolderIcon(v);
2830 }
Michael Jurka5130e402011-10-13 04:55:35 -07002831 }
2832
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002833 /**
2834 * Event handler for the (Add) Widgets button that appears after a long press
2835 * on the home screen.
2836 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002837 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002838 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002839 if (mIsSafeModeEnabled) {
2840 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2841 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002842 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002843 if (mLauncherCallbacks != null) {
2844 mLauncherCallbacks.onClickAddWidgetButton(view);
2845 }
Adam Cohen9211d422014-10-07 18:14:53 -07002846 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002847 }
2848
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002849 /**
2850 * Event handler for the wallpaper picker button that appears after a long press
2851 * on the home screen.
2852 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002853 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002854 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002855 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2856 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002857
2858 if (mLauncherCallbacks != null) {
2859 mLauncherCallbacks.onClickWallpaperPicker(v);
2860 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002861 }
2862
2863 /**
2864 * Event handler for a click on the settings button that appears after a long press
2865 * on the home screen.
2866 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002867 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002868 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002869 if (mLauncherCallbacks != null) {
2870 mLauncherCallbacks.onClickSettingsButton(v);
2871 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002872 }
2873
Michael Jurka5130e402011-10-13 04:55:35 -07002874 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002875 // Provide the same haptic feedback that the system offers for virtual keys.
2876 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002877 }
2878
Adam Cohen61f560d2013-09-30 15:58:20 -07002879 public void performHapticFeedbackOnTouchDown(View v) {
2880 // Provide the same haptic feedback that the system offers for virtual keys.
2881 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2882 }
2883
2884 public View.OnTouchListener getHapticFeedbackTouchListener() {
2885 if (mHapticFeedbackTouchListener == null) {
2886 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2887 @Override
2888 public boolean onTouch(View v, MotionEvent event) {
2889 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2890 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2891 }
2892 return false;
2893 }
2894 };
2895 }
2896 return mHapticFeedbackTouchListener;
2897 }
2898
Adam Cohen9211d422014-10-07 18:14:53 -07002899 public void onDragStarted(View view) {
2900 if (isOnCustomContent()) {
2901 // Custom content screen doesn't participate in drag and drop. If on custom
2902 // content screen, move to default.
2903 moveWorkspaceToDefaultScreen();
2904 }
2905
2906 if (mLauncherCallbacks != null) {
2907 mLauncherCallbacks.onDragStarted(view);
2908 }
2909 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002910
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002911 /**
2912 * Called when the user stops interacting with the launcher.
2913 * This implies that the user is now on the homescreen and is not doing housekeeping.
2914 */
Adam Cohen9211d422014-10-07 18:14:53 -07002915 protected void onInteractionEnd() {
2916 if (mLauncherCallbacks != null) {
2917 mLauncherCallbacks.onInteractionEnd();
2918 }
2919 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002920
2921 /**
2922 * Called when the user starts interacting with the launcher.
2923 * The possible interactions are:
2924 * - open all apps
2925 * - reorder an app shortcut, or a widget
2926 * - open the overview mode.
2927 * This is a good time to stop doing things that only make sense
2928 * when the user is on the homescreen and not doing housekeeping.
2929 */
Adam Cohen9211d422014-10-07 18:14:53 -07002930 protected void onInteractionBegin() {
2931 if (mLauncherCallbacks != null) {
2932 mLauncherCallbacks.onInteractionBegin();
2933 }
2934 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002935
Winson Chungcd99cd32015-04-29 11:03:24 -07002936 /** Updates the interaction state. */
2937 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002938 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002939 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002940 boolean fromStateWithOverlay;
2941 boolean toStateWithOverlay;
2942 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2943 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2944 toStateWithOverlay = toState != Workspace.State.NORMAL;
2945 } else {
2946 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2947 fromState != Workspace.State.NORMAL_HIDDEN;
2948 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2949 toState != Workspace.State.NORMAL_HIDDEN;
2950 }
2951 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002952 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002953 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002954 onInteractionEnd();
2955 }
2956 }
2957
Kenny Guyf07af7b2014-07-31 11:39:16 +01002958 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002959 try {
2960 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002961 launcherApps.showAppDetailsForProfile(componentName, user);
2962 } catch (SecurityException e) {
2963 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2964 Log.e(TAG, "Launcher does not have permission to launch settings");
2965 } catch (ActivityNotFoundException e) {
2966 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2967 Log.e(TAG, "Unable to launch settings");
2968 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002969 }
2970
Michael Jurka1e2f4652013-07-08 18:03:46 -07002971 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002972 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2973 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002974 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002975 // System applications cannot be installed. For now, show a toast explaining that.
2976 // We may give them the option of disabling apps this way.
2977 int messageId = R.string.uninstall_system_app_text;
2978 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002979 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002980 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002981 String packageName = componentName.getPackageName();
2982 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002983 Intent intent = new Intent(
2984 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002985 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2986 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002987 if (user != null) {
2988 user.addToIntent(intent, Intent.EXTRA_USER);
2989 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002990 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002991 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002992 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002993 }
2994
Michael Jurka86a720e2012-05-09 11:23:23 -07002995 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002996 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002998 // Only launch using the new animation if the shortcut has not opted out (this is a
2999 // private contract between launcher and may be ignored in the future).
3000 boolean useLaunchAnimation = (v != null) &&
3001 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01003002 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
3003 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01003004
Kenny Guy1317e2d2014-05-08 18:52:50 +01003005 UserHandleCompat user = null;
3006 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
3007 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
3008 user = userManager.getUserForSerialNumber(serialNumber);
3009 }
3010
3011 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07003012 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08003013 ActivityOptions opts = null;
3014 if (sClipRevealMethod != null) {
3015 // TODO: call method directly when Launcher3 can depend on M APIs
3016 int left = 0, top = 0;
3017 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
3018 if (v instanceof TextView) {
3019 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08003020 Drawable icon = Workspace.getTextViewIcon((TextView) v);
3021 if (icon != null) {
3022 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08003023 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08003024 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08003025 width = bounds.width();
3026 height = bounds.height();
3027 }
3028 }
3029 try {
3030 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
3031 left, top, width, height);
3032 } catch (IllegalAccessException e) {
3033 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3034 sClipRevealMethod = null;
3035 } catch (InvocationTargetException e) {
3036 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3037 sClipRevealMethod = null;
3038 }
3039 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003040 if (opts == null && !Utilities.isLmpOrAbove()) {
3041 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08003042 v.getMeasuredWidth(), v.getMeasuredHeight());
3043 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003044 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07003045 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003046
3047 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3048 // Could be launching some bookkeeping activity
3049 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003050 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003051 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003052 launcherApps.startActivityForProfile(intent.getComponent(), user,
3053 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003054 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003055 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003056 } catch (SecurityException e) {
3057 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003058 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003060 "or use the exported attribute for this activity. "
3061 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003063 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003064 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003065
Michael Jurka86a720e2012-05-09 11:23:23 -07003066 boolean startActivitySafely(View v, Intent intent, Object tag) {
3067 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003068 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3069 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3070 return false;
3071 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003072 try {
3073 success = startActivity(v, intent, tag);
3074 } catch (ActivityNotFoundException e) {
3075 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3076 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3077 }
3078 return success;
3079 }
3080
Adam Cohen268c4752012-06-06 17:47:33 -07003081 /**
3082 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3083 * in the DragLayer in the exact absolute location of the original FolderIcon.
3084 */
3085 private void copyFolderIconToImage(FolderIcon fi) {
3086 final int width = fi.getMeasuredWidth();
3087 final int height = fi.getMeasuredHeight();
3088
3089 // Lazy load ImageView, Bitmap and Canvas
3090 if (mFolderIconImageView == null) {
3091 mFolderIconImageView = new ImageView(this);
3092 }
3093 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3094 mFolderIconBitmap.getHeight() != height) {
3095 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3096 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3097 }
3098
3099 DragLayer.LayoutParams lp;
3100 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3101 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3102 } else {
3103 lp = new DragLayer.LayoutParams(width, height);
3104 }
3105
Adam Cohen307fe232012-08-16 17:55:58 -07003106 // The layout from which the folder is being opened may be scaled, adjust the starting
3107 // view size by this scale factor.
3108 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003109 lp.customPosition = true;
3110 lp.x = mRectForFolderAnimation.left;
3111 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003112 lp.width = (int) (scale * width);
3113 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003114
3115 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3116 fi.draw(mFolderIconCanvas);
3117 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003118 if (fi.getFolder() != null) {
3119 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3120 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003121 }
Adam Cohen268c4752012-06-06 17:47:33 -07003122 // Just in case this image view is still in the drag layer from a previous animation,
3123 // we remove it and re-add it.
3124 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3125 mDragLayer.removeView(mFolderIconImageView);
3126 }
3127 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003128 if (fi.getFolder() != null) {
3129 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003130 }
Adam Cohen268c4752012-06-06 17:47:33 -07003131 }
3132
Adam Cohen2801caf2011-05-13 20:57:39 -07003133 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003134 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003135 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3136 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3137 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3138
Adam Cohenc51934b2011-07-26 21:07:43 -07003139 FolderInfo info = (FolderInfo) fi.getTag();
3140 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3141 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003142 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3143 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003144 }
3145
Adam Cohen268c4752012-06-06 17:47:33 -07003146 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3147 copyFolderIconToImage(fi);
3148 fi.setVisibility(View.INVISIBLE);
3149
Michael Jurka2ecf9952012-06-18 12:52:28 -07003150 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003151 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003152 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003153 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3154 }
3155 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003156 oa.start();
3157 }
3158
Adam Cohen268c4752012-06-06 17:47:33 -07003159 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003160 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003161 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3162 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3163 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3164
Adam Cohen268c4752012-06-06 17:47:33 -07003165 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003166
Adam Cohen268c4752012-06-06 17:47:33 -07003167 // We remove and re-draw the FolderIcon in-case it has changed
3168 mDragLayer.removeView(mFolderIconImageView);
3169 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003170 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003171 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003172 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003173 oa.addListener(new AnimatorListenerAdapter() {
3174 @Override
3175 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003176 if (cl != null) {
3177 cl.clearFolderLeaveBehind();
3178 // Remove the ImageView copy of the FolderIcon and make the original visible.
3179 mDragLayer.removeView(mFolderIconImageView);
3180 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003181 }
3182 }
3183 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003184 oa.start();
3185 }
3186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003187 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003188 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003189 * is animated relative to the specified View. If the View is null, no animation
3190 * is played.
3191 *
3192 * @param folderInfo The FolderInfo describing the folder to open.
3193 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003194 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003195 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003196 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3197 if (openFolder != null && openFolder != folder) {
3198 // Close any open folder before opening a folder.
3199 closeFolder();
3200 }
3201
Adam Cohena9cf38f2011-05-02 15:36:58 -07003202 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203
Adam Cohena9cf38f2011-05-02 15:36:58 -07003204 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205
Sunny Goyalf4066152015-04-15 09:42:19 -07003206 // While the folder is open, the position of the icon cannot change.
3207 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3208
Adam Cohen4554ee12011-08-03 16:13:21 -07003209 // Just verify that the folder hasn't already been added to the DragLayer.
3210 // There was a one-off crash where the folder had a parent already.
3211 if (folder.getParent() == null) {
3212 mDragLayer.addView(folder);
3213 mDragController.addDropTarget((DropTarget) folder);
3214 } else {
3215 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3216 folder.getParent() + ").");
3217 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003218 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003219 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003220
3221 // Notify the accessibility manager that this folder "window" has appeared and occluded
3222 // the workspace items
3223 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3224 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003225 }
3226
3227 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003228 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003229 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003230 if (folder.isEditingName()) {
3231 folder.dismissEditingName();
3232 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003233 closeFolder(folder);
3234 }
3235 }
3236
3237 void closeFolder(Folder folder) {
3238 folder.getInfo().opened = false;
3239
3240 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3241 if (parent != null) {
3242 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3243 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003244 if (fi != null) {
3245 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3246 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003247 }
3248 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003249
3250 // Notify the accessibility manager that this folder "window" has disappeard and no
3251 // longer occludeds the workspace items
3252 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253 }
3254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003256 if (!isDraggingEnabled()) return false;
3257 if (isWorkspaceLocked()) return false;
3258 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003259
Adam Cohen1697b792013-09-17 19:08:21 -07003260 if (v instanceof Workspace) {
3261 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003262 if (!mWorkspace.isTouchActive()) {
3263 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003264 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3265 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3266 return true;
3267 } else {
3268 return false;
3269 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003270 } else {
3271 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003272 }
Adam Cohen1697b792013-09-17 19:08:21 -07003273 }
3274
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003275 CellLayout.CellInfo longClickCellInfo = null;
3276 View itemUnderLongClick = null;
3277 if (v.getTag() instanceof ItemInfo) {
3278 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003279 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003280 itemUnderLongClick = longClickCellInfo.cell;
3281 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003282 }
3283
Winson Chung3d503fb2011-07-13 17:25:49 -07003284 // The hotseat touch handling does not go through Workspace, and we always allow long press
3285 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003286 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003287 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003288 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003289 // User long pressed on empty space
3290 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3291 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003292 if (mWorkspace.isInOverviewMode()) {
3293 mWorkspace.startReordering(v);
3294 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003295 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003297 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003298 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3299 mHotseat.getOrderInHotseat(
3300 longClickCellInfo.cellX,
3301 longClickCellInfo.cellY));
3302 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003303 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003304 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003305 }
3306 }
3307 }
3308 return true;
3309 }
3310
Winson Chung3d503fb2011-07-13 17:25:49 -07003311 boolean isHotseatLayout(View layout) {
3312 return mHotseat != null && layout != null &&
3313 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3314 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003315
Winson Chung3d503fb2011-07-13 17:25:49 -07003316 /**
3317 * Returns the CellLayout of the specified container at the specified screen.
3318 */
Sunny Goyal92820592015-03-02 11:31:35 -08003319 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003320 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3321 if (mHotseat != null) {
3322 return mHotseat.getLayout();
3323 } else {
3324 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003325 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003326 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003327 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003328 }
3329 }
3330
Winson Chungb745afb2015-03-02 11:51:23 -08003331 /**
3332 * For overridden classes.
3333 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003334 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003335 return isAppsViewVisible();
3336 }
3337
3338 public boolean isAppsViewVisible() {
3339 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3340 }
3341
3342 public boolean isWidgetsViewVisible() {
3343 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003344 }
3345
Craig Mautner360310b2012-10-26 15:13:08 -07003346 private void setWorkspaceBackground(boolean workspace) {
3347 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003348 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003349 }
3350
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003351 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003352 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3353 int curflags = getWindow().getAttributes().flags
3354 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3355 if (wpflags != curflags) {
3356 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3357 }
Craig Mautner360310b2012-10-26 15:13:08 -07003358 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003359 }
3360
Michael Jurkae326f182011-11-21 14:05:46 -08003361 @Override
3362 public void onTrimMemory(int level) {
3363 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003364 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3365 // The widget preview db can result in holding onto over
3366 // 3MB of memory for caching which isn't necessary.
3367 SQLiteDatabase.releaseMemory();
3368
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003369 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003370 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003371 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003372 if (mLauncherCallbacks != null) {
3373 mLauncherCallbacks.onTrimMemory(level);
3374 }
Michael Jurkae326f182011-11-21 14:05:46 -08003375 }
3376
Winson Chungb745afb2015-03-02 11:51:23 -08003377 @Override
3378 public void onStateTransitionHideSearchBar() {
3379 // Hide the search bar
3380 if (mSearchDropTargetBar != null) {
3381 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3382 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003383 }
3384
Winson Chungb745afb2015-03-02 11:51:23 -08003385 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003386 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3387 true);
Adam Cohened307df2013-10-02 09:37:31 -07003388 }
3389
Winson Chungdc61c4d2015-04-20 18:26:57 -07003390 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3391 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3392 onCompleteRunnable, true);
3393 }
3394
3395 protected void showWorkspace(int snapToPage, boolean animated) {
3396 showWorkspace(snapToPage, animated, null, true);
3397 }
3398
3399 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3400 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003401 boolean changed = mState != State.WORKSPACE ||
3402 mWorkspace.getState() != Workspace.State.NORMAL;
3403 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003404 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003405 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003406 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003407 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003408
Winson Chungc7d2b602012-05-16 17:02:20 -07003409 // Show the search bar (only animate if we were showing the drop target bar in spring
3410 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003411 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003412 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003413 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003414
Michael Jurkab3e22d92011-10-31 15:58:33 -07003415 // Set focus to the AppsCustomize button
3416 if (mAllAppsButton != null) {
3417 mAllAppsButton.requestFocus();
3418 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003419 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003420
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003421 // Change the state *after* we've called all the transition code
3422 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003423
Winson Chung5fb63472011-02-02 17:03:37 -08003424 // Resume the auto-advance of widgets
3425 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003426 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003427
Winson Chung0f785722015-04-08 10:27:49 -07003428 if (changed) {
3429 // Send an accessibility event to announce the context change
3430 getWindow().getDecorView()
3431 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003432 if (notifyLauncherCallbacks) {
3433 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003434 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003435 mLauncherCallbacks.onAllAppsHidden();
3436 }
3437 }
Winson Chung0f785722015-04-08 10:27:49 -07003438 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003439 }
3440
Adam Cohened307df2013-10-02 09:37:31 -07003441 void showOverviewMode(boolean animated) {
3442 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003443 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003444 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3445 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003446 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003447 }
3448
Winson Chungb745afb2015-03-02 11:51:23 -08003449 /**
3450 * Shows the apps view.
3451 */
Winson Chung4ac30062015-05-08 17:34:17 -07003452 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003453 if (resetListToTop) {
3454 mAppsView.scrollToTop();
3455 }
Winson Chung4ac30062015-05-08 17:34:17 -07003456 if (updatePredictedApps) {
3457 tryAndUpdatePredictedApps();
3458 }
Winson Chungb745afb2015-03-02 11:51:23 -08003459 showAppsOrWidgets(animated, State.APPS);
3460 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003461
Winson Chungb745afb2015-03-02 11:51:23 -08003462 /**
3463 * Shows the widgets view.
3464 */
3465 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003466 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003467 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003468 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003469 }
Winson Chungb745afb2015-03-02 11:51:23 -08003470 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003471
3472 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003473 @Override
3474 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003475 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003476 }
3477 });
Winson Chungb745afb2015-03-02 11:51:23 -08003478 }
3479
3480 /**
3481 * Sets up the transition to show the apps/widgets view.
3482 */
3483 private void showAppsOrWidgets(boolean animated, State toState) {
3484 if (mState != State.WORKSPACE) return;
3485 if (toState != State.APPS && toState != State.WIDGETS) return;
3486
3487 if (toState == State.APPS) {
3488 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003489 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003490 mLauncherCallbacks.onAllAppsShown();
3491 }
Winson Chungb745afb2015-03-02 11:51:23 -08003492 } else {
3493 mStateTransitionAnimation.startAnimationToWidgets(animated);
3494 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003495
Michael Jurkab3e22d92011-10-31 15:58:33 -07003496 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003497 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003498
3499 // Pause the auto-advance of widgets until we are out of AllApps
3500 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003501 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003502 closeFolder();
3503
3504 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003505 getWindow().getDecorView()
3506 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003507 }
3508
Winson Chungcd99cd32015-04-29 11:03:24 -07003509 /**
3510 * Updates the workspace and interaction state on state change, and return the animation to this
3511 * new state.
3512 */
3513 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3514 boolean animated, HashMap<View, Integer> layerViews) {
3515 Workspace.State fromState = mWorkspace.getState();
3516 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3517 updateInteraction(fromState, toState);
3518 return anim;
3519 }
3520
Hyunyoung Song3f471442015-04-08 19:01:34 -07003521 public void enterSpringLoadedDragMode() {
3522 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3523 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003524 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3525 mState == State.WIDGETS_SPRING_LOADED) {
3526 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003527 }
Winson Chungb745afb2015-03-02 11:51:23 -08003528
3529 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003530 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3531 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003532 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003533 }
Adam Cohen7777d962011-08-18 18:58:38 -07003534
Hyunyoung Song3f471442015-04-08 19:01:34 -07003535 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003536 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003537 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003538
Winson Chungcd99cd32015-04-29 11:03:24 -07003539 if (successfulDrop) {
3540 // We need to trigger all apps hidden to notify search to update itself before the
3541 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003542 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003543 mLauncherCallbacks.onAllAppsHidden();
3544 }
3545 }
3546
Winson Chunge7a03942011-08-05 15:05:12 -07003547 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003548 @Override
3549 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003550 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003551 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3552 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003553 // Before we show workspace, hide all apps again because
3554 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3555 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003556 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003557 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003558 } else {
3559 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003560 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003561 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003562 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003563 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003564
Michael Jurkad3ef3062010-11-23 16:23:58 -08003565 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003566 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003567 showAppsView(true /* animated */, false /* resetListToTop */,
3568 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003569 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003570 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003571 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003572 }
3573
Winson Chung4ac30062015-05-08 17:34:17 -07003574 /**
3575 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3576 * resumed.
3577 */
3578 private void tryAndUpdatePredictedApps() {
3579 if (mLauncherCallbacks != null) {
3580 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3581 if (!apps.isEmpty()) {
3582 mAppsView.setPredictedApps(apps);
3583 }
3584 }
3585 }
3586
Michael Jurkab3e22d92011-10-31 15:58:33 -07003587 void lockAllApps() {
3588 // TODO
3589 }
3590
3591 void unlockAllApps() {
3592 // TODO
3593 }
3594
Sunny Goyal594d76d2014-11-06 10:12:54 -08003595 protected void disableVoiceButtonProxy(boolean disable) {
3596 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003597 }
3598
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003599 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003600 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3601 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003602 }
3603
Adam Cohen24ce0b32014-01-14 16:18:14 -08003604 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003605 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3606 if (searchProvider == null) {
3607 return null;
3608 }
3609
3610 Bundle opts = new Bundle();
3611 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003612 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003613
3614 SharedPreferences sp = getSharedPreferences(
3615 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3616 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003617 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003618 if (!searchProvider.provider.flattenToString().equals(
3619 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003620 || (widgetInfo == null)
3621 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003622 // A valid widget is not already bound.
3623 if (widgetId > -1) {
3624 mAppWidgetHost.deleteAppWidgetId(widgetId);
3625 widgetId = -1;
3626 }
3627
3628 // Try to bind a new widget
3629 widgetId = mAppWidgetHost.allocateAppWidgetId();
3630
3631 if (!AppWidgetManagerCompat.getInstance(this)
3632 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3633 mAppWidgetHost.deleteAppWidgetId(widgetId);
3634 widgetId = -1;
3635 }
3636
3637 sp.edit()
3638 .putInt(QSB_WIDGET_ID, widgetId)
3639 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3640 .commit();
3641 }
3642
3643 if (widgetId != -1) {
3644 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3645 mQsb.updateAppWidgetOptions(opts);
3646 mQsb.setPadding(0, 0, 0, 0);
3647 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003648 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003649 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003650 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003651 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003652 }
3653
Sunny Goyal22235bc2015-04-03 09:23:43 -07003654 private void reinflateQSBIfNecessary() {
3655 if (mQsb instanceof LauncherAppWidgetHostView &&
3656 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3657 mSearchDropTargetBar.removeView(mQsb);
3658 mQsb = null;
3659 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3660 }
3661 }
3662
Michael Jurkad7c28052012-04-27 15:43:36 -07003663 @Override
3664 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003665 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003666 final List<CharSequence> text = event.getText();
3667 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003668 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003669 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003670 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003671 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003672 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003673 } else {
3674 text.add(getString(R.string.all_apps_home_button_label));
3675 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003676 return result;
3677 }
3678
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003679 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003680 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003681 */
Adam Cohen091440a2015-03-18 14:16:05 -07003682 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003683 @Override
3684 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003685 closeSystemDialogs();
3686 }
3687 }
3688
3689 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003690 * Receives notifications whenever the appwidgets are reset.
3691 */
3692 private class AppWidgetResetObserver extends ContentObserver {
3693 public AppWidgetResetObserver() {
3694 super(new Handler());
3695 }
3696
3697 @Override
3698 public void onChange(boolean selfChange) {
3699 onAppWidgetReset();
3700 }
3701 }
3702
3703 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003704 * If the activity is currently paused, signal that we need to run the passed Runnable
3705 * in onResume.
3706 *
3707 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003708 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3709 * wrong when we're not running, and if the activity comes back to what the configuration was
3710 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003711 *
3712 * Implementation of the method from LauncherModel.Callbacks.
3713 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003714 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003715 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003716 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003717 if (mPaused) {
3718 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003719 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003720 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003721 }
3722 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003723 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003724 return true;
3725 } else {
3726 return false;
3727 }
3728 }
3729
Michael Jurkac402cd92013-05-20 15:49:32 +02003730 private boolean waitUntilResume(Runnable run) {
3731 return waitUntilResume(run, false);
3732 }
3733
Michael Jurka1e2f4652013-07-08 18:03:46 -07003734 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003735 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003736 }
3737
Michael Jurka7607c2f2013-04-03 14:33:19 -07003738 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003739 * If the activity is currently paused, signal that we need to re-run the loader
3740 * in onResume.
3741 *
3742 * This needs to be called from incoming places where resources might have been loaded
3743 * while we are paused. That is becaues the Configuration might be wrong
3744 * when we're not running, and if it comes back to what it was when we
3745 * were paused, we are not restarted.
3746 *
3747 * Implementation of the method from LauncherModel.Callbacks.
3748 *
3749 * @return true if we are currently paused. The caller might be able to
3750 * skip some work in that case since we will come back again.
3751 */
3752 public boolean setLoadOnResume() {
3753 if (mPaused) {
3754 Log.i(TAG, "setLoadOnResume");
3755 mOnResumeNeedsLoad = true;
3756 return true;
3757 } else {
3758 return false;
3759 }
3760 }
3761
3762 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003763 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003764 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003766 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003767 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003768 } else {
3769 return SCREEN_COUNT / 2;
3770 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003771 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003772
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773 /**
3774 * Refreshes the shortcuts shown on the workspace.
3775 *
3776 * Implementation of the method from LauncherModel.Callbacks.
3777 */
3778 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003779 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003780
Michael Jurka7607c2f2013-04-03 14:33:19 -07003781 // If we're starting binding all over again, clear any bind calls we'd postponed in
3782 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3783 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003784 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003785
Winson Chungd64d1762013-08-20 14:37:16 -07003786 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003787 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003788 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003789
Michael Jurka05bf6442011-11-30 20:28:53 -08003790 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003791 if (mHotseat != null) {
3792 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003793 }
3794 }
3795
Adam Cohendcd297f2013-06-18 13:13:40 -07003796 @Override
3797 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003798 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003799
Adam Cohen5084cba2013-09-03 12:01:16 -07003800 // If there are no screens, we need to have an empty screen
3801 if (orderedScreenIds.size() == 0) {
3802 mWorkspace.addExtraEmptyScreen();
3803 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003804
3805 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003806 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003807 if (hasCustomContentToLeft()) {
3808 mWorkspace.createCustomContentContainer();
3809 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003810 }
Winson Chung64359a52013-07-08 17:17:08 -07003811 }
3812
3813 @Override
3814 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003815 // Log to disk
3816 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3817 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3818 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003819 int count = orderedScreenIds.size();
3820 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003821 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003822 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003823 }
3824
Adam Cohen39a06042013-07-19 14:30:12 -07003825 private boolean shouldShowWeightWatcher() {
3826 String spKey = LauncherAppState.getSharedPreferencesKey();
3827 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003828 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003829
3830 return show;
3831 }
3832
3833 private void toggleShowWeightWatcher() {
3834 String spKey = LauncherAppState.getSharedPreferencesKey();
3835 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3836 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3837
3838 show = !show;
3839
3840 SharedPreferences.Editor editor = sp.edit();
3841 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3842 editor.commit();
3843
3844 if (mWeightWatcher != null) {
3845 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3846 }
3847 }
3848
Winson Chungd64d1762013-08-20 14:37:16 -07003849 public void bindAppsAdded(final ArrayList<Long> newScreens,
3850 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003851 final ArrayList<ItemInfo> addAnimated,
3852 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003853 Runnable r = new Runnable() {
3854 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003855 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003856 }
3857 };
3858 if (waitUntilResume(r)) {
3859 return;
3860 }
3861
Winson Chungd64d1762013-08-20 14:37:16 -07003862 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003863 if (newScreens != null) {
3864 bindAddScreens(newScreens);
3865 }
Winson Chungd64d1762013-08-20 14:37:16 -07003866
3867 // We add the items without animation on non-visible pages, and with
3868 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003869 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003870 bindItems(addNotAnimated, 0,
3871 addNotAnimated.size(), false);
3872 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003873 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003874 bindItems(addAnimated, 0,
3875 addAnimated.size(), true);
3876 }
Winson Chungc58497e2013-09-03 17:48:37 -07003877
Winson Chung87412982013-10-03 18:34:14 -07003878 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003879 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003880
Winson Chungb745afb2015-03-02 11:51:23 -08003881 if (addedApps != null && mAppsView != null) {
3882 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003883 }
Winson Chungd64d1762013-08-20 14:37:16 -07003884 }
3885
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003886 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 * Bind the items start-end from the list.
3888 *
3889 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003890 */
Winson Chung64359a52013-07-08 17:17:08 -07003891 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3892 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003893 Runnable r = new Runnable() {
3894 public void run() {
3895 bindItems(shortcuts, start, end, forceAnimateIcons);
3896 }
3897 };
3898 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003899 return;
3900 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003901
Winson Chungf0c6ae02012-03-21 16:10:31 -07003902 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003903 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3904 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003905 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003906 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003907 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003908 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003909 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003910
3911 // Short circuit if we are loading dock items for a configuration which has no dock
3912 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3913 mHotseat == null) {
3914 continue;
3915 }
3916
Joe Onorato9c1289c2009-08-17 11:03:03 -04003917 switch (item.itemType) {
3918 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3919 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003920 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003921 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003922
Winson Chung64359a52013-07-08 17:17:08 -07003923 /*
3924 * TODO: FIX collision case
3925 */
Winson Chungce376632013-07-11 16:12:41 -07003926 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3927 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3928 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003929 View v = cl.getChildAt(item.cellX, item.cellY);
3930 Object tag = v.getTag();
3931 String desc = "Collision while binding workspace item: " + item
3932 + ". Collides with " + tag;
3933 if (LauncherAppState.isDogfoodBuild()) {
3934 throw (new RuntimeException(desc));
3935 } else {
3936 Log.d(TAG, desc);
3937 }
Winson Chungce376632013-07-11 16:12:41 -07003938 }
Winson Chung64359a52013-07-08 17:17:08 -07003939 }
3940
Adam Cohendcd297f2013-06-18 13:13:40 -07003941 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3942 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003943 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003944 // Animate all the applications up now
3945 shortcut.setAlpha(0f);
3946 shortcut.setScaleX(0f);
3947 shortcut.setScaleY(0f);
3948 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003949 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003950 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003952 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003953 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003954 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003955 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003956 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3957 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003958 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003959 default:
3960 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003962 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003963
Winson Chung997a9232013-07-24 15:33:46 -07003964 if (animateIcons) {
3965 // Animate to the correct page
3966 if (newShortcutsScreenId > -1) {
3967 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003968 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003969 final Runnable startBounceAnimRunnable = new Runnable() {
3970 public void run() {
3971 anim.playTogether(bounceAnims);
3972 anim.start();
3973 }
3974 };
Winson Chung997a9232013-07-24 15:33:46 -07003975 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003976 // We post the animation slightly delayed to prevent slowdowns
3977 // when we are loading right after we return to launcher.
3978 mWorkspace.postDelayed(new Runnable() {
3979 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003980 if (mWorkspace != null) {
3981 mWorkspace.snapToPage(newScreenIndex);
3982 mWorkspace.postDelayed(startBounceAnimRunnable,
3983 NEW_APPS_ANIMATION_DELAY);
3984 }
Winson Chung94d67682013-09-25 16:29:40 -07003985 }
3986 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003987 } else {
3988 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003989 }
3990 }
Winson Chung64359a52013-07-08 17:17:08 -07003991 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003992 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003993 }
3994
Joe Onorato9c1289c2009-08-17 11:03:03 -04003995 /**
3996 * Implementation of the method from LauncherModel.Callbacks.
3997 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003998 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003999 Runnable r = new Runnable() {
4000 public void run() {
4001 bindFolders(folders);
4002 }
4003 };
4004 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004005 return;
4006 }
Sunny Goyale2df0622015-04-24 11:27:00 -07004007 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004008 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004009
4010 /**
4011 * Add the views for a widget to the workspace.
4012 *
4013 * Implementation of the method from LauncherModel.Callbacks.
4014 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004015 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004016 Runnable r = new Runnable() {
4017 public void run() {
4018 bindAppWidget(item);
4019 }
4020 };
4021 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004022 return;
4023 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004024
Daniel Sandler843e8602010-06-07 14:59:01 -04004025 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4026 if (DEBUG_WIDGETS) {
4027 Log.d(TAG, "bindAppWidget: " + item);
4028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004029 final Workspace workspace = mWorkspace;
4030
Adam Cohen59400422014-03-05 18:07:04 -08004031 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00004032 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08004033
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004034 if (!mIsSafeModeEnabled
4035 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4036 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004037 if (appWidgetInfo == null) {
4038 if (DEBUG_WIDGETS) {
4039 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4040 + " belongs to component " + item.providerName
4041 + ", as the povider is null");
4042 }
4043 LauncherModel.deleteItemFromDatabase(this, item);
4044 return;
4045 }
4046 // Note: This assumes that the id remap broadcast is received before this step.
4047 // If that is not the case, the id remap will be ignored and user may see the
4048 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004049 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004050 pendingInfo.spanX = item.spanX;
4051 pendingInfo.spanY = item.spanY;
4052 pendingInfo.minSpanX = item.minSpanX;
4053 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004054 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004055 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004056
Sunny Goyalff572272014-07-23 13:58:07 -07004057 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004058 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4059 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004060
4061 // TODO consider showing a permission dialog when the widget is clicked.
4062 if (!success) {
4063 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4064 if (DEBUG_WIDGETS) {
4065 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4066 + " belongs to component " + item.providerName
4067 + ", as the launcher is unable to bing a new widget id");
4068 }
4069 LauncherModel.deleteItemFromDatabase(this, item);
4070 return;
4071 }
4072
4073 item.appWidgetId = newWidgetId;
4074
4075 // If the widget has a configure activity, it is still needs to set it up, otherwise
4076 // the widget is ready to go.
4077 item.restoreStatus = (appWidgetInfo.configure == null)
4078 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4079 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4080
4081 LauncherModel.updateItemInDatabase(this, item);
4082 }
4083
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004084 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004085 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004086 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004087 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4088 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004089 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004090
Sunny Goyal651077b2014-06-30 14:15:31 -07004091 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4092 } else {
4093 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004094 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4095 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004096 view.updateIcon(mIconCache);
4097 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004098 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004099 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004100 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004101
Joe Onorato9c1289c2009-08-17 11:03:03 -04004102 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004103 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004104
Adam Cohendcd297f2013-06-18 13:13:40 -07004105 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004106 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004107 if (!item.isCustomWidget()) {
4108 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4109 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004110
Joe Onorato9c1289c2009-08-17 11:03:03 -04004111 workspace.requestLayout();
4112
Daniel Sandler843e8602010-06-07 14:59:01 -04004113 if (DEBUG_WIDGETS) {
4114 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4115 + (SystemClock.uptimeMillis()-start) + "ms");
4116 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004117 }
4118
Sunny Goyalff572272014-07-23 13:58:07 -07004119 /**
4120 * Restores a pending widget.
4121 *
4122 * @param appWidgetId The app widget id
4123 * @param cellInfo The position on screen where to create the widget.
4124 */
4125 private void completeRestoreAppWidget(final int appWidgetId) {
4126 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4127 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4128 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4129 return;
4130 }
4131
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004132 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004133 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4134
4135 mWorkspace.reinflateWidgetsIfNecessary();
4136 LauncherModel.updateItemInDatabase(this, info);
4137 }
4138
Adam Cohen1462de32012-07-24 22:34:36 -07004139 public void onPageBoundSynchronously(int page) {
4140 mSynchronouslyBoundPages.add(page);
4141 }
4142
Joe Onorato9c1289c2009-08-17 11:03:03 -04004143 /**
4144 * Callback saying that there aren't any more items to bind.
4145 *
4146 * Implementation of the method from LauncherModel.Callbacks.
4147 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004148 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004149 Runnable r = new Runnable() {
4150 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004151 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004152 }
4153 };
4154 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004155 return;
4156 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004157 if (mSavedState != null) {
4158 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004159 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004160 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004161 mSavedState = null;
4162 }
4163
Adam Cohen1462de32012-07-24 22:34:36 -07004164 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004165
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004166 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004167 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004168
4169 // If we received the result of any pending adds while the loader was running (e.g. the
4170 // widget configuration forced an orientation change), process them now.
4171 if (sPendingAddItem != null) {
4172 final long screenId = completeAdd(sPendingAddItem);
4173
4174 // TODO: this moves the user to the page where the pending item was added. Ideally,
4175 // the screen would be guaranteed to exist after bind, and the page would be set through
4176 // the workspace restore process.
4177 mWorkspace.post(new Runnable() {
4178 @Override
4179 public void run() {
4180 mWorkspace.snapToScreenId(screenId);
4181 }
4182 });
4183 sPendingAddItem = null;
4184 }
4185
Sunny Goyal756adbc2015-04-16 15:20:51 -07004186 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004187
4188 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004189 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004190 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004191 }
4192
Adam Cohen3ed316a2014-07-23 18:21:20 -07004193 private void sendLoadingCompleteBroadcastIfNecessary() {
4194 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4195 String permission =
4196 getResources().getString(R.string.receive_first_load_broadcast_permission);
4197 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4198 sendBroadcast(intent, permission);
4199 SharedPreferences.Editor editor = mSharedPrefs.edit();
4200 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4201 editor.apply();
4202 }
4203 }
4204
Winson Chunga0b7e862013-09-05 16:03:15 -07004205 public boolean isAllAppsButtonRank(int rank) {
4206 if (mHotseat != null) {
4207 return mHotseat.isAllAppsButtonRank(rank);
4208 }
4209 return false;
4210 }
4211
Winson Chunga2413752012-04-03 14:22:34 -07004212 private boolean canRunNewAppsAnimation() {
4213 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4214 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4215 }
4216
Winson Chungc9168342013-06-26 14:54:55 -07004217 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4218 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4219 PropertyValuesHolder.ofFloat("alpha", 1f),
4220 PropertyValuesHolder.ofFloat("scaleX", 1f),
4221 PropertyValuesHolder.ofFloat("scaleY", 1f));
4222 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4223 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4224 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4225 return bounceAnim;
4226 }
4227
Adam Cohen27772732013-11-11 14:00:56 +00004228 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004229 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004230 }
4231
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004232 protected Rect getSearchBarBounds() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004233 return mDeviceProfile.getSearchBarBounds();
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004234 }
4235
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004236 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004237 if (mSearchDropTargetBar == null) {
4238 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004239 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004240 if (mQsb != null) {
4241 mSearchDropTargetBar.removeView(mQsb);
4242 mQsb = null;
4243 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004244 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004245 }
4246
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004247 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004248 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4249 * multiple calls to bind the same list.)
4250 */
4251 @Thunk ArrayList<AppInfo> mTmpAppsList;
4252 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4253 public void run() {
4254 bindAllApplications(mTmpAppsList);
4255 mTmpAppsList = null;
4256 }
4257 };
4258
4259 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004260 * Add the icons for all apps.
4261 *
4262 * Implementation of the method from LauncherModel.Callbacks.
4263 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004264 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004265 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4266 mTmpAppsList = apps;
4267 return;
4268 }
4269
Winson Chungb745afb2015-03-02 11:51:23 -08004270 if (mAppsView != null) {
4271 mAppsView.setApps(apps);
4272 }
Adam Cohen9211d422014-10-07 18:14:53 -07004273 if (mLauncherCallbacks != null) {
4274 mLauncherCallbacks.bindAllApplications(apps);
4275 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004276 }
4277
4278 /**
4279 * A package was updated.
4280 *
4281 * Implementation of the method from LauncherModel.Callbacks.
4282 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004283 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004284 Runnable r = new Runnable() {
4285 public void run() {
4286 bindAppsUpdated(apps);
4287 }
4288 };
4289 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004290 return;
4291 }
4292
Winson Chungb745afb2015-03-02 11:51:23 -08004293 if (mAppsView != null) {
4294 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004295 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004296 }
4297
Sunny Goyal4390ace2014-10-13 11:33:11 -07004298 @Override
4299 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4300 Runnable r = new Runnable() {
4301 public void run() {
4302 bindWidgetsRestored(widgets);
4303 }
4304 };
4305 if (waitUntilResume(r)) {
4306 return;
4307 }
4308 mWorkspace.widgetsRestored(widgets);
4309 }
4310
Joe Onorato9c1289c2009-08-17 11:03:03 -04004311 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004312 * Some shortcuts were updated in the background.
4313 *
4314 * Implementation of the method from LauncherModel.Callbacks.
4315 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004316 @Override
4317 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4318 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004319 Runnable r = new Runnable() {
4320 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004321 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004322 }
4323 };
4324 if (waitUntilResume(r)) {
4325 return;
4326 }
4327
Sunny Goyal4390ace2014-10-13 11:33:11 -07004328 if (!updated.isEmpty()) {
4329 mWorkspace.updateShortcuts(updated);
4330 }
4331
4332 if (!removed.isEmpty()) {
4333 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4334 for (ShortcutInfo si : removed) {
4335 removedComponents.add(si.getTargetComponent());
4336 }
4337 mWorkspace.removeItemsByComponentName(removedComponents, user);
4338 // Notify the drag controller
4339 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004340 }
4341 }
4342
4343 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004344 * Update the state of a package, typically related to install state.
4345 *
4346 * Implementation of the method from LauncherModel.Callbacks.
4347 */
Sunny Goyale755d462014-07-22 13:48:29 -07004348 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004349 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4350 Runnable r = new Runnable() {
4351 public void run() {
4352 bindRestoreItemsChange(updates);
4353 }
4354 };
4355 if (waitUntilResume(r)) {
4356 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004357 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004358
Sunny Goyal756adbc2015-04-16 15:20:51 -07004359 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004360 }
4361
4362 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004363 * A package was uninstalled. We take both the super set of packageNames
4364 * in addition to specific applications to remove, the reason being that
4365 * this can be called when a package is updated as well. In that scenario,
4366 * we only remove specific components from the workspace, where as
4367 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004368 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004369 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004370 * Implementation of the method from LauncherModel.Callbacks.
4371 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004372 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004373 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004374 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004375 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004376 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004377 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004378 }
Winson Chungd64d1762013-08-20 14:37:16 -07004379 };
4380 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004381 return;
4382 }
4383
Sunny Goyal1a745e82014-10-02 15:58:31 -07004384 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004385 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4386 for (AppInfo info : appInfos) {
4387 removedComponents.add(info.componentName);
4388 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004389 if (!packageNames.isEmpty()) {
4390 mWorkspace.removeItemsByPackageName(packageNames, user);
4391 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004392 if (!removedComponents.isEmpty()) {
4393 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004394 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004395 // Notify the drag controller
4396 mDragController.onAppsRemoved(packageNames, removedComponents);
4397
Sunny Goyal1a745e82014-10-02 15:58:31 -07004398 } else {
4399 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004400 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004401
Winson Chungdf95eb12013-10-16 14:57:07 -07004402 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004403 if (mAppsView != null) {
4404 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004405 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004406 }
Joe Onoratobe386092009-11-17 17:32:16 -08004407
Adam Cohen091440a2015-03-18 14:16:05 -07004408 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004409 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004410 public void run() {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004411 bindAllPackages(mWidgetsAndShortcuts);
Winson Chung83892cc2013-05-01 16:53:33 -07004412 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004413 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004414
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004415 @Override
4416 public void bindAllPackages(final ArrayList<Object> widgetsAndShortcuts) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004417 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4418 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004419 return;
4420 }
4421
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004422 if (mWidgetsView != null && widgetsAndShortcuts != null) {
4423 mWidgetsView.addWidgets(widgetsAndShortcuts, getPackageManager());
4424 mWidgetsAndShortcuts = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004425 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004426 }
4427
Winson Chung400438b2011-01-16 17:53:48 -08004428 private int mapConfigurationOriActivityInfoOri(int configOri) {
4429 final Display d = getWindowManager().getDefaultDisplay();
4430 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4431 switch (d.getRotation()) {
4432 case Surface.ROTATION_0:
4433 case Surface.ROTATION_180:
4434 // We are currently in the same basic orientation as the natural orientation
4435 naturalOri = configOri;
4436 break;
4437 case Surface.ROTATION_90:
4438 case Surface.ROTATION_270:
4439 // We are currently in the other basic orientation to the natural orientation
4440 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4441 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4442 break;
4443 }
4444
4445 int[] oriMap = {
4446 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4447 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4448 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4449 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4450 };
4451 // Since the map starts at portrait, we need to offset if this device's natural orientation
4452 // is landscape.
4453 int indexOffset = 0;
4454 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4455 indexOffset = 1;
4456 }
4457 return oriMap[(d.getRotation() + indexOffset) % 4];
4458 }
Adam Cohen446e9402011-09-15 18:21:21 -07004459
Winson Chung4b919f82012-05-01 10:44:08 -07004460 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004461 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004462 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4463 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4464 .getConfiguration().orientation));
4465 } else {
4466 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4467 }
Winson Chung4b919f82012-05-01 10:44:08 -07004468 }
4469 }
4470 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004471 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004472 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004473 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004474 } else {
4475 mHandler.postDelayed(new Runnable() {
4476 public void run() {
4477 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4478 }
4479 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004480 }
Winson Chung4b919f82012-05-01 10:44:08 -07004481 }
Winson Chung400438b2011-01-16 17:53:48 -08004482 }
4483
Winson Chunge43a1e72014-01-15 10:33:02 -08004484 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004485 if (mLauncherCallbacks != null) {
4486 return mLauncherCallbacks.isLauncherPreinstalled();
4487 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004488 PackageManager pm = getPackageManager();
4489 try {
4490 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4491 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4492 return true;
4493 } else {
4494 return false;
4495 }
4496 } catch (NameNotFoundException e) {
4497 e.printStackTrace();
4498 return false;
4499 }
4500 }
4501
Adam Cohenea90f832014-05-21 15:03:34 -07004502 /**
4503 * This method indicates whether or not we should suggest default wallpaper dimensions
4504 * when our wallpaper cropper was not yet used to set a wallpaper.
4505 */
4506 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004507 if (mLauncherCallbacks != null) {
4508 return mLauncherCallbacks.overrideWallpaperDimensions();
4509 }
Adam Cohenea90f832014-05-21 15:03:34 -07004510 return true;
4511 }
4512
Adam Cohen432609a2014-03-13 17:03:22 -07004513 /**
4514 * To be overridden by subclasses to indicate that there is an activity to launch
4515 * before showing the standard launcher experience.
4516 */
4517 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004518 if (mLauncherCallbacks != null) {
4519 return mLauncherCallbacks.hasFirstRunActivity();
4520 }
Adam Cohen432609a2014-03-13 17:03:22 -07004521 return false;
4522 }
4523
4524 /**
4525 * To be overridden by subclasses to launch any first run activity
4526 */
4527 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004528 if (mLauncherCallbacks != null) {
4529 return mLauncherCallbacks.getFirstRunActivity();
4530 }
Adam Cohen432609a2014-03-13 17:03:22 -07004531 return null;
4532 }
4533
Winson Chung2826a402015-04-17 16:18:53 -07004534 /**
4535 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004536 */
4537 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004538 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4539 return false;
4540 }
4541
Winson Chung2826a402015-04-17 16:18:53 -07004542 if (mLauncherCallbacks != null) {
4543 return mLauncherCallbacks.overrideAllAppsSearch();
4544 }
4545 return false;
4546 }
4547
Winson Chunga6945242014-01-08 14:04:34 -08004548 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004549 return !ActivityManager.isRunningInTestHarness() &&
4550 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004551 }
4552
Adam Cohen4a9423d2014-03-28 14:22:31 -07004553 protected boolean hasRunFirstRunActivity() {
4554 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4555 }
4556
Adam Cohen432609a2014-03-13 17:03:22 -07004557 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004558 if (shouldRunFirstRunActivity() &&
4559 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004560 Intent firstRunIntent = getFirstRunActivity();
4561 if (firstRunIntent != null) {
4562 startActivity(firstRunIntent);
4563 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004564 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004565 }
4566 }
Adam Cohen432609a2014-03-13 17:03:22 -07004567 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004568 }
4569
4570 private void markFirstRunActivityShown() {
4571 SharedPreferences.Editor editor = mSharedPrefs.edit();
4572 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4573 editor.apply();
4574 }
4575
Adam Cohen432609a2014-03-13 17:03:22 -07004576 /**
4577 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4578 * screen that must be displayed and dismissed.
4579 */
4580 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004581 if (mLauncherCallbacks != null) {
4582 return mLauncherCallbacks.hasDismissableIntroScreen();
4583 }
Adam Cohen432609a2014-03-13 17:03:22 -07004584 return false;
4585 }
4586
4587 /**
4588 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4589 */
4590 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004591 if (mLauncherCallbacks != null) {
4592 return mLauncherCallbacks.getIntroScreen();
4593 }
Adam Cohen432609a2014-03-13 17:03:22 -07004594 return null;
4595 }
4596
4597 /**
4598 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4599 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4600 */
4601 private boolean shouldShowIntroScreen() {
4602 return hasDismissableIntroScreen() &&
4603 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4604 }
4605
4606 protected void showIntroScreen() {
4607 View introScreen = getIntroScreen();
4608 changeWallpaperVisiblity(false);
4609 if (introScreen != null) {
4610 mDragLayer.showOverlayView(introScreen);
4611 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004612 if (mLauncherOverlayContainer != null) {
4613 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4614 }
Adam Cohen432609a2014-03-13 17:03:22 -07004615 }
4616
4617 public void dismissIntroScreen() {
4618 markIntroScreenDismissed();
4619 if (showFirstRunActivity()) {
4620 // We delay hiding the intro view until the first run activity is showing. This
4621 // avoids a blip.
4622 mWorkspace.postDelayed(new Runnable() {
4623 @Override
4624 public void run() {
4625 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004626 if (mLauncherOverlayContainer != null) {
4627 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4628 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004629 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004630 }
4631 }, ACTIVITY_START_DELAY);
4632 } else {
4633 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004634 if (mLauncherOverlayContainer != null) {
4635 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4636 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004637 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004638 }
4639 changeWallpaperVisiblity(true);
4640 }
4641
4642 private void markIntroScreenDismissed() {
4643 SharedPreferences.Editor editor = mSharedPrefs.edit();
4644 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4645 editor.apply();
4646 }
4647
Adam Cohen091440a2015-03-18 14:16:05 -07004648 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004649 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4650 // on the device, then we always show the first run cling experience (or if there is no
4651 // launcher2). Otherwise, we prompt the user upon started for migration
4652 LauncherClings launcherClings = new LauncherClings(this);
4653 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4654 if (mModel.canMigrateFromOldLauncherDb(this)) {
4655 launcherClings.showMigrationCling();
4656 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004657 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004658 }
4659 }
4660 }
4661
Winson Chunga6945242014-01-08 14:04:34 -08004662 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004663 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4664 if (mHotseat != null) mHotseat.setAlpha(1f);
4665 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4666 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004667 }
4668
4669 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004670 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4671 if (mHotseat != null) mHotseat.setAlpha(0f);
4672 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4673 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004674 }
4675
Mathew Inwood72fbec12013-11-19 15:45:07 +00004676 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004677 // Called from search suggestion, not supported in other profiles.
4678 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4679 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4680 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4681 myUser);
4682 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004683 return null;
4684 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004685 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004686 }
4687
4688 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4689 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004690 // Called from search suggestion, not supported in other profiles.
4691 return createShortcutDragInfo(shortcutIntent, caption, icon,
4692 UserHandleCompat.myUserHandle());
4693 }
4694
4695 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4696 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004697 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004698 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004699 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004700 }
4701
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004702 protected void moveWorkspaceToDefaultScreen() {
4703 mWorkspace.moveToDefaultScreen(false);
4704 }
4705
Mathew Inwood72fbec12013-11-19 15:45:07 +00004706 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4707 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004708 mWorkspace.onExternalDragStartedWithItem(dragView);
4709 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004710 }
4711
Anjali Koppalf5d29132014-02-28 13:33:27 -08004712 @Override
4713 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004714 if (mLauncherCallbacks != null) {
4715 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4716 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004717 }
4718
Winson Chung80baf5a2010-08-09 16:03:15 -07004719 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004720 * Prints out out state for debugging.
4721 */
4722 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004723 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004724 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004725 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4726 Log.d(TAG, "mRestoring=" + mRestoring);
4727 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4728 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004729 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004730 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004731 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004732
Daniel Sandler325dc232013-06-05 22:57:57 -04004733 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004734 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004735
4736 @Override
4737 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4738 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004739 synchronized (sDumpLogs) {
4740 writer.println(" ");
4741 writer.println("Debug logs: ");
4742 for (int i = 0; i < sDumpLogs.size(); i++) {
4743 writer.println(" " + sDumpLogs.get(i));
4744 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004745 }
Adam Cohen9211d422014-10-07 18:14:53 -07004746 if (mLauncherCallbacks != null) {
4747 mLauncherCallbacks.dump(prefix, fd, writer, args);
4748 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004749 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004750
4751 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004752 if (DEBUG_DUMP_LOG) {
4753 synchronized (sDumpLogs) {
4754 Log.d(TAG, "");
4755 Log.d(TAG, "*********************");
4756 Log.d(TAG, "Launcher debug logs: ");
4757 for (int i = 0; i < sDumpLogs.size(); i++) {
4758 Log.d(TAG, " " + sDumpLogs.get(i));
4759 }
4760 Log.d(TAG, "*********************");
4761 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004762 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004763 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004764 }
4765
4766 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004767 addDumpLog(tag, log, null, debugLog);
4768 }
4769
4770 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004771 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004772 if (e != null) {
4773 Log.d(tag, log, e);
4774 } else {
4775 Log.d(tag, log);
4776 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004777 }
Winson Chungede41292013-09-19 16:27:36 -07004778 if (DEBUG_DUMP_LOG) {
4779 sDateStamp.setTime(System.currentTimeMillis());
4780 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004781 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4782 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004783 }
Winson Chungede41292013-09-19 16:27:36 -07004784 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004785 }
4786
Adam Cohen59400422014-03-05 18:07:04 -08004787 public static CustomAppWidget getCustomAppWidget(String name) {
4788 return sCustomAppWidgets.get(name);
4789 }
4790
4791 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4792 return sCustomAppWidgets;
4793 }
4794
Winson Chungede41292013-09-19 16:27:36 -07004795 public void dumpLogsToLocalData() {
4796 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004797 new AsyncTask<Void, Void, Void>() {
4798 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004799 boolean success = false;
4800 sDateStamp.setTime(sRunStart);
4801 String FILENAME = sDateStamp.getMonth() + "-"
4802 + sDateStamp.getDay() + "_"
4803 + sDateStamp.getHours() + "-"
4804 + sDateStamp.getMinutes() + "_"
4805 + sDateStamp.getSeconds() + ".txt";
4806
4807 FileOutputStream fos = null;
4808 File outFile = null;
4809 try {
4810 outFile = new File(getFilesDir(), FILENAME);
4811 outFile.createNewFile();
4812 fos = new FileOutputStream(outFile);
4813 } catch (Exception e) {
4814 e.printStackTrace();
4815 }
4816 if (fos != null) {
4817 PrintWriter writer = new PrintWriter(fos);
4818
4819 writer.println(" ");
4820 writer.println("Debug logs: ");
4821 synchronized (sDumpLogs) {
4822 for (int i = 0; i < sDumpLogs.size(); i++) {
4823 writer.println(" " + sDumpLogs.get(i));
4824 }
4825 }
4826 writer.close();
4827 }
4828 try {
4829 if (fos != null) {
4830 fos.close();
4831 success = true;
4832 }
4833 } catch (IOException e) {
4834 e.printStackTrace();
4835 }
Michael Jurka43467462013-11-14 12:03:58 +01004836 return null;
Winson Chungede41292013-09-19 16:27:36 -07004837 }
Michael Jurka43467462013-11-14 12:03:58 +01004838 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004839 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004841}
Michael Jurka2763be32011-02-24 11:19:57 -08004842
Michael Jurkaabded662011-03-04 12:06:57 -08004843interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004844 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004845 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004846 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004847 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004848 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004849}
Dan Sandlerd5024042014-01-09 15:01:33 -05004850
4851interface DebugIntents {
4852 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4853 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004854}