blob: 11b1e9aa5f9611c738381e89cf56c081313b7229 [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 Sandler6053b8002013-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
Michael Jurkaddd62e92011-02-16 17:49:14 -0800351 private BubbleTextView mWaitingForResume;
352
Adam Cohen59400422014-03-05 18:07:04 -0800353 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
354 new HashMap<String, CustomAppWidget>();
355
356 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
357 static {
358 if (ENABLE_CUSTOM_WIDGET_TEST) {
359 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
360 }
361 }
362
Chet Haasea8f996d2015-02-17 12:56:04 -0800363 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
364 private static Method sClipRevealMethod = null;
365 static {
366 Class<?> activityOptionsClass = ActivityOptions.class;
367 try {
368 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
369 View.class, int.class, int.class, int.class, int.class);
370 } catch (Exception e) {
371 // Earlier version
372 }
373 }
374
Adam Cohen091440a2015-03-18 14:16:05 -0700375 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700376 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700377 if (mWorkspace != null) {
378 mWorkspace.buildPageHardwareLayers();
379 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700380 }
381 };
382
Adam Cohendb364c32014-05-20 14:23:40 -0700383 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800384
Adam Cohen091440a2015-03-18 14:16:05 -0700385 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800386 int requestCode;
387 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700388 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700389 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800390 int cellX;
391 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700392 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800393 }
394
Daniel Sandlerff02d492013-08-05 02:12:05 -0400395 private Stats mStats;
396
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700397 FocusIndicatorView mFocusHandler;
398
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 @Override
400 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700401 if (DEBUG_STRICT_MODE) {
402 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
403 .detectDiskReads()
404 .detectDiskWrites()
405 .detectNetwork() // or .detectAll() for all detectable problems
406 .penaltyLog()
407 .build());
408 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
409 .detectLeakedSqlLiteObjects()
410 .detectLeakedClosableObjects()
411 .penaltyLog()
412 .penaltyDeath()
413 .build());
414 }
415
Adam Cohen9211d422014-10-07 18:14:53 -0700416 if (mLauncherCallbacks != null) {
417 mLauncherCallbacks.preOnCreate();
418 }
419
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400421
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400422 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400423 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700424 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700425
Winson Chung5f8afe62013-08-12 16:19:28 -0700426 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700427 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700428
429 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400430 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700431 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700432 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800433 mModel = app.setLauncher(this);
434 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700435 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400436 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800438 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700439
Daniel Sandlerff02d492013-08-05 02:12:05 -0400440 mStats = new Stats(this);
441
Sunny Goyalffe83f12014-08-14 17:39:34 -0700442 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700443
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700444 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
445 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700446
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700447 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
448 // this also ensures that any synchronous binding below doesn't re-trigger another
449 // LauncherModel load.
450 mPaused = false;
451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200453 android.os.Debug.startMethodTracing(
454 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456
457 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100461 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800463 registerContentObservers();
464
Joe Onorato7c312c12009-08-13 21:36:53 -0700465 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700466
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 mSavedState = savedInstanceState;
468 restoreState(mSavedState);
469
470 if (PROFILE_STARTUP) {
471 android.os.Debug.stopMethodTracing();
472 }
473
474 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700475 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 // If the user leaves launcher, then we should just load items asynchronously when
477 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500478 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700479 } else {
480 // We only load the page synchronously if the user rotates (or triggers a
481 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800482 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700483 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 }
485
486 // For handling default keys
487 mDefaultKeySsb = new SpannableStringBuilder();
488 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800489
490 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
491 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800492
Adam Cohenf9426d52012-06-04 17:26:21 -0700493 // On large interfaces, we want the screen to auto-rotate based on the current orientation
494 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700495
Adam Cohen9211d422014-10-07 18:14:53 -0700496 if (mLauncherCallbacks != null) {
497 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700498 if (mLauncherCallbacks.hasLauncherOverlay()) {
499 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700500 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
501 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
502 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700503 mWorkspace.setLauncherOverlay(mLauncherOverlay);
504 }
Adam Cohen9211d422014-10-07 18:14:53 -0700505 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700506
507 if (shouldShowIntroScreen()) {
508 showIntroScreen();
509 } else {
510 showFirstRunActivity();
511 showFirstRunClings();
512 }
Adam Cohen9211d422014-10-07 18:14:53 -0700513 }
514
515 private LauncherCallbacks mLauncherCallbacks;
516
517 public void onPostCreate(Bundle savedInstanceState) {
518 super.onPostCreate(savedInstanceState);
519 if (mLauncherCallbacks != null) {
520 mLauncherCallbacks.onPostCreate(savedInstanceState);
521 }
522 }
523
524 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
525 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700526 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
527 @Override
528 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700529 if (LOGD) {
530 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
531 }
Winson Chung94804152015-05-08 13:06:44 -0700532 mAppsView.setFixedBounds(bounds);
533 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700534 }
535
536 @Override
537 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700538 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
539 // Dismiss All Apps if we aren't already paused/invisible
540 if (!mPaused) {
541 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
542 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
543 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700544 }
Winson Chung0f785722015-04-08 10:27:49 -0700545 }
546 });
Adam Cohen9211d422014-10-07 18:14:53 -0700547 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700548 }
549
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700550 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700551 public void onLauncherProviderChange() {
552 if (mLauncherCallbacks != null) {
553 mLauncherCallbacks.onLauncherProviderChange();
554 }
555 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700556
Adam Cohen9211d422014-10-07 18:14:53 -0700557 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700558 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700559 if (mLauncherCallbacks != null) {
560 return mLauncherCallbacks.hasCustomContentToLeft();
561 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700562 return false;
563 }
564
Dave Hawkeya8881582013-09-17 15:55:33 -0600565 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500566 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600567 * {@link #addToCustomContentPage}. This will only be invoked if
568 * {@link #hasCustomContentToLeft()} is {@code true}.
569 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500570 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700571 if (mLauncherCallbacks != null) {
572 mLauncherCallbacks.populateCustomContentContainer();
573 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600574 }
575
576 /**
577 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
578 * ensure the custom content page is added or removed if necessary.
579 */
580 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600581 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600582 // Not bound yet, wait for bindScreens to be called.
583 return;
584 }
585
586 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
587 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500588 mWorkspace.createCustomContentContainer();
589 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600590 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
591 mWorkspace.removeCustomContentPage();
592 }
593 }
594
Adam Cohen091440a2015-03-18 14:16:05 -0700595 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700596 if (sLocaleConfiguration == null) {
597 new AsyncTask<Void, Void, LocaleConfiguration>() {
598 @Override
599 protected LocaleConfiguration doInBackground(Void... unused) {
600 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
601 readConfiguration(Launcher.this, localeConfiguration);
602 return localeConfiguration;
603 }
604
605 @Override
606 protected void onPostExecute(LocaleConfiguration result) {
607 sLocaleConfiguration = result;
608 checkForLocaleChange(); // recursive, but now with a locale configuration
609 }
610 }.execute();
611 return;
612 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700613
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 final Configuration configuration = getResources().getConfiguration();
615
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 final String locale = configuration.locale.toString();
618
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700619 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 final int mcc = configuration.mcc;
621
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 final int mnc = configuration.mnc;
624
Romain Guy84f296c2009-11-04 15:00:44 -0800625 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626
Romain Guy84f296c2009-11-04 15:00:44 -0800627 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700628 sLocaleConfiguration.locale = locale;
629 sLocaleConfiguration.mcc = mcc;
630 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700631
Joe Onorato0589f0f2010-02-08 13:44:00 -0800632 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700633
634 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100635 new AsyncTask<Void, Void, Void>() {
636 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700637 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100638 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700639 }
Michael Jurka43467462013-11-14 12:03:58 +0100640 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700641 }
642 }
643
Adam Cohen091440a2015-03-18 14:16:05 -0700644 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700645 public String locale;
646 public int mcc = -1;
647 public int mnc = -1;
648 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700649
Adam Cohen091440a2015-03-18 14:16:05 -0700650 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700651 DataInputStream in = null;
652 try {
Helena Josol28db2802014-10-09 17:04:09 +0100653 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700654 configuration.locale = in.readUTF();
655 configuration.mcc = in.readInt();
656 configuration.mnc = in.readInt();
657 } catch (FileNotFoundException e) {
658 // Ignore
659 } catch (IOException e) {
660 // Ignore
661 } finally {
662 if (in != null) {
663 try {
664 in.close();
665 } catch (IOException e) {
666 // Ignore
667 }
668 }
669 }
670 }
671
Adam Cohen091440a2015-03-18 14:16:05 -0700672 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700673 DataOutputStream out = null;
674 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100675 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100676 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700677 out.writeUTF(configuration.locale);
678 out.writeInt(configuration.mcc);
679 out.writeInt(configuration.mnc);
680 out.flush();
681 } catch (FileNotFoundException e) {
682 // Ignore
683 } catch (IOException e) {
684 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100685 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700686 } finally {
687 if (out != null) {
688 try {
689 out.close();
690 } catch (IOException e) {
691 // Ignore
692 }
693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
695 }
696
Anton Hanssona2f665f2013-09-26 12:18:32 +0100697 public Stats getStats() {
698 return mStats;
699 }
700
Bjorn Bringertc459e522013-06-07 19:36:01 +0100701 public LayoutInflater getInflater() {
702 return mInflater;
703 }
704
Hyunyoung Song3f471442015-04-08 19:01:34 -0700705 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700706 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
707 // that is subsequently removed from the workspace in startBinding().
708 return !mModel.isLoadingWorkspace();
709 }
710
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800711 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000712 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100713 if (Build.VERSION.SDK_INT >= 17) {
714 return View.generateViewId();
715 } else {
716 // View.generateViewId() is not available. The following fallback logic is a copy
717 // of its implementation.
718 for (;;) {
719 final int result = sNextGeneratedId.get();
720 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
721 int newValue = result + 1;
722 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
723 if (sNextGeneratedId.compareAndSet(result, newValue)) {
724 return result;
725 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000726 }
727 }
728 }
729
730 public int getViewIdForItem(ItemInfo info) {
731 // This cast is safe given the > 2B range for int.
732 int itemId = (int) info.id;
733 if (mItemIdToViewId.containsKey(itemId)) {
734 return mItemIdToViewId.get(itemId);
735 }
736 int viewId = generateViewId();
737 mItemIdToViewId.put(itemId, viewId);
738 return viewId;
739 }
740
741 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700742 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
743 * a configuration step, this allows the proper animations to run after other transitions.
744 */
Adam Cohendb364c32014-05-20 14:23:40 -0700745 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700746 long screenId = args.screenId;
747 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
748 // When the screen id represents an actual screen (as opposed to a rank) we make sure
749 // that the drop page actually exists.
750 screenId = ensurePendingDropLayoutExists(args.screenId);
751 }
Adam Cohendb364c32014-05-20 14:23:40 -0700752
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800753 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800754 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700755 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700756 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700757 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800758 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700759 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700760 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700761 case REQUEST_RECONFIGURE_APPWIDGET:
762 completeRestoreAppWidget(args.appWidgetId);
763 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800764 }
Michael Jurka27614d22012-04-02 06:40:22 -0700765 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
766 // if you turned the screen off and then back while in All Apps, Launcher would not
767 // return to the workspace. Clearing mAddInfo.container here fixes this issue
768 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700769 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800770 }
771
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800772 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700774 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700775 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700776 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800777 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700778
Adam Cohenad4e15c2013-10-17 16:21:35 -0700779 Runnable exitSpringLoaded = new Runnable() {
780 @Override
781 public void run() {
782 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
783 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
784 }
785 };
786
Michael Jurka8b805b12012-04-18 14:23:14 -0700787 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700788 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
790 if (resultCode == RESULT_CANCELED) {
791 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700792 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700793 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700794 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800795 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700796 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700797 }
798 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200799 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
800 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700801 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200802 }
803 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700804 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200805
Adam Cohened66b2b2012-01-23 17:28:51 -0800806 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
807 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700808
Adam Cohendb364c32014-05-20 14:23:40 -0700809 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800810 // We have special handling for widgets
811 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800812 final int appWidgetId;
813 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
814 : -1;
815 if (widgetId < 0) {
816 appWidgetId = pendingAddWidgetId;
817 } else {
818 appWidgetId = widgetId;
819 }
820
Adam Cohenad4e15c2013-10-17 16:21:35 -0700821 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800822 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700823 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
824 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700825 result = RESULT_CANCELED;
826 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700827 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700828 @Override
829 public void run() {
830 exitSpringLoadedDragModeDelayed(false, 0, null);
831 }
832 };
Adam Cohendb364c32014-05-20 14:23:40 -0700833 if (workspaceLocked) {
834 // No need to remove the empty screen if we're mid-binding, as the
835 // the bind will not add the empty screen.
836 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
837 } else {
838 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
839 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
840 }
Winson Chung5aaab772012-04-27 15:24:02 -0700841 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700842 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700843 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
844 // When the screen id represents an actual screen (as opposed to a rank)
845 // we make sure that the drop page actually exists.
846 mPendingAddInfo.screenId =
847 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
848 }
Adam Cohendb364c32014-05-20 14:23:40 -0700849 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
850
851 dropLayout.setDropPending(true);
852 final Runnable onComplete = new Runnable() {
853 @Override
854 public void run() {
855 completeTwoStageWidgetDrop(resultCode, appWidgetId);
856 dropLayout.setDropPending(false);
857 }
858 };
859 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
860 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
861 } else {
862 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
863 mPendingAddInfo);
864 sPendingAddItem = args;
865 }
Winson Chung5aaab772012-04-27 15:24:02 -0700866 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800867 return;
868 }
869
Sunny Goyalff572272014-07-23 13:58:07 -0700870 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
871 if (resultCode == RESULT_OK) {
872 // Update the widget view.
873 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
874 pendingAddWidgetId, mPendingAddInfo);
875 if (workspaceLocked) {
876 sPendingAddItem = args;
877 } else {
878 completeAdd(args);
879 }
880 }
881 // Leave the widget in the pending state if the user canceled the configure.
882 return;
883 }
884
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 // The pattern used here is that a user PICKs a specific application,
886 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700887
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
889 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700890 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700891 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
892 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800893 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700894 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800895 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700896 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700897 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
898 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 }
Adam Cohen00074722013-10-11 17:46:09 -0700900 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700901 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700902 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800904 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800905
906 }
907
908 @Override
909 protected void onActivityResult(
910 final int requestCode, final int resultCode, final Intent data) {
911 handleActivityResult(requestCode, resultCode, data);
912 if (mLauncherCallbacks != null) {
913 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
914 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800915 }
916
Adam Cohendb364c32014-05-20 14:23:40 -0700917 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
918 appWidgetId, ItemInfo info) {
919 PendingAddArguments args = new PendingAddArguments();
920 args.requestCode = requestCode;
921 args.intent = data;
922 args.container = info.container;
923 args.screenId = info.screenId;
924 args.cellX = info.cellX;
925 args.cellY = info.cellY;
926 args.appWidgetId = appWidgetId;
927 return args;
928 }
929
930 /**
931 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
932 *
933 * @param screenId the screen id to check
934 * @return the new screen, or screenId if it exists
935 */
936 private long ensurePendingDropLayoutExists(long screenId) {
937 CellLayout dropLayout =
938 (CellLayout) mWorkspace.getScreenWithId(screenId);
939 if (dropLayout == null) {
940 // it's possible that the add screen was removed because it was
941 // empty and a re-bind occurred
942 mWorkspace.addExtraEmptyScreen();
943 return mWorkspace.commitExtraEmptyScreen();
944 } else {
945 return screenId;
946 }
947 }
948
Adam Cohen091440a2015-03-18 14:16:05 -0700949 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700950 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700951 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800952 Runnable onCompleteRunnable = null;
953 int animationType = 0;
954
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700955 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800956 if (resultCode == RESULT_OK) {
957 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
958 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700959 mPendingAddWidgetInfo);
960 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800961 onCompleteRunnable = new Runnable() {
962 @Override
963 public void run() {
964 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700965 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700966 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
967 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800968 }
969 };
970 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800971 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800972 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800973 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700974 if (mDragLayer.getAnimatedView() != null) {
975 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
976 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
977 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700978 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700979 // The animated view may be null in the case of a rotation during widget configuration
980 onCompleteRunnable.run();
981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 }
983
984 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700985 protected void onStop() {
986 super.onStop();
987 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700988
989 if (mLauncherCallbacks != null) {
990 mLauncherCallbacks.onStop();
991 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700992 }
993
994 @Override
995 protected void onStart() {
996 super.onStart();
997 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700998
999 if (mLauncherCallbacks != null) {
1000 mLauncherCallbacks.onStart();
1001 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001002 }
1003
1004 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001006 long startTime = 0;
1007 if (DEBUG_RESUME_TIME) {
1008 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001009 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001010 }
Adam Cohen9211d422014-10-07 18:14:53 -07001011
1012 if (mLauncherCallbacks != null) {
1013 mLauncherCallbacks.preOnResume();
1014 }
1015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001017
Winson Chung4a2afa32012-07-19 14:53:05 -07001018 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001019 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001020 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001021 } else if (mOnResumeState == State.APPS) {
Winson Chung4ac30062015-05-08 17:34:17 -07001022 // Don't update the predicted apps if the user is returning to launcher in the apps
1023 // view as they may be depending on the UI to be static to switch to another app
1024 showAppsView(false /* animated */, false /* resetListToTop */,
1025 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001026 } else if (mOnResumeState == State.WIDGETS) {
1027 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001028 }
1029 mOnResumeState = State.NONE;
1030
Winson Chungcd99cd32015-04-29 11:03:24 -07001031 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001032 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1033 // Otherwise, notify the callbacks if we are in all apps mode
1034 if (mState == State.APPS) {
1035 if (mLauncherCallbacks != null) {
1036 mLauncherCallbacks.onAllAppsShown();
1037 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001038 }
1039 }
1040
Craig Mautner360310b2012-10-26 15:13:08 -07001041 // Background was set to gradient in onPause(), restore to black if in all apps.
1042 setWorkspaceBackground(mState == State.WORKSPACE);
1043
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001044 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001045 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001046 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001047 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001049 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001051 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001052 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1053 // execute them here
1054 long startTimeCallbacks = 0;
1055 if (DEBUG_RESUME_TIME) {
1056 startTimeCallbacks = System.currentTimeMillis();
1057 }
1058
Michael Jurka1e2f4652013-07-08 18:03:46 -07001059 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1060 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001061 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001062 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001063 if (DEBUG_RESUME_TIME) {
1064 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1065 (System.currentTimeMillis() - startTimeCallbacks));
1066 }
Michael Jurka447bf842013-05-15 14:52:15 +02001067 }
Michael Jurka54554252013-08-01 12:52:23 +02001068 if (mOnResumeCallbacks.size() > 0) {
1069 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1070 mOnResumeCallbacks.get(i).run();
1071 }
1072 mOnResumeCallbacks.clear();
1073 }
Winson Chunge4e50662012-01-23 14:45:13 -08001074
1075 // Reset the pressed state of icons that were locked in the press state while activities
1076 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001077 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001078 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001079 mWaitingForResume.setStayPressed(false);
1080 }
Winson Chung82963d52013-10-09 11:20:57 -07001081
Adam Cohen06dff352012-06-01 17:17:08 -07001082 // It is possible that widgets can receive updates while launcher is not in the foreground.
1083 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1084 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1085 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001086 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001087 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001088
Michael Jurka447bf842013-05-15 14:52:15 +02001089 if (DEBUG_RESUME_TIME) {
1090 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1091 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001092
1093 if (mWorkspace.getCustomContentCallbacks() != null) {
1094 // If we are resuming and the custom content is the current page, we call onShow().
1095 // It is also poassible that onShow will instead be called slightly after first layout
1096 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1097 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001098 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001099 }
1100 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001101 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001102 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001103
Sunny Goyal756adbc2015-04-16 15:20:51 -07001104 if (!isWorkspaceLoading()) {
1105 // Process any items that were added while Launcher was away.
1106 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1107 }
Adam Cohen9211d422014-10-07 18:14:53 -07001108
1109 if (mLauncherCallbacks != null) {
1110 mLauncherCallbacks.onResume();
1111 }
Adam Cohen06dff352012-06-01 17:17:08 -07001112 }
1113
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001115 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001116 // Ensure that items added to Launcher are queued until Launcher returns
1117 InstallShortcutReceiver.enableInstallQueue();
1118
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001119 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001120 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001121 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001122 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001123
1124 // We call onHide() aggressively. The custom content callbacks should be able to
1125 // debounce excess onHide calls.
1126 if (mWorkspace.getCustomContentCallbacks() != null) {
1127 mWorkspace.getCustomContentCallbacks().onHide();
1128 }
Romain Guycbb89e42009-06-08 15:52:54 -07001129
Adam Cohen9211d422014-10-07 18:14:53 -07001130 if (mLauncherCallbacks != null) {
1131 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001132 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001133 }
1134
1135 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001136 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1137 // by a onResume or by scrolling otherwise.
1138 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001139
1140 // Custom content is completely hidden
1141 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001142
1143 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1144 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001145
1146 // Indicates whether the user is allowed to scroll away from the custom content.
1147 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001148 }
1149
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 public interface LauncherOverlay {
1151
1152 /**
1153 * Touch interaction leading to overscroll has begun
1154 */
1155 public void onScrollInteractionBegin();
1156
1157 /**
1158 * Touch interaction related to overscroll has ended
1159 */
1160 public void onScrollInteractionEnd();
1161
1162 /**
1163 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1164 * screen (or in the case of RTL, the rightmost screen).
1165 */
1166 public void onScrollChange(int progress, boolean rtl);
1167
1168 /**
1169 * Screen has stopped scrolling
1170 */
1171 public void onScrollSettled();
1172
1173 /**
1174 * This method can be called by the Launcher in order to force the LauncherOverlay
1175 * to exit fully immersive mode.
1176 */
1177 public void forceExitFullImmersion();
1178 }
1179
Winson Chung0f785722015-04-08 10:27:49 -07001180 public interface LauncherAppsCallbacks {
1181 /**
1182 * Updates launcher to the available space that AllApps can take so as not to overlap with
1183 * any other views.
1184 */
1185 public void onAllAppsBoundsChanged(Rect bounds);
1186
1187 /**
1188 * Called to dismiss all apps if it is showing.
1189 */
1190 public void dismissAllApps();
1191 }
1192
Adam Cohenc2d6e892014-10-16 09:49:24 -07001193 public interface LauncherOverlayCallbacks {
1194 /**
1195 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1196 * however it doesn't modify any state within the launcher.
1197 */
1198 public boolean canEnterFullImmersion();
1199
1200 /**
1201 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1202 * eg. by occupying the full screen and handling all touch events.
1203 *
1204 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1205 * case, Launcher will modify any necessary state and assumes the overlay is
1206 * handling all interaction. If false, the LauncherOverlay should cancel any
1207 *
1208 */
1209 public boolean enterFullImmersion();
1210
1211 /**
1212 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1213 * full control over UI and state.
1214 */
1215 public void exitFullImmersion();
1216 }
1217
1218 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1219
1220 @Override
1221 public boolean canEnterFullImmersion() {
1222 return mState == State.WORKSPACE;
1223 }
1224
1225 @Override
1226 public boolean enterFullImmersion() {
1227 if (mState == State.WORKSPACE) {
1228 // When fully immersed, disregard any touches which fall through.
1229 mDragLayer.setBlockTouch(true);
1230 return true;
1231 }
1232 return false;
1233 }
1234
1235 @Override
1236 public void exitFullImmersion() {
1237 mDragLayer.setBlockTouch(false);
1238 }
1239 }
1240
Jorim Jaggid017f882014-01-14 17:08:48 -08001241 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001242 if (mLauncherCallbacks != null) {
1243 return mLauncherCallbacks.hasSettings();
1244 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001245 return false;
1246 }
1247
Adam Cohen9211d422014-10-07 18:14:53 -07001248 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001249 CustomContentCallbacks callbacks, String description) {
1250 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001251 }
1252
Adam Cohenedb40762013-07-18 16:45:45 -07001253 // The custom content needs to offset its content to account for the QSB
1254 public int getTopOffsetForCustomContent() {
1255 return mWorkspace.getPaddingTop();
1256 }
1257
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001258 @Override
1259 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001260 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001261 if (mModel.isCurrentCallbacks(this)) {
1262 mModel.stopLoader();
1263 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001264 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1265
Romain Guy13c2e7b2010-03-10 19:45:00 -08001266 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001267 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001268
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001269 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001270 @Override
1271 public void onWindowFocusChanged(boolean hasFocus) {
1272 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001273 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001274
1275 if (mLauncherCallbacks != null) {
1276 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1277 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001278 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 private boolean acceptFilter() {
1281 final InputMethodManager inputManager = (InputMethodManager)
1282 getSystemService(Context.INPUT_METHOD_SERVICE);
1283 return !inputManager.isFullscreenMode();
1284 }
1285
1286 @Override
1287 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001288 final int uniChar = event.getUnicodeChar();
1289 final boolean handled = super.onKeyDown(keyCode, event);
1290 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1291 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1293 keyCode, event);
1294 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001295 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001296 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001297 // showSearchDialog()
1298 // If there are multiple keystrokes before the search dialog takes focus,
1299 // onSearchRequested() will be called for every keystroke,
1300 // but it is idempotent, so it's fine.
1301 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
1303 }
1304
Joe Onorato8a9625e2010-01-28 15:55:35 -08001305 // Eat the long press event so the keyboard doesn't come up.
1306 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1307 return true;
1308 }
1309
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 return handled;
1311 }
1312
Karl Rosaen138a0412009-04-23 19:00:21 -07001313 private String getTypedText() {
1314 return mDefaultKeySsb.toString();
1315 }
1316
1317 private void clearTypedText() {
1318 mDefaultKeySsb.clear();
1319 mDefaultKeySsb.clearSpans();
1320 Selection.setSelection(mDefaultKeySsb, 0);
1321 }
1322
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001324 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1325 * State
1326 */
1327 private static State intToState(int stateOrdinal) {
1328 State state = State.WORKSPACE;
1329 final State[] stateValues = State.values();
1330 for (int i = 0; i < stateValues.length; i++) {
1331 if (stateValues[i].ordinal() == stateOrdinal) {
1332 state = stateValues[i];
1333 break;
1334 }
1335 }
1336 return state;
1337 }
1338
1339 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 * Restores the previous state, if it exists.
1341 *
1342 * @param savedState The previous state.
1343 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001344 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 private void restoreState(Bundle savedState) {
1346 if (savedState == null) {
1347 return;
1348 }
1349
Michael Jurka883f55b2010-10-21 15:47:14 -07001350 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001351 if (state == State.APPS || state == State.WIDGETS) {
1352 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001353 }
1354
Adam Cohen21cd0022013-10-09 18:57:02 -07001355 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1356 PagedView.INVALID_RESTORE_PAGE);
1357 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001358 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360
Winson Chung3d503fb2011-07-13 17:25:49 -07001361 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001362 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001363
Winson Chung3d503fb2011-07-13 17:25:49 -07001364 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1365 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001366 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001367 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1368 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001369 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1370 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001371 AppWidgetProviderInfo info = savedState.getParcelable(
1372 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1373 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001374 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001375 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 mRestoring = true;
1377 }
1378
1379 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1380 if (renameFolder) {
1381 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001382 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 mRestoring = true;
1384 }
Winson Chunga12a2502010-12-20 14:41:35 -08001385
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001386 mItemIdToViewId = (HashMap<Integer, Integer>)
1387 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
1389
1390 /**
1391 * Finds all the views we need and configure them properly.
1392 */
1393 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001394 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001395
Craig Mautner360310b2012-10-26 15:13:08 -07001396 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001397 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001398 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1399 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001400 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001401 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001402
John Spurlock77e1f472013-09-11 10:09:51 -04001403 mLauncherView.setSystemUiVisibility(
1404 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001405 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406
Winson Chung4c98d922011-05-31 16:50:48 -07001407 // Setup the drag layer
1408 mDragLayer.setup(this, dragController);
1409
Winson Chung3d503fb2011-07-13 17:25:49 -07001410 // Setup the hotseat
1411 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1412 if (mHotseat != null) {
1413 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001414 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001415 }
1416
Jorim Jaggid017f882014-01-14 17:08:48 -08001417 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001418 View widgetButton = findViewById(R.id.widget_button);
1419 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001420 @Override
1421 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001422 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001423 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001424 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001425 }
1426 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001427 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1428
1429 View wallpaperButton = findViewById(R.id.wallpaper_button);
1430 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001431 @Override
1432 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001433 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001434 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001435 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001436 }
1437 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001438 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1439
1440 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001441 if (hasSettings()) {
1442 settingsButton.setOnClickListener(new OnClickListener() {
1443 @Override
1444 public void onClick(View arg0) {
1445 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001446 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001447 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001448 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001449 });
1450 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1451 } else {
1452 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001453 }
1454
Adam Cohendbdff6b2013-09-18 19:09:15 -07001455 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001456
Winson Chung4c98d922011-05-31 16:50:48 -07001457 // Setup the workspace
1458 mWorkspace.setHapticFeedbackEnabled(false);
1459 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001460 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001461 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001462
Winson Chungf0ea4d32011-06-06 14:27:16 -07001463 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001464 mSearchDropTargetBar = (SearchDropTargetBar)
1465 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001466
Winson Chungb745afb2015-03-02 11:51:23 -08001467 // Setup Apps
1468 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001469 if (isAllAppsSearchOverridden()) {
1470 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001471 }
Winson Chungb745afb2015-03-02 11:51:23 -08001472
Winson Chungf0ea4d32011-06-06 14:27:16 -07001473 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001474 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001475
Winson Chung3d503fb2011-07-13 17:25:49 -07001476 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001477 dragController.setDragScoller(mWorkspace);
1478 dragController.setScrollView(mDragLayer);
1479 dragController.setMoveTarget(mWorkspace);
1480 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001481 if (mSearchDropTargetBar != null) {
1482 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001483 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001484 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001485
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001486 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001487 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001488 mWeightWatcher = new WeightWatcher(this);
1489 mWeightWatcher.setAlpha(0.5f);
1490 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001491 new FrameLayout.LayoutParams(
1492 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001493 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001494 Gravity.BOTTOM)
1495 );
Adam Cohen39a06042013-07-19 14:30:12 -07001496
1497 boolean show = shouldShowWeightWatcher();
1498 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001499 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 }
1501
1502 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001503 * Sets the all apps button. This method is called from {@link Hotseat}.
1504 */
1505 public void setAllAppsButton(View allAppsButton) {
1506 mAllAppsButton = allAppsButton;
1507 }
1508
1509 public View getAllAppsButton() {
1510 return mAllAppsButton;
1511 }
1512
1513 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 * Creates a view representing a shortcut.
1515 *
1516 * @param info The data structure describing the shortcut.
1517 *
1518 * @return A View inflated from R.layout.application.
1519 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001520 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001522 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
1524
1525 /**
1526 * Creates a view representing a shortcut inflated from the specified resource.
1527 *
1528 * @param layoutResId The id of the XML layout used to create the shortcut.
1529 * @param parent The group the shortcut belongs to.
1530 * @param info The data structure describing the shortcut.
1531 *
1532 * @return A View inflated from layoutResId.
1533 */
Adam Cohen59400422014-03-05 18:07:04 -08001534 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001535 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001536 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001538 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 return favorite;
1540 }
1541
1542 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 * Add a shortcut to the workspace.
1544 *
1545 * @param data The intent describing the shortcut.
1546 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001549 int cellY) {
1550 int[] cellXY = mTmpAddItemCellCoordinates;
1551 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001552 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001553
Michael Jurkad3ef3062010-11-23 16:23:58 -08001554 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001555
Sunny Goyal2350bc92014-10-14 16:42:54 -07001556 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001557 if (info == null) {
1558 return;
1559 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001560 final View view = createShortcut(info);
1561
Adam Cohenfbba09b2011-07-18 21:43:05 -07001562 // First we check if we already know the exact location where we want to add this item.
1563 if (cellX >= 0 && cellY >= 0) {
1564 cellXY[0] = cellX;
1565 cellXY[1] = cellY;
1566 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001567
1568 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001569 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001570 true, null,null)) {
1571 return;
1572 }
1573 DragObject dragObject = new DragObject();
1574 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001575 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1576 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001577 return;
1578 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001579 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001580 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001581 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001582 foundCellSpan = (result != null);
1583 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001584 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001585 }
1586
1587 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001588 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001589 return;
1590 }
1591
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001592 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593
1594 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001595 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001596 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
1598 }
1599
Adam Cohen2f093b62012-04-30 18:59:53 -07001600 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1601 int minHeight) {
1602 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001603 // We want to account for the extra amount of padding that we are adding to the widget
1604 // to ensure that it gets the full amount of space that it has requested
1605 int requiredWidth = minWidth + padding.left + padding.right;
1606 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001607 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001608 }
1609
Adam Cohen2f093b62012-04-30 18:59:53 -07001610 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1611 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001612 }
1613
Adam Cohen2f093b62012-04-30 18:59:53 -07001614 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1615 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001616 }
1617
Adam Cohen2f093b62012-04-30 18:59:53 -07001618 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1619 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001620 }
1621
Adam Cohen2f093b62012-04-30 18:59:53 -07001622 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1623 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001624 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001625 }
1626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001628 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001630 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 */
Adam Cohen091440a2015-03-18 14:16:05 -07001632 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001633 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1634
1635 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001636 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001637 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1638 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001639 }
Romain Guycbb89e42009-06-08 15:52:54 -07001640
Adam Cohen59400422014-03-05 18:07:04 -08001641 if (appWidgetInfo.isCustomWidget) {
1642 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001643 }
1644
Adam Cohen59400422014-03-05 18:07:04 -08001645 LauncherAppWidgetInfo launcherInfo;
1646 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1647 launcherInfo.spanX = info.spanX;
1648 launcherInfo.spanY = info.spanY;
1649 launcherInfo.minSpanX = info.minSpanX;
1650 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001651 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001654 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655
1656 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001657 if (hostView == null) {
1658 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001659 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1660 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001661 } else {
1662 // The AppWidgetHostView has already been inflated and instantiated
1663 launcherInfo.hostView = hostView;
1664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001666 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001667 launcherInfo.notifyWidgetSizeChanged(this);
1668
Adam Cohen59400422014-03-05 18:07:04 -08001669 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1670 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001671
1672 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001674 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 }
Romain Guycbb89e42009-06-08 15:52:54 -07001676
Adam Cohended9f8d2010-11-03 13:25:16 -07001677 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1678 @Override
1679 public void onReceive(Context context, Intent intent) {
1680 final String action = intent.getAction();
1681 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1682 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001683 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001684 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001685
Winson Chungc100e8e2011-08-09 16:02:43 -07001686 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001687 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001688 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001689 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001690 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001691 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001692 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1693 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001694 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001695 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1696 mModel.resetLoadedState(false, true);
1697 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1698 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1699 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1700 mModel.resetLoadedState(false, true);
1701 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1702 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1703 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 }
1705 }
1706 };
1707
1708 @Override
1709 public void onAttachedToWindow() {
1710 super.onAttachedToWindow();
1711
1712 // Listen for broadcasts related to user-presence
1713 final IntentFilter filter = new IntentFilter();
1714 filter.addAction(Intent.ACTION_SCREEN_OFF);
1715 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001716 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001717 if (ENABLE_DEBUG_INTENTS) {
1718 filter.addAction(DebugIntents.DELETE_DATABASE);
1719 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1720 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001722 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001723 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001724 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 mVisible = true;
1726 }
1727
Adam Cohen0b395352014-06-09 22:54:36 +00001728 /**
1729 * Sets up transparent navigation and status bars in LMP.
1730 * This method is a no-op for other platform versions.
1731 */
Sunny Goyald0091012015-01-20 15:55:34 -08001732 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001733 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001734 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001735 Window window = getWindow();
1736 window.getAttributes().systemUiVisibility |=
1737 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1738 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1739 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1740 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1741 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1742 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1743 window.setStatusBarColor(Color.TRANSPARENT);
1744 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001745 }
1746 }
1747
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 @Override
1749 public void onDetachedFromWindow() {
1750 super.onDetachedFromWindow();
1751 mVisible = false;
1752
Adam Cohend113e0c2010-11-11 10:48:05 -08001753 if (mAttached) {
1754 unregisterReceiver(mReceiver);
1755 mAttached = false;
1756 }
Winson Chungb745afb2015-03-02 11:51:23 -08001757 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 }
1759
1760 public void onWindowVisibilityChanged(int visibility) {
1761 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001762 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001763 // The following code used to be in onResume, but it turns out onResume is called when
1764 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1765 // is a more appropriate event to handle
1766 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001767 if (!mWorkspaceLoading) {
1768 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001769 // We want to let Launcher draw itself at least once before we force it to build
1770 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001771 // apps is nice and speedy.
1772 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001773 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001774 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001775 if (mStarted) return;
1776 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001777 // We delay the layer building a bit in order to give
1778 // other message processing a time to run. In particular
1779 // this avoids a delay in hiding the IME if it was
1780 // currently shown, because doing that may involve
1781 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001782 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001783 final ViewTreeObserver.OnDrawListener listener = this;
1784 mWorkspace.post(new Runnable() {
1785 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001786 if (mWorkspace != null &&
1787 mWorkspace.getViewTreeObserver() != null) {
1788 mWorkspace.getViewTreeObserver().
1789 removeOnDrawListener(listener);
1790 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001791 }
1792 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001793 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001794 }
1795 });
1796 }
1797 clearTypedText();
1798 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001799 }
1800
Adam Cohen091440a2015-03-18 14:16:05 -07001801 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001802 mHandler.removeMessages(ADVANCE_MSG);
1803 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1804 mHandler.sendMessageDelayed(msg, delay);
1805 mAutoAdvanceSentTime = System.currentTimeMillis();
1806 }
1807
Adam Cohen091440a2015-03-18 14:16:05 -07001808 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001809 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1810 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1811 mAutoAdvanceRunning = autoAdvanceRunning;
1812 if (autoAdvanceRunning) {
1813 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1814 sendAdvanceMessage(delay);
1815 } else {
1816 if (!mWidgetsToAdvance.isEmpty()) {
1817 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1818 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1819 }
1820 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001821 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001822 }
1823 }
1824 }
1825
1826 private final Handler mHandler = new Handler() {
1827 @Override
1828 public void handleMessage(Message msg) {
1829 if (msg.what == ADVANCE_MSG) {
1830 int i = 0;
1831 for (View key: mWidgetsToAdvance.keySet()) {
1832 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1833 final int delay = mAdvanceStagger * i;
1834 if (v instanceof Advanceable) {
1835 postDelayed(new Runnable() {
1836 public void run() {
1837 ((Advanceable) v).advance();
1838 }
1839 }, delay);
1840 }
1841 i++;
1842 }
1843 sendAdvanceMessage(mAdvanceInterval);
1844 }
1845 }
1846 };
1847
1848 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001849 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001850 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1851 if (v instanceof Advanceable) {
1852 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001853 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001854 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001855 }
1856 }
1857
1858 void removeWidgetToAutoAdvance(View hostView) {
1859 if (mWidgetsToAdvance.containsKey(hostView)) {
1860 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001861 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001862 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001863 }
1864
Joe Onorato9c1289c2009-08-17 11:03:03 -04001865 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001866 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001867 launcherInfo.hostView = null;
1868 }
1869
Hyunyoung Song3f471442015-04-08 19:01:34 -07001870 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001871 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1872 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001873 }
1874
Winson Chunga6945242014-01-08 14:04:34 -08001875 public DragLayer getDragLayer() {
1876 return mDragLayer;
1877 }
1878
Winson Chungb745afb2015-03-02 11:51:23 -08001879 public AppsContainerView getAppsView() {
1880 return mAppsView;
1881 }
1882
Hyunyoung Song3f471442015-04-08 19:01:34 -07001883 public WidgetsContainerView getWidgetsView() {
1884 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001885 }
1886
Winson Chunga6945242014-01-08 14:04:34 -08001887 public Workspace getWorkspace() {
1888 return mWorkspace;
1889 }
1890
1891 public Hotseat getHotseat() {
1892 return mHotseat;
1893 }
1894
Jorim Jaggid017f882014-01-14 17:08:48 -08001895 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001896 return mOverviewPanel;
1897 }
1898
1899 public SearchDropTargetBar getSearchBar() {
1900 return mSearchDropTargetBar;
1901 }
1902
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001903 public LauncherAppWidgetHost getAppWidgetHost() {
1904 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 }
Romain Guycbb89e42009-06-08 15:52:54 -07001906
Winson Chunga9abd0e2010-10-27 17:18:37 -07001907 public LauncherModel getModel() {
1908 return mModel;
1909 }
1910
Winson Chunga6945242014-01-08 14:04:34 -08001911 protected SharedPreferences getSharedPrefs() {
1912 return mSharedPrefs;
1913 }
1914
Bjorn Bringertc459e522013-06-07 19:36:01 +01001915 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001916 getWindow().closeAllPanels();
1917
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001918 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001919 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001920 }
1921
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 @Override
1923 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001924 long startTime = 0;
1925 if (DEBUG_RESUME_TIME) {
1926 startTime = System.currentTimeMillis();
1927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 super.onNewIntent(intent);
1929
1930 // Close the menu
1931 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001932 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001933 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001934
Adam Cohened307df2013-10-02 09:37:31 -07001935 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1936 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1937 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001938
Adam Cohen6fecd412013-10-02 17:41:50 -07001939 if (mWorkspace == null) {
1940 // Can be cases where mWorkspace is null, this prevents a NPE
1941 return;
1942 }
1943 Folder openFolder = mWorkspace.getOpenFolder();
1944 // In all these cases, only animate if we're already on home
1945 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001946
1947 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1948 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001949 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001950 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001951 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001952 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001953
Adam Cohen6fecd412013-10-02 17:41:50 -07001954 closeFolder();
1955 exitSpringLoadedDragMode();
1956
1957 // If we are already on home, then just animate back to the workspace,
1958 // otherwise, just wait until onResume to set the state back to Workspace
1959 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001960 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001961 } else {
1962 mOnResumeState = State.WORKSPACE;
1963 }
1964
1965 final View v = getWindow().peekDecorView();
1966 if (v != null && v.getWindowToken() != null) {
1967 InputMethodManager imm = (InputMethodManager)getSystemService(
1968 INPUT_METHOD_SERVICE);
1969 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1970 }
1971
Winson Chungb745afb2015-03-02 11:51:23 -08001972 // Reset the apps view
1973 if (!alreadyOnHome && mAppsView != null) {
1974 mAppsView.scrollToTop();
1975 }
1976
Hyunyoung Song3f471442015-04-08 19:01:34 -07001977 // Reset the widgets view
1978 if (!alreadyOnHome && mWidgetsView != null) {
1979 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001980 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001981
Adam Cohen9211d422014-10-07 18:14:53 -07001982 if (mLauncherCallbacks != null) {
1983 mLauncherCallbacks.onHomeIntent();
1984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
Adam Cohened307df2013-10-02 09:37:31 -07001986
Michael Jurka447bf842013-05-15 14:52:15 +02001987 if (DEBUG_RESUME_TIME) {
1988 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990
Adam Cohen9211d422014-10-07 18:14:53 -07001991 if (mLauncherCallbacks != null) {
1992 mLauncherCallbacks.onNewIntent(intent);
1993 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001994 }
1995
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001997 public void onRestoreInstanceState(Bundle state) {
1998 super.onRestoreInstanceState(state);
1999 for (int page: mSynchronouslyBoundPages) {
2000 mWorkspace.restoreInstanceStateForChild(page);
2001 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 }
2003
2004 @Override
2005 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002006 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002007 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2008 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002009 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002010 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011
Michael Jurka883f55b2010-10-21 15:47:14 -07002012 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002013 // We close any open folder since it will not be re-opened, and we need to make sure
2014 // this state is reflected.
2015 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016
Adam Cohendcd297f2013-06-18 13:13:40 -07002017 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002018 mWaitingForResult) {
2019 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002020 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002021 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2022 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002023 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2024 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2025 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002026 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
2029 if (mFolderInfo != null && mWaitingForResult) {
2030 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2031 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2032 }
Michael Jurka946ad472010-07-09 18:05:18 -07002033
Hyunyoung Song3f471442015-04-08 19:01:34 -07002034 // Save the current widgets tray?
2035 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002036 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002037
2038 if (mLauncherCallbacks != null) {
2039 mLauncherCallbacks.onSaveInstanceState(outState);
2040 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 }
2042
2043 @Override
2044 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002046
Winson Chunge7a03942011-08-05 15:05:12 -07002047 // Remove all pending runnables
2048 mHandler.removeMessages(ADVANCE_MSG);
2049 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002050 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002051
Winson Chungcd2b0142011-06-08 16:02:26 -07002052 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002053 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002054
2055 // It's possible to receive onDestroy after a new Launcher activity has
2056 // been created. In this case, don't interfere with the new Launcher.
2057 if (mModel.isCurrentCallbacks(this)) {
2058 mModel.stopLoader();
2059 app.setLauncher(null);
2060 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002063 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002065 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002067 mAppWidgetHost = null;
2068
2069 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070
2071 TextKeyListener.getInstance().release();
2072
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002073 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002074 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002075
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002076 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002077 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002078 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002079 mWorkspace = null;
2080 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002081
Michael Jurka2ecf9952012-06-18 12:52:28 -07002082 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002083
2084 if (mLauncherCallbacks != null) {
2085 mLauncherCallbacks.onDestroy();
2086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088
Adam Cohena9cf38f2011-05-02 15:36:58 -07002089 public DragController getDragController() {
2090 return mDragController;
2091 }
2092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 @Override
2094 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002095 onStartForResult(requestCode);
2096 super.startActivityForResult(intent, requestCode);
2097 }
2098
2099 @Override
2100 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2101 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2102 onStartForResult(requestCode);
2103 try {
2104 super.startIntentSenderForResult(intent, requestCode,
2105 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2106 } catch (IntentSender.SendIntentException e) {
2107 throw new ActivityNotFoundException();
2108 }
2109 }
2110
2111 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002112 if (requestCode >= 0) {
2113 setWaitingForResult(true);
2114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 }
2116
Winson Chung70d72102011-08-12 11:24:35 -07002117 /**
2118 * Indicates that we want global search for this activity by setting the globalSearch
2119 * argument for {@link #startSearch} to true.
2120 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002122 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002124
Karl Rosaen138a0412009-04-23 19:00:21 -07002125 if (initialQuery == null) {
2126 // Use any text typed in the launcher as the initial query
2127 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 if (appSearchData == null) {
2130 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002131 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 }
Winson Chungadf0c182012-08-23 14:59:07 -07002133 Rect sourceBounds = new Rect();
2134 if (mSearchDropTargetBar != null) {
2135 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2136 }
Romain Guycbb89e42009-06-08 15:52:54 -07002137
Ian Parkinson047036e2014-09-01 15:40:53 +01002138 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002139 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002140 if (clearTextImmediately) {
2141 clearTypedText();
2142 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002143
2144 // We need to show the workspace after starting the search
2145 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002146 }
2147
Ian Parkinson047036e2014-09-01 15:40:53 +01002148 /**
2149 * Start a text search.
2150 *
2151 * @return {@code true} if the search will start immediately, so any further keypresses
2152 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2153 * to buffer keypresses.
2154 */
2155 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002156 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002157 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2158 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2159 sourceBounds);
2160 }
2161
Michael Jurkaa33411c2012-06-14 16:18:21 -07002162 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002163 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002164 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002165 }
2166
2167 /**
2168 * Starts the global search activity. This code is a copied from SearchManager
2169 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002170 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002171 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002172 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002173 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2174 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2175 if (globalSearchActivity == null) {
2176 Log.w(TAG, "No global search activity found.");
2177 return;
2178 }
2179 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2180 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2181 intent.setComponent(globalSearchActivity);
2182 // Make sure that we have a Bundle to put source in
2183 if (appSearchData == null) {
2184 appSearchData = new Bundle();
2185 } else {
2186 appSearchData = new Bundle(appSearchData);
2187 }
Adam Cohen9211d422014-10-07 18:14:53 -07002188 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002189 if (!appSearchData.containsKey("source")) {
2190 appSearchData.putString("source", getPackageName());
2191 }
2192 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2193 if (!TextUtils.isEmpty(initialQuery)) {
2194 intent.putExtra(SearchManager.QUERY, initialQuery);
2195 }
2196 if (selectInitialQuery) {
2197 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2198 }
2199 intent.setSourceBounds(sourceBounds);
2200 try {
2201 startActivity(intent);
2202 } catch (ActivityNotFoundException ex) {
2203 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2204 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 }
2206
Yura4f93ec62014-02-04 14:15:21 +00002207 public boolean isOnCustomContent() {
2208 return mWorkspace.isOnOrMovingToCustomContent();
2209 }
2210
Winson Chung70d72102011-08-12 11:24:35 -07002211 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002212 public boolean onPrepareOptionsMenu(Menu menu) {
2213 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002214 if (!isOnCustomContent()) {
2215 // Close any open folders
2216 closeFolder();
2217 // Stop resizing any widgets
2218 mWorkspace.exitWidgetResizeMode();
2219 if (!mWorkspace.isInOverviewMode()) {
2220 // Show the overview mode
2221 showOverviewMode(true);
2222 } else {
2223 showWorkspace(true);
2224 }
Winson Chunge0298742014-01-17 12:03:00 -08002225 }
Adam Cohen9211d422014-10-07 18:14:53 -07002226 if (mLauncherCallbacks != null) {
2227 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2228 }
2229
Michael Jurkab94f3f82013-09-11 18:08:54 +02002230 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002233 @Override
2234 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002235 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002236 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002237 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002238 }
2239
Joe Onorato9c1289c2009-08-17 11:03:03 -04002240 public boolean isWorkspaceLocked() {
2241 return mWorkspaceLoading || mWaitingForResult;
2242 }
2243
Adam Cohen517a7f52014-03-01 12:12:59 -08002244 public boolean isWorkspaceLoading() {
2245 return mWorkspaceLoading;
2246 }
2247
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002248 private void setWorkspaceLoading(boolean value) {
2249 boolean isLocked = isWorkspaceLocked();
2250 mWorkspaceLoading = value;
2251 if (isLocked != isWorkspaceLocked()) {
2252 onWorkspaceLockedChanged();
2253 }
2254 }
2255
2256 private void setWaitingForResult(boolean value) {
2257 boolean isLocked = isWorkspaceLocked();
2258 mWaitingForResult = value;
2259 if (isLocked != isWorkspaceLocked()) {
2260 onWorkspaceLockedChanged();
2261 }
2262 }
2263
Adam Cohen9211d422014-10-07 18:14:53 -07002264 protected void onWorkspaceLockedChanged() {
2265 if (mLauncherCallbacks != null) {
2266 mLauncherCallbacks.onWorkspaceLockedChanged();
2267 }
2268 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002269
Michael Jurka0280c3b2010-09-17 15:00:07 -07002270 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002271 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2274 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002275 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002276 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002277 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002278
Sunny Goyale6b63a32015-01-16 16:14:29 -08002279 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002280 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002281 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2282 }
2283
Sunny Goyale6b63a32015-01-16 16:14:29 -08002284 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002285 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2286 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002287 if (appWidgetInfo.configure != null) {
2288 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002289 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002290
Bjorn Bringert7984c942009-12-09 15:38:25 +00002291 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002292 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2293 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002296 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002297 Runnable onComplete = new Runnable() {
2298 @Override
2299 public void run() {
2300 // Exit spring loaded mode if necessary after adding the widget
2301 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2302 null);
2303 }
2304 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002305 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002306 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002307 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 }
2309 }
Romain Guycbb89e42009-06-08 15:52:54 -07002310
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002311 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002312 // Close any folders that may be open.
2313 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002314 mWorkspace.moveToCustomContentScreen(animate);
2315 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002316
2317 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2318 int[] cell, int spanX, int spanY) {
2319 switch (info.itemType) {
2320 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2321 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2322 int span[] = new int[2];
2323 span[0] = spanX;
2324 span[1] = spanY;
2325 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2326 container, screenId, cell, span);
2327 break;
2328 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2329 processShortcutFromDrop(info.componentName, container, screenId, cell);
2330 break;
2331 default:
2332 throw new IllegalStateException("Unknown item type: " + info.itemType);
2333 }
2334 }
2335
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 /**
2337 * Process a shortcut drop.
2338 *
2339 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002340 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002343 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2344 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002345 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002346 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002347 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002348 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002349
2350 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002351 mPendingAddInfo.cellX = cell[0];
2352 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002353 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002354
2355 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2356 createShortcutIntent.setComponent(componentName);
2357 processShortcut(createShortcutIntent);
2358 }
2359
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360 /**
2361 * Process a widget drop.
2362 *
2363 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002364 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002365 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002366 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002367 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2368 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002369 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002370 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002371 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002372 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002373 mPendingAddInfo.minSpanX = info.minSpanX;
2374 mPendingAddInfo.minSpanY = info.minSpanY;
2375
Adam Cohenfbba09b2011-07-18 21:43:05 -07002376 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002377 mPendingAddInfo.cellX = cell[0];
2378 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002379 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002380 if (span != null) {
2381 mPendingAddInfo.spanX = span[0];
2382 mPendingAddInfo.spanY = span[1];
2383 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002384
Adam Cohened66b2b2012-01-23 17:28:51 -08002385 AppWidgetHostView hostView = info.boundWidget;
2386 int appWidgetId;
2387 if (hostView != null) {
2388 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002389 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002390 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002391 // In this case, we either need to start an activity to get permission to bind
2392 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002393 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002394 Bundle options = info.bindOptions;
2395
Sunny Goyalffe83f12014-08-14 17:39:34 -07002396 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2397 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002398 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002399 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002400 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002401 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002402 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2403 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2404 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002405 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2406 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002407 // TODO: we need to make sure that this accounts for the options bundle.
2408 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002409 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2410 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002411 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002412 }
2413
Joe Onoratodeb98af2010-02-19 14:59:39 -08002414 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002415 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 }
2417
Winson Chung24ab2f12010-09-16 14:10:47 -07002418 void processWallpaper(Intent intent) {
2419 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2420 }
2421
Adam Cohendcd297f2013-06-18 13:13:40 -07002422 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002423 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002424 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 folderInfo.title = getText(R.string.folder_name);
2426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002428 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2429 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002430 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431
2432 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002433 FolderIcon newFolder =
2434 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002435 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002436 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002437 // Force measure the new folder icon
2438 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2439 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002440 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 }
Romain Guycbb89e42009-06-08 15:52:54 -07002442
Joe Onorato9c1289c2009-08-17 11:03:03 -04002443 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002444 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002445 }
2446
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002447 /**
2448 * Registers various content observers. The current implementation registers
2449 * only a favorites observer to keep track of the favorites applications.
2450 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002451 private void registerContentObservers() {
2452 ContentResolver resolver = getContentResolver();
2453 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2454 true, mWidgetObserver);
2455 }
2456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 @Override
2458 public boolean dispatchKeyEvent(KeyEvent event) {
2459 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2460 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002462 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002463 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002464 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002465 dumpState();
2466 return true;
2467 }
2468 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002469 }
2470 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2471 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002472 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 return true;
2474 }
2475 }
2476
2477 return super.dispatchKeyEvent(event);
2478 }
2479
Joe Onorato88ec0992009-11-19 13:16:06 -08002480 @Override
2481 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002482 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2483 return;
2484 }
2485
Winson Chung3d9490a2015-03-24 17:38:42 -07002486 LauncherAccessibilityDelegate delegate =
2487 LauncherAppState.getInstance().getAccessibilityDelegate();
2488 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002489 return;
2490 }
2491
Winson Chungb745afb2015-03-02 11:51:23 -08002492 if (isAppsViewVisible()) {
2493 showWorkspace(true);
2494 } else if (isWidgetsViewVisible()) {
2495 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002496 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002497 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002498 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002499 Folder openFolder = mWorkspace.getOpenFolder();
2500 if (openFolder.isEditingName()) {
2501 openFolder.dismissEditingName();
2502 } else {
2503 closeFolder();
2504 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002505 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002506 mWorkspace.exitWidgetResizeMode();
2507
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002508 // Back button is a no-op here, but give at least some feedback for the button press
2509 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002510 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002511 }
2512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002514 * Re-listen when widgets are reset.
2515 */
Adam Cohen091440a2015-03-18 14:16:05 -07002516 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002517 if (mAppWidgetHost != null) {
2518 mAppWidgetHost.startListening();
2519 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002520 }
2521
2522 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 * Launches the intent referred by the clicked shortcut.
2524 *
2525 * @param v The view representing the clicked shortcut.
2526 */
2527 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002528 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2529 // view has detached (it's possible for this to happen if the view is removed mid touch).
2530 if (v.getWindowToken() == null) {
2531 return;
2532 }
2533
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002534 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002535 return;
2536 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002537
Adam Cohen1697b792013-09-17 19:08:21 -07002538 if (v instanceof Workspace) {
2539 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002540 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002541 }
2542 return;
2543 }
2544
2545 if (v instanceof CellLayout) {
2546 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002547 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002548 }
2549 }
2550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002552 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002553 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002555 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002556 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002557 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002558 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002559 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002560 } else if (tag instanceof AppInfo) {
2561 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002562 } else if (tag instanceof LauncherAppWidgetInfo) {
2563 if (v instanceof PendingAppWidgetHostView) {
2564 onClickPendingWidget((PendingAppWidgetHostView) v);
2565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 }
2567 }
2568
Sunny Goyal508da152014-08-14 10:53:27 -07002569 public void onClickPagedViewIcon(View v) {
2570 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002571 if (mLauncherCallbacks != null) {
2572 mLauncherCallbacks.onClickPagedViewIcon(v);
2573 }
Sunny Goyal508da152014-08-14 10:53:27 -07002574 }
2575
Michael Jurka0e260592010-06-30 17:07:39 -07002576 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002577 return false;
2578 }
2579
Michael Jurkaaf442092010-06-10 17:01:57 -07002580 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002581 * Event handler for the app widget view which has not fully restored.
2582 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002583 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002584 if (mIsSafeModeEnabled) {
2585 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2586 return;
2587 }
2588
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002589 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002590 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002591 int widgetId = info.appWidgetId;
2592 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2593 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002594 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2595 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002596 mPendingAddInfo.copyFrom(info);
2597 mPendingAddWidgetId = widgetId;
2598
Sunny Goyalffe83f12014-08-14 17:39:34 -07002599 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2600 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002601 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002602 } else if (info.installProgress < 0) {
2603 // The install has not been queued
2604 final String packageName = info.providerName.getPackageName();
2605 showBrokenAppInstallDialog(packageName,
2606 new DialogInterface.OnClickListener() {
2607 public void onClick(DialogInterface dialog, int id) {
2608 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2609 }
2610 });
2611 } else {
2612 // Download has started.
2613 final String packageName = info.providerName.getPackageName();
2614 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002615 }
2616 }
2617
2618 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002619 * Event handler for the "grid" button that appears on the home screen, which
2620 * enters all apps mode.
2621 *
2622 * @param v The view that was clicked.
2623 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 protected void onClickAllAppsButton(View v) {
2625 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002626 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 showWorkspace(true);
2628 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002629 // Try and refresh the set of predicted apps before we enter launcher
2630 showAppsView(true /* animated */, false /* resetListToTop */,
2631 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002632 }
2633 }
2634
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002635 private void showBrokenAppInstallDialog(final String packageName,
2636 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002637 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002638 .setTitle(R.string.abandoned_promises_title)
2639 .setMessage(R.string.abandoned_promise_explanation)
2640 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2641 .setNeutralButton(R.string.abandoned_clean_this,
2642 new DialogInterface.OnClickListener() {
2643 public void onClick(DialogInterface dialog, int id) {
2644 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2645 mWorkspace.removeAbandonedPromise(packageName, user);
2646 }
2647 })
2648 .create().show();
2649 return;
2650 }
2651
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002652 /**
2653 * Event handler for an app shortcut click.
2654 *
2655 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2656 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002658 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2659 Object tag = v.getTag();
2660 if (!(tag instanceof ShortcutInfo)) {
2661 throw new IllegalArgumentException("Input must be a Shortcut");
2662 }
2663
2664 // Open shortcut
2665 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002666
2667 if (shortcut.isDisabled != 0) {
2668 int error = R.string.activity_not_available;
2669 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2670 error = R.string.safemode_shortcut_error;
2671 }
2672 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2673 return;
2674 }
2675
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002676 final Intent intent = shortcut.intent;
2677
2678 // Check for special shortcuts
2679 if (intent.getComponent() != null) {
2680 final String shortcutClass = intent.getComponent().getClassName();
2681
2682 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2683 MemoryDumpActivity.startDump(this);
2684 return;
2685 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2686 toggleShowWeightWatcher();
2687 return;
2688 }
2689 }
2690
Chris Wren40c5ed32014-06-24 18:24:23 -04002691 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002692 if ((v instanceof BubbleTextView)
2693 && shortcut.isPromise()
2694 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002695 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002696 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002697 new DialogInterface.OnClickListener() {
2698 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002699 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002700 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002701 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002702 return;
2703 }
2704
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002705 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002706 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002707
2708 if (mLauncherCallbacks != null) {
2709 mLauncherCallbacks.onClickAppShortcut(v);
2710 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002711 }
2712
Adam Cohen091440a2015-03-18 14:16:05 -07002713 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002714 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002715 final ShortcutInfo shortcut;
2716 final Intent intent;
2717 if (tag instanceof ShortcutInfo) {
2718 shortcut = (ShortcutInfo) tag;
2719 intent = shortcut.intent;
2720 int[] pos = new int[2];
2721 v.getLocationOnScreen(pos);
2722 intent.setSourceBounds(new Rect(pos[0], pos[1],
2723 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002724
Sunny Goyal508da152014-08-14 10:53:27 -07002725 } else if (tag instanceof AppInfo) {
2726 shortcut = null;
2727 intent = ((AppInfo) tag).intent;
2728 } else {
2729 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2730 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002731
2732 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 mStats.recordLaunch(intent, shortcut);
2734
2735 if (success && v instanceof BubbleTextView) {
2736 mWaitingForResume = (BubbleTextView) v;
2737 mWaitingForResume.setStayPressed(true);
2738 }
2739 }
2740
2741 /**
2742 * Event handler for a folder icon click.
2743 *
2744 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2745 */
2746 protected void onClickFolderIcon(View v) {
2747 if (LOGD) Log.d(TAG, "onClickFolder");
2748 if (!(v instanceof FolderIcon)){
2749 throw new IllegalArgumentException("Input must be a FolderIcon");
2750 }
2751
2752 FolderIcon folderIcon = (FolderIcon) v;
2753 final FolderInfo info = folderIcon.getFolderInfo();
2754 Folder openFolder = mWorkspace.getFolderForTag(info);
2755
2756 // If the folder info reports that the associated folder is open, then verify that
2757 // it is actually opened. There have been a few instances where this gets out of sync.
2758 if (info.opened && openFolder == null) {
2759 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2760 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2761 info.opened = false;
2762 }
2763
2764 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2765 // Close any open folder
2766 closeFolder();
2767 // Open the requested folder
2768 openFolder(folderIcon);
2769 } else {
2770 // Find the open folder...
2771 int folderScreen;
2772 if (openFolder != null) {
2773 folderScreen = mWorkspace.getPageForView(openFolder);
2774 // .. and close it
2775 closeFolder(openFolder);
2776 if (folderScreen != mWorkspace.getCurrentPage()) {
2777 // Close any folder open on the current screen
2778 closeFolder();
2779 // Pull the folder onto this screen
2780 openFolder(folderIcon);
2781 }
2782 }
2783 }
Adam Cohen9211d422014-10-07 18:14:53 -07002784
2785 if (mLauncherCallbacks != null) {
2786 mLauncherCallbacks.onClickFolderIcon(v);
2787 }
Michael Jurka5130e402011-10-13 04:55:35 -07002788 }
2789
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002790 /**
2791 * Event handler for the (Add) Widgets button that appears after a long press
2792 * on the home screen.
2793 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002794 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002795 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002796 if (mIsSafeModeEnabled) {
2797 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2798 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002799 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002800 if (mLauncherCallbacks != null) {
2801 mLauncherCallbacks.onClickAddWidgetButton(view);
2802 }
Adam Cohen9211d422014-10-07 18:14:53 -07002803 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002804 }
2805
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002806 /**
2807 * Event handler for the wallpaper picker button that appears after a long press
2808 * on the home screen.
2809 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002810 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002811 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002812 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2813 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002814
2815 if (mLauncherCallbacks != null) {
2816 mLauncherCallbacks.onClickWallpaperPicker(v);
2817 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002818 }
2819
2820 /**
2821 * Event handler for a click on the settings button that appears after a long press
2822 * on the home screen.
2823 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002824 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002825 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002826 if (mLauncherCallbacks != null) {
2827 mLauncherCallbacks.onClickSettingsButton(v);
2828 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002829 }
2830
Michael Jurka5130e402011-10-13 04:55:35 -07002831 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002832 // Provide the same haptic feedback that the system offers for virtual keys.
2833 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002834 }
2835
Adam Cohen61f560d2013-09-30 15:58:20 -07002836 public void performHapticFeedbackOnTouchDown(View v) {
2837 // Provide the same haptic feedback that the system offers for virtual keys.
2838 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2839 }
2840
2841 public View.OnTouchListener getHapticFeedbackTouchListener() {
2842 if (mHapticFeedbackTouchListener == null) {
2843 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2844 @Override
2845 public boolean onTouch(View v, MotionEvent event) {
2846 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2847 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2848 }
2849 return false;
2850 }
2851 };
2852 }
2853 return mHapticFeedbackTouchListener;
2854 }
2855
Adam Cohen9211d422014-10-07 18:14:53 -07002856 public void onDragStarted(View view) {
2857 if (isOnCustomContent()) {
2858 // Custom content screen doesn't participate in drag and drop. If on custom
2859 // content screen, move to default.
2860 moveWorkspaceToDefaultScreen();
2861 }
2862
2863 if (mLauncherCallbacks != null) {
2864 mLauncherCallbacks.onDragStarted(view);
2865 }
2866 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002867
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002868 /**
2869 * Called when the user stops interacting with the launcher.
2870 * This implies that the user is now on the homescreen and is not doing housekeeping.
2871 */
Adam Cohen9211d422014-10-07 18:14:53 -07002872 protected void onInteractionEnd() {
2873 if (mLauncherCallbacks != null) {
2874 mLauncherCallbacks.onInteractionEnd();
2875 }
2876 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002877
2878 /**
2879 * Called when the user starts interacting with the launcher.
2880 * The possible interactions are:
2881 * - open all apps
2882 * - reorder an app shortcut, or a widget
2883 * - open the overview mode.
2884 * This is a good time to stop doing things that only make sense
2885 * when the user is on the homescreen and not doing housekeeping.
2886 */
Adam Cohen9211d422014-10-07 18:14:53 -07002887 protected void onInteractionBegin() {
2888 if (mLauncherCallbacks != null) {
2889 mLauncherCallbacks.onInteractionBegin();
2890 }
2891 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002892
Winson Chungcd99cd32015-04-29 11:03:24 -07002893 /** Updates the interaction state. */
2894 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002895 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002896 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002897 boolean fromStateWithOverlay;
2898 boolean toStateWithOverlay;
2899 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2900 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2901 toStateWithOverlay = toState != Workspace.State.NORMAL;
2902 } else {
2903 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2904 fromState != Workspace.State.NORMAL_HIDDEN;
2905 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2906 toState != Workspace.State.NORMAL_HIDDEN;
2907 }
2908 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002909 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002910 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002911 onInteractionEnd();
2912 }
2913 }
2914
Kenny Guyf07af7b2014-07-31 11:39:16 +01002915 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002916 try {
2917 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002918 launcherApps.showAppDetailsForProfile(componentName, user);
2919 } catch (SecurityException e) {
2920 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2921 Log.e(TAG, "Launcher does not have permission to launch settings");
2922 } catch (ActivityNotFoundException e) {
2923 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2924 Log.e(TAG, "Unable to launch settings");
2925 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002926 }
2927
Michael Jurka1e2f4652013-07-08 18:03:46 -07002928 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002929 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2930 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002931 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002932 // System applications cannot be installed. For now, show a toast explaining that.
2933 // We may give them the option of disabling apps this way.
2934 int messageId = R.string.uninstall_system_app_text;
2935 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002936 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002937 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002938 String packageName = componentName.getPackageName();
2939 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002940 Intent intent = new Intent(
2941 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002942 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2943 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002944 if (user != null) {
2945 user.addToIntent(intent, Intent.EXTRA_USER);
2946 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002947 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002948 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002949 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002950 }
2951
Michael Jurka86a720e2012-05-09 11:23:23 -07002952 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002953 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002955 // Only launch using the new animation if the shortcut has not opted out (this is a
2956 // private contract between launcher and may be ignored in the future).
2957 boolean useLaunchAnimation = (v != null) &&
2958 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002959 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2960 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002961
Kenny Guy1317e2d2014-05-08 18:52:50 +01002962 UserHandleCompat user = null;
2963 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2964 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2965 user = userManager.getUserForSerialNumber(serialNumber);
2966 }
2967
2968 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002969 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002970 ActivityOptions opts = null;
2971 if (sClipRevealMethod != null) {
2972 // TODO: call method directly when Launcher3 can depend on M APIs
2973 int left = 0, top = 0;
2974 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2975 if (v instanceof TextView) {
2976 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002977 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2978 if (icon != null) {
2979 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002980 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002981 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002982 width = bounds.width();
2983 height = bounds.height();
2984 }
2985 }
2986 try {
2987 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2988 left, top, width, height);
2989 } catch (IllegalAccessException e) {
2990 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2991 sClipRevealMethod = null;
2992 } catch (InvocationTargetException e) {
2993 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2994 sClipRevealMethod = null;
2995 }
2996 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002997 if (opts == null && !Utilities.isLmpOrAbove()) {
2998 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002999 v.getMeasuredWidth(), v.getMeasuredHeight());
3000 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003001 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07003002 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003003
3004 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3005 // Could be launching some bookkeeping activity
3006 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003007 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003008 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003009 launcherApps.startActivityForProfile(intent.getComponent(), user,
3010 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003011 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003012 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003013 } catch (SecurityException e) {
3014 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003015 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003017 "or use the exported attribute for this activity. "
3018 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003019 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003020 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003021 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003022
Michael Jurka86a720e2012-05-09 11:23:23 -07003023 boolean startActivitySafely(View v, Intent intent, Object tag) {
3024 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003025 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3026 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3027 return false;
3028 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003029 try {
3030 success = startActivity(v, intent, tag);
3031 } catch (ActivityNotFoundException e) {
3032 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3033 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3034 }
3035 return success;
3036 }
3037
Adam Cohen268c4752012-06-06 17:47:33 -07003038 /**
3039 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3040 * in the DragLayer in the exact absolute location of the original FolderIcon.
3041 */
3042 private void copyFolderIconToImage(FolderIcon fi) {
3043 final int width = fi.getMeasuredWidth();
3044 final int height = fi.getMeasuredHeight();
3045
3046 // Lazy load ImageView, Bitmap and Canvas
3047 if (mFolderIconImageView == null) {
3048 mFolderIconImageView = new ImageView(this);
3049 }
3050 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3051 mFolderIconBitmap.getHeight() != height) {
3052 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3053 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3054 }
3055
3056 DragLayer.LayoutParams lp;
3057 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3058 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3059 } else {
3060 lp = new DragLayer.LayoutParams(width, height);
3061 }
3062
Adam Cohen307fe232012-08-16 17:55:58 -07003063 // The layout from which the folder is being opened may be scaled, adjust the starting
3064 // view size by this scale factor.
3065 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003066 lp.customPosition = true;
3067 lp.x = mRectForFolderAnimation.left;
3068 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003069 lp.width = (int) (scale * width);
3070 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003071
3072 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3073 fi.draw(mFolderIconCanvas);
3074 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003075 if (fi.getFolder() != null) {
3076 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3077 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003078 }
Adam Cohen268c4752012-06-06 17:47:33 -07003079 // Just in case this image view is still in the drag layer from a previous animation,
3080 // we remove it and re-add it.
3081 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3082 mDragLayer.removeView(mFolderIconImageView);
3083 }
3084 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003085 if (fi.getFolder() != null) {
3086 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003087 }
Adam Cohen268c4752012-06-06 17:47:33 -07003088 }
3089
Adam Cohen2801caf2011-05-13 20:57:39 -07003090 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003091 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003092 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3093 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3094 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3095
Adam Cohenc51934b2011-07-26 21:07:43 -07003096 FolderInfo info = (FolderInfo) fi.getTag();
3097 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3098 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003099 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3100 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003101 }
3102
Adam Cohen268c4752012-06-06 17:47:33 -07003103 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3104 copyFolderIconToImage(fi);
3105 fi.setVisibility(View.INVISIBLE);
3106
Michael Jurka2ecf9952012-06-18 12:52:28 -07003107 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003108 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003109 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003110 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3111 }
3112 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003113 oa.start();
3114 }
3115
Adam Cohen268c4752012-06-06 17:47:33 -07003116 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003117 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003118 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3119 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3120 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3121
Adam Cohen268c4752012-06-06 17:47:33 -07003122 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003123
Adam Cohen268c4752012-06-06 17:47:33 -07003124 // We remove and re-draw the FolderIcon in-case it has changed
3125 mDragLayer.removeView(mFolderIconImageView);
3126 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003127 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003128 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003129 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003130 oa.addListener(new AnimatorListenerAdapter() {
3131 @Override
3132 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003133 if (cl != null) {
3134 cl.clearFolderLeaveBehind();
3135 // Remove the ImageView copy of the FolderIcon and make the original visible.
3136 mDragLayer.removeView(mFolderIconImageView);
3137 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003138 }
3139 }
3140 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003141 oa.start();
3142 }
3143
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003144 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003145 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003146 * is animated relative to the specified View. If the View is null, no animation
3147 * is played.
3148 *
3149 * @param folderInfo The FolderInfo describing the folder to open.
3150 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003151 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003152 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003153 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3154 if (openFolder != null && openFolder != folder) {
3155 // Close any open folder before opening a folder.
3156 closeFolder();
3157 }
3158
Adam Cohena9cf38f2011-05-02 15:36:58 -07003159 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003160
Adam Cohena9cf38f2011-05-02 15:36:58 -07003161 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162
Sunny Goyalf4066152015-04-15 09:42:19 -07003163 // While the folder is open, the position of the icon cannot change.
3164 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3165
Adam Cohen4554ee12011-08-03 16:13:21 -07003166 // Just verify that the folder hasn't already been added to the DragLayer.
3167 // There was a one-off crash where the folder had a parent already.
3168 if (folder.getParent() == null) {
3169 mDragLayer.addView(folder);
3170 mDragController.addDropTarget((DropTarget) folder);
3171 } else {
3172 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3173 folder.getParent() + ").");
3174 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003175 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003176 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003177
3178 // Notify the accessibility manager that this folder "window" has appeared and occluded
3179 // the workspace items
3180 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3181 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003182 }
3183
3184 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003185 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003186 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003187 if (folder.isEditingName()) {
3188 folder.dismissEditingName();
3189 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003190 closeFolder(folder);
3191 }
3192 }
3193
3194 void closeFolder(Folder folder) {
3195 folder.getInfo().opened = false;
3196
3197 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3198 if (parent != null) {
3199 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3200 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003201 if (fi != null) {
3202 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3203 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003204 }
3205 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003206
3207 // Notify the accessibility manager that this folder "window" has disappeard and no
3208 // longer occludeds the workspace items
3209 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003210 }
3211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003213 if (!isDraggingEnabled()) return false;
3214 if (isWorkspaceLocked()) return false;
3215 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216
Adam Cohen1697b792013-09-17 19:08:21 -07003217 if (v instanceof Workspace) {
3218 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003219 if (!mWorkspace.isTouchActive()) {
3220 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003221 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3222 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3223 return true;
3224 } else {
3225 return false;
3226 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003227 } else {
3228 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003229 }
Adam Cohen1697b792013-09-17 19:08:21 -07003230 }
3231
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003232 CellLayout.CellInfo longClickCellInfo = null;
3233 View itemUnderLongClick = null;
3234 if (v.getTag() instanceof ItemInfo) {
3235 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003236 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003237 itemUnderLongClick = longClickCellInfo.cell;
3238 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003239 }
3240
Winson Chung3d503fb2011-07-13 17:25:49 -07003241 // The hotseat touch handling does not go through Workspace, and we always allow long press
3242 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003243 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003244 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003245 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003246 // User long pressed on empty space
3247 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3248 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003249 if (mWorkspace.isInOverviewMode()) {
3250 mWorkspace.startReordering(v);
3251 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003252 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003253 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003254 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003255 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3256 mHotseat.getOrderInHotseat(
3257 longClickCellInfo.cellX,
3258 longClickCellInfo.cellY));
3259 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003260 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003261 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003262 }
3263 }
3264 }
3265 return true;
3266 }
3267
Winson Chung3d503fb2011-07-13 17:25:49 -07003268 boolean isHotseatLayout(View layout) {
3269 return mHotseat != null && layout != null &&
3270 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3271 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003272
Winson Chung3d503fb2011-07-13 17:25:49 -07003273 /**
3274 * Returns the CellLayout of the specified container at the specified screen.
3275 */
Sunny Goyal92820592015-03-02 11:31:35 -08003276 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003277 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3278 if (mHotseat != null) {
3279 return mHotseat.getLayout();
3280 } else {
3281 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003282 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003283 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003284 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003285 }
3286 }
3287
Winson Chungb745afb2015-03-02 11:51:23 -08003288 /**
3289 * For overridden classes.
3290 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003291 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003292 return isAppsViewVisible();
3293 }
3294
3295 public boolean isAppsViewVisible() {
3296 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3297 }
3298
3299 public boolean isWidgetsViewVisible() {
3300 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003301 }
3302
Craig Mautner360310b2012-10-26 15:13:08 -07003303 private void setWorkspaceBackground(boolean workspace) {
3304 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003305 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003306 }
3307
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003308 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003309 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3310 int curflags = getWindow().getAttributes().flags
3311 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3312 if (wpflags != curflags) {
3313 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3314 }
Craig Mautner360310b2012-10-26 15:13:08 -07003315 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003316 }
3317
Michael Jurkae326f182011-11-21 14:05:46 -08003318 @Override
3319 public void onTrimMemory(int level) {
3320 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003321 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3322 // The widget preview db can result in holding onto over
3323 // 3MB of memory for caching which isn't necessary.
3324 SQLiteDatabase.releaseMemory();
3325
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003326 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003327 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003328 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003329 if (mLauncherCallbacks != null) {
3330 mLauncherCallbacks.onTrimMemory(level);
3331 }
Michael Jurkae326f182011-11-21 14:05:46 -08003332 }
3333
Winson Chungb745afb2015-03-02 11:51:23 -08003334 @Override
3335 public void onStateTransitionHideSearchBar() {
3336 // Hide the search bar
3337 if (mSearchDropTargetBar != null) {
3338 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3339 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003340 }
3341
Winson Chungb745afb2015-03-02 11:51:23 -08003342 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003343 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3344 true);
Adam Cohened307df2013-10-02 09:37:31 -07003345 }
3346
Winson Chungdc61c4d2015-04-20 18:26:57 -07003347 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3348 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3349 onCompleteRunnable, true);
3350 }
3351
3352 protected void showWorkspace(int snapToPage, boolean animated) {
3353 showWorkspace(snapToPage, animated, null, true);
3354 }
3355
3356 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3357 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003358 boolean changed = mState != State.WORKSPACE ||
3359 mWorkspace.getState() != Workspace.State.NORMAL;
3360 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003361 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003362 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003363 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003364 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003365
Winson Chungc7d2b602012-05-16 17:02:20 -07003366 // Show the search bar (only animate if we were showing the drop target bar in spring
3367 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003368 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003369 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003370 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003371
Michael Jurkab3e22d92011-10-31 15:58:33 -07003372 // Set focus to the AppsCustomize button
3373 if (mAllAppsButton != null) {
3374 mAllAppsButton.requestFocus();
3375 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003376 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003377
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003378 // Change the state *after* we've called all the transition code
3379 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003380
Winson Chung5fb63472011-02-02 17:03:37 -08003381 // Resume the auto-advance of widgets
3382 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003383 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003384
Winson Chung0f785722015-04-08 10:27:49 -07003385 if (changed) {
3386 // Send an accessibility event to announce the context change
3387 getWindow().getDecorView()
3388 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003389 if (notifyLauncherCallbacks) {
3390 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003391 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003392 mLauncherCallbacks.onAllAppsHidden();
3393 }
3394 }
Winson Chung0f785722015-04-08 10:27:49 -07003395 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003396 }
3397
Adam Cohened307df2013-10-02 09:37:31 -07003398 void showOverviewMode(boolean animated) {
3399 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003400 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003401 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3402 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003403 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003404 }
3405
Winson Chungb745afb2015-03-02 11:51:23 -08003406 /**
3407 * Shows the apps view.
3408 */
Winson Chung4ac30062015-05-08 17:34:17 -07003409 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003410 if (resetListToTop) {
3411 mAppsView.scrollToTop();
3412 }
Winson Chung4ac30062015-05-08 17:34:17 -07003413 if (updatePredictedApps) {
3414 tryAndUpdatePredictedApps();
3415 }
Winson Chungb745afb2015-03-02 11:51:23 -08003416 showAppsOrWidgets(animated, State.APPS);
3417 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003418
Winson Chungb745afb2015-03-02 11:51:23 -08003419 /**
3420 * Shows the widgets view.
3421 */
3422 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003423 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003424 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003425 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003426 }
Winson Chungb745afb2015-03-02 11:51:23 -08003427 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003428
3429 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003430 @Override
3431 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003432 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003433 }
3434 });
Winson Chungb745afb2015-03-02 11:51:23 -08003435 }
3436
3437 /**
3438 * Sets up the transition to show the apps/widgets view.
3439 */
3440 private void showAppsOrWidgets(boolean animated, State toState) {
3441 if (mState != State.WORKSPACE) return;
3442 if (toState != State.APPS && toState != State.WIDGETS) return;
3443
3444 if (toState == State.APPS) {
3445 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003446 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003447 mLauncherCallbacks.onAllAppsShown();
3448 }
Winson Chungb745afb2015-03-02 11:51:23 -08003449 } else {
3450 mStateTransitionAnimation.startAnimationToWidgets(animated);
3451 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003452
Michael Jurkab3e22d92011-10-31 15:58:33 -07003453 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003454 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003455
3456 // Pause the auto-advance of widgets until we are out of AllApps
3457 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003458 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003459 closeFolder();
3460
3461 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003462 getWindow().getDecorView()
3463 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003464 }
3465
Winson Chungcd99cd32015-04-29 11:03:24 -07003466 /**
3467 * Updates the workspace and interaction state on state change, and return the animation to this
3468 * new state.
3469 */
3470 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3471 boolean animated, HashMap<View, Integer> layerViews) {
3472 Workspace.State fromState = mWorkspace.getState();
3473 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3474 updateInteraction(fromState, toState);
3475 return anim;
3476 }
3477
Hyunyoung Song3f471442015-04-08 19:01:34 -07003478 public void enterSpringLoadedDragMode() {
3479 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3480 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003481 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3482 mState == State.WIDGETS_SPRING_LOADED) {
3483 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003484 }
Winson Chungb745afb2015-03-02 11:51:23 -08003485
3486 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003487 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3488 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003489 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003490 }
Adam Cohen7777d962011-08-18 18:58:38 -07003491
Hyunyoung Song3f471442015-04-08 19:01:34 -07003492 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003493 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003494 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003495
Winson Chungcd99cd32015-04-29 11:03:24 -07003496 if (successfulDrop) {
3497 // We need to trigger all apps hidden to notify search to update itself before the
3498 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003499 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003500 mLauncherCallbacks.onAllAppsHidden();
3501 }
3502 }
3503
Winson Chunge7a03942011-08-05 15:05:12 -07003504 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003505 @Override
3506 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003507 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003508 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3509 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003510 // Before we show workspace, hide all apps again because
3511 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3512 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003513 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003514 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003515 } else {
3516 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003517 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003518 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003519 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003520 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003521
Michael Jurkad3ef3062010-11-23 16:23:58 -08003522 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003523 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003524 showAppsView(true /* animated */, false /* resetListToTop */,
3525 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003526 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003527 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003528 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003529 }
3530
Winson Chung4ac30062015-05-08 17:34:17 -07003531 /**
3532 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3533 * resumed.
3534 */
3535 private void tryAndUpdatePredictedApps() {
3536 if (mLauncherCallbacks != null) {
3537 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3538 if (!apps.isEmpty()) {
3539 mAppsView.setPredictedApps(apps);
3540 }
3541 }
3542 }
3543
Michael Jurkab3e22d92011-10-31 15:58:33 -07003544 void lockAllApps() {
3545 // TODO
3546 }
3547
3548 void unlockAllApps() {
3549 // TODO
3550 }
3551
Sunny Goyal594d76d2014-11-06 10:12:54 -08003552 protected void disableVoiceButtonProxy(boolean disable) {
3553 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003554 }
3555
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003556 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003557 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3558 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003559 }
3560
Adam Cohen24ce0b32014-01-14 16:18:14 -08003561 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003562 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3563 if (searchProvider == null) {
3564 return null;
3565 }
3566
3567 Bundle opts = new Bundle();
3568 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003569 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003570
3571 SharedPreferences sp = getSharedPreferences(
3572 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3573 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003574 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003575 if (!searchProvider.provider.flattenToString().equals(
3576 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003577 || (widgetInfo == null)
3578 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003579 // A valid widget is not already bound.
3580 if (widgetId > -1) {
3581 mAppWidgetHost.deleteAppWidgetId(widgetId);
3582 widgetId = -1;
3583 }
3584
3585 // Try to bind a new widget
3586 widgetId = mAppWidgetHost.allocateAppWidgetId();
3587
3588 if (!AppWidgetManagerCompat.getInstance(this)
3589 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3590 mAppWidgetHost.deleteAppWidgetId(widgetId);
3591 widgetId = -1;
3592 }
3593
3594 sp.edit()
3595 .putInt(QSB_WIDGET_ID, widgetId)
3596 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3597 .commit();
3598 }
3599
3600 if (widgetId != -1) {
3601 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3602 mQsb.updateAppWidgetOptions(opts);
3603 mQsb.setPadding(0, 0, 0, 0);
3604 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003605 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003606 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003607 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003608 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003609 }
3610
Sunny Goyal22235bc2015-04-03 09:23:43 -07003611 private void reinflateQSBIfNecessary() {
3612 if (mQsb instanceof LauncherAppWidgetHostView &&
3613 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3614 mSearchDropTargetBar.removeView(mQsb);
3615 mQsb = null;
3616 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3617 }
3618 }
3619
Michael Jurkad7c28052012-04-27 15:43:36 -07003620 @Override
3621 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003622 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003623 final List<CharSequence> text = event.getText();
3624 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003625 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003626 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003627 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003628 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003629 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003630 } else {
3631 text.add(getString(R.string.all_apps_home_button_label));
3632 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003633 return result;
3634 }
3635
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003636 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003637 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003638 */
Adam Cohen091440a2015-03-18 14:16:05 -07003639 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003640 @Override
3641 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003642 closeSystemDialogs();
3643 }
3644 }
3645
3646 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003647 * Receives notifications whenever the appwidgets are reset.
3648 */
3649 private class AppWidgetResetObserver extends ContentObserver {
3650 public AppWidgetResetObserver() {
3651 super(new Handler());
3652 }
3653
3654 @Override
3655 public void onChange(boolean selfChange) {
3656 onAppWidgetReset();
3657 }
3658 }
3659
3660 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003661 * If the activity is currently paused, signal that we need to run the passed Runnable
3662 * in onResume.
3663 *
3664 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003665 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3666 * wrong when we're not running, and if the activity comes back to what the configuration was
3667 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003668 *
3669 * Implementation of the method from LauncherModel.Callbacks.
3670 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003671 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003672 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003673 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003674 if (mPaused) {
3675 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003676 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003677 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003678 }
3679 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003680 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003681 return true;
3682 } else {
3683 return false;
3684 }
3685 }
3686
Michael Jurkac402cd92013-05-20 15:49:32 +02003687 private boolean waitUntilResume(Runnable run) {
3688 return waitUntilResume(run, false);
3689 }
3690
Michael Jurka1e2f4652013-07-08 18:03:46 -07003691 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003692 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003693 }
3694
Michael Jurka7607c2f2013-04-03 14:33:19 -07003695 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003696 * If the activity is currently paused, signal that we need to re-run the loader
3697 * in onResume.
3698 *
3699 * This needs to be called from incoming places where resources might have been loaded
3700 * while we are paused. That is becaues the Configuration might be wrong
3701 * when we're not running, and if it comes back to what it was when we
3702 * were paused, we are not restarted.
3703 *
3704 * Implementation of the method from LauncherModel.Callbacks.
3705 *
3706 * @return true if we are currently paused. The caller might be able to
3707 * skip some work in that case since we will come back again.
3708 */
3709 public boolean setLoadOnResume() {
3710 if (mPaused) {
3711 Log.i(TAG, "setLoadOnResume");
3712 mOnResumeNeedsLoad = true;
3713 return true;
3714 } else {
3715 return false;
3716 }
3717 }
3718
3719 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003721 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003722 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003723 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003724 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003725 } else {
3726 return SCREEN_COUNT / 2;
3727 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003729
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 /**
3731 * Refreshes the shortcuts shown on the workspace.
3732 *
3733 * Implementation of the method from LauncherModel.Callbacks.
3734 */
3735 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003736 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003737
Michael Jurka7607c2f2013-04-03 14:33:19 -07003738 // If we're starting binding all over again, clear any bind calls we'd postponed in
3739 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3740 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003741 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003742
Winson Chungd64d1762013-08-20 14:37:16 -07003743 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003744 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003745 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003746
Michael Jurka05bf644e2011-11-30 20:28:53 -08003747 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003748 if (mHotseat != null) {
3749 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003750 }
3751 }
3752
Adam Cohendcd297f2013-06-18 13:13:40 -07003753 @Override
3754 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003755 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003756
Adam Cohen5084cba2013-09-03 12:01:16 -07003757 // If there are no screens, we need to have an empty screen
3758 if (orderedScreenIds.size() == 0) {
3759 mWorkspace.addExtraEmptyScreen();
3760 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003761
3762 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003763 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003764 if (hasCustomContentToLeft()) {
3765 mWorkspace.createCustomContentContainer();
3766 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003767 }
Winson Chung64359a52013-07-08 17:17:08 -07003768 }
3769
3770 @Override
3771 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003772 // Log to disk
3773 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3774 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3775 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003776 int count = orderedScreenIds.size();
3777 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003778 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003779 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003780 }
3781
Adam Cohen39a06042013-07-19 14:30:12 -07003782 private boolean shouldShowWeightWatcher() {
3783 String spKey = LauncherAppState.getSharedPreferencesKey();
3784 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003785 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003786
3787 return show;
3788 }
3789
3790 private void toggleShowWeightWatcher() {
3791 String spKey = LauncherAppState.getSharedPreferencesKey();
3792 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3793 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3794
3795 show = !show;
3796
3797 SharedPreferences.Editor editor = sp.edit();
3798 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3799 editor.commit();
3800
3801 if (mWeightWatcher != null) {
3802 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3803 }
3804 }
3805
Winson Chungd64d1762013-08-20 14:37:16 -07003806 public void bindAppsAdded(final ArrayList<Long> newScreens,
3807 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003808 final ArrayList<ItemInfo> addAnimated,
3809 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003810 Runnable r = new Runnable() {
3811 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003812 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003813 }
3814 };
3815 if (waitUntilResume(r)) {
3816 return;
3817 }
3818
Winson Chungd64d1762013-08-20 14:37:16 -07003819 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003820 if (newScreens != null) {
3821 bindAddScreens(newScreens);
3822 }
Winson Chungd64d1762013-08-20 14:37:16 -07003823
3824 // We add the items without animation on non-visible pages, and with
3825 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003826 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003827 bindItems(addNotAnimated, 0,
3828 addNotAnimated.size(), false);
3829 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003830 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003831 bindItems(addAnimated, 0,
3832 addAnimated.size(), true);
3833 }
Winson Chungc58497e2013-09-03 17:48:37 -07003834
Winson Chung87412982013-10-03 18:34:14 -07003835 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003836 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003837
Winson Chungb745afb2015-03-02 11:51:23 -08003838 if (addedApps != null && mAppsView != null) {
3839 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003840 }
Winson Chungd64d1762013-08-20 14:37:16 -07003841 }
3842
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003843 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003844 * Bind the items start-end from the list.
3845 *
3846 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003847 */
Winson Chung64359a52013-07-08 17:17:08 -07003848 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3849 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003850 Runnable r = new Runnable() {
3851 public void run() {
3852 bindItems(shortcuts, start, end, forceAnimateIcons);
3853 }
3854 };
3855 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003856 return;
3857 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003858
Winson Chungf0c6ae02012-03-21 16:10:31 -07003859 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003860 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3861 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003862 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003863 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003864 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003865 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003866 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003867
3868 // Short circuit if we are loading dock items for a configuration which has no dock
3869 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3870 mHotseat == null) {
3871 continue;
3872 }
3873
Joe Onorato9c1289c2009-08-17 11:03:03 -04003874 switch (item.itemType) {
3875 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3876 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003877 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003878 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003879
Winson Chung64359a52013-07-08 17:17:08 -07003880 /*
3881 * TODO: FIX collision case
3882 */
Winson Chungce376632013-07-11 16:12:41 -07003883 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3884 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3885 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003886 View v = cl.getChildAt(item.cellX, item.cellY);
3887 Object tag = v.getTag();
3888 String desc = "Collision while binding workspace item: " + item
3889 + ". Collides with " + tag;
3890 if (LauncherAppState.isDogfoodBuild()) {
3891 throw (new RuntimeException(desc));
3892 } else {
3893 Log.d(TAG, desc);
3894 }
Winson Chungce376632013-07-11 16:12:41 -07003895 }
Winson Chung64359a52013-07-08 17:17:08 -07003896 }
3897
Adam Cohendcd297f2013-06-18 13:13:40 -07003898 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3899 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003900 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003901 // Animate all the applications up now
3902 shortcut.setAlpha(0f);
3903 shortcut.setScaleX(0f);
3904 shortcut.setScaleY(0f);
3905 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003906 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003909 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003910 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003911 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003912 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003913 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3914 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003915 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003916 default:
3917 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003919 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003920
Winson Chung997a9232013-07-24 15:33:46 -07003921 if (animateIcons) {
3922 // Animate to the correct page
3923 if (newShortcutsScreenId > -1) {
3924 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003925 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003926 final Runnable startBounceAnimRunnable = new Runnable() {
3927 public void run() {
3928 anim.playTogether(bounceAnims);
3929 anim.start();
3930 }
3931 };
Winson Chung997a9232013-07-24 15:33:46 -07003932 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003933 // We post the animation slightly delayed to prevent slowdowns
3934 // when we are loading right after we return to launcher.
3935 mWorkspace.postDelayed(new Runnable() {
3936 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003937 if (mWorkspace != null) {
3938 mWorkspace.snapToPage(newScreenIndex);
3939 mWorkspace.postDelayed(startBounceAnimRunnable,
3940 NEW_APPS_ANIMATION_DELAY);
3941 }
Winson Chung94d67682013-09-25 16:29:40 -07003942 }
3943 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003944 } else {
3945 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003946 }
3947 }
Winson Chung64359a52013-07-08 17:17:08 -07003948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003949 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003950 }
3951
Joe Onorato9c1289c2009-08-17 11:03:03 -04003952 /**
3953 * Implementation of the method from LauncherModel.Callbacks.
3954 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003955 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003956 Runnable r = new Runnable() {
3957 public void run() {
3958 bindFolders(folders);
3959 }
3960 };
3961 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003962 return;
3963 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003964 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003966
3967 /**
3968 * Add the views for a widget to the workspace.
3969 *
3970 * Implementation of the method from LauncherModel.Callbacks.
3971 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003972 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003973 Runnable r = new Runnable() {
3974 public void run() {
3975 bindAppWidget(item);
3976 }
3977 };
3978 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003979 return;
3980 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003981
Daniel Sandler843e8602010-06-07 14:59:01 -04003982 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3983 if (DEBUG_WIDGETS) {
3984 Log.d(TAG, "bindAppWidget: " + item);
3985 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003986 final Workspace workspace = mWorkspace;
3987
Adam Cohen59400422014-03-05 18:07:04 -08003988 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003989 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003990
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003991 if (!mIsSafeModeEnabled
3992 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3993 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003994 if (appWidgetInfo == null) {
3995 if (DEBUG_WIDGETS) {
3996 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3997 + " belongs to component " + item.providerName
3998 + ", as the povider is null");
3999 }
4000 LauncherModel.deleteItemFromDatabase(this, item);
4001 return;
4002 }
4003 // Note: This assumes that the id remap broadcast is received before this step.
4004 // If that is not the case, the id remap will be ignored and user may see the
4005 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004006 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004007 pendingInfo.spanX = item.spanX;
4008 pendingInfo.spanY = item.spanY;
4009 pendingInfo.minSpanX = item.minSpanX;
4010 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004011 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004012 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004013
Sunny Goyalff572272014-07-23 13:58:07 -07004014 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004015 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4016 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004017
4018 // TODO consider showing a permission dialog when the widget is clicked.
4019 if (!success) {
4020 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4021 if (DEBUG_WIDGETS) {
4022 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4023 + " belongs to component " + item.providerName
4024 + ", as the launcher is unable to bing a new widget id");
4025 }
4026 LauncherModel.deleteItemFromDatabase(this, item);
4027 return;
4028 }
4029
4030 item.appWidgetId = newWidgetId;
4031
4032 // If the widget has a configure activity, it is still needs to set it up, otherwise
4033 // the widget is ready to go.
4034 item.restoreStatus = (appWidgetInfo.configure == null)
4035 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4036 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4037
4038 LauncherModel.updateItemInDatabase(this, item);
4039 }
4040
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004041 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004042 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004043 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004044 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4045 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004046 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004047
Sunny Goyal651077b2014-06-30 14:15:31 -07004048 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4049 } else {
4050 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004051 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4052 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004053 view.updateIcon(mIconCache);
4054 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004055 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004056 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004057 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058
Joe Onorato9c1289c2009-08-17 11:03:03 -04004059 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004060 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061
Adam Cohendcd297f2013-06-18 13:13:40 -07004062 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004064 if (!item.isCustomWidget()) {
4065 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4066 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004067
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068 workspace.requestLayout();
4069
Daniel Sandler843e8602010-06-07 14:59:01 -04004070 if (DEBUG_WIDGETS) {
4071 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4072 + (SystemClock.uptimeMillis()-start) + "ms");
4073 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074 }
4075
Sunny Goyalff572272014-07-23 13:58:07 -07004076 /**
4077 * Restores a pending widget.
4078 *
4079 * @param appWidgetId The app widget id
4080 * @param cellInfo The position on screen where to create the widget.
4081 */
4082 private void completeRestoreAppWidget(final int appWidgetId) {
4083 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4084 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4085 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4086 return;
4087 }
4088
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004089 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004090 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4091
4092 mWorkspace.reinflateWidgetsIfNecessary();
4093 LauncherModel.updateItemInDatabase(this, info);
4094 }
4095
Adam Cohen1462de32012-07-24 22:34:36 -07004096 public void onPageBoundSynchronously(int page) {
4097 mSynchronouslyBoundPages.add(page);
4098 }
4099
Joe Onorato9c1289c2009-08-17 11:03:03 -04004100 /**
4101 * Callback saying that there aren't any more items to bind.
4102 *
4103 * Implementation of the method from LauncherModel.Callbacks.
4104 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004105 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004106 Runnable r = new Runnable() {
4107 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004108 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004109 }
4110 };
4111 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004112 return;
4113 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004114 if (mSavedState != null) {
4115 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004116 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004117 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004118 mSavedState = null;
4119 }
4120
Adam Cohen1462de32012-07-24 22:34:36 -07004121 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004123 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004124 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004125
4126 // If we received the result of any pending adds while the loader was running (e.g. the
4127 // widget configuration forced an orientation change), process them now.
4128 if (sPendingAddItem != null) {
4129 final long screenId = completeAdd(sPendingAddItem);
4130
4131 // TODO: this moves the user to the page where the pending item was added. Ideally,
4132 // the screen would be guaranteed to exist after bind, and the page would be set through
4133 // the workspace restore process.
4134 mWorkspace.post(new Runnable() {
4135 @Override
4136 public void run() {
4137 mWorkspace.snapToScreenId(screenId);
4138 }
4139 });
4140 sPendingAddItem = null;
4141 }
4142
Sunny Goyal756adbc2015-04-16 15:20:51 -07004143 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004144
4145 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004146 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004147 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004148 }
4149
Adam Cohen3ed316a2014-07-23 18:21:20 -07004150 private void sendLoadingCompleteBroadcastIfNecessary() {
4151 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4152 String permission =
4153 getResources().getString(R.string.receive_first_load_broadcast_permission);
4154 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4155 sendBroadcast(intent, permission);
4156 SharedPreferences.Editor editor = mSharedPrefs.edit();
4157 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4158 editor.apply();
4159 }
4160 }
4161
Winson Chunga0b7e862013-09-05 16:03:15 -07004162 public boolean isAllAppsButtonRank(int rank) {
4163 if (mHotseat != null) {
4164 return mHotseat.isAllAppsButtonRank(rank);
4165 }
4166 return false;
4167 }
4168
Winson Chunga2413752012-04-03 14:22:34 -07004169 private boolean canRunNewAppsAnimation() {
4170 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4171 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4172 }
4173
Winson Chungc9168342013-06-26 14:54:55 -07004174 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4175 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4176 PropertyValuesHolder.ofFloat("alpha", 1f),
4177 PropertyValuesHolder.ofFloat("scaleX", 1f),
4178 PropertyValuesHolder.ofFloat("scaleY", 1f));
4179 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4180 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4181 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4182 return bounceAnim;
4183 }
4184
Adam Cohen27772732013-11-11 14:00:56 +00004185 public boolean useVerticalBarLayout() {
4186 return LauncherAppState.getInstance().getDynamicGrid().
4187 getDeviceProfile().isVerticalBarLayout();
4188 }
4189
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004190 protected Rect getSearchBarBounds() {
4191 return LauncherAppState.getInstance().getDynamicGrid().
4192 getDeviceProfile().getSearchBarBounds();
4193 }
4194
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004195 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004196 if (mSearchDropTargetBar == null) {
4197 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004198 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004199 if (mQsb != null) {
4200 mSearchDropTargetBar.removeView(mQsb);
4201 mQsb = null;
4202 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004203 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004204 }
4205
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004206 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004207 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4208 * multiple calls to bind the same list.)
4209 */
4210 @Thunk ArrayList<AppInfo> mTmpAppsList;
4211 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4212 public void run() {
4213 bindAllApplications(mTmpAppsList);
4214 mTmpAppsList = null;
4215 }
4216 };
4217
4218 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004219 * Add the icons for all apps.
4220 *
4221 * Implementation of the method from LauncherModel.Callbacks.
4222 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004223 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004224 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4225 mTmpAppsList = apps;
4226 return;
4227 }
4228
Winson Chungb745afb2015-03-02 11:51:23 -08004229 if (mAppsView != null) {
4230 mAppsView.setApps(apps);
4231 }
Adam Cohen9211d422014-10-07 18:14:53 -07004232 if (mLauncherCallbacks != null) {
4233 mLauncherCallbacks.bindAllApplications(apps);
4234 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004235 }
4236
4237 /**
4238 * A package was updated.
4239 *
4240 * Implementation of the method from LauncherModel.Callbacks.
4241 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004242 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004243 Runnable r = new Runnable() {
4244 public void run() {
4245 bindAppsUpdated(apps);
4246 }
4247 };
4248 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004249 return;
4250 }
4251
Winson Chungb745afb2015-03-02 11:51:23 -08004252 if (mAppsView != null) {
4253 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004254 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004255 }
4256
Sunny Goyal4390ace2014-10-13 11:33:11 -07004257 @Override
4258 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4259 Runnable r = new Runnable() {
4260 public void run() {
4261 bindWidgetsRestored(widgets);
4262 }
4263 };
4264 if (waitUntilResume(r)) {
4265 return;
4266 }
4267 mWorkspace.widgetsRestored(widgets);
4268 }
4269
Joe Onorato9c1289c2009-08-17 11:03:03 -04004270 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004271 * Some shortcuts were updated in the background.
4272 *
4273 * Implementation of the method from LauncherModel.Callbacks.
4274 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004275 @Override
4276 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4277 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004278 Runnable r = new Runnable() {
4279 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004280 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004281 }
4282 };
4283 if (waitUntilResume(r)) {
4284 return;
4285 }
4286
Sunny Goyal4390ace2014-10-13 11:33:11 -07004287 if (!updated.isEmpty()) {
4288 mWorkspace.updateShortcuts(updated);
4289 }
4290
4291 if (!removed.isEmpty()) {
4292 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4293 for (ShortcutInfo si : removed) {
4294 removedComponents.add(si.getTargetComponent());
4295 }
4296 mWorkspace.removeItemsByComponentName(removedComponents, user);
4297 // Notify the drag controller
4298 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004299 }
4300 }
4301
4302 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004303 * Update the state of a package, typically related to install state.
4304 *
4305 * Implementation of the method from LauncherModel.Callbacks.
4306 */
Sunny Goyale755d462014-07-22 13:48:29 -07004307 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004308 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4309 Runnable r = new Runnable() {
4310 public void run() {
4311 bindRestoreItemsChange(updates);
4312 }
4313 };
4314 if (waitUntilResume(r)) {
4315 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004316 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004317
Sunny Goyal756adbc2015-04-16 15:20:51 -07004318 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004319 }
4320
4321 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004322 * A package was uninstalled. We take both the super set of packageNames
4323 * in addition to specific applications to remove, the reason being that
4324 * this can be called when a package is updated as well. In that scenario,
4325 * we only remove specific components from the workspace, where as
4326 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004327 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004328 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004329 * Implementation of the method from LauncherModel.Callbacks.
4330 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004331 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004332 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004333 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004334 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004335 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004336 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004337 }
Winson Chungd64d1762013-08-20 14:37:16 -07004338 };
4339 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004340 return;
4341 }
4342
Sunny Goyal1a745e82014-10-02 15:58:31 -07004343 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004344 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4345 for (AppInfo info : appInfos) {
4346 removedComponents.add(info.componentName);
4347 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004348 if (!packageNames.isEmpty()) {
4349 mWorkspace.removeItemsByPackageName(packageNames, user);
4350 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004351 if (!removedComponents.isEmpty()) {
4352 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004353 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004354 // Notify the drag controller
4355 mDragController.onAppsRemoved(packageNames, removedComponents);
4356
Sunny Goyal1a745e82014-10-02 15:58:31 -07004357 } else {
4358 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004359 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004360
Winson Chungdf95eb12013-10-16 14:57:07 -07004361 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004362 if (mAppsView != null) {
4363 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004364 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004365 }
Joe Onoratobe386092009-11-17 17:32:16 -08004366
Adam Cohen091440a2015-03-18 14:16:05 -07004367 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004368 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004369 public void run() {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004370 bindAllPackages(mWidgetsAndShortcuts);
Winson Chung83892cc2013-05-01 16:53:33 -07004371 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004372 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004373
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004374 @Override
4375 public void bindAllPackages(final ArrayList<Object> widgetsAndShortcuts) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004376 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4377 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004378 return;
4379 }
4380
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004381 if (mWidgetsView != null && widgetsAndShortcuts != null) {
4382 mWidgetsView.addWidgets(widgetsAndShortcuts, getPackageManager());
4383 mWidgetsAndShortcuts = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004384 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004385 }
4386
Winson Chung400438b2011-01-16 17:53:48 -08004387 private int mapConfigurationOriActivityInfoOri(int configOri) {
4388 final Display d = getWindowManager().getDefaultDisplay();
4389 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4390 switch (d.getRotation()) {
4391 case Surface.ROTATION_0:
4392 case Surface.ROTATION_180:
4393 // We are currently in the same basic orientation as the natural orientation
4394 naturalOri = configOri;
4395 break;
4396 case Surface.ROTATION_90:
4397 case Surface.ROTATION_270:
4398 // We are currently in the other basic orientation to the natural orientation
4399 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4400 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4401 break;
4402 }
4403
4404 int[] oriMap = {
4405 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4406 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4407 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4408 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4409 };
4410 // Since the map starts at portrait, we need to offset if this device's natural orientation
4411 // is landscape.
4412 int indexOffset = 0;
4413 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4414 indexOffset = 1;
4415 }
4416 return oriMap[(d.getRotation() + indexOffset) % 4];
4417 }
Adam Cohen446e9402011-09-15 18:21:21 -07004418
Winson Chung4b919f82012-05-01 10:44:08 -07004419 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004420 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004421 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4422 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4423 .getConfiguration().orientation));
4424 } else {
4425 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4426 }
Winson Chung4b919f82012-05-01 10:44:08 -07004427 }
4428 }
4429 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004430 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004431 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004432 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004433 } else {
4434 mHandler.postDelayed(new Runnable() {
4435 public void run() {
4436 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4437 }
4438 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004439 }
Winson Chung4b919f82012-05-01 10:44:08 -07004440 }
Winson Chung400438b2011-01-16 17:53:48 -08004441 }
4442
Winson Chunge43a1e72014-01-15 10:33:02 -08004443 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004444 if (mLauncherCallbacks != null) {
4445 return mLauncherCallbacks.isLauncherPreinstalled();
4446 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004447 PackageManager pm = getPackageManager();
4448 try {
4449 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4450 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4451 return true;
4452 } else {
4453 return false;
4454 }
4455 } catch (NameNotFoundException e) {
4456 e.printStackTrace();
4457 return false;
4458 }
4459 }
4460
Adam Cohenea90f832014-05-21 15:03:34 -07004461 /**
4462 * This method indicates whether or not we should suggest default wallpaper dimensions
4463 * when our wallpaper cropper was not yet used to set a wallpaper.
4464 */
4465 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004466 if (mLauncherCallbacks != null) {
4467 return mLauncherCallbacks.overrideWallpaperDimensions();
4468 }
Adam Cohenea90f832014-05-21 15:03:34 -07004469 return true;
4470 }
4471
Adam Cohen432609a2014-03-13 17:03:22 -07004472 /**
4473 * To be overridden by subclasses to indicate that there is an activity to launch
4474 * before showing the standard launcher experience.
4475 */
4476 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004477 if (mLauncherCallbacks != null) {
4478 return mLauncherCallbacks.hasFirstRunActivity();
4479 }
Adam Cohen432609a2014-03-13 17:03:22 -07004480 return false;
4481 }
4482
4483 /**
4484 * To be overridden by subclasses to launch any first run activity
4485 */
4486 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004487 if (mLauncherCallbacks != null) {
4488 return mLauncherCallbacks.getFirstRunActivity();
4489 }
Adam Cohen432609a2014-03-13 17:03:22 -07004490 return null;
4491 }
4492
Winson Chung2826a402015-04-17 16:18:53 -07004493 /**
4494 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004495 */
4496 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004497 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4498 return false;
4499 }
4500
Winson Chung2826a402015-04-17 16:18:53 -07004501 if (mLauncherCallbacks != null) {
4502 return mLauncherCallbacks.overrideAllAppsSearch();
4503 }
4504 return false;
4505 }
4506
Winson Chunga6945242014-01-08 14:04:34 -08004507 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004508 return !ActivityManager.isRunningInTestHarness() &&
4509 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004510 }
4511
Adam Cohen4a9423d2014-03-28 14:22:31 -07004512 protected boolean hasRunFirstRunActivity() {
4513 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4514 }
4515
Adam Cohen432609a2014-03-13 17:03:22 -07004516 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004517 if (shouldRunFirstRunActivity() &&
4518 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004519 Intent firstRunIntent = getFirstRunActivity();
4520 if (firstRunIntent != null) {
4521 startActivity(firstRunIntent);
4522 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004523 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004524 }
4525 }
Adam Cohen432609a2014-03-13 17:03:22 -07004526 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004527 }
4528
4529 private void markFirstRunActivityShown() {
4530 SharedPreferences.Editor editor = mSharedPrefs.edit();
4531 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4532 editor.apply();
4533 }
4534
Adam Cohen432609a2014-03-13 17:03:22 -07004535 /**
4536 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4537 * screen that must be displayed and dismissed.
4538 */
4539 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004540 if (mLauncherCallbacks != null) {
4541 return mLauncherCallbacks.hasDismissableIntroScreen();
4542 }
Adam Cohen432609a2014-03-13 17:03:22 -07004543 return false;
4544 }
4545
4546 /**
4547 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4548 */
4549 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004550 if (mLauncherCallbacks != null) {
4551 return mLauncherCallbacks.getIntroScreen();
4552 }
Adam Cohen432609a2014-03-13 17:03:22 -07004553 return null;
4554 }
4555
4556 /**
4557 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4558 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4559 */
4560 private boolean shouldShowIntroScreen() {
4561 return hasDismissableIntroScreen() &&
4562 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4563 }
4564
4565 protected void showIntroScreen() {
4566 View introScreen = getIntroScreen();
4567 changeWallpaperVisiblity(false);
4568 if (introScreen != null) {
4569 mDragLayer.showOverlayView(introScreen);
4570 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004571 if (mLauncherOverlayContainer != null) {
4572 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4573 }
Adam Cohen432609a2014-03-13 17:03:22 -07004574 }
4575
4576 public void dismissIntroScreen() {
4577 markIntroScreenDismissed();
4578 if (showFirstRunActivity()) {
4579 // We delay hiding the intro view until the first run activity is showing. This
4580 // avoids a blip.
4581 mWorkspace.postDelayed(new Runnable() {
4582 @Override
4583 public void run() {
4584 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004585 if (mLauncherOverlayContainer != null) {
4586 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4587 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004588 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004589 }
4590 }, ACTIVITY_START_DELAY);
4591 } else {
4592 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004593 if (mLauncherOverlayContainer != null) {
4594 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4595 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004596 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004597 }
4598 changeWallpaperVisiblity(true);
4599 }
4600
4601 private void markIntroScreenDismissed() {
4602 SharedPreferences.Editor editor = mSharedPrefs.edit();
4603 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4604 editor.apply();
4605 }
4606
Adam Cohen091440a2015-03-18 14:16:05 -07004607 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004608 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4609 // on the device, then we always show the first run cling experience (or if there is no
4610 // launcher2). Otherwise, we prompt the user upon started for migration
4611 LauncherClings launcherClings = new LauncherClings(this);
4612 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4613 if (mModel.canMigrateFromOldLauncherDb(this)) {
4614 launcherClings.showMigrationCling();
4615 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004616 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004617 }
4618 }
4619 }
4620
Winson Chunga6945242014-01-08 14:04:34 -08004621 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004622 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4623 if (mHotseat != null) mHotseat.setAlpha(1f);
4624 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4625 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004626 }
4627
4628 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004629 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4630 if (mHotseat != null) mHotseat.setAlpha(0f);
4631 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4632 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004633 }
4634
Mathew Inwood72fbec12013-11-19 15:45:07 +00004635 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004636 // Called from search suggestion, not supported in other profiles.
4637 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4638 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4639 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4640 myUser);
4641 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004642 return null;
4643 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004644 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004645 }
4646
4647 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4648 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004649 // Called from search suggestion, not supported in other profiles.
4650 return createShortcutDragInfo(shortcutIntent, caption, icon,
4651 UserHandleCompat.myUserHandle());
4652 }
4653
4654 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4655 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004656 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004657 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004658 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004659 }
4660
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004661 protected void moveWorkspaceToDefaultScreen() {
4662 mWorkspace.moveToDefaultScreen(false);
4663 }
4664
Mathew Inwood72fbec12013-11-19 15:45:07 +00004665 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4666 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004667 mWorkspace.onExternalDragStartedWithItem(dragView);
4668 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004669 }
4670
Anjali Koppalf5d29132014-02-28 13:33:27 -08004671 @Override
4672 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004673 if (mLauncherCallbacks != null) {
4674 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4675 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004676 }
4677
Winson Chung80baf5a2010-08-09 16:03:15 -07004678 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004679 * Prints out out state for debugging.
4680 */
4681 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004682 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004683 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004684 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4685 Log.d(TAG, "mRestoring=" + mRestoring);
4686 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4687 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004688 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004689 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004690 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004691
Daniel Sandler325dc232013-06-05 22:57:57 -04004692 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004693 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004694
4695 @Override
4696 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4697 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004698 synchronized (sDumpLogs) {
4699 writer.println(" ");
4700 writer.println("Debug logs: ");
4701 for (int i = 0; i < sDumpLogs.size(); i++) {
4702 writer.println(" " + sDumpLogs.get(i));
4703 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004704 }
Adam Cohen9211d422014-10-07 18:14:53 -07004705 if (mLauncherCallbacks != null) {
4706 mLauncherCallbacks.dump(prefix, fd, writer, args);
4707 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004708 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004709
4710 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004711 if (DEBUG_DUMP_LOG) {
4712 synchronized (sDumpLogs) {
4713 Log.d(TAG, "");
4714 Log.d(TAG, "*********************");
4715 Log.d(TAG, "Launcher debug logs: ");
4716 for (int i = 0; i < sDumpLogs.size(); i++) {
4717 Log.d(TAG, " " + sDumpLogs.get(i));
4718 }
4719 Log.d(TAG, "*********************");
4720 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004721 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004722 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004723 }
4724
4725 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004726 addDumpLog(tag, log, null, debugLog);
4727 }
4728
4729 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004730 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004731 if (e != null) {
4732 Log.d(tag, log, e);
4733 } else {
4734 Log.d(tag, log);
4735 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004736 }
Winson Chungede41292013-09-19 16:27:36 -07004737 if (DEBUG_DUMP_LOG) {
4738 sDateStamp.setTime(System.currentTimeMillis());
4739 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004740 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4741 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004742 }
Winson Chungede41292013-09-19 16:27:36 -07004743 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004744 }
4745
Adam Cohen59400422014-03-05 18:07:04 -08004746 public static CustomAppWidget getCustomAppWidget(String name) {
4747 return sCustomAppWidgets.get(name);
4748 }
4749
4750 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4751 return sCustomAppWidgets;
4752 }
4753
Winson Chungede41292013-09-19 16:27:36 -07004754 public void dumpLogsToLocalData() {
4755 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004756 new AsyncTask<Void, Void, Void>() {
4757 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004758 boolean success = false;
4759 sDateStamp.setTime(sRunStart);
4760 String FILENAME = sDateStamp.getMonth() + "-"
4761 + sDateStamp.getDay() + "_"
4762 + sDateStamp.getHours() + "-"
4763 + sDateStamp.getMinutes() + "_"
4764 + sDateStamp.getSeconds() + ".txt";
4765
4766 FileOutputStream fos = null;
4767 File outFile = null;
4768 try {
4769 outFile = new File(getFilesDir(), FILENAME);
4770 outFile.createNewFile();
4771 fos = new FileOutputStream(outFile);
4772 } catch (Exception e) {
4773 e.printStackTrace();
4774 }
4775 if (fos != null) {
4776 PrintWriter writer = new PrintWriter(fos);
4777
4778 writer.println(" ");
4779 writer.println("Debug logs: ");
4780 synchronized (sDumpLogs) {
4781 for (int i = 0; i < sDumpLogs.size(); i++) {
4782 writer.println(" " + sDumpLogs.get(i));
4783 }
4784 }
4785 writer.close();
4786 }
4787 try {
4788 if (fos != null) {
4789 fos.close();
4790 success = true;
4791 }
4792 } catch (IOException e) {
4793 e.printStackTrace();
4794 }
Michael Jurka43467462013-11-14 12:03:58 +01004795 return null;
Winson Chungede41292013-09-19 16:27:36 -07004796 }
Michael Jurka43467462013-11-14 12:03:58 +01004797 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004798 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004800}
Michael Jurka2763be32011-02-24 11:19:57 -08004801
Michael Jurkaabded662011-03-04 12:06:57 -08004802interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004803 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004804 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004805 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004806 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004807 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004808}
Dan Sandlerd5024042014-01-09 15:01:33 -05004809
4810interface DebugIntents {
4811 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4812 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004813}